-
Notifications
You must be signed in to change notification settings - Fork 23
Add experimental instrumentation for OpenAI client #497
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
Merged
JonasKunz
merged 19 commits into
elastic:main
from
JonasKunz:openai-client-instrumentation
Jan 20, 2025
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
cf6e91c
Add muzzle, upgrade shadow plugin
JonasKunz f752d6c
Transformed instrumentation gradle file to gradle-kotlin convention
JonasKunz c678ac1
Add openai client instrumentation
JonasKunz 56d8fd6
Converted to Java 8
JonasKunz 7d53da1
Reworked request recording
JonasKunz d0ca87e
Make instrumentation off-by-default
JonasKunz 60fbebe
Add instrumentation to distro build
JonasKunz 9b0ac1d
Added documentation
JonasKunz 17fbb52
spotless
JonasKunz 80e39f9
remove outgenerated kotlin settings
JonasKunz 618f5ac
Fix gradle buildtime httpcomponents conflict introduced by muzzle
JonasKunz 9206ea5
Cenbtralize version management
JonasKunz 8edd0cb
Review fixes
JonasKunz 086ae1e
spotless
JonasKunz b9f270f
Remove remaining Nonnull usages
JonasKunz 7fce2e2
Added changelog
JonasKunz 572fd04
Enable instrumentation by default
JonasKunz bc65340
Remove unnecessary test flag
JonasKunz 2f5d64c
Update CHANGELOG.next-release.md
JonasKunz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| * add dynamically disabled instrumentation capability - #422 | ||
| * add disable all instrumentations option - #471 | ||
| * add stop-sending option - #474 | ||
| * Add OpenAI client instrumentation - #497 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
buildSrc/src/main/kotlin/elastic-otel.instrumentation-conventions.gradle.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar | ||
|
|
||
| plugins { | ||
| `java-library` | ||
| id("elastic-otel.java-conventions") | ||
| id("io.opentelemetry.instrumentation.muzzle-generation") | ||
| id("io.opentelemetry.instrumentation.muzzle-check") | ||
| } | ||
|
|
||
| // Other instrumentations to include for testing | ||
| val testInstrumentation: Configuration by configurations.creating { | ||
| isCanBeConsumed = false | ||
| } | ||
| val agentForTesting: Configuration by configurations.creating { | ||
| isCanBeConsumed = false | ||
| } | ||
|
|
||
| //https://github.com/gradle/gradle/issues/15383 | ||
| val catalog = extensions.getByType<VersionCatalogsExtension>().named("catalog") | ||
| dependencies { | ||
| agentForTesting(platform(catalog.findLibrary("opentelemetryInstrumentationAlphaBom").get())) | ||
| agentForTesting("io.opentelemetry.javaagent:opentelemetry-agent-for-testing") | ||
|
|
||
| compileOnly("io.opentelemetry:opentelemetry-sdk") | ||
| compileOnly("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api") | ||
| compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api") | ||
|
|
||
| testImplementation("io.opentelemetry.javaagent:opentelemetry-testing-common") | ||
| testImplementation("io.opentelemetry:opentelemetry-sdk-testing") | ||
|
|
||
| val agentVersion = catalog.findVersion("opentelemetryJavaagentAlpha").get() | ||
| add("codegen", "io.opentelemetry.javaagent:opentelemetry-javaagent-tooling:${agentVersion}") | ||
| add("muzzleBootstrap", "io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations-support:${agentVersion}") | ||
| add("muzzleTooling", "io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api:${agentVersion}") | ||
| add("muzzleTooling", "io.opentelemetry.javaagent:opentelemetry-javaagent-tooling:${agentVersion}") | ||
| } | ||
|
|
||
| fun relocatePackages( shadowJar : ShadowJar) { | ||
| // rewrite dependencies calling Logger.getLogger | ||
| shadowJar.relocate("java.util.logging.Logger", "io.opentelemetry.javaagent.bootstrap.PatchLogger") | ||
|
|
||
| // prevents conflict with library instrumentation, since these classes live in the bootstrap class loader | ||
| shadowJar.relocate("io.opentelemetry.instrumentation", "io.opentelemetry.javaagent.shaded.instrumentation") { | ||
| // Exclude resource providers since they live in the agent class loader | ||
| exclude("io.opentelemetry.instrumentation.resources.*") | ||
| exclude("io.opentelemetry.instrumentation.spring.resources.*") | ||
| } | ||
|
|
||
| // relocate(OpenTelemetry API) since these classes live in the bootstrap class loader | ||
| shadowJar.relocate("io.opentelemetry.api", "io.opentelemetry.javaagent.shaded.io.opentelemetry.api") | ||
| shadowJar.relocate("io.opentelemetry.semconv", "io.opentelemetry.javaagent.shaded.io.opentelemetry.semconv") | ||
| shadowJar.relocate("io.opentelemetry.context", "io.opentelemetry.javaagent.shaded.io.opentelemetry.context") | ||
| shadowJar.relocate("io.opentelemetry.extension.incubator", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.incubator") | ||
|
|
||
| // relocate the OpenTelemetry extensions that are used by instrumentation modules | ||
| // these extensions live in the AgentClassLoader, and are injected into the user's class loader | ||
| // by the instrumentation modules that use them | ||
| shadowJar.relocate("io.opentelemetry.extension.aws", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.aws") | ||
| shadowJar.relocate("io.opentelemetry.extension.kotlin", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.kotlin") | ||
| } | ||
|
|
||
| tasks { | ||
| shadowJar { | ||
| configurations = listOf(project.configurations.runtimeClasspath.get(), testInstrumentation) | ||
| mergeServiceFiles() | ||
|
|
||
| archiveFileName.set("agent-testing.jar") | ||
| relocatePackages(this) | ||
| } | ||
| } | ||
|
|
||
| tasks.withType<Test>().configureEach { | ||
| dependsOn(tasks.shadowJar, agentForTesting) | ||
|
|
||
| jvmArgs( | ||
| "-Dotel.javaagent.debug=true", | ||
| "-javaagent:${agentForTesting.files.first().absolutePath}", | ||
| // loads the given just jar, but in contrast to external extensions doesn't perform runtime shading | ||
| // instead the instrumentations are expected to be correctly shaded already in the jar | ||
| // Also the classes end up in the agent classloader instead of the extension loader | ||
| "-Dotel.javaagent.experimental.initializer.jar=${tasks.shadowJar.get().archiveFile.get().asFile.absolutePath}", | ||
JonasKunz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "-Dotel.javaagent.testing.additional-library-ignores.enabled=false", | ||
| "-Dotel.javaagent.testing.fail-on-context-leak=true", | ||
| "-Dotel.javaagent.testing.transform-safe-logging.enabled=true", | ||
| "-Dotel.metrics.exporter=otlp" | ||
| ) | ||
|
|
||
| // The sources are packaged into the testing jar so we need to make sure to exclude from the test | ||
| // classpath, which automatically inherits them, to ensure our shaded versions are used. | ||
| classpath = classpath.filter { | ||
| return@filter !(it == file("${layout.buildDirectory.get()}/resources/main") || it == file("${layout.buildDirectory.get()}/classes/java/main")) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
instrumentation/openai-client-instrumentation/build.gradle.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| plugins { | ||
| id("elastic-otel.instrumentation-conventions") | ||
| } | ||
|
|
||
| dependencies { | ||
| compileOnly(catalog.openaiClient) | ||
| testImplementation(catalog.openaiClient) | ||
|
|
||
| testImplementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.18.2") | ||
| testImplementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.18.2") | ||
| testImplementation("org.slf4j:slf4j-simple:2.0.16") | ||
| testImplementation(catalog.wiremock) | ||
| } | ||
|
|
||
| muzzle { | ||
| // TODO: setup muzzle to check older versions of openAI client | ||
SylvainJuge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // See the docs on how to do it: | ||
| // https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/contributing/muzzle.md | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.