|
| 1 | +/* |
| 2 | + * Copyright 2026 The gRPC Authors |
| 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 | + |
| 17 | +package io.grpc.testing.integration; |
| 18 | + |
| 19 | +import io.grpc.opentelemetry.GrpcOpenTelemetry; |
| 20 | +import io.grpc.opentelemetry.InternalGrpcOpenTelemetry; |
| 21 | +import io.opentelemetry.sdk.OpenTelemetrySdk; |
| 22 | +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; |
| 23 | +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.Map; |
| 26 | +import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement; |
| 27 | + |
| 28 | +/** |
| 29 | + * Utility methods for OpenTelemetry configuration in integration testing. |
| 30 | + */ |
| 31 | +public final class OpenTelemetryUtil { |
| 32 | + |
| 33 | + private OpenTelemetryUtil() {} |
| 34 | + |
| 35 | + /** |
| 36 | + * Initializes and registers OpenTelemetry tracing for interop client and server. |
| 37 | + * |
| 38 | + * @param otelCollectorAddress optional collector address (e.g. "localhost:4317") |
| 39 | + * @return the configured {@link OpenTelemetrySdk} |
| 40 | + */ |
| 41 | + @IgnoreJRERequirement // OpenTelemetry uses Java 8+ APIs |
| 42 | + public static OpenTelemetrySdk setupOpenTelemetry(String otelCollectorAddress) { |
| 43 | + AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = |
| 44 | + AutoConfiguredOpenTelemetrySdk.builder(); |
| 45 | + Map<String, String> properties = new HashMap<>(); |
| 46 | + properties.put("otel.traces.exporter", "otlp"); |
| 47 | + properties.put("otel.exporter.otlp.protocol", "grpc"); |
| 48 | + // Reduce BatchSpanProcessor export delay from default 5000ms to 100ms for fast test runs. |
| 49 | + properties.put("otel.bsp.schedule.delay", "100"); |
| 50 | + if (otelCollectorAddress != null && !otelCollectorAddress.isEmpty()) { |
| 51 | + String endpoint = otelCollectorAddress; |
| 52 | + if (endpoint.startsWith("https://")) { |
| 53 | + endpoint = "http://" + endpoint.substring(8); |
| 54 | + } else if (!endpoint.startsWith("http://")) { |
| 55 | + endpoint = "http://" + endpoint; |
| 56 | + } |
| 57 | + properties.put("otel.exporter.otlp.endpoint", endpoint); |
| 58 | + } |
| 59 | + sdkBuilder.addPropertiesSupplier(() -> properties); |
| 60 | + AutoConfiguredOpenTelemetrySdk autoSdk = sdkBuilder.build(); |
| 61 | + OpenTelemetrySdk openTelemetrySdk = autoSdk.getOpenTelemetrySdk(); |
| 62 | + GrpcOpenTelemetry.Builder grpcOpentelemetryBuilder = GrpcOpenTelemetry.newBuilder() |
| 63 | + .sdk(openTelemetrySdk); |
| 64 | + InternalGrpcOpenTelemetry.enableTracing(grpcOpentelemetryBuilder, true); |
| 65 | + GrpcOpenTelemetry grpcOpenTelemetry = grpcOpentelemetryBuilder.build(); |
| 66 | + grpcOpenTelemetry.registerGlobal(); |
| 67 | + return openTelemetrySdk; |
| 68 | + } |
| 69 | +} |
0 commit comments