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

Extensions example #3071

Merged
merged 5 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
75 changes: 75 additions & 0 deletions examples/extension/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
## Introduction

This repository demonstrate how to create an extension archive to use with `otel.javaagent.experimental.extensions`
iNikem marked this conversation as resolved.
Show resolved Hide resolved
configuration option of the OpenTelemetry Java instrumentation agent.

For every extension point provided by OpenTelemetry Java instrumentation, this repository contains an example of
its usage.

Please carefully read both the source code and Gradle build script file `build.gradle`.
They contain a lot of documentation and comments explaining the purpose of all major pieces.

## How to use extension archive

When you build this project by running `./gradlew build` you will get a jar file in
`build/libs/opentelemetry-java-instrumentation-extension-demo-1.0-all.jar`.
Copy this jar file to a machine running the application that you are monitoring with
OpenTelemetry Java instrumentation agent.

Assuming that your command line looks similar to this:
```
java -javaagent:path/to/opentelemetry-javaagent-all.jar \
-jar myapp.jar
```
change it to this:
```
java -javaagent:path/to/opentelemetry-javaagent-all.jar \
-Dotel.javaagent.experimental.extensions=path/to/extension.jar
-jar myapp.jar
```
specifying the full path and the correct name of your extensions jar.

## Extensions examples

* [DemoIdGenerator](src/main/java/com/example/javaagent/DemoIdGenerator.java) - custom `IdGenerator`
* [DemoPropagator](src/main/java/com/example/javaagent/DemoPropagator.java) - custom `TextMapPropagator`
* [DemoPropertySource](src/main/java/com/example/javaagent/DemoPropertySource.java) - default configuration
* [DemoSampler](src/main/java/com/example/javaagent/DemoSampler.java) - custom `Sampler`
* [DemoSpanProcessor](src/main/java/com/example/javaagent/DemoSpanProcessor.java) - custom `SpanProcessor`
* [DemoSpanExporter](src/main/java/com/example/javaagent/DemoSpanExporter.java) - custom `SpanExporter`
* [DemoServlet3InstrumentationModule](src/main/java/com/example/javaagent/instrumentation/DemoServlet3InstrumentationModule.java) - additional instrumentation

## Instrumentation customisation

There are several options to override or customise instrumentation provided by the upstream agent.
The following description follows one specific use-case:

> Instrumentation X from Otel distribution creates span that I don't like and I want to change it in my vendor distro.
iNikem marked this conversation as resolved.
Show resolved Hide resolved

As an example, let us take some database client instrumentation that creates a span for database call
and extracts data from db connection to provide attributes for that span.

### I don't want this span at all
The easiest case. You can just pre-configure your distribution and disable given instrumentation.
iNikem marked this conversation as resolved.
Show resolved Hide resolved

### I want to add/modify some attributes and their values does NOT depend on a specific db connection instance.
E.g. you want to add some data from call stack as span attribute.
In this case just provide your custom `SpanProcessor`.
No need for touching instrumentation itself.

### I want to add/modify some attributes and their values depend on a specific db connection instance.
Write a _new_ instrumentation which injects its own advice into the same method as the original one.
Use `getOrder` method to ensure it is run after the original instrumentation.
iNikem marked this conversation as resolved.
Show resolved Hide resolved
Now you can augment current span with new information.

See [DemoServlet3Instrumentation](instrumentation/servlet-3/src/main/java/com/example/javaagent/instrumentation/DemoServlet3Instrumentation.java).
iNikem marked this conversation as resolved.
Show resolved Hide resolved

### I want to remove some attributes
Write custom exporter or use attribute filtering functionality in Collector.

### I don't like Otel span at all. I want to significantly modify it and its lifecycle
Disable existing instrumentation.
Write a new one, which injects `Advice` into the same (or better) method as the original instrumentation.
Write your own `Advice` for this.
Use existing `Tracer` directly or extend it.
As you have your own `Advice`, you can control which `Tracer` you use.
Comment on lines +74 to +75
Copy link
Member

Choose a reason for hiding this comment

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

migrate ref to Tracer now or later?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To Instrumenter? I vote for later.

149 changes: 106 additions & 43 deletions examples/extension/build.gradle
Original file line number Diff line number Diff line change
@@ -1,52 +1,115 @@
plugins {
id "java"

/*
Instrumentation agent extension mechanism expects a single jar containing everything required
for your extension. This also includes any external libraries that your extension uses and
cannot access from application classpath (see comment below about `javax.servlet-api` dependency).

Thus we use Shadow Gradle plugin to package our classes and all required runtime dependencies
into a single fat jar.
iNikem marked this conversation as resolved.
Show resolved Hide resolved
See https://imperceptiblethoughts.com/shadow/ for more details about Shadow plugin.
*/
id "com.github.johnrengelman.shadow" version "6.1.0"
}

group 'io.opentelemetry.example'
version '1.0-SNAPSHOT'

subprojects {
version = rootProject.version

apply plugin: "java"

ext {
versions = [
opentelemetry : "1.2.0",
opentelemetryJavaagent: "1.3.0-SNAPSHOT",
bytebuddy : "1.10.18",
guava : "30.1-jre"
]
versions.opentelemetryAlpha = "${versions.opentelemetry}-alpha"
versions.opentelemetryJavaagentAlpha = "1.3.0-alpha-SNAPSHOT"

deps = [
bytebuddy : dependencies.create(group: 'net.bytebuddy', name: 'byte-buddy', version: versions.bytebuddy),
bytebuddyagent : dependencies.create(group: 'net.bytebuddy', name: 'byte-buddy-agent', version: versions.bytebuddy),
autoservice : [
dependencies.create(group: 'com.google.auto.service', name: 'auto-service', version: '1.0-rc7'),
dependencies.create(group: 'com.google.auto', name: 'auto-common', version: '0.8'),
dependencies.create(group: 'com.google.guava', name: 'guava', version: "${versions.guava}"),
],
autoValueAnnotations: "com.google.auto.value:auto-value-annotations:${versions.autoValue}",
]
}
version '1.0'

repositories {
// needed because relying on locally built SNAPSHOT versions above for now
mavenLocal()
mavenCentral()
}
ext {
versions = [
opentelemetry : "1.2.0",
opentelemetryAlpha : "1.2.0-alpha",
opentelemetryJavaagent : "1.3.0-SNAPSHOT",
opentelemetryJavaagentAlpha: "1.3.0-alpha-SNAPSHOT",
]

dependencies {
testImplementation("org.mockito:mockito-core:3.3.3")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.2")
deps = [
autoservice: dependencies.create(group: 'com.google.auto.service', name: 'auto-service', version: '1.0')
]
}

repositories {
mavenCentral()
maven {
url = uri("https://oss.sonatype.org/content/repositories/snapshots")
}
}

tasks {
test {
useJUnitPlatform()
}
configurations {
/*
We create a separate gradle configuration to grab a published Otel instrumentation agent.
We don't need the agent during development of this extension module.
This agent is used only during integration test.
*/
otel
}

dependencies {
/*
Interfaces and SPIs that we implement. We use `compileOnly` dependency because during
runtime all neccessary classes are provided by javaagent itself.
*/
compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure:${versions.opentelemetryAlpha}")
compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-api:${versions.opentelemetryJavaagentAlpha}")
compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api:${versions.opentelemetryJavaagentAlpha}")

//Provides @AutoService annotation that makes registration of our SPI implementations much easier
compileOnly deps.autoservice
annotationProcessor deps.autoservice

compileJava {
options.release.set(11)
/*
Used by our instrumentation module to references classes of the target instrumented library.
iNikem marked this conversation as resolved.
Show resolved Hide resolved
We again use `compileOnly` here because during runtime these classes are provided by the
actual application that we instrument.

NB! Only Advice classes of instrumentation modules can access classes from application classpath.
iNikem marked this conversation as resolved.
Show resolved Hide resolved
See https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/contributing/writing-instrumentation-module.md#advice-classes
*/
compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'

/*
This dependency is required DemoSpanProcessor both during compile and runtime.
iNikem marked this conversation as resolved.
Show resolved Hide resolved
Only dependencies added to `implementation` configuration will be picked up by Shadow plugin
and added to the resulting fat jar for our extension's distribution.
iNikem marked this conversation as resolved.
Show resolved Hide resolved
*/
implementation 'org.apache.commons:commons-lang3:3.11'

//All dependencies below are only for tests
testImplementation("org.testcontainers:testcontainers:1.15.2")
testImplementation("com.fasterxml.jackson.core:jackson-databind:2.11.2")
testImplementation("com.google.protobuf:protobuf-java-util:3.12.4")
testImplementation("com.squareup.okhttp3:okhttp:3.12.12")
testImplementation("io.opentelemetry:opentelemetry-api:${versions.opentelemetry}")
testImplementation("io.opentelemetry:opentelemetry-proto:${versions.opentelemetryAlpha}")

testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.2")
testRuntimeOnly("ch.qos.logback:logback-classic:1.2.3")

//Otel Java instrumentation that we use and extend during integration tests
otel("io.opentelemetry.javaagent:opentelemetry-javaagent:${versions.opentelemetryJavaagent}:all")
}

tasks {
test {
useJUnitPlatform()

def extensionJar = tasks.shadowJar
inputs.files(layout.files(extensionJar))

doFirst {
//To run our application with javaagent published by OpenTelemetry Java instrumentation project
iNikem marked this conversation as resolved.
Show resolved Hide resolved
jvmArgs("-Dio.opentelemetry.smoketest.agentPath=${configurations.getByName("otel").resolve().find().absolutePath}")
//Instructs our integration test where to find our extension archive
jvmArgs("-Dio.opentelemetry.smoketest.extensionPath=${extensionJar.archiveFile.get()}")
}
}

compileJava {
options.release.set(11)
}

assemble.dependsOn(shadowJar)
}

19 changes: 0 additions & 19 deletions examples/extension/custom/build.gradle

This file was deleted.

4 changes: 0 additions & 4 deletions examples/extension/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
rootProject.name = 'opentelemetry-java-instrumentation-extension-demo'

include "custom"
include "smoke-tests"

32 changes: 0 additions & 32 deletions examples/extension/smoke-tests/build.gradle

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;
import org.apache.commons.lang3.RandomStringUtils;

/**
* See <a href="https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/sdk.md#span-processor">
Expand All @@ -13,8 +14,14 @@
* @see DemoSdkTracerProviderConfigurer
*/
public class DemoSpanProcessor implements SpanProcessor {

@Override
public void onStart(Context parentContext, ReadWriteSpan span) {
/*
The sole purpose of this attribute is to introduce runtime dependency on some external library.
We need this to demonstrate how extension can use them.
*/
span.setAttribute("random", RandomStringUtils.random(10));
span.setAttribute("custom", "demo");
}

Expand Down
Loading