Skip to content

Commit d7243f4

Browse files
authored
feat: Instrument HTTP readAllBytes (#2812)
* feat: instrument HTTP readAllBytes
1 parent dc5a734 commit d7243f4

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,11 @@ public StorageObject compose(
791791

792792
@Override
793793
public byte[] load(StorageObject from, Map<Option, ?> options) {
794+
OpenTelemetryTraceUtil.Span otelSpan =
795+
openTelemetryTraceUtil.startSpan("load", this.getClass().getName());
794796
Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_LOAD);
795797
Scope scope = tracer.withSpan(span);
796-
try {
798+
try (OpenTelemetryTraceUtil.Scope unused = otelSpan.makeCurrent()) {
797799
Storage.Objects.Get getRequest =
798800
storage
799801
.objects()
@@ -812,9 +814,12 @@ public byte[] load(StorageObject from, Map<Option, ?> options) {
812814
getRequest.executeMedia().download(out);
813815
return out.toByteArray();
814816
} catch (IOException ex) {
817+
otelSpan.recordException(ex);
818+
otelSpan.setStatus(StatusCode.ERROR, ex.getClass().getSimpleName());
815819
span.setStatus(Status.UNKNOWN.withDescription(ex.getMessage()));
816820
throw translate(ex);
817821
} finally {
822+
otelSpan.end();
818823
scope.close();
819824
span.end(HttpStorageRpcSpans.END_SPAN_OPTIONS);
820825
}

google-cloud-storage/src/test/java/com/google/cloud/storage/ITHttpOpenTelemetryTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,17 @@ public void runCopy() {
141141
Assert.assertTrue(spanData.stream().anyMatch(x -> x.getName().contains("rewrite")));
142142
}
143143

144+
@Test
145+
public void runReadAllBytes() {
146+
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
147+
storage.create(blobInfo, helloWorldTextBytes);
148+
byte[] read = storage.readAllBytes(blobId);
149+
TestExporter testExported = (TestExporter) exporter;
150+
List<SpanData> spanData = testExported.getExportedSpans();
151+
checkCommonAttributes(spanData);
152+
Assert.assertTrue(spanData.stream().anyMatch(x -> x.getName().contains("load")));
153+
}
154+
144155
private void checkCommonAttributes(List<SpanData> spanData) {
145156
for (SpanData span : spanData) {
146157
Assert.assertEquals("Storage", getAttributeValue(span, "gcp.client.service"));

0 commit comments

Comments
 (0)