-
Notifications
You must be signed in to change notification settings - Fork 979
Add initial instrumentation of OpenAI client #14221
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
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
3cbd01c
Add initial instrumentation of OpenAI client
anuraaga f69d5a2
./gradlew spotlessApply
otelbot[bot] e4a8f66
testing-common
anuraaga 4610cb5
Merge branch 'openai-instrumentation' of github.com:anuraaga/opentele…
anuraaga 39e8eff
Shade wiremock
anuraaga 8b65535
apache
anuraaga 811985c
wrong package
anuraaga 41d944a
Actually migrate aws-sdk recording extension
anuraaga c357fe2
Move suppression
anuraaga c1b346a
Relocate servlet
anuraaga 4b44332
FOOOOSA
anuraaga 1a238e0
Relocate yaml
anuraaga 6ab669b
Guava
anuraaga 9912b77
asm
anuraaga b62cabf
jsonpath
anuraaga a787c10
apache
anuraaga 2fa61f5
Don't invert for now
anuraaga bcd60a9
Cleanup
anuraaga 21673fd
Cleanup
anuraaga fd4734e
More cleanup
anuraaga a86427e
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
anuraaga 193461c
Add README
anuraaga 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
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
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,5 @@ | ||
# Settings for the OpenAI instrumentation | ||
|
||
| System property | Type | Default | Description | | ||
|------------------------------------------------------|---------|---------|------------------------------------------| | ||
| `otel.instrumentation.genai.capture-message-content` | Boolean | `false` | record content of user and LLM messages. | |
30 changes: 30 additions & 0 deletions
30
instrumentation/openai/openai-java-1.1/javaagent/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,30 @@ | ||
plugins { | ||
id("otel.javaagent-instrumentation") | ||
} | ||
|
||
muzzle { | ||
pass { | ||
group.set("com.openai") | ||
module.set("openai-java") | ||
versions.set("[1.1.0,)") | ||
// TODO: assertInverse after completing instrumentation | ||
laurit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
dependencies { | ||
implementation(project(":instrumentation:openai:openai-java-1.1:library")) | ||
|
||
testInstrumentation(project(":instrumentation:okhttp:okhttp-3.0:javaagent")) | ||
|
||
library("com.openai:openai-java:1.1.0") | ||
|
||
testImplementation(project(":instrumentation:openai:openai-java-1.1:testing")) | ||
} | ||
|
||
tasks { | ||
withType<Test>().configureEach { | ||
systemProperty("testLatestDeps", findProperty("testLatestDeps") as Boolean) | ||
// TODO run tests both with and without genai message capture | ||
systemProperty("otel.instrumentation.genai.capture-message-content", "true") | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...a/io/opentelemetry/javaagent/instrumentation/openai/v1_1/OpenAiClientInstrumentation.java
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,40 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.openai.v1_1; | ||
|
||
import static io.opentelemetry.javaagent.instrumentation.openai.v1_1.OpenAiSingletons.TELEMETRY; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.returns; | ||
|
||
import com.openai.client.OpenAIClient; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class OpenAiClientInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("com.openai.client.okhttp.OpenAIOkHttpClient$Builder"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
named("build").and(returns(named("com.openai.client.OpenAIClient"))), | ||
OpenAiClientInstrumentation.class.getName() + "$BuildAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class BuildAdvice { | ||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
@Advice.AssignReturned.ToReturned | ||
public static OpenAIClient onExit(@Advice.Return OpenAIClient client) { | ||
return TELEMETRY.wrap(client); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...a/io/opentelemetry/javaagent/instrumentation/openai/v1_1/OpenAiInstrumentationModule.java
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,32 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.openai.v1_1; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
import static java.util.Collections.singletonList; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import java.util.List; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class OpenAiInstrumentationModule extends InstrumentationModule { | ||
public OpenAiInstrumentationModule() { | ||
super("openai-java", "openai-java-1.1", "openai"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() { | ||
return hasClassesNamed("com.openai.client.OpenAIClient"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return singletonList(new OpenAiClientInstrumentation()); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...rc/main/java/io/opentelemetry/javaagent/instrumentation/openai/v1_1/OpenAiSingletons.java
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,21 @@ | ||||
/* | ||||
* Copyright The OpenTelemetry Authors | ||||
* SPDX-License-Identifier: Apache-2.0 | ||||
*/ | ||||
|
||||
package io.opentelemetry.javaagent.instrumentation.openai.v1_1; | ||||
|
||||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||||
import io.opentelemetry.instrumentation.openai.v1_1.OpenAITelemetry; | ||||
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig; | ||||
|
||||
public final class OpenAiSingletons { | ||||
public static final OpenAITelemetry TELEMETRY = | ||||
OpenAITelemetry.builder(GlobalOpenTelemetry.get()) | ||||
.setCaptureMessageContent( | ||||
AgentInstrumentationConfig.get() | ||||
.getBoolean("otel.instrumentation.genai.capture-message-content", false)) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should document this similarly to
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, added a readme |
||||
.build(); | ||||
|
||||
private OpenAiSingletons() {} | ||||
} |
43 changes: 43 additions & 0 deletions
43
...aagent/src/test/java/io/opentelemetry/javaagent/instrumentation/openai/v1_1/ChatTest.java
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,43 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.openai.v1_1; | ||
|
||
import com.openai.client.OpenAIClient; | ||
import io.opentelemetry.instrumentation.openai.v1_1.AbstractChatTest; | ||
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import io.opentelemetry.sdk.testing.assertj.SpanDataAssert; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
class ChatTest extends AbstractChatTest { | ||
|
||
@RegisterExtension | ||
private static final AgentInstrumentationExtension testing = | ||
AgentInstrumentationExtension.create(); | ||
|
||
@Override | ||
protected InstrumentationExtension getTesting() { | ||
return testing; | ||
} | ||
|
||
@Override | ||
protected OpenAIClient wrap(OpenAIClient client) { | ||
return client; | ||
} | ||
|
||
@Override | ||
protected final List<Consumer<SpanDataAssert>> maybeWithTransportSpan( | ||
Consumer<SpanDataAssert> span) { | ||
List<Consumer<SpanDataAssert>> result = new ArrayList<>(); | ||
result.add(span); | ||
// Do a very simple assertion since the telemetry is not part of this library. | ||
result.add(s -> s.hasName("POST")); | ||
return result; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
instrumentation/openai/openai-java-1.1/library/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,16 @@ | ||
plugins { | ||
id("otel.library-instrumentation") | ||
id("otel.nullaway-conventions") | ||
} | ||
|
||
dependencies { | ||
library("com.openai:openai-java:1.1.0") | ||
|
||
testImplementation(project(":instrumentation:openai:openai-java-1.1:testing")) | ||
} | ||
|
||
tasks { | ||
withType<Test>().configureEach { | ||
systemProperty("testLatestDeps", findProperty("testLatestDeps") as Boolean) | ||
} | ||
} |
154 changes: 154 additions & 0 deletions
154
...rary/src/main/java/io/opentelemetry/instrumentation/openai/v1_1/ChatAttributesGetter.java
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,154 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.openai.v1_1; | ||
|
||
import static java.util.Collections.emptyList; | ||
import static java.util.Collections.singletonList; | ||
|
||
import com.openai.models.chat.completions.ChatCompletion; | ||
import com.openai.models.chat.completions.ChatCompletionCreateParams; | ||
import com.openai.models.completions.CompletionUsage; | ||
import io.opentelemetry.instrumentation.api.incubator.semconv.genai.GenAiAttributesGetter; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
enum ChatAttributesGetter | ||
implements GenAiAttributesGetter<ChatCompletionCreateParams, ChatCompletion> { | ||
INSTANCE; | ||
|
||
@Override | ||
public String getOperationName(ChatCompletionCreateParams request) { | ||
return GenAiAttributes.GenAiOperationNameIncubatingValues.CHAT; | ||
} | ||
|
||
@Override | ||
public String getSystem(ChatCompletionCreateParams request) { | ||
return GenAiAttributes.GenAiSystemIncubatingValues.OPENAI; | ||
} | ||
|
||
@Override | ||
public String getRequestModel(ChatCompletionCreateParams request) { | ||
return request.model().asString(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Long getRequestSeed(ChatCompletionCreateParams request) { | ||
return request.seed().orElse(null); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public List<String> getRequestEncodingFormats(ChatCompletionCreateParams request) { | ||
return null; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Double getRequestFrequencyPenalty(ChatCompletionCreateParams request) { | ||
return request.frequencyPenalty().orElse(null); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Long getRequestMaxTokens(ChatCompletionCreateParams request) { | ||
return request.maxCompletionTokens().orElse(null); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Double getRequestPresencePenalty(ChatCompletionCreateParams request) { | ||
return request.presencePenalty().orElse(null); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public List<String> getRequestStopSequences(ChatCompletionCreateParams request) { | ||
return request | ||
.stop() | ||
.map( | ||
s -> { | ||
if (s.isString()) { | ||
return singletonList(s.asString()); | ||
} | ||
if (s.isStrings()) { | ||
return s.asStrings(); | ||
} | ||
return null; | ||
}) | ||
.orElse(null); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Double getRequestTemperature(ChatCompletionCreateParams request) { | ||
return request.temperature().orElse(null); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Double getRequestTopK(ChatCompletionCreateParams request) { | ||
return null; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Double getRequestTopP(ChatCompletionCreateParams request) { | ||
return request.topP().orElse(null); | ||
} | ||
|
||
@Override | ||
public List<String> getResponseFinishReasons( | ||
ChatCompletionCreateParams request, @Nullable ChatCompletion response) { | ||
if (response == null) { | ||
return emptyList(); | ||
} | ||
return response.choices().stream() | ||
.map(choice -> choice.finishReason().asString()) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public String getResponseId( | ||
ChatCompletionCreateParams request, @Nullable ChatCompletion response) { | ||
if (response == null) { | ||
return null; | ||
} | ||
return response.id(); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public String getResponseModel( | ||
ChatCompletionCreateParams request, @Nullable ChatCompletion response) { | ||
if (response == null) { | ||
return null; | ||
} | ||
return response.model(); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public Long getUsageInputTokens( | ||
ChatCompletionCreateParams request, @Nullable ChatCompletion response) { | ||
if (response == null) { | ||
return null; | ||
} | ||
return response.usage().map(CompletionUsage::promptTokens).orElse(null); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public Long getUsageOutputTokens( | ||
ChatCompletionCreateParams request, @Nullable ChatCompletion response) { | ||
if (response == null) { | ||
return null; | ||
} | ||
return response.usage().map(CompletionUsage::completionTokens).orElse(null); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IntelliJ was having trouble syncing the project without this. It seemed harmless so hopefully it's ok.