packages feed

otel-effectful-1.0.0: src/Effectful/OpenTelemetry/Protocol/Scope.hs

module Effectful.OpenTelemetry.Protocol.Scope where

import Data.Aeson.Types (ToJSON)
import Data.Text (Text)
import Effectful.OpenTelemetry.Protocol.Attributes (Attributes)
import GHC.Generics (Generic)
import Proto3.Wire.Encode.Class qualified as Proto
import Prelude

-- | A logical unit of software with which emitted telemetry is associated.
-- It can represent a module, package, class, library, or framework;
-- any meaningful boundary that distinguishes one source of telemetry from another.
--
-- See <https://opentelemetry.io/docs/concepts/instrumentation-scope/ the OpenTelemetry spec>.
data Scope = Scope
    { name :: Text
    , version :: Text
    , attributes :: Attributes
    }
    deriving stock (Generic, Eq, Show)
    deriving anyclass (ToJSON)

instance Proto.Encode Scope where
    encode Scope{..} =
        mconcat
            [ Proto.encodeField 1 name
            , Proto.encodeField 2 version
            , Proto.encodeField 3 attributes
            -- 4: dropped_attributes_count: Not supported
            ]

-- | A batch of items grouped by 'Scope'.
data Items a = Items
    { scope :: Scope
    , items :: [a]
    }
    deriving stock (Generic)

-- NOTE: The JSON encoding for Items depends on the wrapped item type,
-- so it is defined in the Export.Request class.

instance (Proto.Encode a) => Proto.Encode (Items a) where
    encode Items{..} =
        mconcat
            [ Proto.encodeField 1 scope
            , foldMap (Proto.encodeField 2) items
            ]