Skip to content

Commit 33bb5f5

Browse files
committed
test(o11y): add tests to verify error.type extraction in metrics tracer
Added additional test cases to GoldenSignalsMetricsTracerTest to ensure that ObservabilityUtils.extractErrorType is correctly translating various exceptions (like CancellationException, SocketTimeoutException, and IllegalArgumentException) into the appropriate error.type attributes during failure reporting.
1 parent f91e1ff commit 33bb5f5

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,62 @@ void operationFailed_shouldRecordsOKStatus() {
168168
AttributeKey.stringKey(RPC_RESPONSE_STATUS_ATTRIBUTE),
169169
StatusCode.Code.INTERNAL.toString()));
170170
}
171+
172+
@Test
173+
void operationFailed_shouldRecordCancellationException() {
174+
java.util.concurrent.CancellationException error =
175+
new java.util.concurrent.CancellationException("test cancellation");
176+
tracer.operationFailed(error);
177+
178+
Collection<MetricData> metrics = metricReader.collectAllMetrics();
179+
assertThat(metrics).hasSize(1);
180+
MetricData metricData = metrics.iterator().next();
181+
182+
assertThat(metricData.getHistogramData().getPoints()).hasSize(1);
183+
assertThat(metricData.getHistogramData().getPoints().iterator().next().getAttributes())
184+
.isEqualTo(
185+
Attributes.of(
186+
AttributeKey.stringKey(ObservabilityAttributes.ERROR_TYPE_ATTRIBUTE),
187+
"CancellationException",
188+
AttributeKey.stringKey(RPC_RESPONSE_STATUS_ATTRIBUTE),
189+
StatusCode.Code.CANCELLED.toString()));
190+
}
191+
192+
@Test
193+
void operationFailed_shouldRecordClientTimeout() {
194+
java.net.SocketTimeoutException error = new java.net.SocketTimeoutException("test timeout");
195+
tracer.operationFailed(error);
196+
197+
Collection<MetricData> metrics = metricReader.collectAllMetrics();
198+
assertThat(metrics).hasSize(1);
199+
MetricData metricData = metrics.iterator().next();
200+
201+
assertThat(metricData.getHistogramData().getPoints()).hasSize(1);
202+
assertThat(metricData.getHistogramData().getPoints().iterator().next().getAttributes())
203+
.isEqualTo(
204+
Attributes.of(
205+
AttributeKey.stringKey(ObservabilityAttributes.ERROR_TYPE_ATTRIBUTE),
206+
"CLIENT_TIMEOUT",
207+
AttributeKey.stringKey(RPC_RESPONSE_STATUS_ATTRIBUTE),
208+
StatusCode.Code.UNKNOWN.toString()));
209+
}
210+
211+
@Test
212+
void operationFailed_shouldRecordClientRequestError() {
213+
IllegalArgumentException error = new IllegalArgumentException("test illegal argument");
214+
tracer.operationFailed(error);
215+
216+
Collection<MetricData> metrics = metricReader.collectAllMetrics();
217+
assertThat(metrics).hasSize(1);
218+
MetricData metricData = metrics.iterator().next();
219+
220+
assertThat(metricData.getHistogramData().getPoints()).hasSize(1);
221+
assertThat(metricData.getHistogramData().getPoints().iterator().next().getAttributes())
222+
.isEqualTo(
223+
Attributes.of(
224+
AttributeKey.stringKey(ObservabilityAttributes.ERROR_TYPE_ATTRIBUTE),
225+
"CLIENT_REQUEST_ERROR",
226+
AttributeKey.stringKey(RPC_RESPONSE_STATUS_ATTRIBUTE),
227+
StatusCode.Code.UNKNOWN.toString()));
228+
}
171229
}

0 commit comments

Comments
 (0)