Skip to content

Commit

Permalink
error.type and db.response.status_code for jdbc
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Feb 19, 2025
1 parent 2f66869 commit e71e0cf
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStable;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.semconv.ErrorAttributes.ERROR_TYPE;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -177,6 +178,7 @@ void elasticsearchError() {
maybeStable(DB_SYSTEM),
DbIncubatingAttributes.DbSystemIncubatingValues.ELASTICSEARCH),
equalTo(maybeStable(DB_OPERATION), "RefreshAction"),
equalTo(ERROR_TYPE, "org.elasticsearch.index.IndexNotFoundException"),
equalTo(stringKey("elasticsearch.action"), "RefreshAction"),
equalTo(stringKey("elasticsearch.request"), "RefreshRequest"),
equalTo(stringKey("elasticsearch.request.indices"), indexName))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ void testCommandCompletesExceptionally() {
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "DEL"),
equalTo(maybeStable(ERROR_TYPE), "java.lang.IllegalStateException"))));
equalTo(ERROR_TYPE, "java.lang.IllegalStateException"))));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.instrumentation.mongo.v3_1;

import com.mongodb.MongoException;
import com.mongodb.ServerAddress;
import com.mongodb.connection.ConnectionDescription;
import com.mongodb.event.CommandStartedEvent;
Expand Down Expand Up @@ -97,6 +98,15 @@ public String getDbOperationName(CommandStartedEvent event) {
return event.getCommandName();
}

@Nullable
@Override
public String getResponseStatusFromException(Throwable throwable) {
if (throwable instanceof MongoException) {
return Integer.toString(((MongoException) throwable).getCode());
}
return null;
}

String sanitizeStatement(BsonDocument command) {
StringBuilderWriter stringWriter = new StringBuilderWriter(128);
// jsonWriterSettings is generally not null but could be due to security manager or unknown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static java.util.Collections.singleton;

import io.opentelemetry.instrumentation.api.incubator.semconv.db.SqlClientAttributesGetter;
import io.r2dbc.spi.R2dbcException;
import java.util.Collection;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -47,4 +48,13 @@ public String getConnectionString(DbExecution request) {
public Collection<String> getRawQueryTexts(DbExecution request) {
return singleton(request.getRawQueryText());
}

@Nullable
@Override
public String getResponseStatusFromException(Throwable throwable) {
if (throwable instanceof R2dbcException) {
return ((R2dbcException) throwable).getSqlState();
}
return null;
}
}

0 comments on commit e71e0cf

Please sign in to comment.