Documentation
¶
Overview ¶
Package clog provides structured log events via OpenTracing/Jaeger.
Basic usage:
tracer, err := clog.NewTracer("my-service")
if err != nil {
log.Fatal(err)
}
defer tracer.Close()
clog.SetGlobalTracer(tracer)
span, ctx := clog.StartSpanFromContext(context.Background(), "operation")
defer span.Finish()
span.LogKV("event", "work-done", "count", 42)
Environment variables set by applyJaegerDefaults at first NewTracer call (only when unset):
JAEGER_SAMPLER_TYPE const JAEGER_SAMPLER_PARAM 1 JAEGER_TRACEID_128BIT true
Reporter env vars MaxQueueSize and FlushInterval are NOT set by applyJaegerDefaults; the Jaeger client's built-in defaults (100, 1s) are used instead.
Set any of these before calling NewTracer to override the default. Use NewTracerWithOptions for programmatic control.
Index ¶
- func CloseTracer(tracer *Tracer) error
- func ExtractFromEnv(_ context.Context) opentracing.SpanContext
- func InjectToEnv(span opentracing.Span) (restore func())
- func SetGlobalTracer(tracer *Tracer)
- func SpanFromContext(ctx context.Context) opentracing.Span
- func StartSpanFromContext(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context)
- func TraceEnv(span opentracing.Span) []string
- func TraceparentOf(span opentracing.Span) string
- type Option
- type Tracer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloseTracer ¶ added in v0.1.1
CloseTracer closes a *Tracer, flushing any pending spans. Safe to call with nil — returns nil immediately.
func ExtractFromEnv ¶ added in v0.1.3
func ExtractFromEnv(_ context.Context) opentracing.SpanContext
ExtractFromEnv extracts a remote parent SpanContext from environment variables propagated by a parent process. Returns nil if no trace context is found.
Supported env vars (checked in order):
- CLOG_TRACEPARENT (W3C Trace Context, preferred)
- UBER_TRACE_ID (Jaeger legacy, backward-compatible)
CLOG_TRACEPARENT format (W3C):
00-{trace-id-32hex}-{span-id-16hex}-{flags-2hex}
Example:
00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
UBER_TRACE_ID format (Jaeger legacy):
{trace-id}:{span-id}:{parent-span-id}:{flags}
func InjectToEnv ¶ added in v0.1.3
func InjectToEnv(span opentracing.Span) (restore func())
InjectToEnv injects the span's context into environment variables so that child processes can continue the trace via StartSpanFromContext / ExtractFromEnv.
Returns a restore function that reverts the environment to its previous state. Typical usage:
span, _ := clog.StartSpanFromContext(ctx, "parent-op") defer span.Finish() restore := clog.InjectToEnv(span) defer restore() // exec child ...
Sets CLOG_TRACEPARENT (W3C format) and CLOG_BAGGAGE_* for baggage items.
For scenarios where modifying the global process environment is undesirable (e.g., constructing cmd.Env), use TraceEnv instead.
func SetGlobalTracer ¶
func SetGlobalTracer(tracer *Tracer)
SetGlobalTracer registers the tracer as the OpenTracing global tracer. Must be called before StartSpanFromContext when there is no parent span.
func SpanFromContext ¶ added in v0.1.3
func SpanFromContext(ctx context.Context) opentracing.Span
SpanFromContext returns a span from context if one exists.
func StartSpanFromContext ¶
func StartSpanFromContext(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context)
StartSpanFromContext starts a span from context and returns the span and new context with the span. If a parent span exists in the context, the new span is created as a child of it. Otherwise, if a remote parent SpanContext has been propagated via environment variables (see ExtractFromEnv), the new span is created as a child of that remote parent. Otherwise a root span is started via the global tracer.
func TraceEnv ¶ added in v0.1.4
func TraceEnv(span opentracing.Span) []string
TraceEnv returns environment variable KEY=VALUE strings representing the span's trace context, suitable for propagating to child processes.
Unlike InjectToEnv, TraceEnv does not modify the process environment. The caller controls how the variables reach the child process:
cmd.Env = append(os.Environ(), clog.TraceEnv(span)...)
Currently returns CLOG_TRACEPARENT and CLOG_BAGGAGE_* entries for baggage items. Returns nil if span is nil or context injection fails.
func TraceparentOf ¶ added in v0.2.0
func TraceparentOf(span opentracing.Span) string
TraceparentOf returns the W3C traceparent string for the given span. Returns "" if the span is nil or context injection fails.
Unlike TraceEnv, which returns KEY=VALUE pairs for process propagation, TraceparentOf returns only the traceparent value. This is useful when you need the raw traceparent string — for example, injecting into MCP _meta fields or HTTP headers:
if tp := clog.TraceparentOf(span); tp != "" {
params.SetMeta(map[string]any{"traceparent": tp})
}
Types ¶
type Option ¶ added in v0.1.1
type Option func()
Option programmatically overrides Jaeger tracer configuration. Options are applied before config.FromEnv(), so they take precedence over environment variables (but not over values set explicitly before calling NewTracerWithOptions).
func With128Bit ¶ added in v0.1.1
With128Bit enables or disables 128-bit trace IDs. Enabled by default. Pass With128Bit(false) to use legacy 64-bit trace IDs.
type Tracer ¶
type Tracer struct {
opentracing.Tracer
io.Closer
}
Tracer wraps an OpenTracing tracer with its io.Closer. Call Close to flush pending spans before the process exits.
func GlobalTracer ¶
func GlobalTracer() (tracer *Tracer)
GlobalTracer returns the previously registered global tracer, or nil if none was set or if the global tracer is not a clog.Tracer.