diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java index 45366e5e1..05fce30f9 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java @@ -791,9 +791,11 @@ public StorageObject compose( @Override public byte[] load(StorageObject from, Map options) { + OpenTelemetryTraceUtil.Span otelSpan = + openTelemetryTraceUtil.startSpan("load", this.getClass().getName()); Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_LOAD); Scope scope = tracer.withSpan(span); - try { + try (OpenTelemetryTraceUtil.Scope unused = otelSpan.makeCurrent()) { Storage.Objects.Get getRequest = storage .objects() @@ -812,9 +814,12 @@ public byte[] load(StorageObject from, Map options) { getRequest.executeMedia().download(out); return out.toByteArray(); } catch (IOException ex) { + otelSpan.recordException(ex); + otelSpan.setStatus(StatusCode.ERROR, ex.getClass().getSimpleName()); span.setStatus(Status.UNKNOWN.withDescription(ex.getMessage())); throw translate(ex); } finally { + otelSpan.end(); scope.close(); span.end(HttpStorageRpcSpans.END_SPAN_OPTIONS); } diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/ITHttpOpenTelemetryTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/ITHttpOpenTelemetryTest.java index afe788444..84a3e8301 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/ITHttpOpenTelemetryTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/ITHttpOpenTelemetryTest.java @@ -141,6 +141,19 @@ public void runCopy() { Assert.assertTrue(spanData.stream().anyMatch(x -> x.getName().contains("rewrite"))); } + @Test + public void runReadAllBytes() { + BlobInfo blobInfo = + BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); + storage.create(blobInfo, helloWorldTextBytes); + byte[] read = storage.readAllBytes(blobId); + TestExporter testExported = (TestExporter) exporter; + List spanData = testExported.getExportedSpans(); + checkCommonAttributes(spanData); + Assert.assertTrue(spanData.stream().anyMatch(x -> x.getName().contains("load"))); + + } + private void checkCommonAttributes(List spanData) { for (SpanData span : spanData) { Assert.assertEquals("Storage", getAttributeValue(span, "gcp.client.service"));