Skip to content

Commit f91e1ff

Browse files
committed
fix(o11y): Fully implement metrics attributes according to requirements
This commit ensures that all required metrics attributes for Golden Signals are fully implemented in sdk-platform-java. Specifically: - Always populate `rpc.response.status_code` for both gRPC and HTTP transports. - Conditionally populate `http.response.status_code` only for HTTP transport. - Add `error.type` attribute to metrics exclusively on operation failures. - Ensured all other attributes (`gcp.client.service`, `rpc.system.name`, `rpc.method`, `url.domain`, `url.template`, `server.address`, `server.port`) are consistently populated from `ApiTracerContext.getMetricsAttributes()`.
1 parent 4e18407 commit f91e1ff

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/GoldenSignalsMetricsTracer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,23 @@ class GoldenSignalsMetricsTracer implements ApiTracer {
7777
*/
7878
@Override
7979
public void operationSucceeded() {
80-
attributes.put(RPC_RESPONSE_STATUS_ATTRIBUTE, StatusCode.Code.OK.toString());
80+
ObservabilityUtils.populateStatusAttributes(attributes, null, transport);
8181
metricsRecorder.recordOperationLatency(
8282
clientRequestTimer.elapsed(TimeUnit.NANOSECONDS) / 1_000_000_000.0, attributes);
8383
}
8484

8585
@Override
8686
public void operationCancelled() {
87-
attributes.put(RPC_RESPONSE_STATUS_ATTRIBUTE, StatusCode.Code.CANCELLED.toString());
87+
ObservabilityUtils.populateStatusAttributes(
88+
attributes, new java.util.concurrent.CancellationException(), transport);
8889
metricsRecorder.recordOperationLatency(
8990
clientRequestTimer.elapsed(TimeUnit.NANOSECONDS) / 1_000_000_000.0, attributes);
9091
}
9192

9293
@Override
9394
public void operationFailed(Throwable error) {
95+
attributes.put(
96+
ObservabilityAttributes.ERROR_TYPE_ATTRIBUTE, ObservabilityUtils.extractErrorType(error));
9497
ObservabilityUtils.populateStatusAttributes(attributes, error, transport);
9598
metricsRecorder.recordOperationLatency(
9699
clientRequestTimer.elapsed(TimeUnit.NANOSECONDS) / 1_000_000_000.0, attributes);

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/ObservabilityUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,10 @@ static void populateStatusAttributes(
175175
@Nullable Throwable error,
176176
ApiTracerContext.Transport transport) {
177177
StatusCode.Code code = extractStatus(error);
178+
attributes.put(ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE, code.toString());
178179
if (transport == ApiTracerContext.Transport.HTTP) {
179180
attributes.put(
180181
ObservabilityAttributes.HTTP_RESPONSE_STATUS_ATTRIBUTE, (long) code.getHttpStatusCode());
181-
} else {
182-
attributes.put(ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE, code.toString());
183182
}
184183
}
185184

sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/tracing/GoldenSignalsMetricsTracerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ void operationFailed_shouldRecordsOKStatus() {
163163
assertThat(metricData.getHistogramData().getPoints().iterator().next().getAttributes())
164164
.isEqualTo(
165165
Attributes.of(
166+
AttributeKey.stringKey(ObservabilityAttributes.ERROR_TYPE_ATTRIBUTE),
167+
"INTERNAL",
166168
AttributeKey.stringKey(RPC_RESPONSE_STATUS_ATTRIBUTE),
167169
StatusCode.Code.INTERNAL.toString()));
168170
}

0 commit comments

Comments
 (0)