Skip to content

Commit 5f3ad76

Browse files
committed
always force OTEL sampling
1 parent a251822 commit 5f3ad76

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/Share/Telemetry/Setup.hs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import Data.HashMap.Lazy qualified as HM
66
import Data.Text qualified as Text
77
import Network.URI qualified as URI
88
import OpenTelemetry.Attributes qualified as Trace
9+
import OpenTelemetry.Baggage qualified as Baggage
910
import OpenTelemetry.Context (Context)
11+
import OpenTelemetry.Context qualified as Context
1012
import OpenTelemetry.Trace qualified as Trace
1113
import OpenTelemetry.Trace.Id (TraceId)
1214
import OpenTelemetry.Trace.Sampler qualified as Sampler
1315
import OpenTelemetry.Trace.TraceState (TraceState)
1416
import OpenTelemetry.Trace.TraceState qualified as TraceState
1517
import Share.Prelude
18+
import Unison.Debug qualified as Debug
1619

1720
initSampler :: IO Sampler.Sampler
1821
initSampler =
@@ -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

3955
withTracer :: Text -> (Trace.Tracer -> IO c) -> IO c
4056
withTracer commitHash f =

0 commit comments

Comments
 (0)