From 3d5c433da51507376494bd60cd038a92aaa5a2a6 Mon Sep 17 00:00:00 2001
From: Sydney Munro <97561403+sydney-munro@users.noreply.github.com>
Date: Tue, 7 Jan 2025 12:58:13 -0800
Subject: [PATCH 1/5] docs: Create OpenTelemetry Quickstart Sample
---
samples/snippets/pom.xml | 10 ++++
.../QuickstartOpenTelemetrySample.java | 57 +++++++++++++++++++
.../example/storage/QuickstartSampleIT.java | 8 +++
3 files changed, 75 insertions(+)
create mode 100644 samples/snippets/src/main/java/com/example/storage/QuickstartOpenTelemetrySample.java
diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml
index d0faeeadb..b2db54125 100644
--- a/samples/snippets/pom.xml
+++ b/samples/snippets/pom.xml
@@ -79,6 +79,16 @@
1.136.0
test
+
+ com.google.cloud.opentelemetry
+ exporter-trace
+ 0.31.0
+
+
+ com.google.cloud.opentelemetry
+ propagators-gcp
+ 0.33.0-alpha
+
com.google.cloud
diff --git a/samples/snippets/src/main/java/com/example/storage/QuickstartOpenTelemetrySample.java b/samples/snippets/src/main/java/com/example/storage/QuickstartOpenTelemetrySample.java
new file mode 100644
index 000000000..6ddfdb15b
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/storage/QuickstartOpenTelemetrySample.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2024 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.storage;
+
+// [START storage_enable_otel_tracing]
+
+import com.google.cloud.opentelemetry.propagators.XCloudTraceContextPropagator;
+import com.google.cloud.opentelemetry.trace.TraceExporter;
+import com.google.cloud.storage.Storage;
+import com.google.cloud.storage.StorageOptions;
+import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
+import io.opentelemetry.context.propagation.ContextPropagators;
+import io.opentelemetry.context.propagation.TextMapPropagator;
+import io.opentelemetry.sdk.OpenTelemetrySdk;
+import io.opentelemetry.sdk.trace.SdkTracerProvider;
+import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
+import io.opentelemetry.sdk.trace.export.SpanExporter;
+import io.opentelemetry.sdk.trace.samplers.Sampler;
+
+public class QuickstartOpenTelemetrySample {
+ public static void main(String... args) throws Exception {
+ SpanExporter exporter = TraceExporter.createWithDefaultConfiguration();
+ TextMapPropagator propagators = TextMapPropagator.composite(
+ W3CTraceContextPropagator.getInstance(),
+ new XCloudTraceContextPropagator(/*oneway=*/true));
+
+ OpenTelemetrySdk openTelemetry =
+ OpenTelemetrySdk.builder()
+ .setPropagators(ContextPropagators.create(propagators))
+ .setTracerProvider(
+ SdkTracerProvider.builder()
+ // Sample Rate set to always
+ .setSampler(Sampler.alwaysOn())
+ .addSpanProcessor(BatchSpanProcessor.builder(exporter).build())
+ .build())
+ .build();
+ StorageOptions options = StorageOptions.newBuilder().setOpenTelemetry(openTelemetry).build();
+ Storage storage = options.getService();
+ System.out.println("Created an instance of storage with OpenTelemetry configured");
+
+ }
+}
+// [END storage_enable_otel_tracing]
diff --git a/samples/snippets/src/test/java/com/example/storage/QuickstartSampleIT.java b/samples/snippets/src/test/java/com/example/storage/QuickstartSampleIT.java
index 1cb3b2668..65148c31d 100644
--- a/samples/snippets/src/test/java/com/example/storage/QuickstartSampleIT.java
+++ b/samples/snippets/src/test/java/com/example/storage/QuickstartSampleIT.java
@@ -86,4 +86,12 @@ public void testQuickstartStorageControl() throws Exception {
"Performed getStorageLayout request for %s",
StorageLayoutName.format("_", bucketName)));
}
+
+ @Test
+ public void testQuickstartOpenTelemetry() {
+ QuickstartOpenTelemetrySample.main();
+ String got = stdOutCaptureRule.getCapturedOutputAsUtf8String();
+ assertThat(got)
+ .contains("Created an instance of storage with OpenTelemetry configured");
+ }
}
From 2fd4e5dd5f2810512a8f04f28095863bcb6c2850 Mon Sep 17 00:00:00 2001
From: Sydney Munro <97561403+sydney-munro@users.noreply.github.com>
Date: Thu, 9 Jan 2025 10:20:02 -0800
Subject: [PATCH 2/5] move region tag
---
.../com/example/storage/QuickstartOpenTelemetrySample.java | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/samples/snippets/src/main/java/com/example/storage/QuickstartOpenTelemetrySample.java b/samples/snippets/src/main/java/com/example/storage/QuickstartOpenTelemetrySample.java
index 6ddfdb15b..cdc8020e4 100644
--- a/samples/snippets/src/main/java/com/example/storage/QuickstartOpenTelemetrySample.java
+++ b/samples/snippets/src/main/java/com/example/storage/QuickstartOpenTelemetrySample.java
@@ -16,7 +16,7 @@
package com.example.storage;
-// [START storage_enable_otel_tracing]
+
import com.google.cloud.opentelemetry.propagators.XCloudTraceContextPropagator;
import com.google.cloud.opentelemetry.trace.TraceExporter;
@@ -31,6 +31,7 @@
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.opentelemetry.sdk.trace.samplers.Sampler;
+// [START storage_enable_otel_tracing]
public class QuickstartOpenTelemetrySample {
public static void main(String... args) throws Exception {
SpanExporter exporter = TraceExporter.createWithDefaultConfiguration();
From 1c9f1acb7c92078e2598010aca9a609e2328326e Mon Sep 17 00:00:00 2001
From: Sydney Munro <97561403+sydney-munro@users.noreply.github.com>
Date: Thu, 9 Jan 2025 10:37:48 -0800
Subject: [PATCH 3/5] add dependencies
---
samples/install-without-bom/pom.xml | 10 ++++++++++
samples/snapshot/pom.xml | 10 ++++++++++
2 files changed, 20 insertions(+)
diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml
index b1b68ff38..c037077fb 100644
--- a/samples/install-without-bom/pom.xml
+++ b/samples/install-without-bom/pom.xml
@@ -76,6 +76,16 @@
tests
test
+
+ com.google.cloud.opentelemetry
+ exporter-trace
+ 0.31.0
+
+
+ com.google.cloud.opentelemetry
+ propagators-gcp
+ 0.33.0-alpha
+
diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml
index 3aca12e6b..724341fe7 100644
--- a/samples/snapshot/pom.xml
+++ b/samples/snapshot/pom.xml
@@ -68,6 +68,16 @@
tests
test
+
+ com.google.cloud.opentelemetry
+ exporter-trace
+ 0.31.0
+
+
+ com.google.cloud.opentelemetry
+ propagators-gcp
+ 0.33.0-alpha
+
From bf2a31632697002dcc9b18dfebb089306507163b Mon Sep 17 00:00:00 2001
From: Sydney Munro <97561403+sydney-munro@users.noreply.github.com>
Date: Thu, 9 Jan 2025 10:43:23 -0800
Subject: [PATCH 4/5] add exception to test signature
---
.../src/test/java/com/example/storage/QuickstartSampleIT.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/samples/snippets/src/test/java/com/example/storage/QuickstartSampleIT.java b/samples/snippets/src/test/java/com/example/storage/QuickstartSampleIT.java
index 65148c31d..7f2ac4f77 100644
--- a/samples/snippets/src/test/java/com/example/storage/QuickstartSampleIT.java
+++ b/samples/snippets/src/test/java/com/example/storage/QuickstartSampleIT.java
@@ -88,7 +88,7 @@ public void testQuickstartStorageControl() throws Exception {
}
@Test
- public void testQuickstartOpenTelemetry() {
+ public void testQuickstartOpenTelemetry() throws Exception {
QuickstartOpenTelemetrySample.main();
String got = stdOutCaptureRule.getCapturedOutputAsUtf8String();
assertThat(got)
From 59961a9d890a0726b8525ef64fed8f027c5ca8cb Mon Sep 17 00:00:00 2001
From: Sydney Munro <97561403+sydney-munro@users.noreply.github.com>
Date: Fri, 10 Jan 2025 10:43:12 -0800
Subject: [PATCH 5/5] pr comments
---
.../example/storage/QuickstartOpenTelemetrySample.java | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/samples/snippets/src/main/java/com/example/storage/QuickstartOpenTelemetrySample.java b/samples/snippets/src/main/java/com/example/storage/QuickstartOpenTelemetrySample.java
index cdc8020e4..11c425003 100644
--- a/samples/snippets/src/main/java/com/example/storage/QuickstartOpenTelemetrySample.java
+++ b/samples/snippets/src/main/java/com/example/storage/QuickstartOpenTelemetrySample.java
@@ -34,7 +34,7 @@
// [START storage_enable_otel_tracing]
public class QuickstartOpenTelemetrySample {
public static void main(String... args) throws Exception {
- SpanExporter exporter = TraceExporter.createWithDefaultConfiguration();
+ SpanExporter spanExporter = TraceExporter.createWithDefaultConfiguration();
TextMapPropagator propagators = TextMapPropagator.composite(
W3CTraceContextPropagator.getInstance(),
new XCloudTraceContextPropagator(/*oneway=*/true));
@@ -44,9 +44,11 @@ public static void main(String... args) throws Exception {
.setPropagators(ContextPropagators.create(propagators))
.setTracerProvider(
SdkTracerProvider.builder()
- // Sample Rate set to always
+ // Sample Rate is set to alwaysOn
+ // It is recommended to sample based on a ratio for standard use ie.
+ // .setSampler(Sampler.traceIdRatioBased(0.2)) // sample only 20% of trace ids
.setSampler(Sampler.alwaysOn())
- .addSpanProcessor(BatchSpanProcessor.builder(exporter).build())
+ .addSpanProcessor(BatchSpanProcessor.builder(spanExporter).build())
.build())
.build();
StorageOptions options = StorageOptions.newBuilder().setOpenTelemetry(openTelemetry).build();