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 + 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..11c425003 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/QuickstartOpenTelemetrySample.java @@ -0,0 +1,60 @@ +/* + * 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; + + + +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; + +// [START storage_enable_otel_tracing] +public class QuickstartOpenTelemetrySample { + public static void main(String... args) throws Exception { + SpanExporter spanExporter = 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 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(spanExporter).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..7f2ac4f77 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() throws Exception { + QuickstartOpenTelemetrySample.main(); + String got = stdOutCaptureRule.getCapturedOutputAsUtf8String(); + assertThat(got) + .contains("Created an instance of storage with OpenTelemetry configured"); + } }