baggage

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package baggage moves the OpenTelemetry baggage of an incoming request onto the import instance it creates, so that everything logged about that instance later — by this service and by the import container — can be traced back to the caller's context, for example to the smart service instance an import belongs to.

The context reaches an import twice, because the two consumers are different: as pod labels, which the log aggregation attaches to every container log line without the container knowing anything, and as an environment variable, which import-lib reads to put the same fields into the import's own log records.

Index

Constants

View Source
const (
	// EnvVar is the environment variable every import container receives the baggage
	// in. Its value is a W3C baggage header, not JSON: it is what the propagator
	// produces, and an import that wants to carry the context into an outgoing
	// request of its own can use it verbatim.
	EnvVar = "BAGGAGE"

	// LabelPrefix prefixes every baggage entry turned into a Kubernetes label. A
	// domain prefix is the convention for labels set by something other than the
	// workload's owner; it also keeps the entries clear of the user, importId and
	// importTypeId labels the drivers set themselves, which a baggage key could
	// otherwise overwrite.
	LabelPrefix = "baggage.senergy.infai.org/"

	// ImportIdKey is the baggage key the instance's own id is added under, once one
	// has been generated. Spelled like the keys otelx adds (user_id, username) and
	// like the IMPORT_ID the container already receives, rather than like the
	// importId label, because it ends up in log records next to those.
	ImportIdKey = "import_id"
)

Variables

This section is empty.

Functions

func AddLabels

func AddLabels(ctx context.Context, labels map[string]string, entries map[string]string) map[string]string

AddLabels merges the baggage labels into an existing label set and logs which entries were left out. Kept here so both drivers apply the same rule and log it the same way.

The existing labels win on collision. LabelPrefix makes that unreachable today, but the drivers' own labels identify the workload and a log annotation must not be able to rewrite them.

func FromContext

func FromContext(ctx context.Context) map[string]string

FromContext returns the baggage carried by ctx. Nil when there is none, so that an instance created by a caller that sent no context keeps no baggage field at all rather than an empty object.

func Header(entries map[string]string) string

Header renders the baggage as a W3C baggage header value.

Built from sorted keys rather than through otelbaggage.Baggage.String(), which ranges over a map and so returns the entries in a different order on every call. The value ends up in a container's environment, where an unstable spelling makes two identical deployments look different.

func Labels

func Labels(entries map[string]string) (labels map[string]string, skipped []string)

Labels turns the baggage into Kubernetes labels, prefixed with LabelPrefix.

Entries Kubernetes would refuse are left out, and their keys returned. The labels are a best-effort index for log queries, while EnvVar carries the complete baggage; dropping an entry loses a query path, whereas letting it through fails the whole deployment with a 422 and the import never starts. Sanitizing instead of dropping would be worse than either: a label holding a mangled value silently fails to match a query for the real one.

func Merge

func Merge(stored, incoming map[string]string) map[string]string

Merge overlays the baggage of the current request on top of the baggage already stored on an instance.

The stored one is the base because otelx adds user_id and username on every request, so an incoming request is never empty: without this, the first update from any other caller would drop a smart service instance id set at creation. The request wins per key so that a caller which does know a value can correct it.

func WithStored

func WithStored(ctx context.Context, entries map[string]string) context.Context

WithStored puts baggage read off an instance into ctx, for the paths that have an instance but no request to read a context off — the startup restore, in practice.

Rendered and parsed back rather than built from members directly, so there is one encoding path and not two: NewMember refuses a raw value holding a comma or a space, while Parse decodes the header into exactly those values, which is what a log field should show.

An unparseable value leaves ctx as it was rather than failing the caller. The baggage annotates log lines; losing an annotation is better than losing the recreate that was about to happen.

func WithValue

func WithValue(ctx context.Context, key, value string) (context.Context, error)

WithValue returns ctx with one baggage entry added, and keeps the active span in sync the way otelx.AddBaggageToHTTPRequest does for an inbound request. Used for the instance id, which does not exist yet when the request arrives.

Types

This section is empty.

Jump to

Keyboard shortcuts

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