|
| 1 | +/* |
| 2 | + * Copyright 2025 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.otlpmetric; |
| 17 | + |
| 18 | +import com.google.auth.oauth2.GoogleCredentials; |
| 19 | +import io.opentelemetry.api.metrics.LongCounter; |
| 20 | +import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter; |
| 21 | +import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporterBuilder; |
| 22 | +import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter; |
| 23 | +import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporterBuilder; |
| 24 | +import io.opentelemetry.sdk.OpenTelemetrySdk; |
| 25 | +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; |
| 26 | +import io.opentelemetry.sdk.common.CompletableResultCode; |
| 27 | +import io.opentelemetry.sdk.metrics.export.MetricExporter; |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.Random; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | + |
| 32 | +public class OTLPMetricExample { |
| 33 | + private static final String INSTRUMENTATION_SCOPE_NAME = OTLPMetricExample.class.getName(); |
| 34 | + private static final Random RANDOM = new Random(); |
| 35 | + |
| 36 | + private static OpenTelemetrySdk openTelemetrySdk; |
| 37 | + |
| 38 | + private static OpenTelemetrySdk setupMetricExporter() throws IOException { |
| 39 | + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault(); |
| 40 | + |
| 41 | + // Update the SDK configured using environment variables and system properties |
| 42 | + // TODO: Replace this with the use of gcp-auth-extension |
| 43 | + AutoConfiguredOpenTelemetrySdk autoConfOTelSdk = |
| 44 | + AutoConfiguredOpenTelemetrySdk.builder() |
| 45 | + .addMetricExporterCustomizer( |
| 46 | + (exporter, configProperties) -> addAuthorizationHeaders(exporter, credentials)) |
| 47 | + .build(); |
| 48 | + return autoConfOTelSdk.getOpenTelemetrySdk(); |
| 49 | + } |
| 50 | + |
| 51 | + // Modifies the metric exporter initially auto-configured using environment variables |
| 52 | + // Note: This adds static authorization headers which are set only at initialization time. |
| 53 | + // This will stop working after the token expires, since the token is not refreshed. |
| 54 | + private static MetricExporter addAuthorizationHeaders( |
| 55 | + MetricExporter exporter, GoogleCredentials credentials) { |
| 56 | + if (exporter instanceof OtlpHttpMetricExporter) { |
| 57 | + try { |
| 58 | + credentials.refreshIfExpired(); |
| 59 | + OtlpHttpMetricExporterBuilder builder = |
| 60 | + ((OtlpHttpMetricExporter) exporter) |
| 61 | + .toBuilder() |
| 62 | + .addHeader( |
| 63 | + "Authorization", "Bearer " + credentials.getAccessToken().getTokenValue()); |
| 64 | + |
| 65 | + return builder.build(); |
| 66 | + } catch (IOException e) { |
| 67 | + System.out.println("error:" + e.getMessage()); |
| 68 | + } |
| 69 | + } else if (exporter instanceof OtlpGrpcMetricExporter) { |
| 70 | + try { |
| 71 | + credentials.refreshIfExpired(); |
| 72 | + OtlpGrpcMetricExporterBuilder builder = |
| 73 | + ((OtlpGrpcMetricExporter) exporter) |
| 74 | + .toBuilder() |
| 75 | + .addHeader( |
| 76 | + "Authorization", "Bearer " + credentials.getAccessToken().getTokenValue()); |
| 77 | + return builder.build(); |
| 78 | + } catch (IOException e) { |
| 79 | + throw new RuntimeException(e); |
| 80 | + } |
| 81 | + } |
| 82 | + return exporter; |
| 83 | + } |
| 84 | + |
| 85 | + private static void myUseCase() { |
| 86 | + LongCounter counter = |
| 87 | + openTelemetrySdk |
| 88 | + .getMeter(INSTRUMENTATION_SCOPE_NAME) |
| 89 | + .counterBuilder("example_counter") |
| 90 | + .setDescription("Processed jobs") |
| 91 | + .setUnit("1") |
| 92 | + .build(); |
| 93 | + doWork(counter); |
| 94 | + } |
| 95 | + |
| 96 | + private static void doWork(LongCounter counter) { |
| 97 | + try { |
| 98 | + for (int i = 0; i < 10; i++) { |
| 99 | + counter.add(RANDOM.nextInt(100)); |
| 100 | + Thread.sleep(RANDOM.nextInt(1000)); |
| 101 | + } |
| 102 | + } catch (InterruptedException e) { |
| 103 | + throw new RuntimeException(e); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public static void main(String[] args) throws IOException { |
| 108 | + // Configure the OpenTelemetry pipeline with CloudMetric exporter |
| 109 | + openTelemetrySdk = setupMetricExporter(); |
| 110 | + |
| 111 | + // Application-specific logic |
| 112 | + myUseCase(); |
| 113 | + myUseCase(); |
| 114 | + |
| 115 | + // Flush all buffered metrics |
| 116 | + CompletableResultCode completableResultCode = openTelemetrySdk.getSdkMeterProvider().shutdown(); |
| 117 | + // wait till export finishes |
| 118 | + completableResultCode.join(10000, TimeUnit.MILLISECONDS); |
| 119 | + } |
| 120 | +} |
0 commit comments