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

Demosntrate declarative config 0.3 with otel java agent #492

Merged
merged 11 commits into from
Oct 21, 2024
3 changes: 2 additions & 1 deletion javaagent/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ FROM eclipse-temurin:11-jre

ADD build/libs/app.jar /app.jar
ADD build/agent/opentelemetry-javaagent.jar /opentelemetry-javaagent.jar
ADD build/agent/opentelemetry-javaagent-extension.jar /opentelemetry-javaagent-extension.jar

ENTRYPOINT java -jar -javaagent:/opentelemetry-javaagent.jar /app.jar
ENTRYPOINT java -jar -javaagent:/opentelemetry-javaagent.jar -Dotel.javaagent.extensions=/opentelemetry-javaagent-extension.jar /app.jar
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loading the rule based sampler artifact as an agent extension, so it can be referenced in the declarative config file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when i was running this locally i had issues with the ordering of -jar -javaagent

Suggested change
ENTRYPOINT java -jar -javaagent:/opentelemetry-javaagent.jar -Dotel.javaagent.extensions=/opentelemetry-javaagent-extension.jar /app.jar
ENTRYPOINT java -javaagent:/opentelemetry-javaagent.jar -jar -Dotel.javaagent.extensions=/opentelemetry-javaagent-extension.jar /app.jar

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird.. I wonder why I didn't.. I moved -jar all the way to the end, right before /app.jar.

9 changes: 6 additions & 3 deletions javaagent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ Watch for spans, metrics, and logs in the collector log output

## File Configuration

By default, this example uses the [environment variable configuration schema](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md) to configure the SDK. However, it also includes [sdk-config.yaml](./sdk-config.yaml) which demonstrates how the file configuration scheme can be used instead. `sdk-config.yaml` demonstrates view configuration to disable a metric, something which is not available in the environment variable configuration scheme.
By default, this example uses the [environment variable configuration schema](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md) to configure the SDK. However, it also includes [sdk-migration-config.yaml](./sdk-migration-config.yaml) which demonstrates how the file configuration scheme can be used instead. `sdk-migration-config.yaml` extends the [opentelemetry-configuration sdk-migration-config.yaml](https://github.com/open-telemetry/opentelemetry-configuration/blob/v0.3.0/examples/sdk-migration-config.yaml) template, demonstrating:

- Configuration of instrumentation (see `.instrumentation.java`)
- Configuration of [rule based routing sampler](https://github.com/open-telemetry/opentelemetry-java-contrib/tree/main/samplers) (see `.tracer_provider.sampler`)

To use file configuration instead of the environment variable scheme, add the following before starting the application and collector:

```shell
export OTEL_CONFIG_FILE=/sdk-config.yaml
export OTEL_EXPERIMENTAL_CONFIG_FILE=/sdk-migration-config.yaml
```

Note that toggling file configuration causes the environment variable configuration scheme to be ignored completely. However, there is support for environment variable substitution within configuration files.
Note besides the environment variables referenced in `sdk-migration-config.yaml` using [substitution syntax](https://opentelemetry.io/docs/specs/otel/configuration/data-model/#environment-variable-substitution), environment variables are ignored.
13 changes: 13 additions & 0 deletions javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ java {
}

val agent = configurations.create("agent")
val extension = configurations.create("extension")

dependencies {
implementation(platform(SpringBootPlugin.BOM_COORDINATES))
implementation("io.opentelemetry:opentelemetry-api")

//spring modules
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-actuator")

agent("io.opentelemetry.javaagent:opentelemetry-javaagent:2.9.0")
extension("io.opentelemetry.contrib:opentelemetry-samplers:1.40.0-alpha")
}

val copyAgent = tasks.register<Copy>("copyAgent") {
Expand All @@ -33,9 +36,19 @@ val copyAgent = tasks.register<Copy>("copyAgent") {
rename("opentelemetry-javaagent-.*\\.jar", "opentelemetry-javaagent.jar")
}

val copyExtension = tasks.register<Copy>("copyExtension") {
from(extension.files) {
include("opentelemetry-samplers-*.jar")
exclude("*sources.jar")
exclude("*javadoc.jar")
}
into(layout.buildDirectory.dir("agent"))
rename(".*\\.jar", "opentelemetry-javaagent-extension.jar")
}

tasks.named<BootJar>("bootJar") {
dependsOn(copyAgent)
dependsOn(copyExtension)

archiveFileName = "app.jar"
}
10 changes: 6 additions & 4 deletions javaagent/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ services:
build: ./
environment:
OTEL_SERVICE_NAME: "agent-example-app"
OTEL_EXPORTER_OTLP_ENDPOINT: "http://collector:4318"
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "http://collector:4318/v1/traces"
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: "http://collector:4318/v1/metrics"
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT: "http://collector:4318/v1/logs"
# Logs are disabled by default
OTEL_LOGS_EXPORTER: "otlp"
# Optional specify file configuration instead of using environment variable scheme
# To use, call "export OTEL_CONFIG_FILE=/sdk-config.yaml" before calling docker compose up
OTEL_CONFIG_FILE:
# To use, call "export OTEL_EXPERIMENTAL_CONFIG_FILE=/sdk-migration-config.yaml" before calling docker compose up
OTEL_EXPERIMENTAL_CONFIG_FILE: /sdk-migration-config.yaml
ports:
- "8080:8080"
volumes:
- ./sdk-config.yaml:/sdk-config.yaml
- ./sdk-migration-config.yaml:/sdk-migration-config.yaml
depends_on:
- collector
collector:
Expand Down
38 changes: 0 additions & 38 deletions javaagent/sdk-config.yaml

This file was deleted.

Loading
Loading