packages feed

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

module Effectful.OpenTelemetry.Tracing.Span.Kind where

import Data.Aeson.Types (ToJSON (..), Value (..))
import Proto3.Wire.Encode.Class qualified as Proto
import Prelude

-- | Clarifies the relationship between spans that are correlated via parent/child relationships
-- or span links.
-- Describes two independent properties that benefit tracing systems during analysis:
--
-- - Whether a span represents an outgoing call to a remote service ('Client', 'Producer')
--   or a processing of an incoming request initiated externally ('Server', 'Consumer').
-- - Whether a span represents a request/response operation ('Client', 'Server')
--   or a deferred execution ('Producer', 'Consumer').
--
-- See <https://opentelemetry.io/docs/concepts/signals/traces/#span-kind the OpenTelemetry spec>
data Kind
    = -- | Indicates that a span represents an internal operation within an application,
      -- as opposed to an operation with remote parents or children.
      Internal
    | -- | Indicates that a span covers server-side handling of a remote request
      -- while the client awaits a response.
      Server
    | -- | Indicates that the span describes a request to a remote service
      -- where the client awaits a response.
      -- When the context of a 'Client' span is propagated, 'Client' span usually becomes
      -- a parent of a remote 'Server' span.
      Client
    | -- | Indicates that the span describes the initiation or scheduling of a local
      -- or remote operation. This initiating span often ends before the correlated 'Consumer'
      -- span, possibly even before the 'Consumer' span starts.
      --
      -- In messaging scenarios with batching, tracing individual messages requires a new
      -- 'Producer' span per message to be created.
      Producer
    | -- | Indicates that the span represents the processing of an operation initiated
      -- by a producer, where the producer does not wait for the outcome.
      Consumer
    deriving stock (Show, Eq, Bounded)

instance Enum Kind where
    fromEnum Internal = 1
    fromEnum Server = 2
    fromEnum Client = 3
    fromEnum Producer = 4
    fromEnum Consumer = 5

    toEnum 1 = Internal
    toEnum 2 = Server
    toEnum 3 = Client
    toEnum 4 = Producer
    toEnum 5 = Consumer
    toEnum _ = error "Enum.Kind.toEnum: bad argument"

instance ToJSON Kind where
    toJSON = Number . fromIntegral . fromEnum

instance {-# OVERLAPPING #-} Proto.EncodeField Kind where
    encodeField n = Proto.int32 n . fromIntegral . fromEnum