Skip to content

Commit 59f320c

Browse files
committed
feat: Add more attributes to metrics.
1 parent 3bd6f0a commit 59f320c

5 files changed

Lines changed: 56 additions & 4 deletions

File tree

gax-java/gax/src/main/java/com/google/api/gax/rpc/LibraryMetadata.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public abstract class LibraryMetadata {
5151
@Nullable
5252
public abstract String artifactName();
5353

54+
@Nullable
55+
public abstract String version();
56+
5457
public static LibraryMetadata empty() {
5558
return newBuilder().build();
5659
}
@@ -65,6 +68,8 @@ public abstract static class Builder {
6568

6669
public abstract Builder setArtifactName(@Nullable String artifactName);
6770

71+
public abstract Builder setVersion(@Nullable String version);
72+
6873
public abstract LibraryMetadata build();
6974
}
7075
}

gax-java/gax/src/main/java/com/google/api/gax/tracing/ApiTracerContext.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,22 @@
3333
import com.google.api.core.InternalApi;
3434
import com.google.api.gax.rpc.LibraryMetadata;
3535
import com.google.auto.value.AutoValue;
36+
import org.apache.http.protocol.HTTP;
37+
3638
import java.util.HashMap;
3739
import java.util.Map;
3840
import javax.annotation.Nullable;
41+
//gcp.client.service [consistent across T4 spans]
42+
//gcp.client.version [consistent across T4 spans]
43+
// rpc.system.name (e.g., grpc, http) [consistent across T4 spans]
44+
// rpc.response.status_code (for gRPC and HTTP) [from last T4 span]
45+
// rpc.method (for gRPC and HTTP) [from the last T4 span]
46+
//url.domain [consistent across T4 spans]
47+
// url.template (for HTTP) [from the first T4 span]
48+
// http.response.status_code (for HTTP) [from the last T4 span]
49+
//server.address [from the last T4 span]
50+
//server.port [from the last T4 span]
51+
// error.type (if the overall T3 operation failed)
3952

4053
/**
4154
* A context object that contains information used to infer attributes that are common for all
@@ -51,6 +64,15 @@ public abstract class ApiTracerContext {
5164

5265
public abstract LibraryMetadata libraryMetadata();
5366

67+
@Nullable
68+
public abstract String serviceName();
69+
70+
@Nullable
71+
public abstract String urlDomain();
72+
73+
@Nullable
74+
public abstract String urlTemplate();
75+
5476
/**
5577
* @return a map of attributes to be included in attempt-level spans
5678
*/
@@ -68,6 +90,25 @@ public Map<String, String> getAttemptAttributes() {
6890
return attributes;
6991
}
7092

93+
Map<String, String> getMetricsAttributes() {
94+
Map<String, String> attributes = new HashMap<>();
95+
if (serverAddress() != null) {
96+
attributes.put(ObservabilityAttributes.SERVER_ADDRESS_ATTRIBUTE, serverAddress());
97+
}
98+
if (serviceName() != null) {
99+
attributes.put("gcp.client.service", serviceName());
100+
}
101+
if (transport() == HTTP) {
102+
if (urlDomain() != null) {
103+
attributes.put("url.domain", serviceName());
104+
}
105+
if (serviceName() != null) {
106+
attributes.put("url.template", serviceName());
107+
}
108+
}
109+
return attributes;
110+
}
111+
71112
public static ApiTracerContext empty() {
72113
return newBuilder().setLibraryMetadata(LibraryMetadata.empty()).build();
73114
}

gax-java/gax/src/main/java/com/google/api/gax/tracing/GoldenSignalMetricsTracer.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class GoldenSignalMetricsTracer implements ApiTracer {
4747
private final Stopwatch clientRequestTimer = Stopwatch.createStarted();
4848
private final GoldenSignalsMetricsRecorder metricsRecorder;
4949
private final Map<String, String> attributes = new HashMap<>();
50+
private final ApiTracerContext apiTracerContext;
5051

5152
/**
5253
* Creates the following instruments for the following metrics:
@@ -55,29 +56,34 @@ class GoldenSignalMetricsTracer implements ApiTracer {
5556
* <li>Client Request Duration: Histogram
5657
* </ul>
5758
*
58-
* @param metricsRecorder OpenTelemetry
59+
* @param metricsRecorder OpenTelemetry
60+
* @param apiTracerContext
5961
*/
60-
GoldenSignalMetricsTracer(GoldenSignalsMetricsRecorder metricsRecorder) {
62+
GoldenSignalMetricsTracer(GoldenSignalsMetricsRecorder metricsRecorder, ApiTracerContext apiTracerContext) {
6163
this.metricsRecorder = metricsRecorder;
64+
this.apiTracerContext = apiTracerContext;
6265
}
6366

6467
@Override
6568
public void operationSucceeded() {
6669
attributes.put(RPC_RESPONSE_STATUS_ATTRIBUTE, StatusCode.Code.OK.toString());
70+
attributes.putAll(apiTracerContext.getMetricsAttributes());
6771
metricsRecorder.recordOperationLatency(
6872
clientRequestTimer.elapsed(TimeUnit.SECONDS), attributes);
6973
}
7074

7175
@Override
7276
public void operationCancelled() {
7377
attributes.put(RPC_RESPONSE_STATUS_ATTRIBUTE, StatusCode.Code.CANCELLED.toString());
78+
attributes.putAll(apiTracerContext.getMetricsAttributes());
7479
metricsRecorder.recordOperationLatency(
7580
clientRequestTimer.elapsed(TimeUnit.SECONDS), attributes);
7681
}
7782

7883
@Override
7984
public void operationFailed(Throwable error) {
8085
attributes.put(RPC_RESPONSE_STATUS_ATTRIBUTE, ObservabilityUtils.extractStatus(error));
86+
attributes.putAll(apiTracerContext.getMetricsAttributes());
8187
metricsRecorder.recordOperationLatency(
8288
clientRequestTimer.elapsed(TimeUnit.SECONDS), attributes);
8389
}

gax-java/gax/src/main/java/com/google/api/gax/tracing/GoldenSignalMetricsTracerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public ApiTracer newTracer(ApiTracer parent, SpanName spanName, OperationType op
5757
// regular requests.
5858
return new BaseApiTracer();
5959
}
60-
return new GoldenSignalMetricsTracer(metricsRecorder);
60+
return new GoldenSignalMetricsTracer(metricsRecorder, apiTracerContext);
6161
}
6262

6363
@Override

gax-java/gax/src/test/java/com/google/api/gax/tracing/GoldenSignalMetricsTracerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void setUp() {
6363
OpenTelemetrySdk.builder().setMeterProvider(meterProvider).build();
6464
tracer =
6565
new GoldenSignalMetricsTracer(
66-
new GoldenSignalsMetricsRecorder(openTelemetry, ARTIFACT_NAME));
66+
new GoldenSignalsMetricsRecorder(openTelemetry, ARTIFACT_NAME), ApiTracerContext.empty());
6767
}
6868

6969
@Test

0 commit comments

Comments
 (0)