otlp

package
v0.21.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2026 License: Apache-2.0 Imports: 18 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package otlp is the wire side of an OpenTelemetry receiver over OTLP/HTTP for the traces and logs signals: it reads and decodes export requests (protobuf and JSON), exposes their spans and log records in a neutral shape, and encodes the export response. A SpanMapper or LogMapper per telemetry convention turns the records that describe model calls into Inference records.

Authentication, attribution, pricing and storage belong to the caller. The metrics signal and the gRPC transport are out of scope.

Index

Constants

View Source
const (
	MediaTypeProtobuf = "application/x-protobuf"
	MediaTypeJSON     = "application/json"
)

The two encodings OTLP/HTTP defines.

View Source
const (
	StatusInvalidArgument   int32 = 3
	StatusNotFound          int32 = 5
	StatusResourceExhausted int32 = 8
	StatusUnavailable       int32 = 14
)

gRPC status codes carried in the Status body of an OTLP/HTTP error response.

Variables

View Source
var ErrBodyTooLarge = errors.New("otlp: request body too large")

ErrBodyTooLarge reports a request body over the limit, measured after decompression.

View Source
var ErrUnsupportedEncoding = errors.New("otlp: unsupported content encoding")

ErrUnsupportedEncoding reports a Content-Encoding other than gzip or identity.

View Source
var ErrUnsupportedMediaType = errors.New("otlp: unsupported content type")

ErrUnsupportedMediaType reports a Content-Type that is neither OTLP/HTTP encoding.

Functions

func ExportResponse

func ExportResponse(mediaType string, signal Signal, rejected int64, message string) []byte

ExportResponse encodes the export response of signal (ExportTraceServiceResponse, ExportLogsServiceResponse) in mediaType. rejected > 0 makes it a partial success naming how many records were refused and why. With nothing rejected, a message makes it a warning to the client about an export that was accepted in full, which OTLP carries as a partial success with a count of zero. With neither, the response is the empty success message.

func MediaType

func MediaType(contentType string) (string, error)

MediaType reduces a Content-Type header to MediaTypeProtobuf or MediaTypeJSON.

func ReadBody

func ReadBody(body io.Reader, contentEncoding string, limit int64) ([]byte, error)

ReadBody reads an export request body, decompressing it when contentEncoding is gzip. limit bounds the decompressed size, so a small compressed body cannot expand without bound; limit <= 0 means no bound.

func StatusBody

func StatusBody(mediaType string, code int32, message string) []byte

StatusBody encodes the google.rpc.Status message an OTLP/HTTP error response carries, in mediaType.

Types

type Attrs

type Attrs map[string]any

Attrs is a set of OpenTelemetry attributes with values converted to Go types: string, bool, int64, float64, []byte, []any and map[string]any.

The accessors take several keys because conventions rename attributes between releases; the first key that holds a usable value wins.

func (Attrs) Bool

func (a Attrs) Bool(keys ...string) (value, ok bool)

Bool returns the first boolean among keys.

func (Attrs) Float

func (a Attrs) Float(keys ...string) (float64, bool)

Float returns the first number among keys.

func (Attrs) Int

func (a Attrs) Int(keys ...string) (int64, bool)

Int returns the first integer among keys. Conventions type counts as int, but exporters also send them as whole doubles or numeric strings.

func (Attrs) Str

func (a Attrs) Str(keys ...string) string

Str returns the first non-empty string among keys.

func (Attrs) Strings

func (a Attrs) Strings(keys ...string) []string

Strings returns the first string array among keys, skipping elements that are not strings. A lone string counts as a one-element array.

type Content

type Content struct {
	// Input holds what was sent to the model: instructions, messages, tool definitions.
	Input map[string]any
	// Output holds what the model returned.
	Output map[string]any
}

Content is the message content a client chose to report with a model call. Each side maps the convention's attribute names to their values as sent: structured values, or the JSON text some exporters put on spans instead. A side the telemetry did not carry is nil.

func (Content) Empty

func (c Content) Empty() bool

Empty reports whether the call carried no content.

type Inference

type Inference struct {
	// Convention is the Name of the mapper that produced the record.
	Convention string

	// TraceID and SpanID are those of the span the call ran in. A log record emitted outside a span has neither.
	TraceID string
	SpanID  string
	// Service is the reporting application's service.name resource attribute.
	Service string

	Start    time.Time
	Duration time.Duration

	Operation     string
	Provider      string
	RequestModel  string
	ResponseModel string
	// ResponseID is the provider's identifier for the response.
	ResponseID     string
	ConversationID string
	FinishReason   string
	// Host is the catalog host slug the client says served the call, from relay's own wyolet.relay.host attribute; empty from clients that do not know the catalog.
	Host string

	Streamed         bool
	TimeToFirstChunk time.Duration

	// ErrorType is set when the call failed; HTTPStatus is the provider's response status when the telemetry carried one.
	ErrorType  string
	HTTPStatus int

	Tokens TokenCounts

	Content Content
}

Inference is one model call as a client's telemetry reported it. Fields the telemetry did not carry stay zero.

type LogMapper

type LogMapper interface {
	// Name identifies the convention.
	Name() string
	// MapLog reports false for a record that is not a model call under this convention. Such a record is left for the next mapper.
	MapLog(r LogRecord) (Inference, bool)
}

LogMapper recognises the log records of one telemetry convention that describe a model call.

type LogRecord

type LogRecord struct {
	// TraceID and SpanID are lowercase hex, and empty on a record emitted outside a span.
	TraceID string
	SpanID  string

	// EventName names the event the record is, when it is one. Exporters older than the field carry the name in the event.name attribute instead.
	EventName string

	// Time is when the event happened; Observed is when the collecting SDK saw it. Either may be zero.
	Time     time.Time
	Observed time.Time

	// Body is the record's body converted like an attribute value, nil when absent.
	Body any

	Attrs    Attrs
	Resource Attrs
	// Scope is the instrumentation scope name.
	Scope string
}

LogRecord is one exported log record with its resource and scope flattened onto it.

func DecodeLogs

func DecodeLogs(mediaType string, body []byte) ([]LogRecord, error)

DecodeLogs decodes a logs export request body in mediaType into its records.

type Signal

type Signal string

Signal is an OTLP signal with an export endpoint of its own.

const (
	SignalTraces Signal = "traces"
	SignalLogs   Signal = "logs"
)

type Span

type Span struct {
	// TraceID, SpanID and ParentSpanID are lowercase hex; ParentSpanID is empty on a root span.
	TraceID      string
	SpanID       string
	ParentSpanID string

	Name  string
	Start time.Time
	End   time.Time

	// Failed reports a span status of ERROR.
	Failed        bool
	StatusMessage string

	Attrs    Attrs
	Resource Attrs
	// Scope is the instrumentation scope name.
	Scope string
}

Span is one exported span with its resource and scope flattened onto it, so a mapper reads everything about the span from one value.

func DecodeTraces

func DecodeTraces(mediaType string, body []byte) ([]Span, error)

DecodeTraces decodes a trace export request body in mediaType into its spans.

type SpanMapper

type SpanMapper interface {
	// Name identifies the convention.
	Name() string
	// MapSpan reports false for a span that is not a model call under this convention. Such a span is left for the next mapper.
	MapSpan(s Span) (Inference, bool)
}

SpanMapper recognises the spans of one telemetry convention that describe a model call.

type TokenCounts

type TokenCounts struct {
	Input       int64
	Output      int64
	CacheRead   int64
	CacheWrite  int64
	Reasoning   int64
	AudioInput  int64
	AudioOutput int64
}

TokenCounts are the token counts of one model call. Input excludes cached tokens, which CacheRead and CacheWrite count. Reasoning and AudioOutput are parts of Output, and AudioInput is a part of the input, not additions to them.

Directories

Path Synopsis
Package genai maps spans and events that follow the OpenTelemetry GenAI semantic conventions to otlp.Inference records.
Package genai maps spans and events that follow the OpenTelemetry GenAI semantic conventions to otlp.Inference records.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL