-
Notifications
You must be signed in to change notification settings - Fork 46
Using Baggage to propagate attributes from parent Span, Fixes AB#3262849 #2671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
04b2eea
10dcdb7
f3fbae9
8988bfc
1839e10
ecf9a9b
6290a35
bb49f78
2261a52
60e9609
8203a62
5aef0a7
c20b5a6
c6b72ed
4703f71
ff4d616
952de50
2b5133a
2c0afd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // All rights reserved. | ||
| // | ||
| // This code is licensed under the MIT License. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files(the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions : | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in | ||
| // all copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| // THE SOFTWARE. | ||
| package com.microsoft.identity.common.java.opentelemetry; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator; | ||
| import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.propagation.TextMapGetter; | ||
| import io.opentelemetry.context.propagation.TextMapPropagator; | ||
| import io.opentelemetry.context.propagation.TextMapSetter; | ||
|
|
||
| /** | ||
| * Extension class for handling OpenTelemetry context propagation. | ||
| * Provides utility methods for injecting and extracting context which contains Baggage and SpanContext. | ||
| */ | ||
| public final class TextMapPropagatorExtension { | ||
|
|
||
| private static final TextMapPropagator PROPAGATOR = TextMapPropagator.composite( | ||
|
||
| W3CTraceContextPropagator.getInstance(), | ||
| W3CBaggagePropagator.getInstance() | ||
| ); | ||
|
|
||
| /** | ||
| * Private constructor to prevent instantiation. | ||
| */ | ||
| private TextMapPropagatorExtension() { | ||
| // This utility class is not meant to be instantiated | ||
melissaahn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Injects the current context into a carrier. | ||
| * | ||
| * @param context The context to inject. If null, the current context will be used. | ||
| * @return A map containing the injected context properties. | ||
| */ | ||
| public static HashMap<String, String> inject(Context context) { | ||
| final HashMap<String, String> carrier = new HashMap<>(); | ||
| final Context contextToInject = context != null ? context : Context.current(); | ||
|
|
||
| final TextMapSetter<Map<String, String>> setter = new TextMapSetter<Map<String, String>>() { | ||
| @Override | ||
| public void set(Map<String, String> carrier, String key, String value) { | ||
melissaahn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (carrier != null && key != null && value != null) { | ||
| carrier.put(key, value); | ||
| } | ||
melissaahn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }; | ||
|
|
||
| PROPAGATOR.inject(contextToInject, carrier, setter); | ||
| return carrier; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts context from a carrier map. | ||
| * | ||
| * @param carrier The carrier containing context information. | ||
| * @return The extracted context, or the current context if extraction fails. | ||
| */ | ||
| public static Context extract(Map<String, String> carrier) { | ||
| if (carrier == null || carrier.isEmpty()) { | ||
| return Context.current(); | ||
| } | ||
|
|
||
| final TextMapGetter<Map<String, String>> getter = new TextMapGetter<Map<String, String>>() { | ||
| @Override | ||
| public String get(Map<String, String> carrier, String key) { | ||
| return carrier.get(key); | ||
| } | ||
|
|
||
| @Override | ||
| public Iterable<String> keys(Map<String, String> carrier) { | ||
| return carrier.keySet(); | ||
| } | ||
| }; | ||
|
|
||
| return PROPAGATOR.extract(Context.current(), carrier, getter); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.