Skip to content
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

use otel autoconfigure #1147

Merged
merged 12 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/acceptance-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: OpenTelemetry Acceptance Tests

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
acceptance-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Check out oats
uses: actions/checkout@v4
with:
repository: grafana/oats
ref: 7cd5ca42fff009fd67ea986d42c79134ac99ae48
path: oats
- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: 17
distribution: temurin
cache: 'maven'
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.23'
cache-dependency-path: oats/go.sum
- name: Run the Maven verify phase
run: |
./mvnw clean install -DskipTests
- name: Run acceptance tests
run: ./scripts/run-acceptance-tests.sh
- name: upload log file
uses: actions/upload-artifact@v4
if: failure()
with:
name: docker-compose.log
path: oats/yaml/build/**/*.log
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ This repository uses [Google Java Format](https://github.com/google/google-java-

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

Use `-Dspotless.check.skip=true` to skip the formatting check during development.

## Running Tests

If you're getting errors when running tests:
Expand Down
15 changes: 12 additions & 3 deletions benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
<codahale.version>3.0.2</codahale.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-instrumentation-bom-alpha</artifactId>
<version>${otel.instrumentation.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
Expand Down Expand Up @@ -58,17 +70,14 @@
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
<version>${otel.version}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
<version>${otel.version}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-testing</artifactId>
<version>${otel.version}</version>
</dependency>
</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM eclipse-temurin:21-jre

COPY target/example-exporter-opentelemetry.jar ./app.jar
# check that the resource attributs from the agent are used, epsecially the service.instance.id should be the same
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
ENV JAVA_TOOL_OPTIONS=-javaagent:/usr/src/app/opentelemetry-javaagent.jar

#ENTRYPOINT [ "java", "-Dotel.javaagent.debug=true","-jar", "./app.jar" ] # for debugging
ENTRYPOINT [ "java", "-jar", "./app.jar" ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# OATS is an acceptance testing framework for OpenTelemetry - https://github.com/grafana/oats/tree/main/yaml
version: '3.4'

services:
java:
build:
context: ../..
dockerfile: oats-tests/agent/Dockerfile
environment:
OTEL_SERVICE_NAME: "rolldice"
OTEL_EXPORTER_OTLP_ENDPOINT: http://lgtm:4317
OTEL_EXPORTER_OTLP_PROTOCOL: grpc
OTEL_METRIC_EXPORT_INTERVAL: "5000" # so we don't have to wait 60s for metrics
12 changes: 12 additions & 0 deletions examples/example-exporter-opentelemetry/oats-tests/agent/oats.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# OATS is an acceptance testing framework for OpenTelemetry - https://github.com/grafana/oats/tree/main/yaml
docker-compose:
generator: docker-lgtm
files:
- ./docker-compose.yml
expected:
custom-checks:
- script: ./service-instance-id-check.py
metrics:
- promql: 'uptime_seconds_total{}'
value: '>= 0'

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3

# This script is used to check if the service instance id is present in the exported data
# The script will return 0 if the service instance id is present in the exported data

from urllib.request import urlopen
import urllib.parse
import json

url = ' http://localhost:9090/api/v1/label/instance/values'
res = json.loads(urlopen(url).read().decode('utf-8'))

values = list(res['data'])
print(values)

if "localhost:8888" in values:
values.remove("localhost:8888")

# both the agent and the exporter should report the same instance id
assert len(values) == 1

path = 'target_info{instance="%s"}' % values[0]
path = urllib.parse.quote_plus(path)
url = 'http://localhost:9090/api/v1/query?query=%s' % path
res = json.loads(urlopen(url).read().decode('utf-8'))

infos = res['data']['result']
print(infos)

# they should not have the same target info
# e.g. only the agent has telemetry_distro_name
assert len(infos) == 2

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM eclipse-temurin:21-jre

COPY target/example-exporter-opentelemetry.jar ./app.jar

ENTRYPOINT [ "java", "-jar", "./app.jar" ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# OATS is an acceptance testing framework for OpenTelemetry - https://github.com/grafana/oats/tree/main/yaml
version: '3.4'

services:
java:
build:
context: ../..
dockerfile: oats-tests/http/Dockerfile
environment:
OTEL_SERVICE_NAME: "rolldice"
OTEL_EXPORTER_OTLP_ENDPOINT: http://lgtm:4318
OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf
OTEL_METRIC_EXPORT_INTERVAL: "5000" # so we don't have to wait 60s for metrics
10 changes: 10 additions & 0 deletions examples/example-exporter-opentelemetry/oats-tests/http/oats.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# OATS is an acceptance testing framework for OpenTelemetry - https://github.com/grafana/oats/tree/main/yaml
docker-compose:
generator: docker-lgtm
files:
- ./docker-compose.yml
expected:
metrics:
- promql: 'uptime_seconds_total{}'
value: '>= 0'

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class Main {

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

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

while (true) {
Thread.sleep(1000);
System.out.println("Incrementing counter");
counter.inc();
}
}
Expand Down
67 changes: 67 additions & 0 deletions otel-agent-resources/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.prometheus</groupId>
<artifactId>client_java</artifactId>
<version>0.0.1-releasetest1</version>
</parent>

<artifactId>otel-agent-resources</artifactId>
<packaging>bundle</packaging>

<name>OpenTelemetry Agent Resource Extractor</name>
<description>
Reads the OpenTelemetry Agent resources the GlobalOpenTelemetry instance
</description>

<properties>
<automatic.module.name>io.prometheus.otel.resource.attributes</automatic.module.name>
<!-- don't update with dependabot, the Otel agent can handle older versions -->
<otel-dynamic-load.version>1.29.0</otel-dynamic-load.version>
</properties>

<dependencies>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactItems>
<artifactItem>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
<version>${otel-dynamic-load.version}</version>
<outputDirectory>${project.basedir}/src/main/resources/lib/</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-context</artifactId>
<version>${otel-dynamic-load.version}</version>
<outputDirectory>${project.basedir}/src/main/resources/lib/</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.prometheus.metrics.exporter.opentelemetry;
package io.prometheus.otelagent;

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

Expand All @@ -11,6 +11,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

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

Map<String, String> result = new HashMap<>();
for (Map.Entry<?, ?> entry : attributeMap.entrySet()) {
if (entry.getKey() != null && entry.getValue() != null) {
result.putIfAbsent(entry.getKey().toString(), entry.getValue().toString());
result.put(entry.getKey().toString(), entry.getValue().toString());
}
}
return Collections.unmodifiableMap(result);
}
}
} finally {
Expand All @@ -65,6 +72,7 @@ public static void addIfAbsent(Map<String, String> result, String instrumentatio
} catch (Exception ignored) {
// ignore
}
return Collections.emptyMap();
}

private static Object getField(String name, Object obj) throws Exception {
Expand Down
11 changes: 9 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<automatic.module.name>--module-name-need-to-be-overriden--</automatic.module.name>
<junit-jupiter.version>5.11.2</junit-jupiter.version>
<otel.version>1.42.1</otel.version>
<otel.instrumentation.version>2.8.0-alpha</otel.instrumentation.version>
<java.version>8</java.version>
</properties>

Expand Down Expand Up @@ -73,6 +73,7 @@
<module>prometheus-metrics-instrumentation-dropwizard5</module>
<module>prometheus-metrics-instrumentation-guava</module>
<module>prometheus-metrics-simpleclient-bridge</module>
<module>otel-agent-resources</module>
</modules>

<dependencies>
Expand Down Expand Up @@ -100,6 +101,12 @@
<version>3.26.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -236,7 +243,7 @@
<target>${java.version}</target>
<showWarnings>true</showWarnings>
<compilerArgs>
<arg>-Xlint:all</arg>
<arg>-Xlint:all,-serial,-processing</arg>
<arg>-Werror</arg>
<arg>-XDcompilePolicy=simple</arg>
<arg>
Expand Down
Loading