-
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
base: master
Are you sure you want to change the base?
Changes from 5 commits
378d442
42784e2
f620b66
93a22fe
ce288b1
0066c5d
298f31f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: '+' | ||
|
||
} |
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= | ||
AlexeyKuznetsov-DD marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,105 @@ | ||||||
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-client", "openai-java", "openai-2.8"); | ||||||
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. 🔨 issue: Conventions
Suggested change
|
||||||
} | ||||||
|
||||||
@Override | ||||||
protected boolean defaultEnabled() { | ||||||
return InstrumenterConfig.get().isIntegrationEnabled(Collections.singleton("openai"), 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. 🔨 issue: This should not override tracing instrumentation deactivation. |
||||||
} | ||||||
|
||||||
@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); | ||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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-client", "openai-java", "openai-2.8"); | ||
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. 🔨 issue: Same |
||
} | ||
|
||
@Override | ||
protected boolean defaultEnabled() { | ||
return InstrumenterConfig.get().isIntegrationEnabled(Collections.singleton("openai"), 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. 🔍 nitpick: Same |
||
} | ||
|
||
@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); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
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", "openai-java", "openai-2.8"); | ||
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. 🔨 issue: Conventions |
||
} | ||
|
||
@Override | ||
protected boolean defaultEnabled() { | ||
return InstrumenterConfig.get().isIntegrationEnabled(Collections.singleton("openai"), 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. 🔨 issue: Same |
||
} | ||
|
||
@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); | ||
} | ||
} | ||
} |
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?