hs-opentelemetry-propagator-w3c 0.0.1.2 → 0.0.1.3
raw patch · 5 files changed
+118/−93 lines, 5 filesdep ~hs-opentelemetry-apisetup-changed
Dependency ranges changed: hs-opentelemetry-api
Files
- Setup.hs +2/−0
- hs-opentelemetry-propagator-w3c.cabal +16/−16
- src/OpenTelemetry/Propagator/W3CBaggage.hs +6/−2
- src/OpenTelemetry/Propagator/W3CTraceContext.hs +93/−75
- test/Spec.hs +1/−0
Setup.hs view
@@ -1,2 +1,4 @@ import Distribution.Simple++ main = defaultMain
hs-opentelemetry-propagator-w3c.cabal view
@@ -1,22 +1,22 @@ cabal-version: 1.12 --- This file has been generated from package.yaml by hpack version 0.34.4.+-- This file has been generated from package.yaml by hpack version 0.35.2. -- -- see: https://github.com/sol/hpack -name: hs-opentelemetry-propagator-w3c-version: 0.0.1.2-synopsis: Trace propagation via HTTP headers following the w3c tracestate spec.-description: Please see the README on GitHub at <https://github.com/iand675/hs-opentelemetry/tree/main/propagators/w3c#readme>-category: OpenTelemetry, Tracing, Web-homepage: https://github.com/iand675/hs-opentelemetry#readme-bug-reports: https://github.com/iand675/hs-opentelemetry/issues-author: Ian Duncan-maintainer: ian@iankduncan.com-copyright: 2021 Ian Duncan-license: BSD3-license-file: LICENSE-build-type: Simple+name: hs-opentelemetry-propagator-w3c+version: 0.0.1.3+synopsis: Trace propagation via HTTP headers following the w3c tracestate spec.+description: Please see the README on GitHub at <https://github.com/iand675/hs-opentelemetry/tree/main/propagators/w3c#readme>+category: OpenTelemetry, Tracing, Web+homepage: https://github.com/iand675/hs-opentelemetry#readme+bug-reports: https://github.com/iand675/hs-opentelemetry/issues+author: Ian Duncan, Jade Lovelace+maintainer: ian@iankduncan.com+copyright: 2023 Ian Duncan, Mercury Technologies+license: BSD3+license-file: LICENSE+build-type: Simple extra-source-files: README.md ChangeLog.md@@ -38,7 +38,7 @@ attoparsec , base >=4.7 && <5 , bytestring- , hs-opentelemetry-api ==0.0.3.*+ , hs-opentelemetry-api >=0.0.3 && <0.2 , http-types , text default-language: Haskell2010@@ -55,7 +55,7 @@ attoparsec , base >=4.7 && <5 , bytestring- , hs-opentelemetry-api ==0.0.3.*+ , hs-opentelemetry-api >=0.0.3 && <0.2 , hs-opentelemetry-propagator-w3c , http-types , text
src/OpenTelemetry/Propagator/W3CBaggage.hs view
@@ -1,5 +1,6 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-}+ module OpenTelemetry.Propagator.W3CBaggage where import Data.ByteString@@ -8,18 +9,21 @@ import OpenTelemetry.Context (Context, insertBaggage, lookupBaggage) import OpenTelemetry.Propagator + decodeBaggage :: ByteString -> Maybe Baggage.Baggage decodeBaggage bs = case Baggage.decodeBaggageHeader bs of Left _ -> Nothing Right b -> Just b + encodeBaggage :: Baggage.Baggage -> ByteString encodeBaggage = Baggage.encodeBaggageHeader + w3cBaggagePropagator :: Propagator Context RequestHeaders ResponseHeaders-w3cBaggagePropagator = Propagator{..}+w3cBaggagePropagator = Propagator {..} where- propagatorNames = [ "baggage" ]+ propagatorNames = ["baggage"] extractor hs c = case Prelude.lookup "baggage" hs of Nothing -> pure c
src/OpenTelemetry/Propagator/W3CTraceContext.hs view
@@ -1,53 +1,63 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-}+ -------------------------------------------------------------------------------- |--- Module : OpenTelemetry.Propagators.W3CTraceContext--- Copyright : (c) Ian Duncan, 2021--- License : BSD-3--- Description : Standardized trace context propagation format intended for HTTP headers--- Maintainer : Ian Duncan--- Stability : experimental--- Portability : non-portable (GHC extensions)------ Distributed tracing is a methodology implemented by tracing tools to follow, analyze and debug a transaction across multiple software components. Typically, a distributed trace traverses more than one component which requires it to be uniquely identifiable across all participating systems. Trace context propagation passes along this unique identification. Today, trace context propagation is implemented individually by each tracing vendor. In multi-vendor environments, this causes interoperability problems, like:------ - Traces that are collected by different tracing vendors cannot be correlated as there is no shared unique identifier.--- - Traces that cross boundaries between different tracing vendors can not be propagated as there is no uniformly agreed set of identification that is forwarded.--- - Vendor specific metadata might be dropped by intermediaries.--- - Cloud platform vendors, intermediaries and service providers, cannot guarantee to support trace context propagation as there is no standard to follow.--- - In the past, these problems did not have a significant impact as most applications were monitored by a single tracing vendor and stayed within the boundaries of a single platform provider. Today, an increasing number of applications are highly distributed and leverage multiple middleware services and cloud platforms.------ - This transformation of modern applications calls for a distributed tracing context propagation standard.------ This module therefore provides support for tracing context propagation in accordance with the W3C tracing context--- propagation specifications: https://www.w3.org/TR/trace-context/---+ -----------------------------------------------------------------------------++{- |+ Module : OpenTelemetry.Propagators.W3CTraceContext+ Copyright : (c) Ian Duncan, 2021+ License : BSD-3+ Description : Standardized trace context propagation format intended for HTTP headers+ Maintainer : Ian Duncan+ Stability : experimental+ Portability : non-portable (GHC extensions)++ Distributed tracing is a methodology implemented by tracing tools to follow, analyze and debug a transaction across multiple software components. Typically, a distributed trace traverses more than one component which requires it to be uniquely identifiable across all participating systems. Trace context propagation passes along this unique identification. Today, trace context propagation is implemented individually by each tracing vendor. In multi-vendor environments, this causes interoperability problems, like:++ - Traces that are collected by different tracing vendors cannot be correlated as there is no shared unique identifier.+ - Traces that cross boundaries between different tracing vendors can not be propagated as there is no uniformly agreed set of identification that is forwarded.+ - Vendor specific metadata might be dropped by intermediaries.+ - Cloud platform vendors, intermediaries and service providers, cannot guarantee to support trace context propagation as there is no standard to follow.+ - In the past, these problems did not have a significant impact as most applications were monitored by a single tracing vendor and stayed within the boundaries of a single platform provider. Today, an increasing number of applications are highly distributed and leverage multiple middleware services and cloud platforms.++ - This transformation of modern applications calls for a distributed tracing context propagation standard.++ This module therefore provides support for tracing context propagation in accordance with the W3C tracing context+ propagation specifications: https://www.w3.org/TR/trace-context/+-} module OpenTelemetry.Propagator.W3CTraceContext where -import Data.Attoparsec.ByteString.Char8- ( parseOnly, string, Parser, hexadecimal, takeWhile )+import Data.Attoparsec.ByteString.Char8 (+ Parser,+ hexadecimal,+ parseOnly,+ string,+ takeWhile,+ ) import Data.ByteString (ByteString)-import Data.Char (isHexDigit)-import Data.Word (Word8) import qualified Data.ByteString.Builder as B import qualified Data.ByteString.Lazy as L-import Network.HTTP.Types ( RequestHeaders, ResponseHeaders )+import Data.Char (isHexDigit)+import Data.Word (Word8)+import Network.HTTP.Types (RequestHeaders, ResponseHeaders) import qualified OpenTelemetry.Context as Ctxt-import OpenTelemetry.Trace.Core- ( traceFlagsFromWord8,- traceFlagsValue,- getSpanContext,- wrapSpanContext,- Span,- SpanContext(..),- TraceFlags )-import OpenTelemetry.Propagator ( Propagator(..) )-import OpenTelemetry.Trace.Id (TraceId, SpanId, Base(..), spanIdBaseEncodedBuilder, traceIdBaseEncodedBuilder, baseEncodedToTraceId, baseEncodedToSpanId)-import OpenTelemetry.Trace.TraceState ( TraceState, empty )+import OpenTelemetry.Propagator (Propagator (..))+import OpenTelemetry.Trace.Core (+ Span,+ SpanContext (..),+ TraceFlags,+ getSpanContext,+ traceFlagsFromWord8,+ traceFlagsValue,+ wrapSpanContext,+ )+import OpenTelemetry.Trace.Id (Base (..), SpanId, TraceId, baseEncodedToSpanId, baseEncodedToTraceId, spanIdBaseEncodedBuilder, traceIdBaseEncodedBuilder)+import OpenTelemetry.Trace.TraceState (TraceState, empty) import Prelude hiding (takeWhile) + {- TODO: test against the conformance spec: https://github.com/w3c/trace-context@@ -57,30 +67,34 @@ , traceId :: {-# UNPACK #-} !TraceId , parentId :: {-# UNPACK #-} !SpanId , traceFlags :: {-# UNPACK #-} !TraceFlags- } deriving (Show)+ }+ deriving (Show) --- | Attempt to decode a 'SpanContext' from optional @traceparent@ and @tracestate@ header inputs.------ @since 0.0.1.0-decodeSpanContext :: - Maybe ByteString ++{- | Attempt to decode a 'SpanContext' from optional @traceparent@ and @tracestate@ header inputs.++ @since 0.0.1.0+-}+decodeSpanContext+ :: Maybe ByteString -- ^ @traceparent@ header value- -> Maybe ByteString + -> Maybe ByteString -- ^ @tracestate@ header value -> Maybe SpanContext decodeSpanContext Nothing _ = Nothing decodeSpanContext (Just traceparentHeader) mTracestateHeader = do- TraceParent{..} <- decodeTraceparentHeader traceparentHeader+ TraceParent {..} <- decodeTraceparentHeader traceparentHeader ts <- case mTracestateHeader of Nothing -> pure empty Just tracestateHeader -> pure $ decodeTracestateHeader tracestateHeader- pure $ SpanContext- { traceFlags = traceFlags- , isRemote = True- , traceId = traceId- , spanId = parentId- , traceState = ts- }+ pure $+ SpanContext+ { traceFlags = traceFlags+ , isRemote = True+ , traceId = traceId+ , spanId = parentId+ , traceState = ts+ } where decodeTraceparentHeader :: ByteString -> Maybe TraceParent decodeTraceparentHeader tp = case parseOnly traceparentParser tp of@@ -90,6 +104,7 @@ decodeTracestateHeader :: ByteString -> TraceState decodeTracestateHeader _ = empty + traceparentParser :: Parser TraceParent traceparentParser = do version <- hexadecimal@@ -108,32 +123,36 @@ -- Intentionally not consuming end of input in case of version > 0 pure $ TraceParent {..} --- | Encoded the given 'Span' into a @traceparent@, @tracestate@ tuple.------ @since 0.0.1.0++{- | Encoded the given 'Span' into a @traceparent@, @tracestate@ tuple.++ @since 0.0.1.0+-} encodeSpanContext :: Span -> IO (ByteString, ByteString) encodeSpanContext s = do ctxt <- getSpanContext s -- TODO tracestate pure (L.toStrict $ B.toLazyByteString $ traceparentHeader ctxt, "") where- traceparentHeader SpanContext{..} = + traceparentHeader SpanContext {..} = -- version- B.word8HexFixed 0 <>- B.char7 '-' <>- traceIdBaseEncodedBuilder Base16 traceId <>- B.char7 '-' <>- spanIdBaseEncodedBuilder Base16 spanId <>- B.char7 '-' <>- B.word8HexFixed (traceFlagsValue traceFlags)+ B.word8HexFixed 0+ <> B.char7 '-'+ <> traceIdBaseEncodedBuilder Base16 traceId+ <> B.char7 '-'+ <> spanIdBaseEncodedBuilder Base16 spanId+ <> B.char7 '-'+ <> B.word8HexFixed (traceFlagsValue traceFlags) --- | Propagate trace context information via headers using the w3c specification format------ @since 0.0.1.0++{- | Propagate trace context information via headers using the w3c specification format++ @since 0.0.1.0+-} w3cTraceContextPropagator :: Propagator Ctxt.Context RequestHeaders ResponseHeaders-w3cTraceContextPropagator = Propagator{..}+w3cTraceContextPropagator = Propagator {..} where- propagatorNames = [ "tracecontext" ]+ propagatorNames = ["tracecontext"] extractor hs c = do let traceParentHeader = Prelude.lookup "traceparent" hs@@ -141,15 +160,14 @@ mspanContext = decodeSpanContext traceParentHeader traceStateHeader pure $! case mspanContext of Nothing -> c- Just s -> Ctxt.insertSpan (wrapSpanContext (s { isRemote = True })) c+ Just s -> Ctxt.insertSpan (wrapSpanContext (s {isRemote = True})) c injector c hs = case Ctxt.lookupSpan c of Nothing -> pure hs Just s -> do (traceParentHeader, traceStateHeader) <- encodeSpanContext s- pure - (- ("traceparent", traceParentHeader) :- ("tracestate", traceStateHeader) :- hs+ pure+ ( ("traceparent", traceParentHeader)+ : ("tracestate", traceStateHeader)+ : hs )
test/Spec.hs view
@@ -1,2 +1,3 @@+ main :: IO () main = putStrLn "Test suite not yet implemented"