|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.api.incubator.semconv.genai; |
| 7 | + |
| 8 | +import static io.opentelemetry.api.common.AttributeKey.doubleKey; |
| 9 | +import static io.opentelemetry.api.common.AttributeKey.longKey; |
| 10 | +import static io.opentelemetry.api.common.AttributeKey.stringArrayKey; |
| 11 | +import static io.opentelemetry.api.common.AttributeKey.stringKey; |
| 12 | +import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet; |
| 13 | + |
| 14 | +import io.opentelemetry.api.common.AttributeKey; |
| 15 | +import io.opentelemetry.api.common.AttributesBuilder; |
| 16 | +import io.opentelemetry.context.Context; |
| 17 | +import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; |
| 18 | +import java.util.List; |
| 19 | +import javax.annotation.Nullable; |
| 20 | + |
| 21 | +/** |
| 22 | + * Extractor of <a href="https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans/">GenAI |
| 23 | + * attributes</a>. |
| 24 | + * |
| 25 | + * <p>This class delegates to a type-specific {@link GenAiAttributesGetter} for individual attribute |
| 26 | + * extraction from request/response objects. |
| 27 | + */ |
| 28 | +public final class GenAiAttributesExtractor<REQUEST, RESPONSE> |
| 29 | + implements AttributesExtractor<REQUEST, RESPONSE> { |
| 30 | + |
| 31 | + // copied from GenAiIncubatingAttributes |
| 32 | + private static final AttributeKey<String> GEN_AI_OPERATION_NAME = |
| 33 | + stringKey("gen_ai.operation.name"); |
| 34 | + private static final AttributeKey<List<String>> GEN_AI_REQUEST_ENCODING_FORMATS = |
| 35 | + stringArrayKey("gen_ai.request.encoding_formats"); |
| 36 | + private static final AttributeKey<Double> GEN_AI_REQUEST_FREQUENCY_PENALTY = |
| 37 | + doubleKey("gen_ai.request.frequency_penalty"); |
| 38 | + private static final AttributeKey<Long> GEN_AI_REQUEST_MAX_TOKENS = |
| 39 | + longKey("gen_ai.request.max_tokens"); |
| 40 | + private static final AttributeKey<String> GEN_AI_REQUEST_MODEL = |
| 41 | + stringKey("gen_ai.request.model"); |
| 42 | + private static final AttributeKey<Double> GEN_AI_REQUEST_PRESENCE_PENALTY = |
| 43 | + doubleKey("gen_ai.request.presence_penalty"); |
| 44 | + private static final AttributeKey<Long> GEN_AI_REQUEST_SEED = longKey("gen_ai.request.seed"); |
| 45 | + private static final AttributeKey<List<String>> GEN_AI_REQUEST_STOP_SEQUENCES = |
| 46 | + stringArrayKey("gen_ai.request.stop_sequences"); |
| 47 | + private static final AttributeKey<Double> GEN_AI_REQUEST_TEMPERATURE = |
| 48 | + doubleKey("gen_ai.request.temperature"); |
| 49 | + private static final AttributeKey<Double> GEN_AI_REQUEST_TOP_K = |
| 50 | + doubleKey("gen_ai.request.top_k"); |
| 51 | + private static final AttributeKey<Double> GEN_AI_REQUEST_TOP_P = |
| 52 | + doubleKey("gen_ai.request.top_p"); |
| 53 | + private static final AttributeKey<List<String>> GEN_AI_RESPONSE_FINISH_REASONS = |
| 54 | + stringArrayKey("gen_ai.response.finish_reasons"); |
| 55 | + private static final AttributeKey<String> GEN_AI_RESPONSE_ID = stringKey("gen_ai.response.id"); |
| 56 | + private static final AttributeKey<String> GEN_AI_RESPONSE_MODEL = |
| 57 | + stringKey("gen_ai.response.model"); |
| 58 | + private static final AttributeKey<String> GEN_AI_SYSTEM = stringKey("gen_ai.system"); |
| 59 | + private static final AttributeKey<Long> GEN_AI_USAGE_INPUT_TOKENS = |
| 60 | + longKey("gen_ai.usage.input_tokens"); |
| 61 | + private static final AttributeKey<Long> GEN_AI_USAGE_OUTPUT_TOKENS = |
| 62 | + longKey("gen_ai.usage.output_tokens"); |
| 63 | + |
| 64 | + /** Creates the GenAI attributes extractor. */ |
| 65 | + public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create( |
| 66 | + GenAiAttributesGetter<REQUEST, RESPONSE> attributesGetter) { |
| 67 | + return new GenAiAttributesExtractor<>(attributesGetter); |
| 68 | + } |
| 69 | + |
| 70 | + private final GenAiAttributesGetter<REQUEST, RESPONSE> getter; |
| 71 | + |
| 72 | + private GenAiAttributesExtractor(GenAiAttributesGetter<REQUEST, RESPONSE> getter) { |
| 73 | + this.getter = getter; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) { |
| 78 | + internalSet(attributes, GEN_AI_OPERATION_NAME, getter.getOperationName(request)); |
| 79 | + internalSet(attributes, GEN_AI_SYSTEM, getter.getSystem(request)); |
| 80 | + internalSet(attributes, GEN_AI_REQUEST_MODEL, getter.getRequestModel(request)); |
| 81 | + internalSet(attributes, GEN_AI_REQUEST_SEED, getter.getRequestSeed(request)); |
| 82 | + internalSet( |
| 83 | + attributes, GEN_AI_REQUEST_ENCODING_FORMATS, getter.getRequestEncodingFormats(request)); |
| 84 | + internalSet( |
| 85 | + attributes, GEN_AI_REQUEST_FREQUENCY_PENALTY, getter.getRequestFrequencyPenalty(request)); |
| 86 | + internalSet(attributes, GEN_AI_REQUEST_MAX_TOKENS, getter.getRequestMaxTokens(request)); |
| 87 | + internalSet( |
| 88 | + attributes, GEN_AI_REQUEST_PRESENCE_PENALTY, getter.getRequestPresencePenalty(request)); |
| 89 | + internalSet(attributes, GEN_AI_REQUEST_STOP_SEQUENCES, getter.getRequestStopSequences(request)); |
| 90 | + internalSet(attributes, GEN_AI_REQUEST_TEMPERATURE, getter.getRequestTemperature(request)); |
| 91 | + internalSet(attributes, GEN_AI_REQUEST_TOP_K, getter.getRequestTopK(request)); |
| 92 | + internalSet(attributes, GEN_AI_REQUEST_TOP_P, getter.getRequestTopP(request)); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void onEnd( |
| 97 | + AttributesBuilder attributes, |
| 98 | + Context context, |
| 99 | + REQUEST request, |
| 100 | + @Nullable RESPONSE response, |
| 101 | + @Nullable Throwable error) { |
| 102 | + internalSet( |
| 103 | + attributes, |
| 104 | + GEN_AI_RESPONSE_FINISH_REASONS, |
| 105 | + getter.getResponseFinishReasons(request, response)); |
| 106 | + internalSet(attributes, GEN_AI_RESPONSE_ID, getter.getResponseId(request, response)); |
| 107 | + internalSet(attributes, GEN_AI_RESPONSE_MODEL, getter.getResponseModel(request, response)); |
| 108 | + internalSet( |
| 109 | + attributes, GEN_AI_USAGE_INPUT_TOKENS, getter.getUsageInputTokens(request, response)); |
| 110 | + internalSet( |
| 111 | + attributes, GEN_AI_USAGE_OUTPUT_TOKENS, getter.getUsageOutputTokens(request, response)); |
| 112 | + } |
| 113 | +} |
0 commit comments