-
Notifications
You must be signed in to change notification settings - Fork 311
Add support for instrumenting the OpenAI Client #9284
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
Open
nayeem-kamal
wants to merge
7
commits into
master
Choose a base branch
from
nayeem-kamal/openai
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
378d442
added apm openai instrumentation
nayeem-kamal 42784e2
added chatcompletion llmobs spans
nayeem-kamal f620b66
address PR comments
nayeem-kamal 93a22fe
resolve settings merge conflict
nayeem-kamal ce288b1
import maps
gary-huang 0066c5d
addressed pr comments
nayeem-kamal 298f31f
address pr comments
nayeem-kamal 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
27 changes: 27 additions & 0 deletions
27
dd-java-agent/instrumentation/openai-java-2.8/build.gradle
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,27 @@ | ||
muzzle { | ||
pass { | ||
group = "com.openai" | ||
module = "openai-java" | ||
versions = "[2.8.0,)" | ||
assertInverse = true | ||
} | ||
} | ||
|
||
// Instrumentation is currently in preview. Additional testing will be implemented before enabling this by default. | ||
|
||
apply from: "$rootDir/gradle/java.gradle" | ||
|
||
addTestSuiteForDir('latestDepTest', 'test') | ||
|
||
dependencies { | ||
compileOnly(group: 'com.openai', name: 'openai-java', version: '2.8.0') | ||
|
||
testImplementation(group: 'com.openai', name: 'openai-java') { | ||
version { | ||
strictly '[2.8.0,)' | ||
} | ||
} | ||
|
||
latestDepTestImplementation group: 'com.openai', name: 'openai-java', version: '+' | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
dd-java-agent/instrumentation/openai-java-2.8/gradle.lockfile
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,4 @@ | ||
# This is a Gradle generated file for dependency locking. | ||
# This file is expected to be part of source control. | ||
# Manual entries are allowed. | ||
empty= |
106 changes: 106 additions & 0 deletions
106
...java/datadog/trace/instrumentation/openaiclient/ChatCompletionServiceInstrumentation.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,106 @@ | ||
package datadog.trace.instrumentation.openaiclient; | ||
|
||
import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.implementsInterface; | ||
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.*; | ||
import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
|
||
import com.openai.models.chat.completions.ChatCompletion; | ||
import com.openai.models.chat.completions.ChatCompletionCreateParams; | ||
import com.openai.services.blocking.CompletionService; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
import datadog.trace.agent.tooling.InstrumenterModule; | ||
import datadog.trace.api.InstrumenterConfig; | ||
import datadog.trace.api.llmobs.LLMObsSpan; | ||
import datadog.trace.bootstrap.InstrumentationContext; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentScope; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class ChatCompletionServiceInstrumentation extends InstrumenterModule.Tracing | ||
implements Instrumenter.ForTypeHierarchy, Instrumenter.HasMethodAdvice { | ||
public ChatCompletionServiceInstrumentation() { | ||
super("openai-java", "openai-java-2.8", "openai-client"); | ||
} | ||
|
||
@Override | ||
protected boolean defaultEnabled() { | ||
return super.defaultEnabled() | ||
&& InstrumenterConfig.get().isIntegrationEnabled(Collections.singleton("openai"), false); | ||
} | ||
|
||
@Override | ||
public String[] helperClassNames() { | ||
return new String[] { | ||
packageName + ".OpenAIClientInfo", | ||
}; | ||
} | ||
|
||
@Override | ||
public Map<String, String> contextStore() { | ||
Map<String, String> contextStores = new HashMap<>(1); | ||
contextStores.put( | ||
"com.openai.services.blocking.ChatCompletionService", OpenAIClientInfo.class.getName()); | ||
return contextStores; | ||
} | ||
|
||
@Override | ||
public void methodAdvice(MethodTransformer transformer) { | ||
transformer.applyAdvice( | ||
isMethod() | ||
.and(isPublic()) | ||
.and(named("create")) | ||
.and( | ||
takesArgument( | ||
0, named("com.openai.models.completions.ChatCompletionCreateParams"))), | ||
getClass().getName() + "$ChatCompletionServiceAdvice"); | ||
} | ||
|
||
@Override | ||
public String hierarchyMarkerType() { | ||
return "com.openai.services.blocking.chat.ChatCompletionService"; | ||
} | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> hierarchyMatcher() { | ||
return implementsInterface(named(hierarchyMarkerType())); | ||
} | ||
|
||
public static class ChatCompletionServiceAdvice { | ||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static Map<String, Object> methodEnter( | ||
@Advice.Argument(0) final ChatCompletionCreateParams params) { | ||
Map<String, Object> spans = new HashMap<>(); | ||
spans.put( | ||
"datadog.trace.api.llmobs.LLMObsSpan", | ||
OpenAIClientDecorator.DECORATE.startLLMObsChatCompletionSpan(params)); | ||
spans.put( | ||
"datadog.trace.bootstrap.instrumentation.api.AgentScope", | ||
OpenAIClientDecorator.DECORATE.startChatCompletionSpan(params)); | ||
return spans; | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void methodExit( | ||
@Advice.Enter final Map<String, Object> spans, | ||
@Advice.This final CompletionService completionService, | ||
@Advice.Return final Object result, | ||
@Advice.Thrown final Throwable throwable) { | ||
OpenAIClientInfo info = | ||
InstrumentationContext.get(CompletionService.class, OpenAIClientInfo.class) | ||
.get(completionService); | ||
OpenAIClientDecorator.DECORATE.finishLLMObsChatCompletionSpan( | ||
(LLMObsSpan) spans.get("datadog.trace.api.llmobs.LLMObsSpan"), | ||
(ChatCompletion) result, | ||
throwable); | ||
OpenAIClientDecorator.DECORATE.finishSpan( | ||
(AgentScope) spans.get("datadog.trace.bootstrap.instrumentation.api.AgentScope"), | ||
result, | ||
throwable); | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...ain/java/datadog/trace/instrumentation/openaiclient/CompletionServiceInstrumentation.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,88 @@ | ||
package datadog.trace.instrumentation.openaiclient; | ||
|
||
import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.implementsInterface; | ||
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.*; | ||
import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
|
||
import com.openai.models.completions.CompletionCreateParams; | ||
import com.openai.services.blocking.CompletionService; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
import datadog.trace.agent.tooling.InstrumenterModule; | ||
import datadog.trace.api.InstrumenterConfig; | ||
import datadog.trace.bootstrap.InstrumentationContext; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentScope; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class CompletionServiceInstrumentation extends InstrumenterModule.Tracing | ||
implements Instrumenter.ForTypeHierarchy, Instrumenter.HasMethodAdvice { | ||
public CompletionServiceInstrumentation() { | ||
super("openai-java", "openai-java-2.8", "openai-client"); | ||
} | ||
|
||
@Override | ||
protected boolean defaultEnabled() { | ||
return super.defaultEnabled() | ||
&& InstrumenterConfig.get().isIntegrationEnabled(Collections.singleton("openai"), false); | ||
} | ||
|
||
@Override | ||
public String[] helperClassNames() { | ||
return new String[] { | ||
packageName + ".OpenAIClientInfo", | ||
}; | ||
} | ||
|
||
@Override | ||
public Map<String, String> contextStore() { | ||
Map<String, String> contextStores = new HashMap<>(1); | ||
contextStores.put( | ||
"com.openai.services.blocking.CompletionService", OpenAIClientInfo.class.getName()); | ||
return contextStores; | ||
} | ||
|
||
@Override | ||
public void methodAdvice(MethodTransformer transformer) { | ||
transformer.applyAdvice( | ||
isMethod() | ||
.and(isPublic()) | ||
.and(named("create")) | ||
.and(takesArgument(0, named("com.openai.models.completions.CompletionCreateParams"))), | ||
getClass().getName() + "$CompletionServiceAdvice"); | ||
} | ||
|
||
@Override | ||
public String hierarchyMarkerType() { | ||
return "com.openai.services.blocking.CompletionService"; | ||
} | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> hierarchyMatcher() { | ||
return implementsInterface(named(hierarchyMarkerType())); | ||
} | ||
|
||
public static class CompletionServiceAdvice { | ||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static AgentScope methodEnter(@Advice.Argument(0) final CompletionCreateParams params) { | ||
|
||
return OpenAIClientDecorator.DECORATE.startCompletionSpan(params); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void methodExit( | ||
@Advice.Enter final AgentScope span, | ||
@Advice.This final CompletionService completionService, | ||
@Advice.Return final Object result, | ||
@Advice.Thrown final Throwable throwable) { | ||
OpenAIClientInfo info = | ||
InstrumentationContext.get(CompletionService.class, OpenAIClientInfo.class) | ||
.get(completionService); | ||
OpenAIClientDecorator.DECORATE.finishSpan(span, result, throwable); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...main/java/datadog/trace/instrumentation/openaiclient/EmbeddingServiceInstrumentation.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,79 @@ | ||
package datadog.trace.instrumentation.openaiclient; | ||
|
||
import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.implementsInterface; | ||
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.openai.models.embeddings.EmbeddingCreateParams; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
import datadog.trace.agent.tooling.InstrumenterModule; | ||
import datadog.trace.api.InstrumenterConfig; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentScope; | ||
import java.util.Collections; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
@AutoService(InstrumenterModule.class) | ||
public class EmbeddingServiceInstrumentation extends InstrumenterModule.Tracing | ||
implements Instrumenter.ForTypeHierarchy, Instrumenter.HasMethodAdvice { | ||
|
||
public EmbeddingServiceInstrumentation() { | ||
super("openai-java", "openai-java-2.8", "openai-client"); | ||
} | ||
|
||
@Override | ||
protected boolean defaultEnabled() { | ||
return super.defaultEnabled() | ||
&& InstrumenterConfig.get().isIntegrationEnabled(Collections.singleton("openai"), false); | ||
} | ||
|
||
@Override | ||
public String[] helperClassNames() { | ||
return new String[] { | ||
packageName + ".OpenAIDecorator", | ||
}; | ||
} | ||
|
||
@Override | ||
public void methodAdvice(MethodTransformer transformer) { | ||
// Instrument embedding creation methods | ||
transformer.applyAdvice( | ||
isMethod() | ||
.and(named("create")) | ||
.and(isPublic()) | ||
.and(takesArgument(0, named("com.openai.models.embeddings.EmbeddingCreateParams"))), | ||
EmbeddingServiceInstrumentation.class.getName() + "$EmbeddingAdvice"); | ||
} | ||
|
||
@Override | ||
public String hierarchyMarkerType() { | ||
return "com.openai.services.embedding.EmbeddingService"; | ||
} | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> hierarchyMatcher() { | ||
return implementsInterface(named(hierarchyMarkerType())); | ||
} | ||
|
||
public static class EmbeddingAdvice { | ||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static AgentScope onEnter( | ||
@Advice.Argument(0) final EmbeddingCreateParams embeddingParams) { | ||
|
||
return OpenAIClientDecorator.DECORATE.startEmbeddingSpan(embeddingParams); | ||
} | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
public static void onExit( | ||
@Advice.Enter final AgentScope span, | ||
@Advice.Return final Object result, | ||
@Advice.Thrown final Throwable throwable) { | ||
|
||
OpenAIClientDecorator.DECORATE.finishSpan(span, result, throwable); | ||
} | ||
} | ||
} |
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.
🎯 suggestion: If this arg is always
null
and unused, why keeping it in the first place?