|
16 | 16 |
|
17 | 17 | package com.google.cloud.spanner;
|
18 | 18 |
|
| 19 | +import static com.google.cloud.spanner.BuiltInMetricsConstant.ATTEMPT_COUNT_NAME; |
19 | 20 | import static com.google.cloud.spanner.BuiltInMetricsConstant.CLIENT_HASH_KEY;
|
20 | 21 | import static com.google.cloud.spanner.BuiltInMetricsConstant.CLIENT_NAME_KEY;
|
21 | 22 | import static com.google.cloud.spanner.BuiltInMetricsConstant.CLIENT_UID_KEY;
|
|
44 | 45 | import com.google.monitoring.v3.TimeSeries;
|
45 | 46 | import com.google.protobuf.Empty;
|
46 | 47 | import io.opentelemetry.api.common.Attributes;
|
| 48 | +import io.opentelemetry.sdk.common.CompletableResultCode; |
47 | 49 | import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
|
48 | 50 | import io.opentelemetry.sdk.metrics.InstrumentType;
|
49 | 51 | import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
|
@@ -340,6 +342,55 @@ public void getAggregationTemporality() throws IOException {
|
340 | 342 | .isEqualTo(AggregationTemporality.CUMULATIVE);
|
341 | 343 | }
|
342 | 344 |
|
| 345 | + @Test |
| 346 | + public void testSkipExportingDataIfMissingInstanceId() throws IOException { |
| 347 | + Attributes attributesWithoutInstanceId = |
| 348 | + Attributes.builder().putAll(attributes).remove(INSTANCE_ID_KEY).build(); |
| 349 | + |
| 350 | + SpannerCloudMonitoringExporter actualExporter = |
| 351 | + SpannerCloudMonitoringExporter.create(projectId, null); |
| 352 | + assertThat(actualExporter.getAggregationTemporality(InstrumentType.COUNTER)) |
| 353 | + .isEqualTo(AggregationTemporality.CUMULATIVE); |
| 354 | + ArgumentCaptor<CreateTimeSeriesRequest> argumentCaptor = |
| 355 | + ArgumentCaptor.forClass(CreateTimeSeriesRequest.class); |
| 356 | + |
| 357 | + UnaryCallable<CreateTimeSeriesRequest, Empty> mockCallable = Mockito.mock(UnaryCallable.class); |
| 358 | + Mockito.when(mockMetricServiceStub.createServiceTimeSeriesCallable()).thenReturn(mockCallable); |
| 359 | + ApiFuture<Empty> future = ApiFutures.immediateFuture(Empty.getDefaultInstance()); |
| 360 | + Mockito.when(mockCallable.futureCall(argumentCaptor.capture())).thenReturn(future); |
| 361 | + |
| 362 | + long fakeValue = 11L; |
| 363 | + |
| 364 | + long startEpoch = 10; |
| 365 | + long endEpoch = 15; |
| 366 | + LongPointData longPointData = |
| 367 | + ImmutableLongPointData.create(startEpoch, endEpoch, attributesWithoutInstanceId, fakeValue); |
| 368 | + |
| 369 | + MetricData operationLongData = |
| 370 | + ImmutableMetricData.createLongSum( |
| 371 | + resource, |
| 372 | + scope, |
| 373 | + "spanner.googleapis.com/internal/client/" + OPERATION_COUNT_NAME, |
| 374 | + "description", |
| 375 | + "1", |
| 376 | + ImmutableSumData.create( |
| 377 | + true, AggregationTemporality.CUMULATIVE, ImmutableList.of(longPointData))); |
| 378 | + |
| 379 | + MetricData attemptLongData = |
| 380 | + ImmutableMetricData.createLongSum( |
| 381 | + resource, |
| 382 | + scope, |
| 383 | + "spanner.googleapis.com/internal/client/" + ATTEMPT_COUNT_NAME, |
| 384 | + "description", |
| 385 | + "1", |
| 386 | + ImmutableSumData.create( |
| 387 | + true, AggregationTemporality.CUMULATIVE, ImmutableList.of(longPointData))); |
| 388 | + |
| 389 | + CompletableResultCode resultCode = |
| 390 | + exporter.export(Arrays.asList(operationLongData, attemptLongData)); |
| 391 | + assertThat(resultCode).isEqualTo(CompletableResultCode.ofFailure()); |
| 392 | + } |
| 393 | + |
343 | 394 | private static class FakeMetricServiceClient extends MetricServiceClient {
|
344 | 395 |
|
345 | 396 | protected FakeMetricServiceClient(MetricServiceStub stub) {
|
|
0 commit comments