-
Notifications
You must be signed in to change notification settings - Fork 917
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
Add instrumentation of AWS Bedrock to use gen_ai conventions #13355
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ce57073
Add instrumentation of AWS Bedrock to use gen_ai conventions
anuraaga 1fd295d
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
anuraaga e8a70b6
Drift
anuraaga 1a6cda3
Update muzzle
anuraaga 8954a51
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 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
113 changes: 113 additions & 0 deletions
113
...o/opentelemetry/instrumentation/api/incubator/semconv/genai/GenAiAttributesExtractor.java
This file contains 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,113 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.incubator.semconv.genai; | ||
|
||
import static io.opentelemetry.api.common.AttributeKey.doubleKey; | ||
import static io.opentelemetry.api.common.AttributeKey.longKey; | ||
import static io.opentelemetry.api.common.AttributeKey.stringArrayKey; | ||
import static io.opentelemetry.api.common.AttributeKey.stringKey; | ||
import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; | ||
import java.util.List; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Extractor of <a href="https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans/">GenAI | ||
* attributes</a>. | ||
* | ||
* <p>This class delegates to a type-specific {@link GenAiAttributesGetter} for individual attribute | ||
* extraction from request/response objects. | ||
*/ | ||
public final class GenAiAttributesExtractor<REQUEST, RESPONSE> | ||
implements AttributesExtractor<REQUEST, RESPONSE> { | ||
|
||
// copied from GenAiIncubatingAttributes | ||
private static final AttributeKey<String> GEN_AI_OPERATION_NAME = | ||
stringKey("gen_ai.operation.name"); | ||
private static final AttributeKey<List<String>> GEN_AI_REQUEST_ENCODING_FORMATS = | ||
stringArrayKey("gen_ai.request.encoding_formats"); | ||
private static final AttributeKey<Double> GEN_AI_REQUEST_FREQUENCY_PENALTY = | ||
doubleKey("gen_ai.request.frequency_penalty"); | ||
private static final AttributeKey<Long> GEN_AI_REQUEST_MAX_TOKENS = | ||
longKey("gen_ai.request.max_tokens"); | ||
private static final AttributeKey<String> GEN_AI_REQUEST_MODEL = | ||
stringKey("gen_ai.request.model"); | ||
private static final AttributeKey<Double> GEN_AI_REQUEST_PRESENCE_PENALTY = | ||
doubleKey("gen_ai.request.presence_penalty"); | ||
private static final AttributeKey<Long> GEN_AI_REQUEST_SEED = longKey("gen_ai.request.seed"); | ||
private static final AttributeKey<List<String>> GEN_AI_REQUEST_STOP_SEQUENCES = | ||
stringArrayKey("gen_ai.request.stop_sequences"); | ||
private static final AttributeKey<Double> GEN_AI_REQUEST_TEMPERATURE = | ||
doubleKey("gen_ai.request.temperature"); | ||
private static final AttributeKey<Double> GEN_AI_REQUEST_TOP_K = | ||
doubleKey("gen_ai.request.top_k"); | ||
private static final AttributeKey<Double> GEN_AI_REQUEST_TOP_P = | ||
doubleKey("gen_ai.request.top_p"); | ||
private static final AttributeKey<List<String>> GEN_AI_RESPONSE_FINISH_REASONS = | ||
stringArrayKey("gen_ai.response.finish_reasons"); | ||
private static final AttributeKey<String> GEN_AI_RESPONSE_ID = stringKey("gen_ai.response.id"); | ||
private static final AttributeKey<String> GEN_AI_RESPONSE_MODEL = | ||
stringKey("gen_ai.response.model"); | ||
private static final AttributeKey<String> GEN_AI_SYSTEM = stringKey("gen_ai.system"); | ||
private static final AttributeKey<Long> GEN_AI_USAGE_INPUT_TOKENS = | ||
longKey("gen_ai.usage.input_tokens"); | ||
private static final AttributeKey<Long> GEN_AI_USAGE_OUTPUT_TOKENS = | ||
longKey("gen_ai.usage.output_tokens"); | ||
|
||
/** Creates the GenAI attributes extractor. */ | ||
public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create( | ||
GenAiAttributesGetter<REQUEST, RESPONSE> attributesGetter) { | ||
return new GenAiAttributesExtractor<>(attributesGetter); | ||
} | ||
|
||
private final GenAiAttributesGetter<REQUEST, RESPONSE> getter; | ||
|
||
private GenAiAttributesExtractor(GenAiAttributesGetter<REQUEST, RESPONSE> getter) { | ||
this.getter = getter; | ||
} | ||
|
||
@Override | ||
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) { | ||
internalSet(attributes, GEN_AI_OPERATION_NAME, getter.getOperationName(request)); | ||
internalSet(attributes, GEN_AI_SYSTEM, getter.getSystem(request)); | ||
internalSet(attributes, GEN_AI_REQUEST_MODEL, getter.getRequestModel(request)); | ||
internalSet(attributes, GEN_AI_REQUEST_SEED, getter.getRequestSeed(request)); | ||
internalSet( | ||
attributes, GEN_AI_REQUEST_ENCODING_FORMATS, getter.getRequestEncodingFormats(request)); | ||
internalSet( | ||
attributes, GEN_AI_REQUEST_FREQUENCY_PENALTY, getter.getRequestFrequencyPenalty(request)); | ||
internalSet(attributes, GEN_AI_REQUEST_MAX_TOKENS, getter.getRequestMaxTokens(request)); | ||
internalSet( | ||
attributes, GEN_AI_REQUEST_PRESENCE_PENALTY, getter.getRequestPresencePenalty(request)); | ||
internalSet(attributes, GEN_AI_REQUEST_STOP_SEQUENCES, getter.getRequestStopSequences(request)); | ||
internalSet(attributes, GEN_AI_REQUEST_TEMPERATURE, getter.getRequestTemperature(request)); | ||
internalSet(attributes, GEN_AI_REQUEST_TOP_K, getter.getRequestTopK(request)); | ||
internalSet(attributes, GEN_AI_REQUEST_TOP_P, getter.getRequestTopP(request)); | ||
} | ||
|
||
@Override | ||
public void onEnd( | ||
AttributesBuilder attributes, | ||
Context context, | ||
REQUEST request, | ||
@Nullable RESPONSE response, | ||
@Nullable Throwable error) { | ||
internalSet( | ||
attributes, | ||
GEN_AI_RESPONSE_FINISH_REASONS, | ||
getter.getResponseFinishReasons(request, response)); | ||
internalSet(attributes, GEN_AI_RESPONSE_ID, getter.getResponseId(request, response)); | ||
internalSet(attributes, GEN_AI_RESPONSE_MODEL, getter.getResponseModel(request, response)); | ||
internalSet( | ||
attributes, GEN_AI_USAGE_INPUT_TOKENS, getter.getUsageInputTokens(request, response)); | ||
internalSet( | ||
attributes, GEN_AI_USAGE_OUTPUT_TOKENS, getter.getUsageOutputTokens(request, response)); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...a/io/opentelemetry/instrumentation/api/incubator/semconv/genai/GenAiAttributesGetter.java
This file contains 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,67 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.incubator.semconv.genai; | ||
|
||
import java.util.List; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* An interface for getting GenAI attributes. | ||
* | ||
* <p>Instrumentation authors will create implementations of this interface for their specific | ||
* library/framework. It will be used by the {@link GenAiAttributesExtractor} to obtain the various | ||
* GenAI attributes in a type-generic way. | ||
*/ | ||
public interface GenAiAttributesGetter<REQUEST, RESPONSE> { | ||
String getOperationName(REQUEST request); | ||
|
||
String getSystem(REQUEST request); | ||
|
||
@Nullable | ||
String getRequestModel(REQUEST request); | ||
|
||
@Nullable | ||
Long getRequestSeed(REQUEST request); | ||
|
||
@Nullable | ||
List<String> getRequestEncodingFormats(REQUEST request); | ||
|
||
@Nullable | ||
Double getRequestFrequencyPenalty(REQUEST request); | ||
|
||
@Nullable | ||
Long getRequestMaxTokens(REQUEST request); | ||
|
||
@Nullable | ||
Double getRequestPresencePenalty(REQUEST request); | ||
|
||
@Nullable | ||
List<String> getRequestStopSequences(REQUEST request); | ||
|
||
@Nullable | ||
Double getRequestTemperature(REQUEST request); | ||
|
||
@Nullable | ||
Double getRequestTopK(REQUEST request); | ||
|
||
@Nullable | ||
Double getRequestTopP(REQUEST request); | ||
|
||
@Nullable | ||
List<String> getResponseFinishReasons(REQUEST request, RESPONSE response); | ||
|
||
@Nullable | ||
String getResponseId(REQUEST request, RESPONSE response); | ||
|
||
@Nullable | ||
String getResponseModel(REQUEST request, RESPONSE response); | ||
|
||
@Nullable | ||
Long getUsageInputTokens(REQUEST request, RESPONSE response); | ||
|
||
@Nullable | ||
Long getUsageOutputTokens(REQUEST request, RESPONSE response); | ||
} |
37 changes: 37 additions & 0 deletions
37
.../io/opentelemetry/instrumentation/api/incubator/semconv/genai/GenAiSpanNameExtractor.java
This file contains 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,37 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.incubator.semconv.genai; | ||
|
||
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; | ||
|
||
/** A {@link SpanNameExtractor} for GenAI requests. */ | ||
public final class GenAiSpanNameExtractor<REQUEST> implements SpanNameExtractor<REQUEST> { | ||
|
||
/** | ||
* Returns a {@link SpanNameExtractor} that constructs the span name according to GenAI semantic | ||
* conventions: {@code <gen_ai.operation.name> <gen_ai.request.model>}. | ||
*/ | ||
public static <REQUEST> SpanNameExtractor<REQUEST> create( | ||
GenAiAttributesGetter<REQUEST, ?> attributesGetter) { | ||
return new GenAiSpanNameExtractor<>(attributesGetter); | ||
} | ||
|
||
private final GenAiAttributesGetter<REQUEST, ?> getter; | ||
|
||
private GenAiSpanNameExtractor(GenAiAttributesGetter<REQUEST, ?> getter) { | ||
this.getter = getter; | ||
} | ||
|
||
@Override | ||
public String extract(REQUEST request) { | ||
String operation = getter.getOperationName(request); | ||
String model = getter.getRequestModel(request); | ||
if (model == null) { | ||
return operation; | ||
} | ||
return operation + ' ' + model; | ||
} | ||
} |
This file contains 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
20 changes: 20 additions & 0 deletions
20
...ava/io/opentelemetry/instrumentation/awssdk/v2_2/internal/BedrockRuntimeAdviceBridge.java
This file contains 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,20 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.awssdk.v2_2.internal; | ||
|
||
/** | ||
* This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
* any time. | ||
*/ | ||
public final class BedrockRuntimeAdviceBridge { | ||
private BedrockRuntimeAdviceBridge() {} | ||
|
||
public static void referenceForMuzzleOnly() { | ||
throw new UnsupportedOperationException( | ||
BedrockRuntimeImpl.class.getName() | ||
+ " referencing for muzzle, should never be actually called"); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ntelemetry/javaagent/instrumentation/awssdk/v2_2/BedrockRuntimeInstrumentationModule.java
This file contains 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,45 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.awssdk.v2_2; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
import static net.bytebuddy.matcher.ElementMatchers.none; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.instrumentation.awssdk.v2_2.internal.BedrockRuntimeAdviceBridge; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class BedrockRuntimeInstrumentationModule extends AbstractAwsSdkInstrumentationModule { | ||
|
||
public BedrockRuntimeInstrumentationModule() { | ||
super("aws-sdk-2.2-bedrock"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() { | ||
return hasClassesNamed("software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient"); | ||
} | ||
|
||
@Override | ||
public void doTransform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
none(), BedrockRuntimeInstrumentationModule.class.getName() + "$RegisterAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class RegisterAdvice { | ||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void onExit() { | ||
// (indirectly) using BedrockRuntimeImpl class here to make sure it is available from | ||
// BedrockRuntimeAccess (injected into app classloader) and checked by Muzzle | ||
BedrockRuntimeAdviceBridge.referenceForMuzzleOnly(); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...me/java/io/opentelemetry/instrumentation/awssdk/v2_2/internal/Aws2BedrockRuntimeTest.java
This file contains 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 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.awssdk.v2_2.internal; | ||
|
||
import io.opentelemetry.instrumentation.awssdk.v2_2.AbstractAws2BedrockRuntimeTest; | ||
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; | ||
|
||
class Aws2BedrockRuntimeTest extends AbstractAws2BedrockRuntimeTest { | ||
@RegisterExtension | ||
static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); | ||
|
||
@Override | ||
protected InstrumentationExtension getTesting() { | ||
return testing; | ||
} | ||
|
||
@Override | ||
protected ClientOverrideConfiguration.Builder createOverrideConfigurationBuilder() { | ||
return ClientOverrideConfiguration.builder(); | ||
} | ||
} |
This file contains 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 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.
Note this causes other suites defined above to be run that I think previously weren't