-- | The W3C trace context captured on a job at enqueue.
module Arbiter.Core.Job.TraceContext
  ( TraceContext (..)
  , toTraceContext
  ) where

import Data.Text (Text)
import GHC.Generics (Generic)

-- | A job's W3C trace context.
data TraceContext = TraceContext
  { TraceContext -> Text
traceparent :: Text
  , TraceContext -> Maybe Text
tracestate :: Maybe Text
  }
  deriving stock (TraceContext -> TraceContext -> Bool
(TraceContext -> TraceContext -> Bool)
-> (TraceContext -> TraceContext -> Bool) -> Eq TraceContext
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TraceContext -> TraceContext -> Bool
== :: TraceContext -> TraceContext -> Bool
$c/= :: TraceContext -> TraceContext -> Bool
/= :: TraceContext -> TraceContext -> Bool
Eq, (forall x. TraceContext -> Rep TraceContext x)
-> (forall x. Rep TraceContext x -> TraceContext)
-> Generic TraceContext
forall x. Rep TraceContext x -> TraceContext
forall x. TraceContext -> Rep TraceContext x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. TraceContext -> Rep TraceContext x
from :: forall x. TraceContext -> Rep TraceContext x
$cto :: forall x. Rep TraceContext x -> TraceContext
to :: forall x. Rep TraceContext x -> TraceContext
Generic, Int -> TraceContext -> ShowS
[TraceContext] -> ShowS
TraceContext -> String
(Int -> TraceContext -> ShowS)
-> (TraceContext -> String)
-> ([TraceContext] -> ShowS)
-> Show TraceContext
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TraceContext -> ShowS
showsPrec :: Int -> TraceContext -> ShowS
$cshow :: TraceContext -> String
show :: TraceContext -> String
$cshowList :: [TraceContext] -> ShowS
showList :: [TraceContext] -> ShowS
Show)

-- | A trace context from its two stored halves. An orphan @tracestate@ is dropped.
toTraceContext :: Maybe Text -> Maybe Text -> Maybe TraceContext
toTraceContext :: Maybe Text -> Maybe Text -> Maybe TraceContext
toTraceContext Maybe Text
parent Maybe Text
state = (Text -> Maybe Text -> TraceContext)
-> Maybe Text -> Text -> TraceContext
forall a b c. (a -> b -> c) -> b -> a -> c
flip Text -> Maybe Text -> TraceContext
TraceContext Maybe Text
state (Text -> TraceContext) -> Maybe Text -> Maybe TraceContext
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
parent