Skip to content

Commit 7893f24

Browse files
authored
feat: add gcp client attributes in OpenTelemetry traces (#3595)
1 parent 9ab4c9f commit 7893f24

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ If you are using Maven without the BOM, add this to your dependencies:
4141
<dependency>
4242
<groupId>com.google.cloud</groupId>
4343
<artifactId>google-cloud-spanner</artifactId>
44-
<version>6.81.1</version>
44+
<version>6.84.0</version>
4545
</dependency>
4646

4747
```

google-cloud-spanner/src/main/java/com/google/cloud/spanner/TraceWrapper.java

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.spanner;
1818

19+
import com.google.api.gax.core.GaxProperties;
1920
import com.google.cloud.spanner.Options.TagOption;
2021
import com.google.cloud.spanner.Options.TransactionOption;
2122
import com.google.cloud.spanner.SpannerOptions.TracingFramework;
@@ -46,6 +47,12 @@ class TraceWrapper {
4647
private static final AttributeKey<List<String>> DB_STATEMENT_ARRAY_KEY =
4748
AttributeKey.stringArrayKey("db.statement");
4849
private static final AttributeKey<String> DB_TABLE_NAME_KEY = AttributeKey.stringKey("db.table");
50+
private static final AttributeKey<String> GCP_CLIENT_SERVICE_KEY =
51+
AttributeKey.stringKey("gcp.client.service");
52+
private static final AttributeKey<String> GCP_CLIENT_VERSION_KEY =
53+
AttributeKey.stringKey("gcp.client.version");
54+
private static final AttributeKey<String> GCP_CLIENT_REPO_KEY =
55+
AttributeKey.stringKey("gcp.client.repo");
4956
private static final AttributeKey<String> THREAD_NAME_KEY = AttributeKey.stringKey("thread.name");
5057

5158
private final Tracer openCensusTracer;
@@ -204,6 +211,9 @@ Attributes createCommonAttributes(DatabaseId db) {
204211
AttributesBuilder builder = Attributes.builder();
205212
builder.put(DB_NAME_KEY, db.getDatabase());
206213
builder.put(INSTANCE_NAME_KEY, db.getInstanceId().getInstance());
214+
builder.put(GCP_CLIENT_SERVICE_KEY, "spanner");
215+
builder.put(GCP_CLIENT_REPO_KEY, "googleapis/java-spanner");
216+
builder.put(GCP_CLIENT_VERSION_KEY, GaxProperties.getLibraryVersion(TraceWrapper.class));
207217
return builder.build();
208218
}
209219

google-cloud-spanner/src/test/java/com/google/cloud/spanner/OpenTelemetrySpanTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.Assert.assertThrows;
2222
import static org.junit.Assert.assertTrue;
2323

24+
import com.google.api.gax.core.GaxProperties;
2425
import com.google.api.gax.grpc.testing.LocalChannelProvider;
2526
import com.google.cloud.NoCredentials;
2627
import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult;
@@ -871,6 +872,13 @@ private static void verifySpans(List<String> actualSpanItems, List<String> expec
871872
private static void verifyCommonAttributes(SpanData span) {
872873
assertEquals(span.getAttributes().get(AttributeKey.stringKey("instance.name")), "my-instance");
873874
assertEquals(span.getAttributes().get(AttributeKey.stringKey("db.name")), "my-database");
875+
assertEquals(span.getAttributes().get(AttributeKey.stringKey("gcp.client.service")), "spanner");
876+
assertEquals(
877+
span.getAttributes().get(AttributeKey.stringKey("gcp.client.repo")),
878+
"googleapis/java-spanner");
879+
assertEquals(
880+
span.getAttributes().get(AttributeKey.stringKey("gcp.client.version")),
881+
GaxProperties.getLibraryVersion(TraceWrapper.class));
874882
}
875883

876884
private static void verifyTableAttributes(SpanData span) {

0 commit comments

Comments
 (0)