Skip to content

Commit 3c4b4f6

Browse files
MyDigitalLifeCopilotclayton-cornellkalleep
authored
Traceparent option (#4874)
* Added an option to enable the sending to traceparent headers * Updated CHANGELOG.md * Fixed typo * Update docs/sources/reference/config-blocks/tracing.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update CHANGELOG.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Remove duplicate code and fix changing of send_traceparent option * Improved * Moved to 1 append call * Improved naming * Improved naming * Update internal/runtime/tracing/tracing.go Co-authored-by: Karl Persson <23356117+kalleep@users.noreply.github.com> * Update CHANGELOG.md Co-authored-by: Karl Persson <23356117+kalleep@users.noreply.github.com> * Update internal/runtime/tracing/tracing.go Co-authored-by: Karl Persson <23356117+kalleep@users.noreply.github.com> * Update internal/runtime/tracing/tracing.go Co-authored-by: Karl Persson <23356117+kalleep@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com> Co-authored-by: Karl Persson <23356117+kalleep@users.noreply.github.com>
1 parent e0063e2 commit 3c4b4f6

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Main (unreleased)
2222

2323
- update promtail converter to use `file_match` block for `loki.source.file` instead of going through `local.file_match`. (@kalleep)
2424

25+
- Added `send_traceparent` option for `tracing` config to enable traceparent header propagation. (@MyDigitalLife)
26+
2527
- Add `meta_cache_address` to `beyla.ebpf` component. (@skl)
2628

2729
### Bugfixes

docs/sources/reference/config-blocks/tracing.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ tracing {
2626
You can use the following arguments with `tracing`:
2727

2828
| Name | Type | Description | Default | Required |
29-
| ------------------- | ------------------------ | --------------------------------------------------- | ------- | -------- |
29+
|---------------------|--------------------------|-----------------------------------------------------|---------|----------|
3030
| `sampling_fraction` | `number` | Fraction of traces to keep. | `0.1` | no |
3131
| `write_to` | `list(otelcol.Consumer)` | Inputs from `otelcol` components to send traces to. | `[]` | no |
32+
| `send_traceparent` | `bool` | Send traceparent header with requests. | `false` | no |
3233

3334
The `write_to` argument controls which components to send traces to for processing.
3435
The elements in the array can be any `otelcol` component that accept traces, including processors and exporters.
@@ -42,6 +43,8 @@ The `sampling_fraction` argument controls what percentage of generated traces sh
4243
When set to `1` or greater, 100% of traces are kept.
4344
When set to `0` or lower, 0% of traces are kept.
4445

46+
The `send_traceparent` argument controls whether the `traceparent` header is sent with requests to other services.
47+
4548
## Blocks
4649

4750
You can use the following blocks with `tracing`:
@@ -85,6 +88,7 @@ If the remote sampling strategy exceeds the limit, sampling decisions fall back
8588
```alloy
8689
tracing {
8790
sampling_fraction = 0.1
91+
send_traceparent = true
8892
8993
write_to = [otelcol.exporter.otlp.tempo.input]
9094
}

internal/runtime/tracing/tracing.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
"github.com/grafana/alloy/internal/build"
1212
"github.com/grafana/alloy/internal/component/otelcol"
1313
"github.com/grafana/alloy/internal/runtime/tracing/internal/jaegerremote"
14+
"go.opentelemetry.io/otel"
1415
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
16+
"go.opentelemetry.io/otel/propagation"
1517
"go.opentelemetry.io/otel/sdk/resource"
1618
tracesdk "go.opentelemetry.io/otel/sdk/trace"
1719
semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
@@ -25,6 +27,7 @@ var (
2527
DefaultOptions = Options{
2628
SamplingFraction: 0.1, // Keep 10% of spans
2729
WriteTo: []otelcol.Consumer{}, // Don't send spans anywhere.
30+
SendTraceparent: false,
2831
}
2932

3033
DefaultJaegerRemoteSamplerOptions = JaegerRemoteSamplerOptions{
@@ -39,6 +42,7 @@ type Options struct {
3942
// SamplingFraction determines which rate of traces to sample. A value of 1
4043
// means to keep 100% of traces. A value of 0 means to keep 0% of traces.
4144
SamplingFraction float64 `alloy:"sampling_fraction,attr,optional"`
45+
SendTraceparent bool `alloy:"send_traceparent,attr,optional"`
4246

4347
// Sampler holds optional samplers to configure on top of the sampling
4448
// fraction.
@@ -109,6 +113,8 @@ func New(cfg Options) (*Tracer, error) {
109113
var sampler lazySampler
110114
sampler.SetSampler(tracesdk.TraceIDRatioBased(cfg.SamplingFraction))
111115

116+
setOTELTraceContextPropagators(cfg)
117+
112118
shimClient := &client{}
113119
exp := otlptrace.NewUnstarted(shimClient)
114120

@@ -136,6 +142,8 @@ func (t *Tracer) Update(opts Options) error {
136142
t.samplerMut.Lock()
137143
defer t.samplerMut.Unlock()
138144

145+
setOTELTraceContextPropagators(opts)
146+
139147
t.client.UpdateWriteTo(opts.WriteTo)
140148

141149
// Stop the previous instance of the Jaeger remote sampler if it exists. The
@@ -169,6 +177,19 @@ func (t *Tracer) Update(opts Options) error {
169177
return nil
170178
}
171179

180+
func setOTELTraceContextPropagators(opts Options) {
181+
var propagators []propagation.TextMapPropagator
182+
if opts.SendTraceparent {
183+
propagators = append(
184+
propagators,
185+
propagation.TraceContext{},
186+
propagation.Baggage{},
187+
)
188+
}
189+
190+
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagators...))
191+
}
192+
172193
// Run starts the tracing subsystem and runs it until the provided context is
173194
// canceled. If the tracing subsystem could not be started, an error is
174195
// returned.

0 commit comments

Comments
 (0)