|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.opentelemetry.example.otlptrace; |
| 17 | + |
| 18 | +import com.google.auth.oauth2.GoogleCredentials; |
| 19 | +import io.opentelemetry.api.trace.Span; |
| 20 | +import io.opentelemetry.context.Scope; |
| 21 | +import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter; |
| 22 | +import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder; |
| 23 | +import io.opentelemetry.sdk.OpenTelemetrySdk; |
| 24 | +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; |
| 25 | +import io.opentelemetry.sdk.common.CompletableResultCode; |
| 26 | +import io.opentelemetry.sdk.trace.export.SpanExporter; |
| 27 | +import java.io.*; |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.Random; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | + |
| 32 | +public class OTLPTraceExample { |
| 33 | + private static final String INSTRUMENTATION_SCOPE_NAME = OTLPTraceExample.class.getName(); |
| 34 | + private static final Random random = new Random(); |
| 35 | + |
| 36 | + private static OpenTelemetrySdk openTelemetrySdk; |
| 37 | + |
| 38 | + private static OpenTelemetrySdk setupTraceExporter() throws IOException { |
| 39 | + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault(); |
| 40 | + |
| 41 | + // Update the SDK configured using environment variables and system properties |
| 42 | + AutoConfiguredOpenTelemetrySdk autoConfOTelSdk = |
| 43 | + AutoConfiguredOpenTelemetrySdk.builder() |
| 44 | + .addSpanExporterCustomizer( |
| 45 | + (exporter, configProperties) -> addAuthorizationHeaders(exporter, credentials)) |
| 46 | + .build(); |
| 47 | + return autoConfOTelSdk.getOpenTelemetrySdk(); |
| 48 | + } |
| 49 | + |
| 50 | + // Modifies the span exporter initially auto-configured using environment variables |
| 51 | + private static SpanExporter addAuthorizationHeaders( |
| 52 | + SpanExporter exporter, GoogleCredentials credentials) { |
| 53 | + if (exporter instanceof OtlpHttpSpanExporter) { |
| 54 | + try { |
| 55 | + credentials.refreshIfExpired(); |
| 56 | + OtlpHttpSpanExporterBuilder builder = |
| 57 | + ((OtlpHttpSpanExporter) exporter) |
| 58 | + .toBuilder() |
| 59 | + .addHeader( |
| 60 | + "Authorization", "Bearer " + credentials.getAccessToken().getTokenValue()); |
| 61 | + |
| 62 | + return builder.build(); |
| 63 | + } catch (IOException e) { |
| 64 | + System.out.println("error:" + e.getMessage()); |
| 65 | + } |
| 66 | + } |
| 67 | + return exporter; |
| 68 | + } |
| 69 | + |
| 70 | + private static void myUseCase(String description) { |
| 71 | + // Generate a span |
| 72 | + Span span = |
| 73 | + openTelemetrySdk.getTracer(INSTRUMENTATION_SCOPE_NAME).spanBuilder(description).startSpan(); |
| 74 | + try (Scope scope = span.makeCurrent()) { |
| 75 | + span.addEvent("Event A"); |
| 76 | + // Do some work for the use case |
| 77 | + for (int i = 0; i < 3; i++) { |
| 78 | + String work = String.format("%s - Work #%d", description, (i + 1)); |
| 79 | + doWork(work); |
| 80 | + } |
| 81 | + |
| 82 | + span.addEvent("Event B"); |
| 83 | + } finally { |
| 84 | + span.end(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private static void doWork(String description) { |
| 89 | + // Child span |
| 90 | + Span span = |
| 91 | + openTelemetrySdk.getTracer(INSTRUMENTATION_SCOPE_NAME).spanBuilder(description).startSpan(); |
| 92 | + try (Scope scope = span.makeCurrent()) { |
| 93 | + // Simulate work: this could be simulating a network request or an expensive disk operation |
| 94 | + Thread.sleep(100 + random.nextInt(5) * 100); |
| 95 | + } catch (InterruptedException e) { |
| 96 | + throw new RuntimeException(e); |
| 97 | + } finally { |
| 98 | + span.end(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public static void main(String[] args) throws IOException { |
| 103 | + // Configure the OpenTelemetry pipeline with CloudTrace exporter |
| 104 | + openTelemetrySdk = setupTraceExporter(); |
| 105 | + |
| 106 | + // Application-specific logic |
| 107 | + myUseCase("One"); |
| 108 | + myUseCase("Two"); |
| 109 | + |
| 110 | + // Flush all buffered traces |
| 111 | + CompletableResultCode completableResultCode = |
| 112 | + openTelemetrySdk.getSdkTracerProvider().shutdown(); |
| 113 | + // wait till export finishes |
| 114 | + completableResultCode.join(10000, TimeUnit.MILLISECONDS); |
| 115 | + } |
| 116 | +} |
0 commit comments