Skip to content

Commit

Permalink
Merge pull request quarkiverse#1170 from quarkiverse/observability-npes
Browse files Browse the repository at this point in the history
Be more defensive in setting metadata for observability events
  • Loading branch information
geoand authored Dec 18, 2024
2 parents d56aa6b + 10d8159 commit e097a17
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.Timer;
import io.quarkiverse.langchain4j.cost.Cost;
Expand Down Expand Up @@ -78,9 +77,10 @@ public void onResponse(ChatModelResponseContext responseContext) {

ChatModelRequest request = responseContext.request();
ChatModelResponse response = responseContext.response();
Tags tags = Tags.of(
Tag.of("gen_ai.request.model", request.model()),
Tag.of("gen_ai.response.model", response.model()));
Tags tags = Tags.of("gen_ai.request.model", request.model());
if (response.model() != null) {
tags = tags.and("gen_ai.response.model", response.model());
}
if (ContextLocals.duplicatedContextActive()) {
String aiServiceClassName = ContextLocals.get(AiServiceConstants.AI_SERVICE_CLASS_NAME);
if (aiServiceClassName != null) {
Expand Down Expand Up @@ -112,10 +112,13 @@ public void onError(ChatModelErrorContext errorContext) {
return;
}

Tags tags = Tags.of(
Tag.of("gen_ai.request.model", errorContext.request().model()),
Tag.of("gen_ai.response.model", errorContext.partialResponse().model()),
Tag.of("error.type", aiMessage.text()));
Tags tags = Tags.of("gen_ai.request.model", errorContext.request().model());
if (errorContext.partialResponse().model() != null) {
tags = tags.and("gen_ai.response.model", errorContext.partialResponse().model());
}
if (aiMessage.text() != null) {
tags = tags.and("error.type", aiMessage.text());
}
duration.withTags(tags).record(endTime - startTime, TimeUnit.NANOSECONDS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public void onResponse(ChatModelResponseContext responseContext) {
Span span = (Span) attributes.get(OTEL_SPAN_KEY_NAME);
if (span != null) {
ChatModelResponse response = responseContext.response();
span.setAttribute("gen_ai.response.id", response.id()).setAttribute("gen_ai.response.model", response.model());
span.setAttribute("gen_ai.response.id", response.id());
if (response.model() != null) {
span.setAttribute("gen_ai.response.model", response.model());
}
if (response.finishReason() != null) {
span.setAttribute("gen_ai.response.finish_reasons", response.finishReason().toString());
}
Expand Down

0 comments on commit e097a17

Please sign in to comment.