otlp

package
v1.0.0-alpha.21 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 15 Imported by: 0 modules, 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 reads OTLP/HTTP traces and logs into per-request usage.

It is the ingest edge for telemetry. Decoding keeps only allowlisted scalar attributes, so content never reaches a Usage, and every field left behind is counted by name. A record that carries no token counts yields nothing.

Index

Constants

View Source
const (
	// DropClientCost counts a sender's own cost figure.
	DropClientCost = "client_cost"
	// DropContent counts a field whose name reads as content but is not listed.
	DropContent = "content_other"
	// DropUnlisted counts anything else outside the allowlist.
	DropUnlisted = "unlisted"
	// DropOversized counts an allowed attribute over maxValueBytes.
	DropOversized = "oversized_value"
	// DropNonScalar counts an allowed attribute that is an array or a map.
	DropNonScalar = "non_scalar_value"
	// DropLogBody counts a log body, which is free text.
	DropLogBody = "log.body"
	// DropStatusMessage counts a span status message, which is free text.
	DropStatusMessage = "span.status.message"
	// DropEmail counts a user.email, replaced by a login or a pseudonym.
	DropEmail = "user.email"
	// DropMetrics counts a metrics export. Rows come from logs and spans.
	DropMetrics = "metrics_export"
)

Counter labels for fields the edge drops. Labels are a closed set, so a sender cannot grow the counter table by naming new attributes.

View Source
const MaxTokens = 1_000_000_000_000

MaxTokens bounds one class of one request, far above any model's context, so a sum of counts cannot overflow.

View Source
const NoteTotalDisagrees = "the agent's total token count is not the sum of its counts, so this figure may read low or high"

NoteTotalDisagrees marks a request whose stated total is not the sum of its counts. The server logs a warning for each such request.

Variables

View Source
var ErrMalformed = errors.New("otlp: malformed payload")

ErrMalformed means a body did not decode.

Functions

func DropLabel

func DropLabel(key string) string

DropLabel names the counter for a field the edge will not keep.

Types

type Batch

type Batch struct {
	Usage []Usage
	// Rejected counts records the edge could not use: a record that did not
	// decode, or one whose token counts could not be read.
	Rejected int
	// Drops counts every field left behind, by label.
	Drops map[string]int64
}

Batch is one decoded request.

func Parse

func Parse(body []byte, encoding Encoding, signal Signal) (Batch, error)

Parse reads one request body.

type Count

type Count struct {
	Class  focus.TokenClass
	Tokens int64
}

Count is one token class of one request.

type Dialect

type Dialect string

Dialect names how one agent spells its token counts.

const (
	Standard   Dialect = "genai"
	ClaudeCode Dialect = "claude-code"
	Codex      Dialect = "codex"
	Copilot    Dialect = "copilot"
	Cursor     Dialect = "cursor"
	OpenCode   Dialect = "opencode"
	Gemini     Dialect = "gemini"
)

The dialects of the November 2026 adapter matrix.

type Encoding

type Encoding int

Encoding is an OTLP/HTTP body format.

const (
	JSON Encoding = iota + 1
	Protobuf
)

The two body formats of OTLP/HTTP.

type Signal

type Signal int

Signal is the OTLP signal a request carries.

const (
	Traces Signal = iota + 1
	Logs
)

The signals that can hold per-request token counts.

type Usage

type Usage struct {
	// ID is the provider's request or response id when the record has one, and
	// a stand-in derived from the record when it does not.
	ID                 string
	Dialect            Dialect
	Provider           string
	Model              string
	ServiceTier        string
	ContextInputTokens int64
	Start, End         time.Time
	Counts             []Count
	// AssumedWrite is the cache write class that took tokens whose lifetime the
	// dialect did not report. Empty when it reported all.
	AssumedWrite focus.TokenClass
	// Notes state a limit of this dialect in words and travel on the row.
	Notes []string

	Session          string
	Agent            string
	AgentVersion     string
	CostBasis        focus.CostBasis
	EnforcementGrade string

	// Repo, PullRequest, CommitSHA and RunID are the sender's join keys.
	Repo        string
	PullRequest string
	CommitSHA   string
	RunID       string
	// Email is held in memory only, to be replaced before anything is stored.
	Email string
	// RequestID is the provider's id alone, empty when ID is a stand-in.
	RequestID string
}

Usage is one model request as a dialect reports it.

Jump to

Keyboard shortcuts

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