Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Create OpenTelemetry Quickstart Sample #2861

Merged
merged 5 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions samples/install-without-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud.opentelemetry</groupId>
<artifactId>exporter-trace</artifactId>
<version>0.31.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud.opentelemetry</groupId>
<artifactId>propagators-gcp</artifactId>
<version>0.33.0-alpha</version>
</dependency>
</dependencies>

<!-- compile and run all snippet tests -->
Expand Down
10 changes: 10 additions & 0 deletions samples/snapshot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud.opentelemetry</groupId>
<artifactId>exporter-trace</artifactId>
<version>0.31.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud.opentelemetry</groupId>
<artifactId>propagators-gcp</artifactId>
<version>0.33.0-alpha</version>
</dependency>
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
</dependencies>

<!-- compile and run all snippet tests -->
Expand Down
10 changes: 10 additions & 0 deletions samples/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@
<version>1.136.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud.opentelemetry</groupId>
<artifactId>exporter-trace</artifactId>
<version>0.31.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud.opentelemetry</groupId>
<artifactId>propagators-gcp</artifactId>
<version>0.33.0-alpha</version>
</dependency>
<dependency>
<!-- tests jars aren't in the bom, manually include the version here -->
<groupId>com.google.cloud</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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())
sydney-munro marked this conversation as resolved.
Show resolved Hide resolved
.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]
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Loading