-
Notifications
You must be signed in to change notification settings - Fork 986
Instrument embeddings in openai client #14353
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 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
085fa4d
Instrument openai async client
anuraaga 4b78428
Cleanup
anuraaga df19b40
Rename context
anuraaga a048b3b
Merge branch 'main' into openai-async
anuraaga caaf1b5
Instrument openai embeddings
anuraaga 87820df
Fix context
anuraaga bfa7202
Merge branch 'openai-async' of github.com:anuraaga/opentelemetry-java…
anuraaga 6406945
WIP
anuraaga 970fb2e
Merge branch 'openai-async' into openai-embeddings
anuraaga 78bee52
Finish
anuraaga 57418b4
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
anuraaga cd8911f
Fix merge
anuraaga 2a482b2
Cleanup
anuraaga 2f5ad29
Fix
anuraaga 8f4b1a4
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
anuraaga 57c111e
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
anuraaga 9db733b
Cleanup
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
49 changes: 49 additions & 0 deletions
49
.../src/test/java/io/opentelemetry/javaagent/instrumentation/openai/v1_1/EmbeddingsTest.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,49 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.openai.v1_1; | ||
|
||
import com.openai.client.OpenAIClient; | ||
import com.openai.client.OpenAIClientAsync; | ||
import io.opentelemetry.instrumentation.openai.v1_1.AbstractEmbeddingsTest; | ||
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 EmbeddingsTest extends AbstractEmbeddingsTest { | ||
|
||
@RegisterExtension | ||
private static final AgentInstrumentationExtension testing = | ||
AgentInstrumentationExtension.create(); | ||
|
||
@Override | ||
protected InstrumentationExtension getTesting() { | ||
return testing; | ||
} | ||
|
||
@Override | ||
protected OpenAIClient wrap(OpenAIClient client) { | ||
return client; | ||
} | ||
|
||
@Override | ||
protected OpenAIClientAsync wrap(OpenAIClientAsync 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; | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
...src/main/java/io/opentelemetry/instrumentation/openai/v1_1/EmbeddingAttributesGetter.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,129 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.openai.v1_1; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
import com.openai.models.embeddings.CreateEmbeddingResponse; | ||
import com.openai.models.embeddings.EmbeddingCreateParams; | ||
import io.opentelemetry.instrumentation.api.incubator.semconv.genai.GenAiAttributesGetter; | ||
import java.util.List; | ||
import javax.annotation.Nullable; | ||
|
||
enum EmbeddingAttributesGetter | ||
implements GenAiAttributesGetter<EmbeddingCreateParams, CreateEmbeddingResponse> { | ||
INSTANCE; | ||
|
||
@Override | ||
public String getOperationName(EmbeddingCreateParams request) { | ||
return GenAiAttributes.GenAiOperationNameIncubatingValues.EMBEDDINGS; | ||
} | ||
|
||
@Override | ||
public String getSystem(EmbeddingCreateParams request) { | ||
return GenAiAttributes.GenAiSystemIncubatingValues.OPENAI; | ||
} | ||
|
||
@Override | ||
public String getRequestModel(EmbeddingCreateParams request) { | ||
return request.model().asString(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Long getRequestSeed(EmbeddingCreateParams request) { | ||
return null; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public List<String> getRequestEncodingFormats(EmbeddingCreateParams request) { | ||
return request.encodingFormat().map(f -> singletonList(f.asString())).orElse(null); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Double getRequestFrequencyPenalty(EmbeddingCreateParams request) { | ||
return null; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Long getRequestMaxTokens(EmbeddingCreateParams request) { | ||
return null; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Double getRequestPresencePenalty(EmbeddingCreateParams request) { | ||
return null; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public List<String> getRequestStopSequences(EmbeddingCreateParams request) { | ||
return null; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Double getRequestTemperature(EmbeddingCreateParams request) { | ||
return null; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Double getRequestTopK(EmbeddingCreateParams request) { | ||
return null; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Double getRequestTopP(EmbeddingCreateParams request) { | ||
return null; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public List<String> getResponseFinishReasons( | ||
EmbeddingCreateParams request, @Nullable CreateEmbeddingResponse response) { | ||
return null; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String getResponseId( | ||
EmbeddingCreateParams request, @Nullable CreateEmbeddingResponse response) { | ||
return null; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String getResponseModel( | ||
EmbeddingCreateParams request, @Nullable CreateEmbeddingResponse response) { | ||
if (response == null) { | ||
return null; | ||
} | ||
return response.model(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Long getUsageInputTokens( | ||
EmbeddingCreateParams request, @Nullable CreateEmbeddingResponse response) { | ||
if (response == null) { | ||
return null; | ||
} | ||
return response.usage().promptTokens(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Long getUsageOutputTokens( | ||
EmbeddingCreateParams request, @Nullable CreateEmbeddingResponse response) { | ||
return null; | ||
} | ||
} |
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
71 changes: 71 additions & 0 deletions
71
.../main/java/io/opentelemetry/instrumentation/openai/v1_1/InstrumentedEmbeddingService.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,71 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.openai.v1_1; | ||
|
||
import com.openai.core.RequestOptions; | ||
import com.openai.models.embeddings.CreateEmbeddingResponse; | ||
import com.openai.models.embeddings.EmbeddingCreateParams; | ||
import com.openai.services.blocking.EmbeddingService; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import java.lang.reflect.Method; | ||
|
||
final class InstrumentedEmbeddingService | ||
extends DelegatingInvocationHandler<EmbeddingService, InstrumentedEmbeddingService> { | ||
|
||
private final Instrumenter<EmbeddingCreateParams, CreateEmbeddingResponse> instrumenter; | ||
|
||
public InstrumentedEmbeddingService( | ||
EmbeddingService delegate, | ||
Instrumenter<EmbeddingCreateParams, CreateEmbeddingResponse> instrumenter) { | ||
super(delegate); | ||
this.instrumenter = instrumenter; | ||
} | ||
|
||
@Override | ||
protected Class<EmbeddingService> getProxyType() { | ||
return EmbeddingService.class; | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
String methodName = method.getName(); | ||
Class<?>[] parameterTypes = method.getParameterTypes(); | ||
|
||
if (methodName.equals("create") | ||
&& parameterTypes.length >= 1 | ||
&& parameterTypes[0] == EmbeddingCreateParams.class) { | ||
if (parameterTypes.length == 1) { | ||
return create((EmbeddingCreateParams) args[0], RequestOptions.none()); | ||
} else if (parameterTypes.length == 2 && parameterTypes[1] == RequestOptions.class) { | ||
return create((EmbeddingCreateParams) args[0], (RequestOptions) args[1]); | ||
} | ||
} | ||
|
||
return super.invoke(proxy, method, args); | ||
} | ||
|
||
private CreateEmbeddingResponse create( | ||
EmbeddingCreateParams request, RequestOptions requestOptions) { | ||
Context parentContext = Context.current(); | ||
if (!instrumenter.shouldStart(parentContext, request)) { | ||
return delegate.create(request, requestOptions); | ||
} | ||
|
||
Context context = instrumenter.start(parentContext, request); | ||
CreateEmbeddingResponse response; | ||
try (Scope ignored = context.makeCurrent()) { | ||
response = delegate.create(request, requestOptions); | ||
} catch (Throwable t) { | ||
instrumenter.end(context, request, null, t); | ||
throw t; | ||
} | ||
|
||
instrumenter.end(context, request, response, null); | ||
return response; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
.../java/io/opentelemetry/instrumentation/openai/v1_1/InstrumentedEmbeddingServiceAsync.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,72 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.openai.v1_1; | ||
|
||
import com.openai.core.RequestOptions; | ||
import com.openai.models.embeddings.CreateEmbeddingResponse; | ||
import com.openai.models.embeddings.EmbeddingCreateParams; | ||
import com.openai.services.async.EmbeddingServiceAsync; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import java.lang.reflect.Method; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
final class InstrumentedEmbeddingServiceAsync | ||
extends DelegatingInvocationHandler<EmbeddingServiceAsync, InstrumentedEmbeddingServiceAsync> { | ||
|
||
private final Instrumenter<EmbeddingCreateParams, CreateEmbeddingResponse> instrumenter; | ||
|
||
public InstrumentedEmbeddingServiceAsync( | ||
EmbeddingServiceAsync delegate, | ||
Instrumenter<EmbeddingCreateParams, CreateEmbeddingResponse> instrumenter) { | ||
super(delegate); | ||
this.instrumenter = instrumenter; | ||
} | ||
|
||
@Override | ||
protected Class<EmbeddingServiceAsync> getProxyType() { | ||
return EmbeddingServiceAsync.class; | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
String methodName = method.getName(); | ||
Class<?>[] parameterTypes = method.getParameterTypes(); | ||
|
||
if (methodName.equals("create") | ||
&& parameterTypes.length >= 1 | ||
&& parameterTypes[0] == EmbeddingCreateParams.class) { | ||
if (parameterTypes.length == 1) { | ||
return create((EmbeddingCreateParams) args[0], RequestOptions.none()); | ||
} else if (parameterTypes.length == 2 && parameterTypes[1] == RequestOptions.class) { | ||
return create((EmbeddingCreateParams) args[0], (RequestOptions) args[1]); | ||
} | ||
} | ||
|
||
return super.invoke(proxy, method, args); | ||
} | ||
|
||
private CompletableFuture<CreateEmbeddingResponse> create( | ||
EmbeddingCreateParams request, RequestOptions requestOptions) { | ||
Context parentContext = Context.current(); | ||
if (!instrumenter.shouldStart(parentContext, request)) { | ||
return delegate.create(request, requestOptions); | ||
} | ||
|
||
Context context = instrumenter.start(parentContext, request); | ||
CompletableFuture<CreateEmbeddingResponse> future; | ||
try (Scope ignored = context.makeCurrent()) { | ||
future = delegate.create(request, requestOptions); | ||
} catch (Throwable t) { | ||
instrumenter.end(context, request, null, t); | ||
throw t; | ||
} | ||
|
||
future = future.whenComplete((res, t) -> instrumenter.end(context, request, res, t)); | ||
return CompletableFutureWrapper.wrap(future, parentContext); | ||
} | ||
} |
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
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.
We initially had discussion that it's confusing if a list can be empty or null, which was definitely correct for chat. But these semantic conventions are shared between chat / embeddings where ones like this one aren't present at all in embeddings, so I guess we should allow
null
for itThere 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.
Since the
GenAiAttributesExtractor
treatsnull
and empty the same way making this nullable isn't strictly necessary. @trask do you have a preference here?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.
Thanks - I reverted since indeed the behavior is the same