otlp

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package otlp provides shared helpers for decoding OTLP payloads and converting common OTLP values into cliutils point primitives.

Index

Constants

View Source
const (
	ContentTypeJSON        = "application/json"
	ContentTypeProtobuf    = "application/x-protobuf"
	ContentTypeAltProtobuf = "application/protobuf"
)
View Source
const (
	DefaultLogSource        = "otel_logs"
	DefaultLogService       = "unSetServiceName"
	DefaultSchemaURLKey     = "schema_url"
	DefaultMessageField     = "message"
	DefaultTraceIDField     = "trace_id"
	DefaultSpanIDField      = "span_id"
	DefaultStatusTag        = "status"
	DefaultServiceTag       = "service"
	DefaultSourceTag        = "source"
	DefaultHostTag          = "host"
	DefaultFingerprintTag   = "dk_fingerprint"
	DefaultMaxMessageLength = 32 * 1024
)
View Source
const (
	DefaultMetricMeasurement  = "otel_service"
	DefaultMetricTemporality  = "__temporality"
	DefaultCollectorSourceTag = "collector_source_ip"
	DefaultScopeNameKey       = "scope_name"
)
View Source
const (
	AttrServiceName = "service.name"

	AttrDBSystem        = "db.system"
	AttrMessagingSystem = "messaging.system"
	AttrRPCSystem       = "rpc.system"

	EventException = "exception"

	AttrExceptionType       = "exception.type"
	AttrExceptionMessage    = "exception.message"
	AttrExceptionStacktrace = "exception.stacktrace"
)
View Source
const (
	MetricTagUnit = "unit"
	MetricTagLE   = "le"

	MetricSuffixBucket = "_bucket"
	MetricSuffixSum    = "_sum"
	MetricSuffixCount  = "_count"
	MetricSuffixAvg    = "_avg"
	MetricSuffixMin    = "_min"
	MetricSuffixMax    = "_max"
	MetricSuffixInf    = "+Inf"
)
View Source
const (
	SpanStatusInfo  = "info"
	SpanStatusOK    = "ok"
	SpanStatusError = "error"
)
View Source
const (
	DefaultTracePointName      = "tracing"
	DefaultUnknownService      = "unknown_service"
	DefaultTraceSource         = "opentelemetry"
	DefaultParentSpanID        = "0"
	DefaultTraceBaseServiceTag = "base_service"
	DefaultTraceStatusTag      = "status"
	DefaultTraceSpanTypeTag    = "span_type"
	DefaultTraceSpanKindTag    = "span_kind"
	DefaultTraceSourceTypeTag  = "source_type"
	DefaultTraceFingerprintTag = "dk_fingerprint"
	DefaultTraceServiceTag     = "service"
	DefaultTraceSourceTag      = "source"
	DefaultTraceTraceIDField   = "trace_id"
	DefaultTraceParentIDField  = "parent_id"
	DefaultTraceSpanIDField    = "span_id"
	DefaultTraceResourceField  = "resource"
	DefaultTraceStartField     = "start"
	DefaultTraceDurationField  = "duration"
	DefaultTraceMessageField   = "message"
	DefaultTraceRuntimeIDField = "runtime_id"
	DefaultTraceDBHostTag      = "db_host"
	TraceSourceTypeWeb         = "web"
	TraceSourceTypeDB          = "db"
	TraceSourceTypeMessage     = "message_queue"
	TraceSourceTypeCustomer    = "custom"
)

Variables

View Source
var ErrUnsupportedContentType = errors.New("unsupported OTLP content type")
View Source
var PublicAttributeAliases = map[string]string{
	"db.system":    "db_system",
	"db.operation": "db_operation",
	"db.name":      "db_name",
	"db.statement": "db_statement",

	"server.address":       "server_address",
	"net.host.name":        "net_host_name",
	"server.port":          "server_port",
	"net.host.port":        "net_host_port",
	"network.peer.address": "network_peer_address",
	"network.peer.port":    "network_peer_port",
	"network.transport":    "network_transport",

	"http.request.method":       "http_method",
	"http.method":               "http_method",
	"error.type":                "error_type",
	"http.response.status_code": "http_status_code",
	"http.status_code":          "http_status_code",
	"http.route":                "http_route",
	"http.target":               "http_target",
	"http.scheme":               "http_scheme",
	"http.url":                  "http_url",
	"url.full":                  "url_full",
	"url.scheme":                "url_scheme",
	"url.path":                  "url_path",
	"url.query":                 "url_query",
	"client.address":            "client_address",
	"client.port":               "client_port",

	"messaging.system":           "messaging_system",
	"messaging.operation":        "messaging_operation",
	"messaging.message.id":       "messaging_message.id",
	"messaging.destination.name": "messaging_destination.name",

	"rpc.service": "rpc_service",
	"rpc.system":  "rpc_system",

	"exception":            "exception",
	"exception.type":       "exception_type",
	"exception.message":    "exception_message",
	"exception.stacktrace": "exception_stacktrace",

	"container.name": "container_name",
	"process.pid":    "process_pid",
	"project":        "project",
	"version":        "version",
	"env":            "env",
	"host":           "host",
	"pod_name":       "pod_name",
	"pod_namespace":  "pod_namespace",

	"telemetry.sdk.language": "sdk_language",
	"telemetry.sdk.name":     "sdk_name",
	"telemetry.sdk.version":  "sdk_version",
}
View Source
var SpanKindNames = map[int32]string{
	0: "unspecified",
	1: "internal",
	2: "server",
	3: "client",
	4: "producer",
	5: "consumer",
}

Functions

func AnyValueToInterface

func AnyValueToInterface(value *common.AnyValue) (any, bool)

AnyValueToInterface converts an OTLP AnyValue into a Go value.

func AnyValueToString

func AnyValueToString(value *common.AnyValue) (string, bool)

AnyValueToString converts an OTLP AnyValue into a string form suitable for tags or fields.

func AttributesToStringMap

func AttributesToStringMap(attrs []*common.KeyValue, opts StringMapOptions) map[string]string

AttributesToStringMap converts OTLP attributes to a string map.

func AttributesToTagMap

func AttributesToTagMap(attrs []*common.KeyValue, opts StringMapOptions) map[string]string

func Base64RawID

func Base64RawID(id []byte) string

Base64RawID converts an OTLP binary ID into raw base64 without padding.

func ChunkStringByRuneLength

func ChunkStringByRuneLength(s string, size int) []string

ChunkStringByRuneLength splits s into chunks measured by rune count.

func DBHostFromAttributes

func DBHostFromAttributes(resourceAttrs, spanAttrs map[string]string) string

func DecimalIDFromLast8Bytes

func DecimalIDFromLast8Bytes(id []byte) (string, bool)

DecimalIDFromLast8Bytes converts the last 8 bytes of id as a big-endian uint64.

func DefaultHostName

func DefaultHostName(attrs []*common.KeyValue) string

func DefaultLogServiceAndSource

func DefaultLogServiceAndSource(attrs []*common.KeyValue) (service string, source string)

func FindAttribute

func FindAttribute(attrs []*common.KeyValue, key string) (*common.KeyValue, int, bool)

FindAttribute returns the first attribute with key and its position.

func HexID

func HexID(id []byte) string

HexID converts an OTLP binary ID into its hex string form.

func IsZeroID

func IsZeroID(id []byte) bool

IsZeroID reports whether id is empty or contains only zero bytes.

func LogStatusFromSeverityNumber

func LogStatusFromSeverityNumber(severityNumber int32, fallback string) string

LogStatusFromSeverityNumber maps OTLP log severity numbers to a status string.

func MergeStringMapsAsFields

func MergeStringMapsAsFields(maps ...map[string]string) point.KVs

MergeStringMapsAsFields merges maps into point fields, replacing dots in keys.

func MergeStringMapsAsTags

func MergeStringMapsAsTags(maps ...map[string]string) point.KVs

MergeStringMapsAsTags merges maps into point tags, replacing dots in keys.

func MetricMeasurementFromAttribute

func MetricMeasurementFromAttribute(pointAttrs map[string]string, fallback string) string

func MetricNameFromAttribute

func MetricNameFromAttribute(pointAttrs map[string]string, fallback string) string

func NormalizeContentType

func NormalizeContentType(contentType string) string

NormalizeContentType strips parameters and normalizes a content-type value.

func NumberDataPointToPoint

func NumberDataPointToPoint(measurement, field string, kvs point.KVs, pt *metrics.NumberDataPoint, kvOpts []point.KVOption) *point.Point

NumberDataPointToPoint converts an OTLP NumberDataPoint into a metric point.

func NumberDataPointValue

func NumberDataPointValue(pt *metrics.NumberDataPoint) (any, bool)

NumberDataPointValue extracts the scalar value from an OTLP NumberDataPoint.

func ParseLogRequest

func ParseLogRequest(resourceLogs []*logs.ResourceLogs, opts LogsParserOptions) []*point.Point

func ParseResourceMetricsV2

func ParseResourceMetricsV2(resmcs []*metrics.ResourceMetrics, opts MetricsParserOptions) []*point.Point

func PublicAttributeAlias

func PublicAttributeAlias(key string) (string, bool)

PublicAttributeAlias returns the normalized alias of a semantic-convention attribute key.

func SelectPublicTraceAttributes

func SelectPublicTraceAttributes(attrs []*common.KeyValue) (point.KVs, []*common.KeyValue)

func ShouldDropMetricAttribute

func ShouldDropMetricAttribute(key string) bool

ShouldDropMetricAttribute reports whether a resource or datapoint attribute is ignored for metrics.

func SpanKindName

func SpanKindName(kind int32) string

SpanKindName returns the readable span kind. Unknown values fallback to "unspecified".

func SummaryDataPointToPoint

func SummaryDataPointToPoint(measurement, field string, kvs point.KVs, summary *metrics.SummaryDataPoint, kvOpts []point.KVOption) *point.Point

SummaryDataPointToPoint converts an OTLP SummaryDataPoint into a metric point.

func SystemNameForService

func SystemNameForService(attrs map[string]string) string

SystemNameForService returns db/rpc/messaging system value for service-name fallback.

func TraceSourceTypeFromTags

func TraceSourceTypeFromTags(tags point.KVs) string

func TraceStatusFromCode

func TraceStatusFromCode(code int32) string

TraceStatusFromCode maps OTLP trace status code to a compact status string.

func Unmarshal

func Unmarshal(body []byte, contentType string, msg proto.Message) error

Unmarshal decodes an OTLP payload into msg according to contentType.

Types

type LogsParserOptions

type LogsParserOptions struct {
	CollectorSourceIP    string
	CollectorSourceIPTag string
	DKFingerprint        string
	DKFingerprintTag     string

	ResourceStringOptions StringMapOptions
	ScopeStringOptions    StringMapOptions
	RecordStringOptions   StringMapOptions

	SchemaURLKey  string
	ScopeNameKey  string
	MessageField  string
	TraceIDField  string
	SpanIDField   string
	StatusTag     string
	ServiceTag    string
	SourceTag     string
	HostTag       string
	MaxMessageLen int

	GlobalFields map[string]any
	GlobalTags   map[string]string

	IDConverter      func([]byte) string
	ServiceAndSource func([]*common.KeyValue) (service, source string)
	HostName         func([]*common.KeyValue) string
	SeverityMapper   func(logs.SeverityNumber, string) string
	PointOptions     func(ts time.Time) []point.Option
}

type MetricsParserOptions

type MetricsParserOptions struct {
	Measurement          string
	CollectorSourceIP    string
	CollectorSourceIPTag string
	ScopeNameKey         string
	TemporalityTagKey    string

	ResourceStringOptions StringMapOptions
	ScopeStringOptions    StringMapOptions
	PointStringOptions    StringMapOptions

	FieldName                func(metric *metrics.Metric, pointAttrs map[string]string) string
	MeasurementFromPointAttr func(metric *metrics.Metric, pointAttrs map[string]string) string
	KVOptions                func(metric *metrics.Metric) []point.KVOption
	PointOptions             func(ts time.Time) []point.Option
	ExponentialHistogramAvg  func(sum float64, count uint64) any
}

type StringMapOptions

type StringMapOptions struct {
	KeyNormalizer func(string) string
	MaxValueLen   int
	DropKeys      map[string]struct{}
}

func DefaultMetricStringMapOptions

func DefaultMetricStringMapOptions() StringMapOptions

type TraceBatch

type TraceBatch []*point.Point

func ParseResourceSpans

func ParseResourceSpans(resourceSpans []*trace.ResourceSpans, opts TracesParserOptions) []TraceBatch

type TracesParserOptions

type TracesParserOptions struct {
	PointName string
	Source    string

	CollectorSourceIP    string
	CollectorSourceIPTag string
	DKFingerprint        string
	DKFingerprintTag     string

	UnknownService string

	ServiceTag     string
	BaseServiceTag string
	SpanStatusTag  string
	SpanTypeTag    string
	SpanKindTag    string
	SourceTag      string
	SourceTypeTag  string
	DBHostTag      string

	TraceIDField   string
	ParentIDField  string
	SpanIDField    string
	ResourceField  string
	StartField     string
	DurationField  string
	MessageField   string
	RuntimeIDField string

	GlobalTags map[string]string

	IDConverter    func([]byte) string
	SelectAttrs    func([]*common.KeyValue) (point.KVs, []*common.KeyValue)
	ScopeKVs       func(*trace.ScopeSpans) point.KVs
	SpanType       func(spanID, parentID string, spanIDs, parentIDs map[string]bool) string
	SpanStatus     func(*trace.Status) string
	SpanKind       func(int32) string
	SourceType     func(point.KVs) string
	BaseService    func(map[string]string) string
	DecorateKVs    func(point.KVs, *trace.ResourceSpans, *trace.ScopeSpans, *trace.Span) point.KVs
	MessageEncoder func(*trace.Span) (string, error)
	CleanSpan      func(*trace.Span) *trace.Span
}

Jump to

Keyboard shortcuts

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