Repository navigation
feat(storage): add gcp.storage.client.response.body.size metric for read operations #14747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/storage-request-body-size
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -659,4 +659,95 @@ public void recordOperation_doesNotRecordRequestBodySize_whenNotWriteObject() { | |
|
|
||
| assertThat(bodySizeData).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| public void recordOperation_recordsResponseBodySize_whenReadObjectAndBytesGreaterThanZero() { | ||
| io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader reader = | ||
| io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader.create(); | ||
| io.opentelemetry.sdk.metrics.SdkMeterProvider provider = | ||
| io.opentelemetry.sdk.metrics.SdkMeterProvider.builder() | ||
| .registerMetricReader(reader) | ||
| .build(); | ||
| StorageClientMetrics metrics = StorageClientMetrics.create(provider, true, false); | ||
|
|
||
| StorageMetricsContext context = StorageMetricsContext.create("ReadObject"); | ||
| context.setResponseBytes(4096L); | ||
| context.recordOperation(metrics, null); | ||
|
|
||
| java.util.Collection<io.opentelemetry.sdk.metrics.data.MetricData> collected = | ||
| reader.collectAllMetrics(); | ||
| io.opentelemetry.sdk.metrics.data.MetricData bodySizeData = | ||
| collected.stream() | ||
| .filter( | ||
| m -> | ||
| m.getName() | ||
| .equals(StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE)) | ||
| .findFirst() | ||
| .orElse(null); | ||
|
|
||
| assertThat(bodySizeData).isNotNull(); | ||
| assertThat(bodySizeData.getHistogramData().getPoints()).hasSize(1); | ||
| io.opentelemetry.sdk.metrics.data.HistogramPointData point = | ||
| bodySizeData.getHistogramData().getPoints().iterator().next(); | ||
|
Comment on lines
+665
to
+691
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid using fully qualified class names (such as InMemoryMetricReader reader = InMemoryMetricReader.create();
SdkMeterProvider provider =
SdkMeterProvider.builder()
.registerMetricReader(reader)
.build();
StorageClientMetrics metrics = StorageClientMetrics.create(provider, true, false);
StorageMetricsContext context = StorageMetricsContext.create("ReadObject");
context.setResponseBytes(4096L);
context.recordOperation(metrics, null);
Collection<MetricData> collected = reader.collectAllMetrics();
MetricData bodySizeData =
collected.stream()
.filter(
m ->
m.getName()
.equals(StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE))
.findFirst()
.orElse(null);
assertThat(bodySizeData).isNotNull();
assertThat(bodySizeData.getHistogramData().getPoints()).hasSize(1);
HistogramPointData point =
bodySizeData.getHistogramData().getPoints().iterator().next();References
|
||
| assertThat(point.getCount()).isEqualTo(1L); | ||
| assertThat(point.getSum()).isEqualTo(4096.0); | ||
| assertThat(point.getAttributes().get(StorageClientMetrics.KEY_RPC_METHOD)) | ||
| .isEqualTo("ReadObject"); | ||
|
Comment on lines
+665
to
+695
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid using fully qualified class names (such as InMemoryMetricReader reader =
InMemoryMetricReader.create();
SdkMeterProvider provider =
SdkMeterProvider.builder()
.registerMetricReader(reader)
.build();
StorageClientMetrics metrics = StorageClientMetrics.create(provider, true, false);
StorageMetricsContext context = StorageMetricsContext.create("ReadObject");
context.setResponseBytes(4096L);
context.recordOperation(metrics, null);
Collection<MetricData> collected =
reader.collectAllMetrics();
MetricData bodySizeData =
collected.stream()
.filter(
m ->
m.getName()
.equals(StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE))
.findFirst()
.orElse(null);
assertThat(bodySizeData).isNotNull();
assertThat(bodySizeData.getHistogramData().getPoints()).hasSize(1);
HistogramPointData point =
bodySizeData.getHistogramData().getPoints().iterator().next();
assertThat(point.getCount()).isEqualTo(1L);
assertThat(point.getSum()).isEqualTo(4096.0);
assertThat(point.getAttributes().get(StorageClientMetrics.KEY_RPC_METHOD))
.isEqualTo("ReadObject");References
|
||
| } | ||
|
|
||
| @Test | ||
| public void recordOperation_doesNotRecordResponseBodySize_whenBytesIsZero() { | ||
| io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader reader = | ||
| io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader.create(); | ||
| io.opentelemetry.sdk.metrics.SdkMeterProvider provider = | ||
| io.opentelemetry.sdk.metrics.SdkMeterProvider.builder() | ||
| .registerMetricReader(reader) | ||
| .build(); | ||
| StorageClientMetrics metrics = StorageClientMetrics.create(provider, true, false); | ||
|
|
||
| StorageMetricsContext context = StorageMetricsContext.create("ReadObject"); | ||
| context.setResponseBytes(0L); | ||
| context.recordOperation(metrics, null); | ||
|
|
||
| java.util.Collection<io.opentelemetry.sdk.metrics.data.MetricData> collected = | ||
| reader.collectAllMetrics(); | ||
| io.opentelemetry.sdk.metrics.data.MetricData bodySizeData = | ||
| collected.stream() | ||
| .filter( | ||
| m -> | ||
| m.getName() | ||
| .equals(StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE)) | ||
| .findFirst() | ||
| .orElse(null); | ||
|
|
||
| assertThat(bodySizeData).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| public void recordOperation_doesNotRecordResponseBodySize_whenNotReadObject() { | ||
| io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader reader = | ||
| io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader.create(); | ||
| io.opentelemetry.sdk.metrics.SdkMeterProvider provider = | ||
| io.opentelemetry.sdk.metrics.SdkMeterProvider.builder() | ||
| .registerMetricReader(reader) | ||
| .build(); | ||
| StorageClientMetrics metrics = StorageClientMetrics.create(provider, true, false); | ||
|
|
||
| StorageMetricsContext context = StorageMetricsContext.create("WriteObject"); | ||
| context.setResponseBytes(4096L); | ||
| context.recordOperation(metrics, null); | ||
|
|
||
| java.util.Collection<io.opentelemetry.sdk.metrics.data.MetricData> collected = | ||
| reader.collectAllMetrics(); | ||
| io.opentelemetry.sdk.metrics.data.MetricData bodySizeData = | ||
| collected.stream() | ||
| .filter( | ||
| m -> | ||
| m.getName() | ||
| .equals(StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE)) | ||
| .findFirst() | ||
| .orElse(null); | ||
|
|
||
| assertThat(bodySizeData).isNull(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To improve maintainability and readability, avoid duplicating the long chain of
!key.equals(...)checks. Instead, define a private static finalSet<AttributeKey<?>> EXCLUDED_KEYSconstant containing the excluded keys, and use!EXCLUDED_KEYS.contains(key)here.