Skip to content
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 5 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,10 @@ tasks.withType<Test>().configureEach {
val trustStore = project(":testing-common").file("src/misc/testing-keystore.p12")
// Work around payara not working when this is set for some reason.
// Don't set for:
// - aws-sdk as we have tests that interact with AWS and need normal trustStore
// - camel as we have tests that interact with AWS and need normal trustStore
// - vaadin as tests need to be able to download nodejs when not cached in ~/.vaadin/
if (project.name != "jaxrs-2.0-payara-testing" && !project.path.contains("vaadin") && project.description != "camel-2-20") {
if (project.name != "jaxrs-2.0-payara-testing" && !project.path.contains("vaadin") && project.description != "camel-2-20" && !project.path.contains("aws-sdk")) {
jvmArgumentProviders.add(KeystoreArgumentsProvider(trustStore))
}

Expand Down
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));
}
}
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);
}
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;
}
}
12 changes: 12 additions & 0 deletions instrumentation/aws-sdk/aws-sdk-2.2/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ testing {
implementation(project(":instrumentation:aws-sdk:aws-sdk-2.2:library"))
}
}

val testBedrockRuntime by registering(JvmTestSuite::class) {
dependencies {
implementation(project(":instrumentation:aws-sdk:aws-sdk-2.2:testing"))
if (findProperty("testLatestDeps") as Boolean) {
implementation("software.amazon.awssdk:bedrockruntime:+")
} else {
// First .0 release with Converse API
implementation("software.amazon.awssdk:bedrockruntime:2.26.0")
}
}
}
}
}

Expand Down
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");
}
}
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();
}
}
}
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();
}
}
9 changes: 9 additions & 0 deletions instrumentation/aws-sdk/aws-sdk-2.2/library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,12 @@ run over API limitations set by AWS.
If this does not fulfill your use case, perhaps because you are
using the same SDK with a different non-AWS managed service, let us know so we can provide
configuration for this behavior.

## Development

### Testing

Some tests use recorded API responses to run through instrumentation. By default, recordings
are used, but if needing to add new tests/recordings or update existing ones, run the tests with
the `RECORD_WITH_REAL_API` environment variable set. AWS credentials will need to be correctly
configured to work.
18 changes: 18 additions & 0 deletions instrumentation/aws-sdk/aws-sdk-2.2/library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ dependencies {
compileOnly("software.amazon.awssdk:json-utils:2.17.0")
compileOnly(project(":muzzle")) // For @NoMuzzle

// don't use library to make sure base test is run with the floor version.
// bedrock runtime is tested separately with newer versions.
compileOnly("software.amazon.awssdk:bedrockruntime:2.26.0")

testImplementation(project(":instrumentation:aws-sdk:aws-sdk-2.2:testing"))

testLibrary("software.amazon.awssdk:dynamodb:2.2.0")
Expand Down Expand Up @@ -56,6 +60,19 @@ testing {
}
}
}

val testBedrockRuntime by registering(JvmTestSuite::class) {
dependencies {
implementation(project())
implementation(project(":instrumentation:aws-sdk:aws-sdk-2.2:testing"))
if (findProperty("testLatestDeps") as Boolean) {
implementation("software.amazon.awssdk:bedrockruntime:+")
} else {
// First .0 release with Converse API
implementation("software.amazon.awssdk:bedrockruntime:2.26.0")
}
}
}
}
}

Expand All @@ -72,6 +89,7 @@ tasks {
}

check {
dependsOn(testing.suites)
Copy link
Contributor Author

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

dependsOn(testStableSemconv)
}
}
Loading
Loading