tracer

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 27 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package tracer provides convenience functions for OpenTelemetry tracing.

It offers two main capabilities:

1. A "Start" function that simplifies trace creation by automatically determining the tracer and span names from the calling function's package and name.

2. Functions for setting up tracing: an HTTP middleware (NewHandler) and a TracerProvider (NewProvider) that exports over OTLP, to Google Cloud Trace or to an endpoint of the caller's own, and propagates W3C trace context.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHandler

func NewHandler(opts ...HandlerOption) func(http.Handler) http.Handler

NewHandler creates the HTTP middleware for OpenTelemetry tracing: a server span per request, continued from the trace context the request carries (read with Propagator: W3C traceparent, or the legacy X-Cloud-Trace-Context header a caller still sends), named by the request's URL path. The returned function wraps an http.Handler. Surfaces sets a per-surface trace setting, and WithOTelHTTPOptions passes options of the instrumentation itself.

func Propagator

func Propagator() propagation.TextMapPropagator

Propagator is the context propagator the handler uses and the provider sets as the global one: W3C Trace Context (traceparent, tracestate) and Baggage, with the legacy X-Cloud-Trace-Context header read as well, so a caller that sends only it continues its trace; when both headers arrive, traceparent wins. Outgoing requests carry the W3C headers alone.

Types

type HandlerOption

type HandlerOption func(*handlerConfig)

HandlerOption configures NewHandler.

func Surfaces

func Surfaces(table map[string]Traces) HandlerOption

Surfaces declares how each surface's spans are sampled, keyed by the path the router mounts the surface at: a prefix such as /droids/ or /beacons/, or a route pattern such as /api/widgets/{widgetID}/content, where a segment in braces matches any one segment. As a request starts, the handler finds the longest declared surface the request's path sits under and hands its setting to the sampler the provider built, which applies it as the span starts; a request under no declared surface follows the front end. The generated router passes the table from the declarations in the generator program.

func WithOTelHTTPOptions

func WithOTelHTTPOptions(opts ...otelhttp.Option) HandlerOption

WithOTelHTTPOptions adds options of the OpenTelemetry HTTP instrumentation (otelhttp) to the handler, after the handler's own: a filter, a span name formatter of the caller's, or the tracer provider to start spans from in a test.

type Provider

type Provider struct {
	*sdktrace.TracerProvider
}

Provider is an OpenTelemetry Provider. It provides Tracers to instrumentation so it can trace operational flow through a system.

This wrapper is used to keep otel dependency management contained in this package. Cordinating package versions between otel/sdk/resource package and the otel/semconv package is painful, so eliminating the need to import otel as a direct import in your project helps to keep the version cordination in one place.

func NewProvider

func NewProvider(serviceName string, opts ...ProviderOption) (*Provider, error)

NewProvider creates the OpenTelemetry TracerProvider for a process and sets it as the global one, with Propagator (W3C Trace Context, the legacy X-Cloud-Trace-Context header read as well) as the global propagator, so every span the process starts (Start) is recorded through it. The spans carry the service name; where they go is the destination option, WithGoogleCloud or WithEndpoint, and which are recorded is WithSampling (the edge's choice without it). A provider with no destination is refused.

type ProviderOption

type ProviderOption func(*providerConfig)

ProviderOption configures the Provider.

func WithEndpoint

func WithEndpoint(hostport string) ProviderOption

WithEndpoint sends the spans to an OTLP gRPC endpoint (host:port) of the caller's own: a collector, or another cloud's OTLP intake. Over TLS with the default credentials or WithTokenSource, unless WithInsecure.

func WithGoogleCloud

func WithGoogleCloud(projectID string) ProviderOption

WithGoogleCloud sends the spans to Google Cloud Trace, into the project given: over OTLP to the Telemetry API, signed with the application's default credentials (or WithTokenSource), the resource carrying what the Google Cloud detector finds about where the process runs. The application's identity needs roles/telemetry.tracesWriter on the project, and the project the Telemetry API (telemetry.googleapis.com) enabled.

func WithInsecure

func WithInsecure() ProviderOption

WithInsecure sends without TLS and without credentials, for a collector on a loopback or private address; the Telemetry API refuses it.

func WithSampling

func WithSampling(s Sampling) ProviderOption

WithSampling says which spans are recorded; SamplingEdge without it.

func WithTokenSource

func WithTokenSource(ts oauth2.TokenSource) ProviderOption

WithTokenSource signs the calls to the Telemetry API with a token source of the caller's own in place of the application's default credentials: a process whose identity is not the environment's, such as a device's.

func WithTracerProviderOptions

func WithTracerProviderOptions(opts ...sdktrace.TracerProviderOption) ProviderOption

WithTracerProviderOptions adds OpenTelemetry SDK tracer provider options.

type Sampling

type Sampling string

Sampling says which spans are recorded.

const (
	// SamplingEdge records a request's spans when the trace context the caller sent says
	// the request is sampled, and no others; a span with no parent is never recorded.
	// Google's load balancers and Cloud Run sample a share of the requests at the edge
	// and say so in the headers they forward, so an environment on this setting traces
	// what the edge traces. The default.
	SamplingEdge Sampling = "edge"
	// SamplingAll records every span, whatever the caller said: an environment where
	// every request should be traceable, a pull request's or a test one.
	SamplingAll Sampling = "all"
)

func ParseSampling

func ParseSampling(value string) (Sampling, error)

ParseSampling reads a Sampling from configuration: "edge", "all", or "" for the default.

type Span

type Span = trace.Span

Span is the individual component of a trace. It represents a single named and timed operation of a workflow that is traced. A Tracer is used to create a Span and it is then up to the operation the Span represents to properly end the Span when the operation itself ends.

func Start

func Start(ctx context.Context) (newCtx context.Context, span Span)

Start uses runtime reflection to determine the fully qualified package path and the function/method name of the caller.

The Tracer name is set to the fully qualified package path (e.g., "github.com/impulseframework/impulse"). The Span name is set to the short function name (e.g., "Struct.Method()").

type Traces

type Traces struct {
	// contains filtered or unexported fields
}

Traces says how one surface's spans are sampled. It narrows the provider's Sampling and never widens it: a surface follows the front end, is capped at a rate, or is off. The generated router declares each surface's setting by its path (Surfaces), and the sampler the provider builds applies the setting as the request's span starts, since a span's sampled flag is fixed then and every child span, log line and outgoing header inherits it.

func TracesCapped

func TracesCapped(rate float64) Traces

TracesCapped returns the setting that records a surface's span when the provider's Sampling would and the trace falls under the rate, decided by its trace ID so one trace is kept or dropped the same way wherever it is capped at the rate, for a chatty surface the front end traces too often. The rate must be above 0 and at most 1; any other value is refused with a panic, because the setting is a declaration and a bad rate is a mistake in the code that declares it.

func TracesFollowFrontEnd

func TracesFollowFrontEnd() Traces

TracesFollowFrontEnd returns the setting that records a surface's spans as the provider's Sampling alone decides: when the front end sampled the request, or every request under SamplingAll. It is today's behavior and what a surface that declares nothing gets.

func TracesOff

func TracesOff() Traces

TracesOff returns the setting that records no span for the surface, whatever the caller sent: a health check, or a streaming surface whose fetches are not worth a trace.

func (Traces) String

func (t Traces) String() string

String names the setting the way a chain comment reads it: "follow the front end", "capped at 0.1" or "off".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL