-
Notifications
You must be signed in to change notification settings - Fork 775
/
MultiTextMapPropagator.java
74 lines (63 loc) · 2.13 KB
/
MultiTextMapPropagator.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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.context.propagation;
import io.opentelemetry.context.Context;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
final class MultiTextMapPropagator implements TextMapPropagator {
private final TextMapPropagator[] textMapPropagators;
private final Collection<String> allFields;
MultiTextMapPropagator(TextMapPropagator... textMapPropagators) {
this(Arrays.asList(textMapPropagators));
}
MultiTextMapPropagator(List<TextMapPropagator> textMapPropagators) {
this.textMapPropagators = new TextMapPropagator[textMapPropagators.size()];
textMapPropagators.toArray(this.textMapPropagators);
this.allFields = Collections.unmodifiableList(getAllFields(this.textMapPropagators));
}
@Override
public Collection<String> fields() {
return allFields;
}
private static List<String> getAllFields(TextMapPropagator[] textPropagators) {
Set<String> fields = new LinkedHashSet<>();
for (TextMapPropagator textPropagator : textPropagators) {
fields.addAll(textPropagator.fields());
}
return new ArrayList<>(fields);
}
@Override
public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {
if (context == null || setter == null) {
return;
}
for (TextMapPropagator textPropagator : textMapPropagators) {
textPropagator.inject(context, carrier, setter);
}
}
@Override
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
if (context == null) {
return Context.root();
}
if (getter == null) {
return context;
}
for (TextMapPropagator textPropagator : textMapPropagators) {
context = textPropagator.extract(context, carrier, getter);
}
return context;
}
@Override
public String toString() {
return "MultiTextMapPropagator{textMapPropagators=" + Arrays.toString(textMapPropagators) + '}';
}
}