Documentation
¶
Overview ¶
Package otel implements W3C Trace Context (`traceparent`) propagation and a non-blocking, zero-dependency OpenTelemetry OTLP/HTTP JSON span exporter (`POST <OTEL_EXPORTER_OTLP_ENDPOINT>/v1/traces`).
Configured via standard OpenTelemetry environment variables (`OTEL_EXPORTER_OTLP_ENDPOINT`, `OTEL_EXPORTER_OTLP_HEADERS`, `OTEL_SERVICE_NAME`), allowing Go server, agent, and self-hosted hub nodes to stream child HTTP/2 + `httptrace` spans directly to Grafana Tempo, Jaeger, Honeycomb, Axiom, SigNoz, or any OpenTelemetry Collector without adding external SDK dependencies to `go.mod`.
Index ¶
- func BuildOTLPJSON(serviceName, serviceVersion string, spans []Span) ([]byte, error)
- func FormatTraceparent(tc TraceContext) string
- func ParseHeaders(raw string) map[string]string
- func RandomHex(byteLen int) string
- func ResolveTracesURL(endpoint string) string
- type Exporter
- type Span
- type TraceContext
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildOTLPJSON ¶
BuildOTLPJSON serializes `spans` into a standard OpenTelemetry OTLP/HTTP JSON `ExportTraceServiceRequest`.
func FormatTraceparent ¶
func FormatTraceparent(tc TraceContext) string
FormatTraceparent formats `tc` as a W3C `traceparent` header value.
func ParseHeaders ¶
ParseHeaders parses standard `OTEL_EXPORTER_OTLP_HEADERS` (`k1=v1,k2=v2`, supporting URL-encoded values).
func ResolveTracesURL ¶
ResolveTracesURL normalizes `OTEL_EXPORTER_OTLP_ENDPOINT` onto its `/v1/traces` HTTP URL.
Types ¶
type Exporter ¶
type Exporter struct {
// contains filtered or unexported fields
}
Exporter batches and flushes OTLP/HTTP JSON spans asynchronously off the crawl critical path.
func NewExporterFromEnv ¶
func NewExporterFromEnv(getenv func(string) string, defaultService, version string, transport http.RoundTripper) *Exporter
NewExporterFromEnv returns a live `*Exporter` when `OTEL_EXPORTER_OTLP_ENDPOINT` is configured (or when `EDGEFETCH_OTEL_STDOUT=1` / `OTEL_EXPORTER_OTLP_ENDPOINT=stdout` is set for local issue debugging), or `nil` when unset.
type Span ¶
type Span struct {
TraceID string
SpanID string
ParentSpanID string
Name string
Kind int // 1=INTERNAL, 2=SERVER, 3=CLIENT
Start time.Time
End time.Time
StatusOK bool
ErrorMessage string
Attributes map[string]any
}
Span represents one OpenTelemetry trace span ready for OTLP/HTTP JSON export.
type TraceContext ¶
TraceContext holds W3C Trace Context identifiers (`00-<TraceID>-<SpanID>-<Flags>`).
func ExtractOrNew ¶
func ExtractOrNew(h http.Header) TraceContext
ExtractOrNew inspects `traceparent` (or fallback `X-Trace-Id` / `X-Span-Id`) on `h`, creating a new root trace when absent.
func ParseTraceparent ¶
func ParseTraceparent(raw string) (TraceContext, bool)
ParseTraceparent parses an incoming W3C `traceparent` header and mints a fresh child `SpanID`.