Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenTelemetry Bridge Baggage support (#3249)
- Loading branch information
Showing
11 changed files
with
200 additions
and
48 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
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
58 changes: 58 additions & 0 deletions
58
...elemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/baggage/OtelBaggage.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,58 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package co.elastic.apm.agent.opentelemetry.baggage; | ||
|
||
import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent; | ||
import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; | ||
import io.opentelemetry.api.baggage.Baggage; | ||
import io.opentelemetry.api.baggage.BaggageBuilder; | ||
import io.opentelemetry.api.baggage.BaggageEntryMetadata; | ||
|
||
public class OtelBaggage { | ||
|
||
private static final WeakMap<Baggage, co.elastic.apm.agent.impl.baggage.Baggage> translationCache = WeakConcurrent.buildMap(); | ||
|
||
public static Baggage fromElasticBaggage(co.elastic.apm.agent.impl.baggage.Baggage elasticBaggage) { | ||
BaggageBuilder builder = Baggage.builder(); | ||
for (String key : elasticBaggage.keys()) { | ||
builder.put(key, elasticBaggage.get(key), BaggageEntryMetadata.create(elasticBaggage.getMetadata(key))); | ||
} | ||
Baggage result = builder.build(); | ||
// remember the translation for future toElasticBaggage calls | ||
translationCache.put(result, elasticBaggage); | ||
return result; | ||
} | ||
|
||
public static co.elastic.apm.agent.impl.baggage.Baggage toElasticBaggage(Baggage otelBaggage) { | ||
if (otelBaggage == null || otelBaggage.isEmpty()) { | ||
return co.elastic.apm.agent.impl.baggage.Baggage.EMPTY; | ||
} | ||
co.elastic.apm.agent.impl.baggage.Baggage translated = translationCache.get(otelBaggage); | ||
if (translated == null) { | ||
co.elastic.apm.agent.impl.baggage.Baggage.Builder builder = co.elastic.apm.agent.impl.baggage.Baggage.builder(); | ||
otelBaggage.forEach((key, value) -> { | ||
String metadata = value.getMetadata().getValue(); | ||
builder.put(key, value.getValue(), metadata.isEmpty() ? null : metadata); | ||
}); | ||
translated = builder.build(); | ||
translationCache.put(otelBaggage, translated); | ||
} | ||
return translated; | ||
} | ||
} |
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
Oops, something went wrong.