Skip to content

Commit 4e2a8e9

Browse files
authored
Merge pull request #838 from openconfig/otlp-metadata
feat: add support for custom headers in OTLP output configuration
2 parents 9e2781b + a150fb6 commit 4e2a8e9

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

docs/user_guide/outputs/otlp_output.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
`gnmic` supports exporting subscription updates as [OpenTelemetry](https://opentelemetry.io/) metrics using the [OTLP](https://opentelemetry.io/docs/specs/otlp/) protocol.
22

3-
This output can be used to push metrics to any OTLP-compatible backend such as [Grafana Alloy](https://grafana.com/docs/alloy/latest/), [OpenTelemetry Collector](https://opentelemetry.io/docs/collector/), [Datadog](https://www.datadoghq.com/), [Dynatrace](https://www.dynatrace.com/), or any system that accepts OTLP metrics over gRPC.
3+
This output can be used to push metrics to any OTLP-compatible backend such as [Grafana Alloy](https://grafana.com/docs/alloy/latest/), [Grafana Mimir](https://grafana.com/docs/mimir/latest/), [OpenTelemetry Collector](https://opentelemetry.io/docs/collector/), [Datadog](https://www.datadoghq.com/), [Dynatrace](https://www.dynatrace.com/), or any system that accepts OTLP metrics over gRPC.
44

55
## Configuration
66

@@ -72,6 +72,11 @@ outputs:
7272
# map of string:string, additional static attributes to add to the OTLP Resource.
7373
resource-attributes:
7474
# key: value
75+
# map of string:string, HTTP headers (or gRPC metadata) to include with every export request.
76+
# Use this to set tenant/org identifiers required by multi-tenant backends such as
77+
# Grafana Mimir, Loki, or Tempo.
78+
headers:
79+
# X-Scope-OrgID: my-tenant
7580
# integer, defaults to 1.
7681
# number of workers processing events.
7782
num-workers: 1
@@ -152,6 +157,29 @@ resource-attributes:
152157

153158
Events are grouped by their `source` tag (the target device address). Each unique source becomes a separate OTLP `ResourceMetrics` entry with its own set of resource attributes.
154159

160+
## Custom Headers
161+
162+
The `headers` field attaches key/value pairs to every export request — as gRPC metadata when using the `grpc` protocol, or as HTTP headers when using `http`.
163+
164+
This is required by multi-tenant Grafana backends (Mimir, Loki, Tempo) which use the `X-Scope-OrgID` header to route data to the correct tenant:
165+
166+
```yaml
167+
outputs:
168+
mimir-output:
169+
type: otlp
170+
endpoint: mimir.example.com:4317
171+
headers:
172+
X-Scope-OrgID: my-tenant-id
173+
```
174+
175+
Multiple headers can be set simultaneously:
176+
177+
```yaml
178+
headers:
179+
X-Scope-OrgID: my-tenant-id
180+
X-Custom-Header: some-value
181+
```
182+
155183
## OTLP Output Metrics
156184

157185
When `enable-metrics` is set to `true`, the OTLP output exposes the following Prometheus metrics:

pkg/outputs/otlp_output/otlp_converter.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"strings"
1616
"sync"
1717

18+
"google.golang.org/grpc/metadata"
19+
1820
metricsv1 "go.opentelemetry.io/proto/otlp/collector/metrics/v1"
1921
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
2022
metricspb "go.opentelemetry.io/proto/otlp/metrics/v1"
@@ -452,6 +454,11 @@ func (o *otlpOutput) sendGRPC(ctx context.Context, req *metricsv1.ExportMetricsS
452454
return fmt.Errorf("request validation failed: %w", err)
453455
}
454456

457+
if len(cfg.Headers) > 0 {
458+
md := metadata.New(cfg.Headers)
459+
ctx = metadata.NewOutgoingContext(ctx, md)
460+
}
461+
455462
if cfg.Timeout > 0 {
456463
var cancel func()
457464
ctx, cancel = context.WithTimeout(ctx, cfg.Timeout)

pkg/outputs/otlp_output/otlp_output.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ type config struct {
129129
// Resource attributes
130130
ResourceAttributes map[string]string `mapstructure:"resource-attributes,omitempty"`
131131

132+
// Headers to include with every export request (gRPC metadata / HTTP headers).
133+
// Use this to set e.g. "X-Scope-OrgID" for Grafana Mimir, Loki, Tempo, etc.
134+
Headers map[string]string `mapstructure:"headers,omitempty"`
135+
132136
// Precomputed lookup set for ResourceTagKeys (not from config file).
133137
resourceTagSet map[string]bool
134138
// Compiled regexes from CounterPatterns.

0 commit comments

Comments
 (0)