Skip to content
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
60 changes: 60 additions & 0 deletions examples/baggage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Baggage examples

This is an elementary application providing an end-to-end example usage of [OpenTelemetry baggage](https://opentelemetry.io/docs/concepts/signals/baggage/).

It is split in two services: a gateway and a backend
- an HTTP backend service running on http://localhost:9000/backend/
- an HTTP gateway service running on http://localhost:8000/gateway/

The gateway is in charge of authentication and delegates to the backend by providing a technical ID for the backend to use for authentication.

## Build

```bash
./gradlew clean assemble
```

## Configure

The javaagent is already packaged and included in the application by using [runtime attach](https://www.elastic.co/docs/reference/opentelemetry/edot-sdks/java/setup/runtime-attach) feature.

OTel configuration must be provided through environment variables or JVM system properties.

## Run

Backend:
```shell
export OTEL_SERVICE_NAME='backend'
java \
-Dotel.java.experimental.span-attributes.copy-from-baggage.include=example.customer.id,example.customer.name \
-Dotel.java.experimental.log-attributes.copy-from-baggage.include=example.customer.id,example.customer.name \
-jar ./build/libs/baggage-example-all.jar backend
```

Gateway:
```shell
export OTEL_SERVICE_NAME='gateway'
java \
-Dotel.java.experimental.span-attributes.copy-from-baggage.include=example.customer.id,example.customer.name \
-Dotel.java.experimental.log-attributes.copy-from-baggage.include=example.customer.id,example.customer.name \
-jar ./build/libs/baggage-example-all.jar gateway
```

Once both services are started, you can execute queries on the gateway with queries like the following:
```shell
curl -H 'Authorization: secret=12345' http://localhost:8000/gateway/
```

This will create the following log messages on `gateway` and `backend` respectively:
```
gateway request for customer ID = 12345
backend request for customer ID = 12345
```

All the spans and logs captured within the scope of the baggage will have the following attributes:
- `example.customer.name` with value `my-awesome-customer-12345`
- `example.customer.id` with value `12345`

As a result, it is now possible to use those custom attributes to filter data and create
dedicated dashboards. The only code modification required is in the gateway code.

44 changes: 44 additions & 0 deletions examples/baggage/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
plugins {
application
java

id("com.gradleup.shadow") version "8.3.5"
}

group = "baggage.example"
version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
}

dependencies {
implementation("co.elastic.otel:elastic-otel-runtime-attach:1.4.1")
implementation(platform("org.slf4j:slf4j-bom:2.0.16"))

// using a "real" logger backend as slf4j-simple just uses stdout/stderr
// and is not instrumented for log capture by otel instrumentation
implementation("org.apache.logging.log4j:log4j-slf4j2-impl:2.25.0")

// otel API to access Baggage API
implementation("io.opentelemetry:opentelemetry-api:1.51.0")

}

application {
mainClass = "baggage.example.Main"
}

tasks {
compileJava {
options.release.set(21)
}

shadowJar {
archiveFileName.set("baggage-example-all.jar")
}

assemble {
dependsOn("shadowJar")
}
}
Binary file not shown.
8 changes: 8 additions & 0 deletions examples/baggage/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionSha256Sum=20f1b1176237254a6fc204d8434196fa11a4cfb387567519c61556e8710aed78
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
249 changes: 249 additions & 0 deletions examples/baggage/gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading