srcconv

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 22 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 srcconv holds the decoders that turn third-party wire formats (Datadog Agent, Splunk HEC, MessagePack) into OTLP messages. The gateway receivers and the relay sources share it.

Index

Constants

View Source
const (

	// MPMaxValues caps decoded values (and protobuf fields) per payload.
	MPMaxValues = 1 << 20
)

Variables

View Source
var (
	ErrDatadog  = errors.New("malformed datadog payload")
	ErrTooLarge = errors.New("payload too large")
)

ErrDatadog is a malformed Datadog payload; ErrTooLarge a payload past the field budget.

View Source
var ErrHECFormat = errors.New("invalid data format")

ErrHECFormat is a body that is not a stream of HEC events.

View Source
var (

	// ErrMsgpackShort means the input ended inside a value: a stream reader should wait for more bytes.
	ErrMsgpackShort = errors.New("truncated msgpack")
)

Functions

func AddTracerPayload

func AddTracerPayload(g *spanGroups, top map[string]any, n *int, maxSpans int) error

AddTracerPayload adds one TracerPayload's spans to g; n counts spans across the request.

func AppendKV

func AppendKV(dst []*commonpb.KeyValue, k string, v *commonpb.AnyValue) []*commonpb.KeyValue

AppendKV adds an attribute unless its key is in the reserved wt.* namespace.

func DDEventLog

func DDEventLog(e map[string]any, now uint64) *logspb.LogRecord

DDEventLog maps one event to a log record (severity from alert_type), tagged `datadog.event = true` with the event fields under `datadog.event.*`.

func DDLog

func DDLog(e map[string]any, now uint64) *logspb.LogRecord

DDLog converts one Datadog log entry.

func DDLogEntries

func DDLogEntries(body []byte) ([]map[string]any, error)

DDLogEntries decodes a Datadog logs body (a JSON array, numbers kept exact).

func DDLogsRequest

func DDLogsRequest(entries []map[string]any, now uint64) *colllogs.ExportLogsServiceRequest

DDLogsRequest converts Datadog v2 log entries (one resource per service) to OTLP.

func DDSpan

func DDSpan(m map[string]any) (*tracepb.Span, string)

DDSpan converts one span map. trace_id is 64-bit; the high half comes from meta `_dd.p.tid`.

func DDSpanV05

func DDSpanV05(dict []string, a []any) (map[string]any, bool)

DDSpanV05 expands a v0.5 span array (indices into the payload's string table) into the v0.4 map.

func DDStatsMetrics

func DDStatsMetrics(body []byte, isMsgpack bool, maxPoints int) ([]*metricspb.Metric, error)

DDStatsMetrics maps the trace-agent's APM stats (msgpack when isMsgpack, else protobuf) to delta counters: dd.trace.hits, .errors, .top_level_hits and .duration_ns per service/operation/resource. More than maxPoints points is ErrTooLarge.

func DDTagList

func DDTagList(v any) []string

DDTagList reads a JSON array of tag strings.

func DDTraces

func DDTraces(v any, maxSpans int) (*colltrace.ExportTraceServiceRequest, error)

DDTraces converts a decoded v0.4 payload (an array of traces, each an array of span maps) to OTLP, one resource per service. More than maxSpans spans is ErrTooLarge.

func DDTracesV02

func DDTracesV02(body []byte, maxSpans int) (*colltrace.ExportTraceServiceRequest, error)

DDTracesV02 reads the trace-agent's AgentPayload (protobuf; the caller undoes gzip or zstd). More than maxSpans spans is ErrTooLarge.

func DDTracesV05

func DDTracesV05(v any, maxSpans int) (*colltrace.ExportTraceServiceRequest, error)

DDTracesV05 converts a decoded v0.5 payload [string table, traces] by expanding every span to the v0.4 shape and reusing DDTraces.

func DDTracesV07

func DDTracesV07(top map[string]any, maxSpans int) (*colltrace.ExportTraceServiceRequest, error)

DDTracesV07 reads the TracerPayload map {chunks:[{spans:[<v0.4 span map>]}], env, hostname, appVersion}.

func DecodeObjects

func DecodeObjects(body []byte, max int) ([]map[string]any, error)

DecodeObjects reads JSON objects from body: a single object, an array of objects, or NDJSON (numbers kept exact). Malformed input is ErrDatadog, more than max objects ErrTooLarge.

func DropWt

func DropWt(v any) any

DropWt removes reserved wt.* keys from a decoded JSON value so they cannot reach storage in the body either.

func Flatten

func Flatten(prefix string, v any, out map[string]any)

Flatten turns nested JSON objects into dotted keys; null values are dropped.

func HECGauges

func HECGauges(e *HECEvt, ns, now uint64) []*metricspb.Metric

HECGauges maps a metric event: fields {metric_name, _value} or {"metric_name:<name>": value, ...}; every other field is a dimension.

func HECLog

func HECLog(e *HECEvt, ns, now uint64) *logspb.LogRecord

HECLog converts one log event.

func HECLogCount

func HECLogCount(req *colllogs.ExportLogsServiceRequest) (n int)

HECLogCount is the number of log records in a HECRequests logs request.

func HECRawRequest

func HECRawRequest(body string, e HECEvt, now uint64, max int) (*colllogs.ExportLogsServiceRequest, error)

HECRawRequest turns a raw body (one event per non-empty line) into logs; max lines at most.

func HECRequests

HECRequests converts events: log events to one logs request, `event:"metric"` events to gauges in one metrics request (nil when there are none). now is unix ns.

func JSONLogRecord

func JSONLogRecord(e map[string]any, bodyKeys []string, ns, now uint64) *logspb.LogRecord

JSONLogRecord turns a JSON object into a record: the first string among bodyKeys is the body (and is not repeated as an attribute); every other field becomes a flattened attribute. Without a string body the object itself, minus wt.* keys, is the body.

func JSONVal

func JSONVal(v any) *commonpb.AnyValue

JSONVal converts a decoded JSON scalar (UseNumber) to an AnyValue; arrays become JSON text.

func LogRecord

func LogRecord(ns, now uint64, body string, attrs []*commonpb.KeyValue) *logspb.LogRecord

LogRecord builds a record; ns 0 means now.

func MPStr

func MPStr(v any) string

MPStr reads a msgpack string or bin as a string.

func MPU64

func MPU64(v any) uint64

MPU64 reads a msgpack/proto integer as uint64 (two's complement for large ids).

func MsgpackDecode

func MsgpackDecode(b []byte) (any, error)

MsgpackDecode decodes one value. Maps become map[string]any (non-string keys are rejected), arrays []any, strings string, bin []byte, non-negative ints uint64, negative ints int64, floats float64.

func MsgpackNext

func MsgpackNext(b []byte, ext bool) (any, []byte, error)

MsgpackNext decodes the first value of a stream and returns the bytes after it. With ext, the Fluent forward EventTime extension (type 0) decodes to time.Time and other extensions to []byte; without it extensions are rejected (the Datadog behaviour).

func NewScope

NewScope appends a resource (wt.* attributes dropped) with one scope and returns that scope.

func ParseSketches

func ParseSketches(b []byte, maxSketches int) ([]*metricspb.Metric, error)

ParseSketches decodes a SketchPayload into exponential histogram metrics; more than max sketches is ErrTooLarge.

func ParseTime

func ParseTime(v any, unit time.Duration) uint64

ParseTime reads a number (scaled by unit) or an RFC 3339 string into ns; 0 when unparseable.

func SeriesRequest

func SeriesRequest(series []Series) *collmetrics.ExportMetricsServiceRequest

SeriesRequest converts series (empty ones skipped, semantic twins added) to one OTLP request.

func SetSeverity

func SetSeverity(rec *logspb.LogRecord, level string)

SetSeverity fills the severity text and number from a level name.

func SortedKeys

func SortedKeys[V any](m map[string]V) []string

SortedKeys returns the keys of m in order.

func StrVal

func StrVal(s string) *commonpb.AnyValue

StrVal is a string AnyValue.

func TagAttrs

func TagAttrs(attrs []*commonpb.KeyValue, tags []string) []*commonpb.KeyValue

TagAttrs turns "k:v" tags (a bare tag is "true") into attributes.

Types

type HECEvt

type HECEvt struct {
	Time       any            `json:"time"`
	Host       string         `json:"host"`
	Source     string         `json:"source"`
	Sourcetype string         `json:"sourcetype"`
	Index      string         `json:"index"`
	Service    string         `json:"service"`
	Event      any            `json:"event"`
	Fields     map[string]any `json:"fields"`
}

HECEvt is one Splunk HTTP Event Collector event.

func HECDecode

func HECDecode(body []byte, max int) ([]HECEvt, error)

HECDecode reads a stream of concatenated JSON events (numbers exact). More than max events is ErrTooLarge.

func (*HECEvt) Meta

func (e *HECEvt) Meta(attrs []*commonpb.KeyValue) []*commonpb.KeyValue

Meta appends the event's host, source, sourcetype and index as attributes.

func (*HECEvt) ServiceName

func (e *HECEvt) ServiceName() string

ServiceName is the event's service: the top-level `service`, else fields.service, else service in a JSON event object.

type Point

type Point struct{ TS, Val float64 }

Point is one series sample (TS in seconds).

func (*Point) UnmarshalJSON

func (p *Point) UnmarshalJSON(b []byte) error

UnmarshalJSON reads v1 [ts, value] pairs and v2 {"timestamp":..,"value":..} objects.

type Series

type Series struct {
	Metric    string                        `json:"metric"`
	Type      json.RawMessage               `json:"type"` // v1 "gauge"|"count"|"rate"; v2 0..3
	Points    []Point                       `json:"points"`
	Tags      []string                      `json:"tags"`
	Host      string                        `json:"host"`
	Interval  float64                       `json:"interval"`
	Resources []struct{ Type, Name string } `json:"resources"`
}

Series is a Datadog v1/v2 metric series.

func SeriesFromJSON

func SeriesFromJSON(body []byte) ([]Series, error)

SeriesFromJSON decodes a v1 or v2 JSON series body.

func SeriesFromProto

func SeriesFromProto(b []byte) ([]Series, error)

SeriesFromProto decodes the Agent 7 v2 MetricPayload (DataDog/agent-payload metrics proto): series = 1 { resources 1 {type 1, name 2}, metric 2, tags 3, points 4 {value 1 double, timestamp 2}, type 5, interval 8 }. Unknown fields are skipped; more than MPMaxValues fields is ErrTooLarge.

func (*Series) Kind

func (s *Series) Kind() string

Kind maps both encodings to gauge, count or rate (v2: 1 count, 2 rate, 3 gauge; 0 = gauge).

func (*Series) OTLP

func (s *Series) OTLP() *metricspb.Metric

OTLP converts the series to a gauge or a delta sum.

func (*Series) SemanticTwin

func (s *Series) SemanticTwin() *Series

SemanticTwin returns the OTel-named copy of a gauge series, or nil when there is none.

Jump to

Keyboard shortcuts

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