-
Notifications
You must be signed in to change notification settings - Fork 858
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add User-Agent header to OTLP exporter requests (#4784)
* Add User-Agent header to OTLP exporter requests * PR feedback * Make OtlpUserAgent final * Add user agent note to managed channel
- Loading branch information
Showing
11 changed files
with
125 additions
and
1 deletion.
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
55 changes: 55 additions & 0 deletions
55
...ters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/OtlpUserAgent.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,55 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.exporter.internal.otlp; | ||
|
||
import java.util.Properties; | ||
import java.util.function.BiConsumer; | ||
|
||
/** | ||
* This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
* any time. | ||
*/ | ||
public final class OtlpUserAgent { | ||
|
||
private static final String userAgent = "OTel OTLP Exporter Java/" + readVersion(); | ||
|
||
private static String readVersion() { | ||
Properties properties = new Properties(); | ||
try { | ||
properties.load(OtlpUserAgent.class.getResourceAsStream("version.properties")); | ||
} catch (Exception e) { | ||
// we left the attribute empty | ||
return "unknown"; | ||
} | ||
return properties.getProperty("sdk.version", "unknown"); | ||
} | ||
|
||
/** | ||
* Return an OTLP {@code User-Agent} header value of the form {@code "OTel OTLP Exporter | ||
* Java/{version}"}. | ||
* | ||
* @see <a | ||
* href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#user-agent">OTLP | ||
* Exporter User Agent</a> | ||
*/ | ||
public static String getUserAgent() { | ||
return userAgent; | ||
} | ||
|
||
/** | ||
* Call the {@code consumer with} an OTLP {@code User-Agent} header value of the form {@code "OTel | ||
* OTLP Exporter Java/{version}"}. | ||
* | ||
* @see <a | ||
* href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#user-agent">OTLP | ||
* Exporter User Agent</a> | ||
*/ | ||
public static void addUserAgentHeader(BiConsumer<String, String> consumer) { | ||
consumer.accept("User-Agent", userAgent); | ||
} | ||
|
||
private OtlpUserAgent() {} | ||
} |
32 changes: 32 additions & 0 deletions
32
.../otlp/common/src/test/java/io/opentelemetry/exporter/internal/otlp/OtlpUserAgentTest.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.exporter.internal.otlp; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class OtlpUserAgentTest { | ||
|
||
@Test | ||
void getUserAgent() { | ||
assertThat(OtlpUserAgent.getUserAgent()).matches("OTel OTLP Exporter Java/1\\..*"); | ||
} | ||
|
||
@Test | ||
void addUserAgentHeader() { | ||
AtomicReference<String> keyRef = new AtomicReference<>(); | ||
AtomicReference<String> valueRef = new AtomicReference<>(); | ||
OtlpUserAgent.addUserAgentHeader( | ||
(key, value) -> { | ||
keyRef.set(key); | ||
valueRef.set(value); | ||
}); | ||
assertThat(keyRef.get()).isEqualTo("User-Agent"); | ||
assertThat(valueRef.get()).matches("OTel OTLP Exporter Java/1\\..*"); | ||
} | ||
} |
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