Documentation
¶
Overview ¶
Package telemetry provides OpenTelemetry export for session recordings. This enables exporting session events as distributed traces to observability platforms.
Index ¶
- func ContextWithTrace(ctx context.Context, tc TraceContext) context.Context
- func InjectTraceHeaders(ctx context.Context, req *http.Request)
- func TraceMiddleware(next http.Handler) http.Handler
- type EventConverter
- type Exporter
- type HTTPClient
- type OTLPExporter
- type OTLPExporterOption
- type Resource
- type Span
- type SpanEvent
- type SpanKind
- type SpanStatus
- type StatusCode
- type TraceContext
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextWithTrace ¶ added in v1.3.1
func ContextWithTrace(ctx context.Context, tc TraceContext) context.Context
ContextWithTrace stores a TraceContext in a Go context.
func InjectTraceHeaders ¶ added in v1.3.1
InjectTraceHeaders writes trace headers from the context onto an outbound HTTP request. It is a no-op if the context contains no trace data.
Types ¶
type EventConverter ¶
type EventConverter struct {
// Resource is the resource to attach to spans.
Resource *Resource
}
EventConverter converts runtime events to OTLP spans.
func NewEventConverter ¶
func NewEventConverter(resource *Resource) *EventConverter
NewEventConverter creates a new event converter.
func (*EventConverter) ConvertSession ¶
func (c *EventConverter) ConvertSession( sessionID string, sessionEvents []events.Event, ) ([]*Span, error)
ConvertSession converts a session's events to spans. The session becomes the root span, with pipeline/middleware/provider calls as child spans.
func (*EventConverter) ConvertSessionWithParent ¶ added in v1.3.1
func (c *EventConverter) ConvertSessionWithParent( sessionID string, sessionEvents []events.Event, traceCtx *TraceContext, ) ([]*Span, error)
ConvertSessionWithParent converts a session's events to spans, using the provided trace context as the parent trace instead of generating a fresh one from session ID. If traceCtx is nil or has an empty Traceparent, it falls back to ConvertSession behavior.
type Exporter ¶
type Exporter interface {
// Export sends events to the backend.
Export(ctx context.Context, spans []*Span) error
// Shutdown performs cleanup and flushes any pending data.
Shutdown(ctx context.Context) error
}
Exporter exports session events to an observability backend.
type HTTPClient ¶
HTTPClient interface for testing. NOSONAR(godre:S8196) - HTTPClient is clearer than Doer for this use case.
type OTLPExporter ¶
type OTLPExporter struct {
// contains filtered or unexported fields
}
OTLPExporter exports spans to an OTLP endpoint over HTTP.
func NewOTLPExporter ¶
func NewOTLPExporter(endpoint string, opts ...OTLPExporterOption) *OTLPExporter
NewOTLPExporter creates a new OTLP exporter.
type OTLPExporterOption ¶
type OTLPExporterOption func(*OTLPExporter)
OTLPExporterOption configures an OTLPExporter.
func WithBatchSize ¶
func WithBatchSize(size int) OTLPExporterOption
WithBatchSize sets the batch size for exports.
func WithHTTPClient ¶
func WithHTTPClient(client HTTPClient) OTLPExporterOption
WithHTTPClient sets a custom HTTP client.
func WithHeaders ¶
func WithHeaders(headers map[string]string) OTLPExporterOption
WithHeaders sets custom headers for OTLP requests.
func WithResource ¶
func WithResource(resource *Resource) OTLPExporterOption
WithResource sets the resource for exported spans.
type Resource ¶
type Resource struct {
// Attributes are key-value pairs describing the resource.
Attributes map[string]interface{} `json:"attributes"`
}
Resource represents the entity producing telemetry.
func DefaultResource ¶
func DefaultResource() *Resource
DefaultResource returns a default resource for PromptKit.
func ResourceWithPackID ¶ added in v1.3.1
ResourceWithPackID returns a default resource with the pack.id attribute set.
type Span ¶
type Span struct {
// TraceID is the unique identifier for the trace (16 bytes, hex-encoded).
TraceID string `json:"traceId"`
// SpanID is the unique identifier for this span (8 bytes, hex-encoded).
SpanID string `json:"spanId"`
// ParentSpanID is the ID of the parent span (empty for root spans).
ParentSpanID string `json:"parentSpanId,omitempty"`
// Name is the operation name.
Name string `json:"name"`
// Kind is the span kind (client, server, producer, consumer, internal).
Kind SpanKind `json:"kind"`
// StartTime is when the span started.
StartTime time.Time `json:"startTimeUnixNano"`
// EndTime is when the span ended.
EndTime time.Time `json:"endTimeUnixNano"`
// Attributes are key-value pairs associated with the span.
Attributes map[string]interface{} `json:"attributes,omitempty"`
// Status is the span status.
Status *SpanStatus `json:"status,omitempty"`
// Events are timestamped events within the span.
Events []*SpanEvent `json:"events,omitempty"`
}
Span represents a trace span in OpenTelemetry format.
type SpanEvent ¶
type SpanEvent struct {
// Name is the event name.
Name string `json:"name"`
// Time is when the event occurred.
Time time.Time `json:"timeUnixNano"`
// Attributes are key-value pairs associated with the event.
Attributes map[string]interface{} `json:"attributes,omitempty"`
}
SpanEvent represents an event within a span.
type SpanStatus ¶
type SpanStatus struct {
// Code is the status code (0=Unset, 1=Ok, 2=Error).
Code StatusCode `json:"code"`
// Message is the status message.
Message string `json:"message,omitempty"`
}
SpanStatus represents the status of a span.
type StatusCode ¶
type StatusCode int
StatusCode represents the status of a span.
const ( StatusCodeUnset StatusCode = 0 StatusCodeOk StatusCode = 1 StatusCodeError StatusCode = 2 )
Status codes.
type TraceContext ¶ added in v1.3.1
type TraceContext struct {
Traceparent string // W3C traceparent header
Tracestate string // W3C tracestate header
XRayTraceID string // AWS X-Ray X-Amzn-Trace-Id header
}
TraceContext holds distributed trace headers extracted from an inbound HTTP request.
func ExtractTraceContext ¶ added in v1.3.1
func ExtractTraceContext(r *http.Request) TraceContext
ExtractTraceContext reads trace headers from an inbound HTTP request. Invalid traceparent values are silently discarded.
func TraceContextFromContext ¶ added in v1.3.1
func TraceContextFromContext(ctx context.Context) TraceContext
TraceContextFromContext retrieves a TraceContext from a Go context. Returns an empty TraceContext if none is stored.
func (TraceContext) IsEmpty ¶ added in v1.3.1
func (tc TraceContext) IsEmpty() bool
IsEmpty returns true when no trace data is present.