Skip to content

Commit 658c65d

Browse files
authored
declarative config: set distro + user-agent (#1050)
* add distro resource attributes for dc * add some tests for distro resource * start implementation of custom user-agent * finish work on user-agent * cleanup * remove duplication * fix things
1 parent 13dc9a2 commit 658c65d

8 files changed

Lines changed: 661 additions & 28 deletions

File tree

custom/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ dependencies {
2323
}
2424

2525
compileOnly("io.opentelemetry:opentelemetry-sdk")
26+
compileOnly("io.opentelemetry:opentelemetry-api-incubator")
2627
compileOnly("io.opentelemetry:opentelemetry-exporter-otlp")
2728
compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi")
29+
compileOnly("io.opentelemetry:opentelemetry-sdk-extension-incubator")
2830
compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api")
2931
compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-tooling")
3032
compileOnly(libs.bundles.semconv)
@@ -54,8 +56,10 @@ dependencies {
5456
// test dependencies
5557
testImplementation(project(":testing-common"))
5658
testImplementation("io.opentelemetry:opentelemetry-sdk")
59+
testImplementation("io.opentelemetry:opentelemetry-api-incubator")
5760
testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")
5861
testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi")
62+
testImplementation("io.opentelemetry:opentelemetry-sdk-extension-incubator")
5963
testImplementation("io.opentelemetry:opentelemetry-exporter-otlp")
6064
testImplementation("io.opentelemetry.javaagent:opentelemetry-javaagent-tooling") {
6165
//The following dependency isn't actually needed, but breaks the classpath when testing with Java 8
@@ -67,6 +71,7 @@ dependencies {
6771
testImplementation("io.opentelemetry:opentelemetry-sdk-testing")
6872
testImplementation(libs.freemarker)
6973
testImplementation(libs.wiremockjre8)
74+
testImplementation(libs.jsonunitassertj)
7075
}
7176

7277
tasks {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package co.elastic.otel;
20+
21+
import io.opentelemetry.api.common.Attributes;
22+
import io.opentelemetry.javaagent.tooling.AgentVersion;
23+
import io.opentelemetry.sdk.resources.Resource;
24+
import io.opentelemetry.semconv.incubating.TelemetryIncubatingAttributes;
25+
26+
public class ElasticDistroResource {
27+
28+
private ElasticDistroResource() {}
29+
30+
public static Resource get() {
31+
if (AgentVersion.VERSION == null) {
32+
return Resource.empty();
33+
}
34+
try {
35+
Class.forName("co.elastic.otel.agent.ElasticAgent");
36+
} catch (ClassNotFoundException e) {
37+
// this means that we are running as an extension of the vanilla agent
38+
// and not as distro.
39+
return Resource.empty();
40+
}
41+
return Resource.create(
42+
Attributes.of(
43+
TelemetryIncubatingAttributes.TELEMETRY_DISTRO_NAME,
44+
"elastic",
45+
TelemetryIncubatingAttributes.TELEMETRY_DISTRO_VERSION,
46+
AgentVersion.VERSION));
47+
}
48+
}

custom/src/main/java/co/elastic/otel/ElasticDistroResourceProvider.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,19 @@
1919
package co.elastic.otel;
2020

2121
import com.google.auto.service.AutoService;
22-
import io.opentelemetry.api.common.Attributes;
23-
import io.opentelemetry.javaagent.tooling.AgentVersion;
2422
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
2523
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
2624
import io.opentelemetry.sdk.resources.Resource;
27-
import io.opentelemetry.semconv.incubating.TelemetryIncubatingAttributes;
2825

26+
/**
27+
* Provides {@code telemetry.distro.name} and {@code telemetry.distro.version} resource attributes
28+
* for automatic configuration
29+
*/
2930
@AutoService(ResourceProvider.class)
3031
public class ElasticDistroResourceProvider implements ResourceProvider {
3132

3233
@Override
3334
public Resource createResource(ConfigProperties configProperties) {
34-
if (AgentVersion.VERSION == null) {
35-
return Resource.empty();
36-
}
37-
try {
38-
Class.forName("co.elastic.otel.agent.ElasticAgent");
39-
} catch (ClassNotFoundException e) {
40-
// this means that we are running as an extension of the vanilla agent
41-
// and not as distro.
42-
return Resource.empty();
43-
}
44-
return Resource.create(
45-
Attributes.of(
46-
TelemetryIncubatingAttributes.TELEMETRY_DISTRO_NAME,
47-
"elastic",
48-
TelemetryIncubatingAttributes.TELEMETRY_DISTRO_VERSION,
49-
AgentVersion.VERSION));
35+
return ElasticDistroResource.get();
5036
}
5137
}

custom/src/main/java/co/elastic/otel/ElasticUserAgentHeader.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,41 +29,42 @@
2929
import io.opentelemetry.sdk.metrics.export.MetricExporter;
3030
import io.opentelemetry.sdk.trace.export.SpanExporter;
3131

32+
/** set User-Agent for automatic configuration */
3233
public class ElasticUserAgentHeader {
3334

34-
private static final String HEADER_NAME = "User-Agent";
35-
private static final String GRPC_VALUE = "elastic-otlp-grpc-java/" + AgentVersion.VERSION;
36-
private static final String HTTP_VALUE = "elastic-otlp-http-java/" + AgentVersion.VERSION;
35+
public static final String HEADER_NAME = "User-Agent";
36+
public static final String OTLP_GRPC = "elastic-otlp-grpc-java/" + AgentVersion.VERSION;
37+
public static final String OTLP_HTTP = "elastic-otlp-http-java/" + AgentVersion.VERSION;
3738

3839
public static SpanExporter configureIfPossible(SpanExporter spanExporter) {
3940
if (spanExporter instanceof OtlpGrpcSpanExporter) {
4041
return ((OtlpGrpcSpanExporter) spanExporter)
41-
.toBuilder().addHeader(HEADER_NAME, GRPC_VALUE).build();
42+
.toBuilder().addHeader(HEADER_NAME, OTLP_GRPC).build();
4243
} else if (spanExporter instanceof OtlpHttpSpanExporter) {
4344
return ((OtlpHttpSpanExporter) spanExporter)
44-
.toBuilder().addHeader(HEADER_NAME, HTTP_VALUE).build();
45+
.toBuilder().addHeader(HEADER_NAME, OTLP_HTTP).build();
4546
}
4647
return spanExporter;
4748
}
4849

4950
public static MetricExporter configureIfPossible(MetricExporter metricExporter) {
5051
if (metricExporter instanceof OtlpGrpcMetricExporter) {
5152
return ((OtlpGrpcMetricExporter) metricExporter)
52-
.toBuilder().addHeader(HEADER_NAME, GRPC_VALUE).build();
53+
.toBuilder().addHeader(HEADER_NAME, OTLP_GRPC).build();
5354
} else if (metricExporter instanceof OtlpHttpMetricExporter) {
5455
return ((OtlpHttpMetricExporter) metricExporter)
55-
.toBuilder().addHeader(HEADER_NAME, HTTP_VALUE).build();
56+
.toBuilder().addHeader(HEADER_NAME, OTLP_HTTP).build();
5657
}
5758
return metricExporter;
5859
}
5960

6061
public static LogRecordExporter configureIfPossible(LogRecordExporter logExporter) {
6162
if (logExporter instanceof OtlpGrpcLogRecordExporter) {
6263
return ((OtlpGrpcLogRecordExporter) logExporter)
63-
.toBuilder().addHeader(HEADER_NAME, GRPC_VALUE).build();
64+
.toBuilder().addHeader(HEADER_NAME, OTLP_GRPC).build();
6465
} else if (logExporter instanceof OtlpHttpLogRecordExporter) {
6566
return ((OtlpHttpLogRecordExporter) logExporter)
66-
.toBuilder().addHeader(HEADER_NAME, HTTP_VALUE).build();
67+
.toBuilder().addHeader(HEADER_NAME, OTLP_HTTP).build();
6768
}
6869
return logExporter;
6970
}

0 commit comments

Comments
 (0)