Skip to content

Commit 38188b9

Browse files
authored
use otel autoconfigure (#1147)
* use otel autoconfig Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com> * add oats test Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com> * add oats test Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com> * add oats test Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com> * add oats test Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com> * add oats test Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com> * add oats test Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com> * extra project for otel resource attributes Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com> * extra project for otel resource attributes Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com> * extra project for otel resource attributes Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com> * add otel tests Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com> * release automation Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com> --------- Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
1 parent 6f4793c commit 38188b9

File tree

28 files changed

+932
-509
lines changed

28 files changed

+932
-509
lines changed
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: OpenTelemetry Acceptance Tests
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
acceptance-tests:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- name: Check out oats
15+
uses: actions/checkout@v4
16+
with:
17+
repository: grafana/oats
18+
ref: 7cd5ca42fff009fd67ea986d42c79134ac99ae48
19+
path: oats
20+
- name: Set up JDK
21+
uses: actions/setup-java@v4
22+
with:
23+
java-version: 17
24+
distribution: temurin
25+
cache: 'maven'
26+
- name: Set up Go
27+
uses: actions/setup-go@v5
28+
with:
29+
go-version: '1.23'
30+
cache-dependency-path: oats/go.sum
31+
- name: Run the Maven verify phase
32+
run: |
33+
./mvnw clean install -DskipTests
34+
- name: Run acceptance tests
35+
run: ./scripts/run-acceptance-tests.sh
36+
- name: upload log file
37+
uses: actions/upload-artifact@v4
38+
if: failure()
39+
with:
40+
name: docker-compose.log
41+
path: oats/yaml/build/**/*.log

CONTRIBUTING.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ This repository uses [Google Java Format](https://github.com/google/google-java-
1717

1818
Run `./mvnw spotless:apply` to format the code (only changed files) before committing.
1919

20+
Use `-Dspotless.check.skip=true` to skip the formatting check during development.
21+
2022
## Running Tests
2123

2224
If you're getting errors when running tests:

benchmarks/pom.xml

+12-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@
2222
<codahale.version>3.0.2</codahale.version>
2323
</properties>
2424

25+
<dependencyManagement>
26+
<dependencies>
27+
<dependency>
28+
<groupId>io.opentelemetry.instrumentation</groupId>
29+
<artifactId>opentelemetry-instrumentation-bom-alpha</artifactId>
30+
<version>${otel.instrumentation.version}</version>
31+
<type>pom</type>
32+
<scope>import</scope>
33+
</dependency>
34+
</dependencies>
35+
</dependencyManagement>
36+
2537
<dependencies>
2638
<dependency>
2739
<groupId>org.openjdk.jmh</groupId>
@@ -58,17 +70,14 @@
5870
<dependency>
5971
<groupId>io.opentelemetry</groupId>
6072
<artifactId>opentelemetry-api</artifactId>
61-
<version>${otel.version}</version>
6273
</dependency>
6374
<dependency>
6475
<groupId>io.opentelemetry</groupId>
6576
<artifactId>opentelemetry-sdk</artifactId>
66-
<version>${otel.version}</version>
6777
</dependency>
6878
<dependency>
6979
<groupId>io.opentelemetry</groupId>
7080
<artifactId>opentelemetry-sdk-testing</artifactId>
71-
<version>${otel.version}</version>
7281
</dependency>
7382
</dependencies>
7483
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM eclipse-temurin:21-jre
2+
3+
COPY target/example-exporter-opentelemetry.jar ./app.jar
4+
# check that the resource attributs from the agent are used, epsecially the service.instance.id should be the same
5+
ADD --chmod=644 https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v2.8.0/opentelemetry-javaagent.jar /usr/src/app/opentelemetry-javaagent.jar
6+
ENV JAVA_TOOL_OPTIONS=-javaagent:/usr/src/app/opentelemetry-javaagent.jar
7+
8+
#ENTRYPOINT [ "java", "-Dotel.javaagent.debug=true","-jar", "./app.jar" ] # for debugging
9+
ENTRYPOINT [ "java", "-jar", "./app.jar" ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# OATS is an acceptance testing framework for OpenTelemetry - https://github.com/grafana/oats/tree/main/yaml
2+
version: '3.4'
3+
4+
services:
5+
java:
6+
build:
7+
context: ../..
8+
dockerfile: oats-tests/agent/Dockerfile
9+
environment:
10+
OTEL_SERVICE_NAME: "rolldice"
11+
OTEL_EXPORTER_OTLP_ENDPOINT: http://lgtm:4317
12+
OTEL_EXPORTER_OTLP_PROTOCOL: grpc
13+
OTEL_METRIC_EXPORT_INTERVAL: "5000" # so we don't have to wait 60s for metrics
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# OATS is an acceptance testing framework for OpenTelemetry - https://github.com/grafana/oats/tree/main/yaml
2+
docker-compose:
3+
generator: docker-lgtm
4+
files:
5+
- ./docker-compose.yml
6+
expected:
7+
custom-checks:
8+
- script: ./service-instance-id-check.py
9+
metrics:
10+
- promql: 'uptime_seconds_total{}'
11+
value: '>= 0'
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
3+
# This script is used to check if the service instance id is present in the exported data
4+
# The script will return 0 if the service instance id is present in the exported data
5+
6+
from urllib.request import urlopen
7+
import urllib.parse
8+
import json
9+
10+
url = ' http://localhost:9090/api/v1/label/instance/values'
11+
res = json.loads(urlopen(url).read().decode('utf-8'))
12+
13+
values = list(res['data'])
14+
print(values)
15+
16+
if "localhost:8888" in values:
17+
values.remove("localhost:8888")
18+
19+
# both the agent and the exporter should report the same instance id
20+
assert len(values) == 1
21+
22+
path = 'target_info{instance="%s"}' % values[0]
23+
path = urllib.parse.quote_plus(path)
24+
url = 'http://localhost:9090/api/v1/query?query=%s' % path
25+
res = json.loads(urlopen(url).read().decode('utf-8'))
26+
27+
infos = res['data']['result']
28+
print(infos)
29+
30+
# they should not have the same target info
31+
# e.g. only the agent has telemetry_distro_name
32+
assert len(infos) == 2
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM eclipse-temurin:21-jre
2+
3+
COPY target/example-exporter-opentelemetry.jar ./app.jar
4+
5+
ENTRYPOINT [ "java", "-jar", "./app.jar" ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# OATS is an acceptance testing framework for OpenTelemetry - https://github.com/grafana/oats/tree/main/yaml
2+
version: '3.4'
3+
4+
services:
5+
java:
6+
build:
7+
context: ../..
8+
dockerfile: oats-tests/http/Dockerfile
9+
environment:
10+
OTEL_SERVICE_NAME: "rolldice"
11+
OTEL_EXPORTER_OTLP_ENDPOINT: http://lgtm:4318
12+
OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf
13+
OTEL_METRIC_EXPORT_INTERVAL: "5000" # so we don't have to wait 60s for metrics
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# OATS is an acceptance testing framework for OpenTelemetry - https://github.com/grafana/oats/tree/main/yaml
2+
docker-compose:
3+
generator: docker-lgtm
4+
files:
5+
- ./docker-compose.yml
6+
expected:
7+
metrics:
8+
- promql: 'uptime_seconds_total{}'
9+
value: '>= 0'
10+

examples/example-exporter-opentelemetry/src/main/java/io/prometheus/metrics/examples/opentelemetry/Main.java

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
public class Main {
1010

1111
public static void main(String[] args) throws Exception {
12+
System.out.println("Starting example application");
1213

1314
// Note: Some JVM metrics are also defined as OpenTelemetry's semantic conventions.
1415
// We have plans to implement a configuration option for JvmMetrics to use OpenTelemetry
@@ -34,6 +35,7 @@ public static void main(String[] args) throws Exception {
3435

3536
while (true) {
3637
Thread.sleep(1000);
38+
System.out.println("Incrementing counter");
3739
counter.inc();
3840
}
3941
}

otel-agent-resources/pom.xml

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>io.prometheus</groupId>
8+
<artifactId>client_java</artifactId>
9+
<version>0.0.1-releasetest1</version>
10+
</parent>
11+
12+
<artifactId>otel-agent-resources</artifactId>
13+
<packaging>bundle</packaging>
14+
15+
<name>OpenTelemetry Agent Resource Extractor</name>
16+
<description>
17+
Reads the OpenTelemetry Agent resources the GlobalOpenTelemetry instance
18+
</description>
19+
20+
<properties>
21+
<automatic.module.name>io.prometheus.otel.resource.attributes</automatic.module.name>
22+
<!-- don't update with dependabot, the Otel agent can handle older versions -->
23+
<otel-dynamic-load.version>1.29.0</otel-dynamic-load.version>
24+
</properties>
25+
26+
<dependencies>
27+
</dependencies>
28+
29+
<build>
30+
<resources>
31+
<resource>
32+
<directory>src/main/resources</directory>
33+
</resource>
34+
</resources>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-dependency-plugin</artifactId>
39+
<executions>
40+
<execution>
41+
<id>copy</id>
42+
<phase>validate</phase>
43+
<goals>
44+
<goal>copy</goal>
45+
</goals>
46+
</execution>
47+
</executions>
48+
<configuration>
49+
<artifactItems>
50+
<artifactItem>
51+
<groupId>io.opentelemetry</groupId>
52+
<artifactId>opentelemetry-api</artifactId>
53+
<version>${otel-dynamic-load.version}</version>
54+
<outputDirectory>${project.basedir}/src/main/resources/lib/</outputDirectory>
55+
</artifactItem>
56+
<artifactItem>
57+
<groupId>io.opentelemetry</groupId>
58+
<artifactId>opentelemetry-context</artifactId>
59+
<version>${otel-dynamic-load.version}</version>
60+
<outputDirectory>${project.basedir}/src/main/resources/lib/</outputDirectory>
61+
</artifactItem>
62+
</artifactItems>
63+
</configuration>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
</project>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.prometheus.metrics.exporter.opentelemetry;
1+
package io.prometheus.otelagent;
22

33
import static java.nio.file.Files.createTempDirectory;
44

@@ -11,6 +11,8 @@
1111
import java.nio.file.Files;
1212
import java.nio.file.Path;
1313
import java.nio.file.StandardCopyOption;
14+
import java.util.Collections;
15+
import java.util.HashMap;
1416
import java.util.Map;
1517

1618
public class ResourceAttributesFromOtelAgent {
@@ -31,8 +33,11 @@ public class ResourceAttributesFromOtelAgent {
3133
*
3234
* <p>After that we discard the class loader so that all OTel specific classes are unloaded. No
3335
* runtime dependency on any OTel version remains.
36+
*
37+
* <p>The test for this class is in
38+
* examples/example-exporter-opentelemetry/oats-tests/agent/service-instance-id-check.py
3439
*/
35-
public static void addIfAbsent(Map<String, String> result, String instrumentationScopeName) {
40+
public static Map<String, String> getResourceAttributes(String instrumentationScopeName) {
3641
try {
3742
Path tmpDir = createTempDirectory(instrumentationScopeName + "-");
3843
try {
@@ -43,7 +48,7 @@ public static void addIfAbsent(Map<String, String> result, String instrumentatio
4348
classLoader.loadClass("io.opentelemetry.api.GlobalOpenTelemetry");
4449
Object globalOpenTelemetry = globalOpenTelemetryClass.getMethod("get").invoke(null);
4550
if (globalOpenTelemetry.getClass().getSimpleName().contains("ApplicationOpenTelemetry")) {
46-
// GlobalOpenTelemetry is injected by the OTel Java aqent
51+
// GlobalOpenTelemetry is injected by the OTel Java agent
4752
Object applicationMeterProvider = callMethod("getMeterProvider", globalOpenTelemetry);
4853
Object agentMeterProvider = getField("agentMeterProvider", applicationMeterProvider);
4954
Object sdkMeterProvider = getField("delegate", agentMeterProvider);
@@ -52,11 +57,13 @@ public static void addIfAbsent(Map<String, String> result, String instrumentatio
5257
Object attributes = callMethod("getAttributes", resource);
5358
Map<?, ?> attributeMap = (Map<?, ?>) callMethod("asMap", attributes);
5459

60+
Map<String, String> result = new HashMap<>();
5561
for (Map.Entry<?, ?> entry : attributeMap.entrySet()) {
5662
if (entry.getKey() != null && entry.getValue() != null) {
57-
result.putIfAbsent(entry.getKey().toString(), entry.getValue().toString());
63+
result.put(entry.getKey().toString(), entry.getValue().toString());
5864
}
5965
}
66+
return Collections.unmodifiableMap(result);
6067
}
6168
}
6269
} finally {
@@ -65,6 +72,7 @@ public static void addIfAbsent(Map<String, String> result, String instrumentatio
6572
} catch (Exception ignored) {
6673
// ignore
6774
}
75+
return Collections.emptyMap();
6876
}
6977

7078
private static Object getField(String name, Object obj) throws Exception {

pom.xml

+9-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1919
<automatic.module.name>--module-name-need-to-be-overriden--</automatic.module.name>
2020
<junit-jupiter.version>5.11.2</junit-jupiter.version>
21-
<otel.version>1.42.1</otel.version>
21+
<otel.instrumentation.version>2.8.0-alpha</otel.instrumentation.version>
2222
<java.version>8</java.version>
2323
</properties>
2424

@@ -73,6 +73,7 @@
7373
<module>prometheus-metrics-instrumentation-dropwizard5</module>
7474
<module>prometheus-metrics-instrumentation-guava</module>
7575
<module>prometheus-metrics-simpleclient-bridge</module>
76+
<module>otel-agent-resources</module>
7677
</modules>
7778

7879
<dependencies>
@@ -100,6 +101,12 @@
100101
<version>3.26.3</version>
101102
<scope>test</scope>
102103
</dependency>
104+
<dependency>
105+
<groupId>org.slf4j</groupId>
106+
<artifactId>slf4j-simple</artifactId>
107+
<version>1.7.36</version>
108+
<scope>test</scope>
109+
</dependency>
103110
</dependencies>
104111

105112
<build>
@@ -236,7 +243,7 @@
236243
<target>${java.version}</target>
237244
<showWarnings>true</showWarnings>
238245
<compilerArgs>
239-
<arg>-Xlint:all</arg>
246+
<arg>-Xlint:all,-serial,-processing</arg>
240247
<arg>-Werror</arg>
241248
<arg>-XDcompilePolicy=simple</arg>
242249
<arg>

0 commit comments

Comments
 (0)