@@ -6,13 +6,16 @@ import Data.HashMap.Lazy qualified as HM
66import Data.Text qualified as Text
77import Network.URI qualified as URI
88import OpenTelemetry.Attributes qualified as Trace
9+ import OpenTelemetry.Baggage qualified as Baggage
910import OpenTelemetry.Context (Context )
11+ import OpenTelemetry.Context qualified as Context
1012import OpenTelemetry.Trace qualified as Trace
1113import OpenTelemetry.Trace.Id (TraceId )
1214import OpenTelemetry.Trace.Sampler qualified as Sampler
1315import OpenTelemetry.Trace.TraceState (TraceState )
1416import OpenTelemetry.Trace.TraceState qualified as TraceState
1517import Share.Prelude
18+ import Unison.Debug qualified as Debug
1619
1720initSampler :: IO Sampler. Sampler
1821initSampler =
@@ -27,14 +30,27 @@ initSampler =
2730 _ -> Nothing
2831 dropSample = (Sampler. Drop , HM. empty, TraceState. empty)
2932 -- Configure some custom sampling logic.
33+ shouldForceSample :: Context -> Bool
34+ shouldForceSample ctx = fromMaybe False do
35+ baggage <- Context. lookupBaggage ctx
36+ let baggageMap = Baggage. values baggage
37+ forceTraceToken <- Baggage. mkToken " force-trace"
38+ forceTrace <- Baggage. value <$> HM. lookup forceTraceToken baggageMap
39+ Debug. debugLogM Debug. Temp (" Force trace token: " <> show forceTrace)
40+ case Text. toLower forceTrace of
41+ " false" -> Just False
42+ " 0" -> Just False
43+ _ -> Just True
3044 shouldSample :: Sampler. Sampler -> Context -> TraceId -> Text -> Trace. SpanArguments -> IO (Sampler. SamplingResult , HashMap Text Trace. Attribute , TraceState )
31- shouldSample defaultSampler ctx tid name args = do
32- case (lookupTextAttribute " http.target" args >>= URI. parseURIReference . Text. unpack) <&> URI. pathSegments of
33- Just (" metrics" : _) -> pure dropSample
34- Just (" health" : _) -> pure dropSample
35- -- This is currently used in a health check.
36- Just (" users" : " zarelit" : _) -> pure dropSample
37- _ -> Sampler. shouldSample defaultSampler ctx tid name args
45+ shouldSample defaultSampler ctx tid name args
46+ | shouldForceSample ctx = Sampler. shouldSample Sampler. alwaysOn ctx tid name args
47+ | otherwise = do
48+ case (lookupTextAttribute " http.target" args >>= URI. parseURIReference . Text. unpack) <&> URI. pathSegments of
49+ Just (" metrics" : _) -> pure dropSample
50+ Just (" health" : _) -> pure dropSample
51+ -- This is currently used in a health check.
52+ Just (" users" : " zarelit" : _) -> pure dropSample
53+ _ -> Sampler. shouldSample defaultSampler ctx tid name args
3854
3955withTracer :: Text -> (Trace. Tracer -> IO c ) -> IO c
4056withTracer commitHash f =
0 commit comments