OpenCensus and AppEngine Flex

Dino Oliva
Jan 11 · 3 min read

try (
// create a new scoped span for calling client libraries
Scope traceScope = OpenCensusTraceUtil
.createSpanBuilder("Work:Get").startScopedSpan()) {
OpenCensusTraceUtil.addAnnotation("Work:Get:Begin");
Storage storage = StorageOptions. ...
BlobId blobId = BlobId.of(BUCKET_NAME, BLOB_NAME);
OpenCensusTraceUtil.addAnnotation("Work:Get:Create");
// Upload a blob.
BlobInfo blobInfo = BlobInfo.newBuilder(blobId)...;
Blob blob =
storage.create(blobInfo, CONTENT_STRING.getBytes(UTF_8));
// Read a blob.
OpenCensusTraceUtil.addAnnotation("Work:Get:Read");
byte[] content = storage.readAllBytes(blobId);
String contentString = new String(content, UTF_8);
if (!contentString.equals(CONTENT_STRING)) {
throw new RuntimeException("Invalid read after upload.");
}
// Delete a blob.
OpenCensusTraceUtil.addAnnotation("Work:Get:Delete");
storage.delete(blobId);
}

Enabling OpenCensus

<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-api</artifactId>
<version>${opencensus.version}</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-impl</artifactId>
<version>${opencensus.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-exporter-trace-stackdriver</artifactId>
<version>${opencensus.version}</version>
</dependency>
// Initialization
static {
if (!PROJECT_ID.isEmpty()) {
try {
// Initialize trace exporter.
StackdriverTraceExporter.createAndRegister(
StackdriverTraceConfiguration
.builder()
.setProjectId(PROJECT_ID)
.build());
} catch (IOException exn) {
if (exn == null) {
System.err.println("Null exn");
}
}
}
}

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade