Skip to content

Commit c601cca

Browse files
committed
chore: add otel tracing for appendable upload
1 parent c58fe1f commit c601cca

File tree

1 file changed

+69
-3
lines changed

1 file changed

+69
-3
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/OtelStorageDecorator.java

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import java.nio.file.Path;
5959
import java.util.List;
6060
import java.util.Locale;
61+
import java.util.concurrent.ExecutionException;
6162
import java.util.concurrent.TimeUnit;
6263
import java.util.function.UnaryOperator;
6364
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -1505,12 +1506,21 @@ public ApiFuture<BlobReadSession> blobReadSession(BlobId id, BlobSourceOption...
15051506
}
15061507

15071508
@Override
1508-
@TransportCompatibility({Transport.GRPC})
1509-
@BetaApi
15101509
public AppendableBlobUpload appendableBlobUpload(
15111510
BlobInfo blob, AppendableBlobUploadConfig uploadConfig, BlobWriteOption... options)
15121511
throws IOException {
1513-
return delegate.appendableBlobUpload(blob, uploadConfig, options);
1512+
Span span = tracer.spanBuilder("appendableBlobUpload").startSpan();
1513+
try (Scope ignore = span.makeCurrent()) {
1514+
1515+
return new OtelDecoratingAppendableBlobUpload(
1516+
delegate.appendableBlobUpload(blob, uploadConfig, options), span, Context.current());
1517+
1518+
} catch (Throwable t) {
1519+
span.recordException(t);
1520+
span.setStatus(StatusCode.ERROR, t.getClass().getSimpleName());
1521+
span.end();
1522+
throw t;
1523+
}
15141524
}
15151525

15161526
@Override
@@ -2092,4 +2102,60 @@ public String toString() {
20922102
.toString();
20932103
}
20942104
}
2105+
2106+
final class OtelDecoratingAppendableBlobUpload implements AppendableBlobUpload {
2107+
private final AppendableBlobUpload delegate;
2108+
private final Span openSpan;
2109+
private final Context openContext;
2110+
2111+
private OtelDecoratingAppendableBlobUpload(
2112+
AppendableBlobUpload delegate, Span openSpan, Context openContext) {
2113+
this.delegate = delegate;
2114+
this.openSpan = openSpan;
2115+
this.openContext = openContext;
2116+
}
2117+
2118+
@Override
2119+
public int write(ByteBuffer src) throws IOException {
2120+
return delegate.write(src);
2121+
}
2122+
2123+
@Override
2124+
public void close() throws IOException {
2125+
try {
2126+
delegate.close();
2127+
} catch (Throwable t) {
2128+
openSpan.recordException(t);
2129+
openSpan.setStatus(StatusCode.ERROR, t.getClass().getSimpleName());
2130+
throw t;
2131+
} finally {
2132+
openSpan.end();
2133+
}
2134+
}
2135+
2136+
@Override
2137+
@BetaApi
2138+
public BlobInfo finalizeUpload() throws IOException, ExecutionException, InterruptedException {
2139+
Span span =
2140+
tracer
2141+
.spanBuilder("appendableBlobUpload/finalizeUpload")
2142+
.setParent(openContext)
2143+
.startSpan();
2144+
try (Scope ignore = span.makeCurrent()) {
2145+
return delegate.finalizeUpload();
2146+
} catch (Throwable t) {
2147+
span.recordException(t);
2148+
span.setStatus(StatusCode.ERROR, t.getClass().getSimpleName());
2149+
throw t;
2150+
} finally {
2151+
span.end();
2152+
openSpan.end();
2153+
}
2154+
}
2155+
2156+
@Override
2157+
public boolean isOpen() {
2158+
return delegate.isOpen();
2159+
}
2160+
}
20952161
}

0 commit comments

Comments
 (0)