Skip to content

Commit

Permalink
feat: add gcp client attributes in OpenTelemetry traces
Browse files Browse the repository at this point in the history
  • Loading branch information
surbhigarg92 committed Jan 10, 2025
1 parent eee333b commit d4bfa36
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.spanner;

import com.google.api.gax.core.GaxProperties;
import com.google.cloud.spanner.Options.TagOption;
import com.google.cloud.spanner.Options.TransactionOption;
import com.google.cloud.spanner.SpannerOptions.TracingFramework;
Expand Down Expand Up @@ -46,6 +47,12 @@ class TraceWrapper {
private static final AttributeKey<List<String>> DB_STATEMENT_ARRAY_KEY =
AttributeKey.stringArrayKey("db.statement");
private static final AttributeKey<String> DB_TABLE_NAME_KEY = AttributeKey.stringKey("db.table");
private static final AttributeKey<String> GCP_CLIENT_SERVICE_KEY =
AttributeKey.stringKey("gcp.client.service");
private static final AttributeKey<String> GCP_CLIENT_VERSION_KEY =
AttributeKey.stringKey("gcp.client.version");
private static final AttributeKey<String> GCP_CLIENT_REPO_KEY =
AttributeKey.stringKey("gcp.client.repo");
private static final AttributeKey<String> THREAD_NAME_KEY = AttributeKey.stringKey("thread.name");

private final Tracer openCensusTracer;
Expand Down Expand Up @@ -204,6 +211,9 @@ Attributes createCommonAttributes(DatabaseId db) {
AttributesBuilder builder = Attributes.builder();
builder.put(DB_NAME_KEY, db.getDatabase());
builder.put(INSTANCE_NAME_KEY, db.getInstanceId().getInstance());
builder.put(GCP_CLIENT_SERVICE_KEY, "spanner");
builder.put(GCP_CLIENT_REPO_KEY, "googleapis/java-spanner");
builder.put(GCP_CLIENT_VERSION_KEY, GaxProperties.getLibraryVersion(TraceWrapper.class));
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import com.google.api.gax.core.GaxProperties;
import com.google.api.gax.grpc.testing.LocalChannelProvider;
import com.google.cloud.NoCredentials;
import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult;
Expand Down Expand Up @@ -871,6 +872,13 @@ private static void verifySpans(List<String> actualSpanItems, List<String> expec
private static void verifyCommonAttributes(SpanData span) {
assertEquals(span.getAttributes().get(AttributeKey.stringKey("instance.name")), "my-instance");
assertEquals(span.getAttributes().get(AttributeKey.stringKey("db.name")), "my-database");
assertEquals(span.getAttributes().get(AttributeKey.stringKey("gcp.client.service")), "spanner");
assertEquals(
span.getAttributes().get(AttributeKey.stringKey("gcp.client.repo")),
"googleapis/java-spanner");
assertEquals(
span.getAttributes().get(AttributeKey.stringKey("gcp.client.version")),
GaxProperties.getLibraryVersion(TraceWrapper.class));
}

private static void verifyTableAttributes(SpanData span) {
Expand Down

0 comments on commit d4bfa36

Please sign in to comment.