generated from honeycombio/.github
-
Notifications
You must be signed in to change notification settings - Fork 7
/
baggage-span-processor.ts
104 lines (98 loc) · 3.53 KB
/
baggage-span-processor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { Context, propagation } from '@opentelemetry/api';
import {
BatchSpanProcessor,
NoopSpanProcessor,
Span,
SpanExporter,
} from '@opentelemetry/sdk-trace-base';
import { HoneycombOptions } from './honeycomb-options';
import { configureConsoleTraceLinkExporter } from './local-exporter';
import { configureCompositeExporter } from './composite-exporter';
import { getHoneycombSpanExporter } from './exporter-utils';
/**
* Builds and returns a span processor with an exporter configured
* for sending telemetry to Honeycomb. The processor will duplicate
* baggage entries on span start and queue spans for batch send.
*
* ⚠ Caution ⚠️
*
* Do not put sensitive information in Baggage. See {@link BaggageSpanProcessor}
* for more details.
*
* @param opts {@link HoneycombOptions} used to configure export to Honeycomb
* @returns a configured {@link BatchWithBaggageSpanProcessor} for baggage attribute duping,
* span batching, and export to Honeycomb
*/
export function configureBatchWithBaggageSpanProcessor(
opts?: HoneycombOptions,
): BatchWithBaggageSpanProcessor {
const hnyExporter = getHoneycombSpanExporter(opts);
// if local visualisations enabled, create composite exporter configured
// to send to both local exporter and main exporter
if (opts?.localVisualizations) {
return new BatchWithBaggageSpanProcessor(
configureCompositeExporter([
hnyExporter,
configureConsoleTraceLinkExporter(opts),
]),
);
}
return new BatchWithBaggageSpanProcessor(hnyExporter);
}
/**
* A span processor that behaves like a {@link BatchSpanProcessor} with the
* addition of {@link BaggageSpanProcessor} behavior during onStart.
*/
export class BatchWithBaggageSpanProcessor extends BatchSpanProcessor {
private bsp: BaggageSpanProcessor;
constructor(exporter: SpanExporter) {
super(exporter);
this.bsp = new BaggageSpanProcessor();
}
/**
* Delegates to {@link BaggageSpanProcessor.onStart()}
*
* @param span a {@link Span} being started
* @param parentContext the {@link Context} in which `span` was started
*/
onStart(span: Span, parentContext: Context): void {
this.bsp.onStart(span, parentContext);
}
}
/**
* The BaggageSpanProcessor reads entries stored in {@link Baggage}
* from the parent context and adds the baggage entries' keys and
* values to the span as attributes on span start.
*
* Add this span processor to a tracer provider.
*
* Keys and values added to Baggage will appear on subsequent child
* spans for a trace within this service *and* be propagated to external
* services in accordance with any configured propagation formats
* configured. If the external services also have a Baggage span
* processor, the keys and values will appear in those child spans as
* well.
*
* ⚠ Warning ⚠️
*
* Do not put sensitive information in Baggage.
*
* To repeat: a consequence of adding data to Baggage is that the keys and
* values will appear in all outgoing HTTP headers from the application.
*/
export class BaggageSpanProcessor extends NoopSpanProcessor {
/**
* Adds an attribute to the `span` for each {@link Baggage} key and {@link BaggageEntry | entry value}
* present in the `parentContext`.
*
* @param span a {@link Span} being started
* @param parentContext the {@link Context} in which `span` was started
*/
onStart(span: Span, parentContext: Context): void {
(propagation.getBaggage(parentContext)?.getAllEntries() ?? []).forEach(
(entry) => {
span.setAttribute(entry[0], entry[1].value);
},
);
}
}