Skip to content

Commit

Permalink
feat: add gcp client attributes in OpenTelemetry traces (#3595)
Browse files Browse the repository at this point in the history
  • Loading branch information
surbhigarg92 authored Jan 10, 2025
1 parent 9ab4c9f commit 7893f24
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ If you are using Maven without the BOM, add this to your dependencies:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner</artifactId>
<version>6.81.1</version>
<version>6.84.0</version>
</dependency>

```
Expand Down
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 7893f24

Please sign in to comment.