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 ¶
- Constants
- func BuildOTLPJSON(serviceName, serviceVersion string, spans []Span) ([]byte, error)
- func ClassifyErrorClass(httpStatus int, outcome, wafVendor, errMsg string) string
- func FormatTraceparent(tc TraceContext) string
- func InjectHeaders(dst http.Header, tc TraceContext, requestID string)
- func IsDevTelemetryEnabled(getenv func(string) string) bool
- func IsDoNotTrack(raw string) bool
- func ParseHeaders(raw string) map[string]string
- func RandomHex(byteLen int) string
- func RedactEmbeddedURLs(s string) string
- func ResolveDevTelemetryURL(getenv func(string) string) string
- func ResolveTracesURL(endpoint string) string
- func SanitizeURL(raw string) string
- func StartChildSpan(ctx context.Context, exp *Exporter, name string, kind int, ...) (context.Context, *ActiveSpan, TraceContext)
- func ValidateDevErrorReport(r DevErrorReport) error
- func WithTraceContext(ctx context.Context, tc TraceContext) context.Context
- type ActiveSpan
- type DevErrorReport
- type Exporter
- type Span
- type TraceContext
Constants ¶
const DefaultDevTelemetryURL = "https://telemetry.edgefetch.dev/v1/dev-errors"
DefaultDevTelemetryURL is the opt-in EdgeFetch structural error collector endpoint. Never contacted unless `EDGEFETCH_DEV_TELEMETRY=1` AND `DO_NOT_TRACK` is not truthy.
Variables ¶
This section is empty.
Functions ¶
func BuildOTLPJSON ¶
BuildOTLPJSON serializes `spans` into a standard OpenTelemetry OTLP/HTTP JSON `ExportTraceServiceRequest`.
func ClassifyErrorClass ¶ added in v2.7.0
ClassifyErrorClass maps span outcomes, HTTP status codes, WAF vendors, and error categories to a closed set of structural tokens — never echoing raw user URLs, hostnames, paths, or arbitrary exception strings.
func FormatTraceparent ¶
func FormatTraceparent(tc TraceContext) string
FormatTraceparent formats `tc` as a W3C `traceparent` header value.
func InjectHeaders ¶ added in v2.6.3
func InjectHeaders(dst http.Header, tc TraceContext, requestID string)
InjectHeaders writes W3C `traceparent` and EdgeFetch correlation headers (`X-Trace-Id`, `X-Span-Id`, `X-Request-Id`) onto `dst`.
func IsDevTelemetryEnabled ¶ added in v2.7.0
IsDevTelemetryEnabled enforces the strict unattended-OFF & `DO_NOT_TRACK=1` hard-override invariant:
- Always `false` if `getenv == nil` or `DO_NOT_TRACK=1` (even when `EDGEFETCH_DEV_TELEMETRY=1`).
- Only `true` when `EDGEFETCH_DEV_TELEMETRY` is explicitly truthy (`1`, `true`, `yes`, `on`).
func IsDoNotTrack ¶ added in v2.7.0
IsDoNotTrack returns true when `DO_NOT_TRACK` is set to a truthy value (`1`, `true`, `yes`, `on`).
func ParseHeaders ¶
ParseHeaders parses standard `OTEL_EXPORTER_OTLP_HEADERS` (`k1=v1,k2=v2`, supporting URL-encoded values).
func RedactEmbeddedURLs ¶ added in v2.6.3
RedactEmbeddedURLs delegates directly to `politeness.RedactEmbeddedURLs` so OTLP span error strings have 100% redaction parity across `http://`, `https://`, `ws://`, `wss://`, `ftp://`, schemeless userinfo, percent-encoded credentials, JSON-escaped slashes, query strings, `#fragments`, and inline Bearer/Basic/token secrets.
func ResolveDevTelemetryURL ¶ added in v2.7.0
ResolveDevTelemetryURL returns the active collector URL when opt-in telemetry is enabled, or `""` when disabled.
func ResolveTracesURL ¶
ResolveTracesURL normalizes `OTEL_EXPORTER_OTLP_ENDPOINT` onto its `/v1/traces` HTTP URL.
func SanitizeURL ¶ added in v2.6.3
SanitizeURL strips `user:pass@` credentials (including schemeless userinfo), query parameters, `#fragments`, matrix parameters, and reset path tokens from `raw` for safe OTLP logging (`politeness.SanitizeURL` parity).
func StartChildSpan ¶ added in v2.6.3
func StartChildSpan(ctx context.Context, exp *Exporter, name string, kind int, attrs map[string]any) (context.Context, *ActiveSpan, TraceContext)
StartChildSpan derives a child `TraceContext` from `ctx` (or mints a fresh root when absent), attaches it to the returned `context.Context` even when `exp == nil` or `!Sampled`, and starts an `ActiveSpan`.
func ValidateDevErrorReport ¶ added in v2.7.0
func ValidateDevErrorReport(r DevErrorReport) error
ValidateDevErrorReport verifies that `r` conforms strictly to the collector schema and contains zero URLs or PII.
func WithTraceContext ¶ added in v2.6.3
func WithTraceContext(ctx context.Context, tc TraceContext) context.Context
WithTraceContext attaches `tc` onto `ctx` (using `context.Background()` when `ctx == nil`).
Types ¶
type ActiveSpan ¶ added in v2.6.3
type ActiveSpan struct {
// contains filtered or unexported fields
}
ActiveSpan tracks an in-flight OTLP span and exports it once on `End`.
func StartSpan ¶ added in v2.6.3
func StartSpan(exp *Exporter, tc TraceContext, name string, kind int, attrs map[string]any) *ActiveSpan
StartSpan begins an `ActiveSpan` using `tc`'s exact `TraceID`/`SpanID`/`ParentSpanID` (preserving `Sampled`).
func (*ActiveSpan) End ¶ added in v2.6.3
func (a *ActiveSpan) End(statusOK bool, errMsg string)
End finishes the span once and records it to the exporter when non-nil and sampled.
func (*ActiveSpan) SetAttribute ¶ added in v2.6.3
func (a *ActiveSpan) SetAttribute(k string, v any)
SetAttribute attaches or updates an attribute on an active span prior to `End`.
type DevErrorReport ¶ added in v2.7.0
type DevErrorReport struct {
SchemaVersion int `json:"schemaVersion"`
Version string `json:"version"`
Runtime string `json:"runtime"`
OS string `json:"os"`
Arch string `json:"arch"`
Mode string `json:"mode"`
SpanName string `json:"spanName"`
AdapterID string `json:"adapterId"`
WAFVendor string `json:"wafVendor"`
HTTPStatus int `json:"httpStatus"`
ErrorClass string `json:"errorClass"`
}
DevErrorReport contains ONLY structural, zero-PII error fields matching `telemetry.edgefetch.dev`'s strict allowlist. ZERO URLs, ZERO hostnames, ZERO paths, ZERO query strings, ZERO HTML/Markdown bodies, ZERO raw error messages, ZERO headers.
func ScrubSpanToDevErrorReport ¶ added in v2.7.0
func ScrubSpanToDevErrorReport(s Span, serviceName, serviceVersion string) (DevErrorReport, bool)
ScrubSpanToDevErrorReport converts a failed (`!StatusOK` or WAF-blocked) `Span` into a strictly allowlisted `DevErrorReport`. Healthy (`StatusOK == true` without WAF failure) spans return `(_, false)`.
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.
func (*Exporter) Close ¶ added in v2.6.3
Close flushes pending spans with a bounded 3-second timeout and stops the exporter (nil-safe).
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`.
func TraceContextFromContext ¶ added in v2.6.3
func TraceContextFromContext(ctx context.Context) (TraceContext, bool)
TraceContextFromContext retrieves a valid `TraceContext` attached by `WithTraceContext`.
func (TraceContext) Child ¶ added in v2.6.3
func (tc TraceContext) Child() TraceContext
Child derives a child `TraceContext` preserving `TraceID` and `Sampled` while linking `ParentSpanID` to `tc.SpanID`.