Documentation
¶
Overview ¶
Package tracez provides a minimal, primitive distributed tracing library.
tracez focuses on span creation and processing without the complexity of full OpenTelemetry. It's designed for systems that need basic distributed tracing with predictable performance and zero memory overhead when unused.
Core Components:
- Tracer: Manages span lifecycle and handlers.
- Span: Represents a single unit of work.
- ActiveSpan: Thread-safe wrapper for ongoing spans.
- SpanHandler: Callback function invoked when spans complete.
Basic Usage:
tracer := tracez.New()
defer tracer.Close()
// Register a handler for completed spans.
tracer.OnSpanComplete(func(span Span) {
log.Printf("%s: %v", span.Name, span.Duration)
})
// Start a new span.
ctx, span := tracer.StartSpan(ctx, "operation-name")
defer span.Finish()
// Add metadata.
span.SetTag("user.id", "123")
// Pass context to child operations.
childCtx, childSpan := tracer.StartSpan(ctx, "child-operation")
defer childSpan.Finish()
Thread Safety:
Tracer is safe for concurrent use by multiple goroutines. Handler registration/removal is thread-safe. ActiveSpan SetTag/GetTag operations are safe for concurrent use. Handlers receive immutable span copies, safe for any use.
Context Propagation:
Spans are automatically linked via context.Context. Child spans inherit their parent's TraceID and reference the parent's SpanID.
Memory Management:
Zero memory overhead when no handlers are registered. Handlers receive span copies by value to prevent data races. Optional worker pool for bounded async handler execution.
Resource Cleanup:
Call tracer.Close() to properly shut down worker pools and handlers.
Index ¶
- type ActiveSpan
- func (a *ActiveSpan) Context(parent context.Context) context.Context
- func (a *ActiveSpan) Finish()
- func (a *ActiveSpan) GetTag(key Tag) (string, bool)
- func (a *ActiveSpan) SetBoolTag(key Tag, value bool)
- func (a *ActiveSpan) SetIntTag(key Tag, value int)
- func (a *ActiveSpan) SetTag(key Tag, value string)
- func (a *ActiveSpan) SpanID() string
- func (a *ActiveSpan) TraceID() string
- type IDPool
- type Key
- type Span
- type SpanHandler
- type Tag
- type Tracer
- func (t *Tracer) Close()
- func (t *Tracer) DroppedSpans() uint64
- func (t *Tracer) EnableWorkerPool(workers, queueSize int) error
- func (t *Tracer) HasHandlers() bool
- func (t *Tracer) OnSpanComplete(handler SpanHandler) uint64
- func (t *Tracer) OnSpanCompleteAsync(handler SpanHandler) uint64
- func (t *Tracer) RemoveHandler(id uint64)
- func (t *Tracer) SetPanicHook(hook func(handlerID uint64, r interface{}))
- func (t *Tracer) StartSpan(ctx context.Context, operation Key) (context.Context, *ActiveSpan)
- func (*Tracer) WithClock(clock clockz.Clock) *Tracer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ActiveSpan ¶
type ActiveSpan struct {
// contains filtered or unexported fields
}
ActiveSpan wraps a Span with thread-safe tag operations and lifecycle management. Safe for concurrent use by multiple goroutines.
func (*ActiveSpan) Context ¶
func (a *ActiveSpan) Context(parent context.Context) context.Context
Context creates a new context with this span embedded. The returned context can be used to start child spans.
func (*ActiveSpan) Finish ¶
func (a *ActiveSpan) Finish()
Finish completes the span and sends it to the tracer for collection. Safe to call multiple times - subsequent calls are no-ops.
func (*ActiveSpan) GetTag ¶
func (a *ActiveSpan) GetTag(key Tag) (string, bool)
GetTag retrieves a tag value by key. Thread-safe for concurrent access.
func (*ActiveSpan) SetBoolTag ¶ added in v0.0.7
func (a *ActiveSpan) SetBoolTag(key Tag, value bool)
SetBoolTag adds a boolean key-value pair to the span. The boolean value is converted to a string for storage. Thread-safe for concurrent access. No-op if span is already finished.
func (*ActiveSpan) SetIntTag ¶ added in v0.0.7
func (a *ActiveSpan) SetIntTag(key Tag, value int)
SetIntTag adds an integer key-value pair to the span. The integer value is converted to a string for storage. Thread-safe for concurrent access. No-op if span is already finished.
func (*ActiveSpan) SetTag ¶
func (a *ActiveSpan) SetTag(key Tag, value string)
SetTag adds a key-value pair to the span. Thread-safe for concurrent access. No-op if span is already finished.
func (*ActiveSpan) SpanID ¶
func (a *ActiveSpan) SpanID() string
SpanID returns the span ID of this span. Thread-safe for concurrent access.
func (*ActiveSpan) TraceID ¶
func (a *ActiveSpan) TraceID() string
TraceID returns the trace ID of this span. Thread-safe for concurrent access.
type IDPool ¶
type IDPool struct {
// contains filtered or unexported fields
}
IDPool manages a pool of pre-generated IDs to amortize crypto/rand overhead.
type Span ¶
type Span struct {
Tags map[Tag]string `json:"tags,omitempty"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time,omitempty"`
Duration time.Duration `json:"duration"`
TraceID string `json:"trace_id"`
SpanID string `json:"span_id"`
ParentID string `json:"parent_id,omitempty"`
Name string `json:"name"`
}
Span represents a single unit of work in a distributed trace. Spans are NOT thread-safe - do not modify from multiple goroutines.
type SpanHandler ¶ added in v0.0.6
type SpanHandler func(span Span)
SpanHandler is called when a span completes.
type Tracer ¶
type Tracer struct {
// contains filtered or unexported fields
}
Tracer manages span lifecycle and collection. Safe for concurrent use by multiple goroutines.
func New ¶
func New() *Tracer
New creates a new tracer. Uses the real clock for production behavior.
func (*Tracer) Close ¶
func (t *Tracer) Close()
Close shuts down the tracer gracefully and cleans up resources. This should be called when the tracer is no longer needed.
func (*Tracer) DroppedSpans ¶ added in v0.0.6
DroppedSpans returns the number of spans dropped due to full worker queue.
func (*Tracer) EnableWorkerPool ¶ added in v0.0.6
EnableWorkerPool creates a bounded worker pool for async handlers.
func (*Tracer) HasHandlers ¶ added in v0.0.9
HasHandlers returns true if any handlers are registered. Useful for checking if tracing is actively being collected.
func (*Tracer) OnSpanComplete ¶ added in v0.0.6
func (t *Tracer) OnSpanComplete(handler SpanHandler) uint64
OnSpanComplete registers a synchronous handler called when spans complete.
func (*Tracer) OnSpanCompleteAsync ¶ added in v0.0.6
func (t *Tracer) OnSpanCompleteAsync(handler SpanHandler) uint64
OnSpanCompleteAsync registers an asynchronous handler called when spans complete.
func (*Tracer) RemoveHandler ¶ added in v0.0.6
RemoveHandler removes a handler by ID.
func (*Tracer) SetPanicHook ¶ added in v0.0.6
SetPanicHook sets a function to be called when a handler panics.