Documentation
¶
Overview ¶
Package jigar is a small, batteries-included OpenTelemetry tracing helper for Go services.
It wraps OTLP exporter setup, request-level sampling via a header, and ergonomic span helpers so application code can add tracing in a few lines without learning the full OpenTelemetry API.
Quick start:
t, err := jigar.Init(ctx,
jigar.WithService("my-service"),
jigar.WithEndpoint("localhost:4317"),
jigar.WithTraceRatio(0.01),
)
if err != nil { return err }
defer t.Shutdown(context.Background())
ctx, span := jigar.Start(ctx, "do-work")
defer span.End()
span.SetString("user.id", userID)
Per-request sampling is controlled by the X-Should-Trace header (forced on when "true") or the configured TraceRatio (probabilistic). Set TraceRatio to 1 to sample every request, or 0 to disable probabilistic sampling and only trace requests that carry the header.
Index ¶
- Constants
- Variables
- func DecideShouldTrace(headerValue string, ratio float64) bool
- func ShouldTrace(ctx context.Context) bool
- func WithShouldTrace(ctx context.Context, on bool) context.Context
- type Config
- type Option
- func WithEndpoint(endpoint string) Option
- func WithExporter(e sdktrace.SpanExporter) Option
- func WithInsecure(insecure bool) Option
- func WithProtocol(p Protocol) Option
- func WithResourceAttributes(attrs ...attribute.KeyValue) Option
- func WithService(name string) Option
- func WithServiceVersion(v string) Option
- func WithTraceRatio(ratio float64) Option
- func WithTracerName(name string) Option
- type Protocol
- type Span
- func (s *Span) AddError(err error)
- func (s *Span) End(opts ...trace.SpanEndOption)
- func (s *Span) EndWithError(err error)
- func (s *Span) SetAttributes(kv ...attribute.KeyValue)
- func (s *Span) SetBool(key string, value bool)
- func (s *Span) SetFloat(key string, value float64)
- func (s *Span) SetInt(key string, value int64)
- func (s *Span) SetJSON(key string, value any)
- func (s *Span) SetString(key, value string)
- func (s *Span) SpanID() string
- func (s *Span) TraceID() string
- func (s *Span) TraceInfo() (traceID, spanID string)
- func (s *Span) Underlying() trace.Span
- type Tracer
Constants ¶
const ShouldTraceHeader = "X-Should-Trace"
ShouldTraceHeader is the HTTP header that forces a request to be traced when set to "true", regardless of the configured sampling ratio.
Variables ¶
var ( // ErrEndpointRequired is returned by Init/New when WithEndpoint was not set. ErrEndpointRequired = errors.New("jigar: endpoint is required") // ErrServiceRequired is returned by Init/New when WithService was not set. ErrServiceRequired = errors.New("jigar: service is required") // ErrNotInitialized is returned by package-level helpers when called // before Init. ErrNotInitialized = errors.New("jigar: tracer not initialized; call jigar.Init first") )
Functions ¶
func DecideShouldTrace ¶
DecideShouldTrace returns true when the X-Should-Trace header forces sampling on ("true") or, failing that, when a draw against the configured ratio succeeds. ratio<=0 disables probabilistic sampling; ratio>=1 always samples.
Exposed so framework integrations (and tests) can reuse the same decision logic without duplicating it.
func ShouldTrace ¶
ShouldTrace reports whether the context was marked for tracing by a jigar middleware. Returns false when the value is absent.
Types ¶
type Config ¶
type Config struct {
Service string
ServiceVersion string
Endpoint string
Protocol Protocol
Insecure bool
TraceRatio float64
TracerName string
ResourceAttrs []attribute.KeyValue
// Exporter, if set, is used instead of building one from Endpoint/Protocol.
Exporter sdktrace.SpanExporter
}
Config holds tracer configuration. Build with Option functions rather than constructing this directly; the zero value is not usable.
type Option ¶
type Option func(*Config)
Option configures a Tracer.
func WithEndpoint ¶
WithEndpoint sets the OTLP collector endpoint, e.g. "localhost:4317". Required unless WithExporter is used.
func WithExporter ¶
func WithExporter(e sdktrace.SpanExporter) Option
WithExporter installs a custom span exporter, bypassing the built-in OTLP exporter. Useful for tests or non-OTLP backends.
func WithInsecure ¶
WithInsecure controls whether the OTLP connection uses TLS. Default is true, set false for collectors that require TLS.
func WithProtocol ¶
WithProtocol selects gRPC (default) or HTTP transport for the default exporter.
func WithResourceAttributes ¶
WithResourceAttributes adds extra OpenTelemetry resource attributes.
func WithService ¶
WithService sets the service name reported in spans. Required.
func WithServiceVersion ¶
WithServiceVersion sets the service version reported in spans.
func WithTraceRatio ¶
WithTraceRatio sets the probabilistic sampling rate for requests that do not carry the X-Should-Trace header. 0 disables probabilistic sampling (only header-forced requests are traced); 1 samples every request.
func WithTracerName ¶
WithTracerName overrides the OpenTelemetry tracer name. Defaults to the service name.
type Protocol ¶
type Protocol string
Protocol selects the OTLP transport used by the default exporter.
type Span ¶
type Span struct {
// contains filtered or unexported fields
}
Span is a thin wrapper around an OpenTelemetry span that:
- is safe to use when tracing is disabled (all methods become no-ops),
- exposes convenience setters for the common attribute types,
- lets you marshal arbitrary values to JSON for inspection in the UI.
Always pair Start with span.End, typically with defer.
func Start ¶
func Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, *Span)
Start begins a new span on the package's default tracer (set by Init). If the context isn't marked for tracing or Init wasn't called, the returned Span is a no-op and the context is returned unchanged.
func (*Span) AddError ¶
AddError records err on the span and marks the span as errored. No-op when err is nil or tracing is disabled.
func (*Span) End ¶
func (s *Span) End(opts ...trace.SpanEndOption)
End completes the span. No-op if tracing is disabled or the span is nil.
func (*Span) EndWithError ¶
EndWithError records err and ends the span. Equivalent to AddError + End.
func (*Span) SetAttributes ¶
SetAttributes sets one or more raw OpenTelemetry attributes on the span. Use this when none of the typed helpers fit.
func (*Span) SetJSON ¶
SetJSON JSON-marshals value and stores it as a string attribute. Useful for dumping a struct into a span for debugging. If marshaling fails the call is a no-op.
func (*Span) SpanID ¶
SpanID returns the span ID as a hex string. Returns an empty string if the span is nil, tracing is disabled, or the span context is invalid.
func (*Span) TraceID ¶
TraceID returns the trace ID as a hex string. Returns an empty string if the span is nil, tracing is disabled, or the span context is invalid.
func (*Span) TraceInfo ¶
TraceInfo returns both the trace ID and span ID as hex strings for easy linking to trace backends like Jaeger. Returns empty strings if the span is nil, tracing is disabled, or the span context is invalid.
func (*Span) Underlying ¶
Underlying returns the wrapped OpenTelemetry span. May be nil when tracing is disabled.
type Tracer ¶
type Tracer struct {
// contains filtered or unexported fields
}
Tracer is a configured OpenTelemetry tracer with an OTLP exporter and the helpers in this package. Create one with New or Init.
func Default ¶
func Default() *Tracer
Default returns the tracer installed by Init, or nil if Init has not been called. The package-level Start uses this on every span — kept lock-free.
func Init ¶
Init builds a Tracer and stores it as the package default used by the package-level Start, Default, Shutdown and the middleware sub-packages. Call this once at process startup.
Init is safe to call concurrently with reads of Default but should not be called more than once; subsequent calls return the existing tracer and ignore the new options.
func New ¶
New builds a Tracer from the given options without touching package-level state. Use this when you want explicit control of the tracer's lifetime or when wiring multiple tracers in tests.
The returned Tracer's TracerProvider is also installed as the OpenTelemetry global TracerProvider so that third-party instrumentations pick it up. Call Shutdown to flush and close.
func (*Tracer) Provider ¶
func (t *Tracer) Provider() trace.TracerProvider
Provider returns the underlying OpenTelemetry TracerProvider. Useful when wiring third-party instrumentations that take a provider explicitly.
func (*Tracer) Shutdown ¶
Shutdown flushes pending spans and shuts down the underlying provider. Safe to call on a nil receiver.
func (*Tracer) Start ¶
func (t *Tracer) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, *Span)
Start begins a new span. If the context isn't marked for tracing (see middleware or WithShouldTrace), the returned Span is a no-op. A nil receiver is safe and also yields a no-op span.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
fiber
command
Example: minimal Fiber server instrumented with jigar.
|
Example: minimal Fiber server instrumented with jigar. |
|
gin
command
Example: minimal Gin server instrumented with jigar.
|
Example: minimal Gin server instrumented with jigar. |
|
http
command
Example: minimal net/http server instrumented with jigar.
|
Example: minimal net/http server instrumented with jigar. |
|
Package fiber provides a Fiber middleware that integrates with jigar.
|
Package fiber provides a Fiber middleware that integrates with jigar. |
|
Package gin provides a Gin middleware that integrates with jigar.
|
Package gin provides a Gin middleware that integrates with jigar. |
|
Package nethttp provides a net/http middleware that integrates with jigar.
|
Package nethttp provides a net/http middleware that integrates with jigar. |