Categorygithub.com/getlantern/telemetry
modulepackage
0.0.0-20230523155019-be7c1d8cd8cb
Repository: https://github.com/getlantern/telemetry.git
Documentation: pkg.go.dev

# README

telemetry

Common library for any Go code that wants to interface with telemetry.

telemetry expects everything to be configured via default otel environment variables to maximize portability and flexibility.

To modify the sample rate and sampling strategy, for example, you can use:

OTEL_TRACES_SAMPLER=parentbased_traceidratio
OTEL_TRACES_SAMPLER_ARG=0.001

See https://opentelemetry-python.readthedocs.io/en/latest/sdk/trace.sampling.html

Setup

telemetry is designed to minimize the boilerplate needed to get up and running quickly. To enable it, you simply add the following:

ctx := context.Background()
closeFunc := telemetry.EnableOTELTracing(ctx)
defer func() { _ = closeFunc(ctx) }()

From that point on, tracing is configured for you, and you can use it as normal. For example, you can run:

tracer := otel.Tracer("my-tracer")

Sampling

This library also contains convenience functions for forcing certain traces to be sampled. For example, to always sample HTTP requests with a specific HTTP header and value, you can run:

telemetry.AlwaysSampleHeaderHandler("name", "value", otelhttp.NewHandler(...)))

If the value is "*", then it will always sample requests that have the header set to any value.

telemetry includes this ability to force traces to be sampled more generally, though. If you want to force a trace to be sampled, you can call the following to add forced sampling to its context:

ctx := telemetry.AlwaysSample(r.Context())

That should be done for the root span.

# Functions

AlwaysSample returns a context that will always be sampled by the sampler.
AlwaysSampleHeaderHandler wraps the passed handler and always samples requests that have the specified header set to the specified value.
AlwaysSampleHTTPHeader returns a ForceSampleFilter that will always sample requests that have the specified header set to the specified value.
EnableOTELMetrics enables OTEL metrics, configuring the OTEL provider using environment variables.
Sample rates should be configured via environment variables.
ForceableSampler returns a Sampler that uses the sampler from the environment but that checks the parent context for a special key that overrides the sampler to always sample.
NewHandler wraps the passed handler and allows callers to set rules for things that should always be sampled.

# Interfaces

No description provided by the author