Documentation
¶
Overview ¶
Package tracez provides a minimal, primitive distributed tracing library.
tracez focuses on span collection and export without the complexity of. full OpenTelemetry. It's designed for systems that need basic distributed tracing with predictable performance and resource usage.
Core Components:.
- Tracer: Manages span lifecycle and collection.
- Span: Represents a single unit of work.
- ActiveSpan: Thread-safe wrapper for ongoing spans.
- Collector: Buffers completed spans for export.
Basic Usage:.
tracer := tracez.New()
defer tracer.Close()
// 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. Collectors are safe for concurrent span buffering. ActiveSpan SetTag/GetTag operations are safe for concurrent use.
Spans themselves are NOT thread-safe - do not modify the same. Span struct from multiple goroutines simultaneously.
Context Propagation:.
Spans are automatically linked via context.Context. Child spans inherit their parent's TraceID and reference the parent's SpanID.
Memory Management:.
Collectors automatically manage memory by shrinking buffers after. export operations. Under high load, spans may be dropped to prevent memory exhaustion - use Collector.DroppedCount() to monitor.
Resource Cleanup:.
Call tracer.Close() to properly shut down all background goroutines. Call tracer.Reset() to clear all collectors and spans.
Index ¶
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) 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 Collector ¶
type Collector struct {
// contains filtered or unexported fields
}
Collector buffers completed spans for batch export. Safe for concurrent use by multiple goroutines.
func NewCollector ¶
NewCollector creates a new collector with the specified name and buffer size. Uses the real clock for production behavior.
func (*Collector) Collect ¶
Collect attempts to buffer a span with backpressure protection. If the internal channel is full, the span is dropped and the drop counter is incremented. In sync mode, spans are collected directly for deterministic testing.
func (*Collector) DroppedCount ¶
DroppedCount returns the total number of spans dropped due to backpressure.
func (*Collector) Export ¶
Export returns a copy of all buffered spans and clears the internal buffer. The returned slice is safe to modify without affecting the collector.
func (*Collector) Reset ¶
func (c *Collector) Reset()
Reset clears all buffered spans and resets the drop counter. Does not affect the running goroutine - use close() for that.
func (*Collector) SetSyncMode ¶
SetSyncMode enables synchronous collection for testing. When enabled, spans are collected directly without using the channel. This makes tests deterministic by eliminating async behavior.
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 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) AddCollector ¶
AddCollector registers a new collector with the tracer. Users must track collector names themselves if needed.
func (*Tracer) Close ¶
func (t *Tracer) Close()
Close shuts down all collectors gracefully and cleans up ID pools. This should be called when the tracer is no longer needed.
func (*Tracer) Reset ¶
func (t *Tracer) Reset()
Reset clears all collectors' buffers without destroying them. The collectors remain registered and their goroutines continue running.