Skip to content

Commit 9e5c9e4

Browse files
committed
Add GenAI exception event extractor helper
1 parent b2035d0 commit 9e5c9e4

2 files changed

Lines changed: 55 additions & 12 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.api.incubator.semconv.genai.internal;
7+
8+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
9+
import io.opentelemetry.api.logs.Severity;
10+
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
11+
import io.opentelemetry.instrumentation.api.internal.Experimental;
12+
13+
/**
14+
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
15+
* any time.
16+
*/
17+
public final class GenAiExceptionEventExtractors {
18+
19+
/**
20+
* Configures the GenAI client operation exception event name and severity. Only takes effect when
21+
* emitting exceptions as logs is enabled via the {@code otel.semconv.exception.signal.preview}
22+
* flag.
23+
*/
24+
@CanIgnoreReturnValue
25+
public static <REQUEST, RESPONSE>
26+
InstrumenterBuilder<REQUEST, RESPONSE> setGenAiClientExceptionEventExtractor(
27+
InstrumenterBuilder<REQUEST, RESPONSE> builder) {
28+
Experimental.setExceptionEventExtractor(
29+
builder,
30+
(logRecordBuilder, context, request) -> {
31+
logRecordBuilder.setEventName("gen_ai.client.operation.exception");
32+
logRecordBuilder.setSeverity(Severity.WARN);
33+
});
34+
return builder;
35+
}
36+
37+
private GenAiExceptionEventExtractors() {}
38+
}

instrumentation/openai/openai-java-1.1/library/src/main/java/io/opentelemetry/instrumentation/openai/v1_1/OpenAITelemetryBuilder.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package io.opentelemetry.instrumentation.openai.v1_1;
77

8+
import static io.opentelemetry.instrumentation.api.incubator.semconv.genai.internal.GenAiExceptionEventExtractors.setGenAiClientExceptionEventExtractor;
9+
810
import com.google.errorprone.annotations.CanIgnoreReturnValue;
911
import com.openai.models.chat.completions.ChatCompletion;
1012
import com.openai.models.chat.completions.ChatCompletionCreateParams;
@@ -49,22 +51,25 @@ public OpenAITelemetryBuilder setCaptureMessageContent(boolean captureMessageCon
4951
public OpenAITelemetry build() {
5052
ChatAttributesGetter chatAttributesGetter = new ChatAttributesGetter();
5153
Instrumenter<ChatCompletionCreateParams, ChatCompletion> chatInstrumenter =
52-
Instrumenter.<ChatCompletionCreateParams, ChatCompletion>builder(
53-
openTelemetry,
54-
INSTRUMENTATION_NAME,
55-
GenAiSpanNameExtractor.create(chatAttributesGetter))
56-
.addAttributesExtractor(GenAiAttributesExtractor.create(chatAttributesGetter))
57-
.addOperationMetrics(GenAiClientMetrics.get())
54+
setGenAiClientExceptionEventExtractor(
55+
Instrumenter.<ChatCompletionCreateParams, ChatCompletion>builder(
56+
openTelemetry,
57+
INSTRUMENTATION_NAME,
58+
GenAiSpanNameExtractor.create(chatAttributesGetter))
59+
.addAttributesExtractor(GenAiAttributesExtractor.create(chatAttributesGetter))
60+
.addOperationMetrics(GenAiClientMetrics.get()))
5861
.buildInstrumenter();
5962

6063
EmbeddingAttributesGetter embeddingAttributesGetter = new EmbeddingAttributesGetter();
6164
Instrumenter<EmbeddingCreateParams, CreateEmbeddingResponse> embeddingsInstrumenter =
62-
Instrumenter.<EmbeddingCreateParams, CreateEmbeddingResponse>builder(
63-
openTelemetry,
64-
INSTRUMENTATION_NAME,
65-
GenAiSpanNameExtractor.create(embeddingAttributesGetter))
66-
.addAttributesExtractor(GenAiAttributesExtractor.create(embeddingAttributesGetter))
67-
.addOperationMetrics(GenAiClientMetrics.get())
65+
setGenAiClientExceptionEventExtractor(
66+
Instrumenter.<EmbeddingCreateParams, CreateEmbeddingResponse>builder(
67+
openTelemetry,
68+
INSTRUMENTATION_NAME,
69+
GenAiSpanNameExtractor.create(embeddingAttributesGetter))
70+
.addAttributesExtractor(
71+
GenAiAttributesExtractor.create(embeddingAttributesGetter))
72+
.addOperationMetrics(GenAiClientMetrics.get()))
6873
.buildInstrumenter(SpanKindExtractor.alwaysClient());
6974

7075
Logger eventLogger = openTelemetry.getLogsBridge().get(INSTRUMENTATION_NAME);

0 commit comments

Comments
 (0)