Documentation
¶
Overview ¶
Package trace provides a dependency-free tracing core for GOWDK Runtime and plain Go applications.
Index ¶
- Constants
- func ContextWithTraceContext(ctx context.Context, traceContext TraceContext) context.Context
- func ContextWithTracer(ctx context.Context, tracer *Tracer) context.Context
- func Extract(ctx context.Context, carrier Carrier) context.Context
- func Inject(ctx context.Context, carrier Carrier)
- func Traceparent(traceContext TraceContext) string
- type Attribute
- type Carrier
- type Collector
- type ConsoleSink
- type CountedSampler
- type Event
- type Exporter
- type JSONLSink
- type Lane
- type OTLPSpan
- type RingSink
- type Sampler
- type SamplingContext
- type Sink
- type Snapshot
- type SourceRef
- type Span
- func (span *Span) End()
- func (span *Span) EndTime(t time.Time)
- func (span *Span) Event(level string, message string, attrs map[string]any)
- func (span *Span) Set(key string, value any)
- func (span *Span) SetStatus(code StatusCode, message string)
- func (span *Span) Snapshot() Snapshot
- func (span *Span) TraceContext() TraceContext
- type SpanID
- type StartOption
- type Status
- type StatusCode
- type Surface
- type TraceContext
- type TraceID
- type Tracer
- type TracerOption
Constants ¶
const ( AttrGOWDKSurface = "gowdk.surface" AttrGOWDKLane = "gowdk.lane" AttrGOWDKSourceFile = "gowdk.source.file" AttrGOWDKSourceLine = "gowdk.source.line" AttrGOWDKSourceCol = "gowdk.source.column" AttrHTTPRoute = "http.route" AttrHTTPRequestMethod = "http.request.method" AttrHTTPResponseStatusCode = "http.response.status_code" AttrURLPath = "url.path" AttrServerAddress = "server.address" )
Variables ¶
This section is empty.
Functions ¶
func ContextWithTraceContext ¶
func ContextWithTraceContext(ctx context.Context, traceContext TraceContext) context.Context
ContextWithTraceContext returns a context carrying trace identity.
func ContextWithTracer ¶
ContextWithTracer returns a context carrying tracer for child spans started by helper packages such as runtime/app and runtime/contracts.
func Traceparent ¶
func Traceparent(traceContext TraceContext) string
Traceparent encodes traceContext as a W3C traceparent header.
Types ¶
type Carrier ¶
Carrier is the minimal interface implemented by http.Header and compatible request metadata maps.
type Collector ¶
type Collector struct {
// contains filtered or unexported fields
}
Collector combines an in-memory ring sink with a small HTTP JSON/SSE handler for local inspection.
func NewCollector ¶
NewCollector creates a collector backed by a RingSink.
func (*Collector) Handler ¶
Handler serves recent spans as JSON. Requests to /events or requests with an Accept header containing text/event-stream receive an SSE stream of existing and future spans. POST requests accept one Snapshot or a JSON array of Snapshots so generated browser runtimes can join frontend spans to backend traces without another collector dependency.
func (*Collector) RecordSpan ¶
RecordSpan implements Sink.
func (*Collector) ViewerHandler ¶
ViewerHandler serves a self-contained local trace viewer plus JSON and SSE endpoints. It is intentionally unprotected; generated apps must mount it behind an access gate when enabling it outside dev.
type ConsoleSink ¶
type ConsoleSink struct {
// contains filtered or unexported fields
}
ConsoleSink writes one readable line per completed span.
func NewConsoleSink ¶
func NewConsoleSink(writer io.Writer) *ConsoleSink
NewConsoleSink creates a console sink.
func (*ConsoleSink) RecordSpan ¶
func (sink *ConsoleSink) RecordSpan(ctx context.Context, span Snapshot) error
RecordSpan implements Sink.
type CountedSampler ¶
type CountedSampler struct {
N uint64
// contains filtered or unexported fields
}
CountedSampler is a deterministic test helper that samples every n-th span.
func (*CountedSampler) Sample ¶
func (sampler *CountedSampler) Sample(SamplingContext) bool
Sample implements Sampler.
type Event ¶
type Event struct {
Time time.Time `json:"time"`
Level string `json:"level,omitempty"`
Message string `json:"message"`
Attributes []Attribute `json:"attributes,omitempty"`
}
Event records a timestamped span event.
type JSONLSink ¶
type JSONLSink struct {
// contains filtered or unexported fields
}
JSONLSink writes one JSON object per completed span.
func NewJSONLSink ¶
NewJSONLSink creates a JSON Lines sink.
type OTLPSpan ¶
type OTLPSpan struct {
TraceID TraceID `json:"traceId"`
SpanID SpanID `json:"spanId"`
ParentSpanID SpanID `json:"parentSpanId,omitempty"`
Name string `json:"name"`
StartTimeUnixNano int64 `json:"startTimeUnixNano"`
EndTimeUnixNano int64 `json:"endTimeUnixNano"`
Attributes []Attribute `json:"attributes,omitempty"`
Events []Event `json:"events,omitempty"`
Status Status `json:"status,omitempty"`
}
OTLPSpan is intentionally shaped like the OTLP span model while remaining a dependency-free value type.
func OTLPSpanFromSnapshot ¶
OTLPSpanFromSnapshot converts a completed span to the OTLP-like model.
type RingSink ¶
type RingSink struct {
// contains filtered or unexported fields
}
RingSink stores the most recent completed spans in memory. On overflow it drops the oldest span and increments Dropped.
func NewRingSink ¶
NewRingSink creates a bounded in-memory sink.
func (*RingSink) RecordSpan ¶
RecordSpan implements Sink. It never blocks on external I/O.
type Sampler ¶
type Sampler interface {
Sample(SamplingContext) bool
}
Sampler decides whether a span should be recorded.
func RatioSampler ¶
RatioSampler returns a deterministic trace-id based sampler. Ratios <= 0 are always off; ratios >= 1 are always on.
type SamplingContext ¶
type SamplingContext struct {
TraceID TraceID
ParentSpanID SpanID
Name string
Surface Surface
Lane Lane
Attributes []Attribute
}
SamplingContext is passed to samplers before a span is allocated.
type Sink ¶
Sink receives completed span snapshots.
func ExporterSink ¶
ExporterSink adapts an Exporter into a Sink.
type Snapshot ¶
type Snapshot struct {
TraceID TraceID `json:"traceId"`
SpanID SpanID `json:"spanId"`
ParentSpanID SpanID `json:"parentSpanId,omitempty"`
Name string `json:"name"`
Surface Surface `json:"surface,omitempty"`
Lane Lane `json:"lane,omitempty"`
Source SourceRef `json:"source,omitempty"`
Attributes []Attribute `json:"attributes,omitempty"`
Events []Event `json:"events,omitempty"`
Status Status `json:"status,omitempty"`
StartTime time.Time `json:"startTime"`
EndTime time.Time `json:"endTime"`
DurationNS int64 `json:"durationNs"`
}
Snapshot is the immutable JSON/export shape of a completed span.
type SourceRef ¶
type SourceRef struct {
File string `json:"file,omitempty"`
Line int `json:"line,omitempty"`
Column int `json:"column,omitempty"`
OwnerKind string `json:"ownerKind,omitempty"`
OwnerID string `json:"ownerId,omitempty"`
}
SourceRef points a span back to GOWDK source or generated runtime metadata.
type Span ¶
type Span struct {
// contains filtered or unexported fields
}
Span records one sampled unit of work. Methods are nil-safe so callers can defer span.End() even when sampling is disabled.
func (*Span) End ¶
func (span *Span) End()
End completes the span and sends an immutable snapshot to the configured sink. Calling End more than once is safe.
func (*Span) SetStatus ¶
func (span *Span) SetStatus(code StatusCode, message string)
SetStatus records the final span status.
func (*Span) TraceContext ¶
func (span *Span) TraceContext() TraceContext
TraceContext returns the span's W3C trace identity.
type SpanID ¶
type SpanID string
SpanID is a W3C Trace Context parent-id/span-id: 8 bytes encoded as 16 lowercase hexadecimal characters.
type StartOption ¶
type StartOption func(*startConfig)
StartOption configures one span.
func WithAttributes ¶
func WithAttributes(attrs map[string]any) StartOption
WithAttributes records initial span attributes.
func WithSource ¶
func WithSource(source SourceRef) StartOption
WithSource records a source reference.
func WithStartTime ¶
func WithStartTime(start time.Time) StartOption
WithStartTime sets a deterministic start time.
func WithSurface ¶
func WithSurface(surface Surface) StartOption
WithSurface records a span surface.
func WithTracer ¶
func WithTracer(tracer *Tracer) StartOption
WithTracer starts a span with tracer instead of the default tracer.
type Status ¶
type Status struct {
Code StatusCode `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
Status records the final span status.
type StatusCode ¶
type StatusCode string
StatusCode follows the OpenTelemetry span status shape without importing the OpenTelemetry SDK.
const ( StatusUnset StatusCode = "unset" StatusOK StatusCode = "ok" StatusError StatusCode = "error" )
type TraceContext ¶
TraceContext is the trace identity stored in context.Context and encoded in W3C traceparent headers.
func ParseTraceparent ¶
func ParseTraceparent(value string) (TraceContext, error)
ParseTraceparent parses a W3C traceparent header.
func TraceContextFromContext ¶
func TraceContextFromContext(ctx context.Context) (TraceContext, bool)
TraceContextFromContext returns trace identity from ctx when present.
type TraceID ¶
type TraceID string
TraceID is a W3C Trace Context trace-id: 16 bytes encoded as 32 lowercase hexadecimal characters.
type Tracer ¶
type Tracer struct {
// contains filtered or unexported fields
}
Tracer owns sampling and completed-span delivery.
func TracerFromContext ¶
TracerFromContext returns the tracer stored in ctx, or the tracer that owns the active sampled span.
type TracerOption ¶
type TracerOption func(*Tracer)
TracerOption configures a Tracer.
func WithSampler ¶
func WithSampler(sampler Sampler) TracerOption
WithSampler sets the sampler. Nil means AlwaysOn.