forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
B3Propagator.java
131 lines (118 loc) · 4.5 KB
/
B3Propagator.java
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.extension.trace.propagation;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.context.propagation.TextMapSetter;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/**
* Implementation of the B3 propagation protocol. See <a
* href=https://github.com/openzipkin/b3-propagation>openzipkin/b3-propagation</a>.
*
* <p>Also see <a
* href=https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/api-propagators.md#b3-requirements>B3
* Requirements</a>
*
* <p>To register the default B3 propagator, which injects a single header, use:
*
* <pre>{@code
* OpenTelemetry.setPropagators(
* DefaultContextPropagators
* .builder()
* .addTextMapPropagator(B3Propagator.injectingSingleHeader())
* .build());
* }</pre>
*
* <p>To register a B3 propagator that injects multiple headers, use:
*
* <pre>{@code
* OpenTelemetry.setPropagators(
* DefaultContextPropagators
* .builder()
* .addTextMapPropagator(B3Propagator.injectingMultiHeaders())
* .build());
* }</pre>
*/
@Immutable
public final class B3Propagator implements TextMapPropagator {
static final String TRACE_ID_HEADER = "X-B3-TraceId";
static final String SPAN_ID_HEADER = "X-B3-SpanId";
static final String SAMPLED_HEADER = "X-B3-Sampled";
static final String DEBUG_HEADER = "X-B3-Flags";
static final String COMBINED_HEADER = "b3";
static final String COMBINED_HEADER_DELIMITER = "-";
static final ContextKey<Boolean> DEBUG_CONTEXT_KEY = ContextKey.named("b3-debug");
static final String MULTI_HEADER_DEBUG = "1";
static final String SINGLE_HEADER_DEBUG = "d";
static final char COMBINED_HEADER_DELIMITER_CHAR = '-';
static final char IS_SAMPLED = '1';
static final char NOT_SAMPLED = '0';
static final char DEBUG_SAMPLED = 'd';
private static final B3Propagator SINGLE_HEADER_INSTANCE =
new B3Propagator(new B3PropagatorInjectorSingleHeader());
private static final B3Propagator MULTI_HEADERS_INSTANCE =
new B3Propagator(new B3PropagatorInjectorMultipleHeaders());
private final B3PropagatorExtractor singleHeaderExtractor =
new B3PropagatorExtractorSingleHeader();
private final B3PropagatorExtractor multipleHeadersExtractor =
new B3PropagatorExtractorMultipleHeaders();
private final B3PropagatorInjector b3PropagatorInjector;
private B3Propagator(B3PropagatorInjector b3PropagatorInjector) {
this.b3PropagatorInjector = b3PropagatorInjector;
}
/**
* Returns an instance of the {@link B3Propagator} that injects multi headers format.
*
* <p>This instance extracts both formats, in the order: single header, multi header.
*
* @return an instance of the {@link B3Propagator} that injects multi headers format.
*/
public static B3Propagator injectingMultiHeaders() {
return MULTI_HEADERS_INSTANCE;
}
/**
* Returns an instance of the {@link B3Propagator} that injects single header format.
*
* <p>This instance extracts both formats, in the order: single header, multi header.
*
* <p>This is the default instance for {@link B3Propagator}.
*
* @return an instance of the {@link B3Propagator} that injects single header format.
*/
public static B3Propagator injectingSingleHeader() {
return SINGLE_HEADER_INSTANCE;
}
@Override
public Collection<String> fields() {
return b3PropagatorInjector.fields();
}
@Override
public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {
b3PropagatorInjector.inject(context, carrier, setter);
}
@Override
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
return Stream.<Supplier<Optional<Context>>>of(
() -> singleHeaderExtractor.extract(context, carrier, getter),
() -> multipleHeadersExtractor.extract(context, carrier, getter),
() -> Optional.of(context))
.map(Supplier::get)
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst()
.get();
}
@Override
public String toString() {
return "B3Propagator{b3PropagatorInjector=" + b3PropagatorInjector + "}";
}
}