Skip to content

Commit

Permalink
Instrument HTTP readAllBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
sydney-munro committed Nov 11, 2024
1 parent dc5a734 commit 0249923
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -791,9 +791,11 @@ public StorageObject compose(

@Override
public byte[] load(StorageObject from, Map<Option, ?> 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()
Expand All @@ -812,9 +814,12 @@ public byte[] load(StorageObject from, Map<Option, ?> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> spanData = testExported.getExportedSpans();
checkCommonAttributes(spanData);
Assert.assertTrue(spanData.stream().anyMatch(x -> x.getName().contains("load")));

}

private void checkCommonAttributes(List<SpanData> spanData) {
for (SpanData span : spanData) {
Assert.assertEquals("Storage", getAttributeValue(span, "gcp.client.service"));
Expand Down

0 comments on commit 0249923

Please sign in to comment.