-
Notifications
You must be signed in to change notification settings - Fork 848
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hide default context propagators implementation behind interface. (#2089
- Loading branch information
Anuraag Agrawal
authored
Nov 20, 2020
1 parent
d940947
commit 0e013e5
Showing
11 changed files
with
187 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
context/src/main/java/io/opentelemetry/context/propagation/MultiTextMapPropagator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* 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.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[] textPropagators; | ||
private final List<String> allFields; | ||
|
||
MultiTextMapPropagator(List<TextMapPropagator> textPropagators) { | ||
this.textPropagators = new TextMapPropagator[textPropagators.size()]; | ||
textPropagators.toArray(this.textPropagators); | ||
this.allFields = Collections.unmodifiableList(getAllFields(this.textPropagators)); | ||
} | ||
|
||
@Override | ||
public List<String> fields() { | ||
return allFields; | ||
} | ||
|
||
private static List<String> getAllFields(TextMapPropagator[] textPropagators) { | ||
Set<String> fields = new LinkedHashSet<>(); | ||
for (int i = 0; i < textPropagators.length; i++) { | ||
fields.addAll(textPropagators[i].fields()); | ||
} | ||
|
||
return new ArrayList<>(fields); | ||
} | ||
|
||
@Override | ||
public <C> void inject(Context context, @Nullable C carrier, Setter<C> setter) { | ||
for (int i = 0; i < textPropagators.length; i++) { | ||
textPropagators[i].inject(context, carrier, setter); | ||
} | ||
} | ||
|
||
@Override | ||
public <C> Context extract(Context context, @Nullable C carrier, Getter<C> getter) { | ||
for (int i = 0; i < textPropagators.length; i++) { | ||
context = textPropagators[i].extract(context, carrier, getter); | ||
} | ||
return context; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
context/src/main/java/io/opentelemetry/context/propagation/NoopTextMapPropagator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.context.propagation; | ||
|
||
import io.opentelemetry.context.Context; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import javax.annotation.Nullable; | ||
|
||
final class NoopTextMapPropagator implements TextMapPropagator { | ||
private static final NoopTextMapPropagator INSTANCE = new NoopTextMapPropagator(); | ||
|
||
static TextMapPropagator getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public List<String> fields() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public <C> void inject(Context context, @Nullable C carrier, Setter<C> setter) {} | ||
|
||
@Override | ||
public <C> Context extract(Context context, @Nullable C carrier, Getter<C> getter) { | ||
return context; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.