packages feed

otel-effectful-1.0.0: src/Effectful/OpenTelemetry/Tracing/Span/Event.hs

module Effectful.OpenTelemetry.Tracing.Span.Event where

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

-- | A structured log message or annotation on a @Span@,
-- typically used to denote a meaningful, singular point in time during the @Span@'s duration.
--
-- See <https://opentelemetry.io/docs/concepts/signals/traces/#span-events the OpenTelemetry spec>.
data Event = Event
    { time :: Timestamp
    , name :: Text
    , attributes :: Attributes
    }
    deriving stock (Generic, Eq, Show)

now :: Text -> Attributes -> IO Event
now name attributes = do
    time <- Timestamp.now
    pure Event{..}

instance ToJSON Event where
    toJSON Event{..} =
        object
            [ "timeUnixNano" .= time
            , "name" .= name
            , "attributes" .= attributes
            ]

instance Proto.Encode Event where
    encode Event{..} =
        mconcat
            [ Proto.encodeField 1 time
            , Proto.encodeField 2 name
            , Proto.encodeField 3 attributes
            ]