|
| 1 | +/* |
| 2 | + * Copyright 2024 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.examples.csmobservability; |
| 18 | + |
| 19 | +import io.grpc.Channel; |
| 20 | +import io.grpc.Grpc; |
| 21 | +import io.grpc.InsecureChannelCredentials; |
| 22 | +import io.grpc.ManagedChannel; |
| 23 | +import io.grpc.ManagedChannelBuilder; |
| 24 | +import io.grpc.StatusRuntimeException; |
| 25 | +import io.grpc.examples.helloworld.GreeterGrpc; |
| 26 | +import io.grpc.examples.helloworld.HelloReply; |
| 27 | +import io.grpc.examples.helloworld.HelloRequest; |
| 28 | +import io.grpc.gcp.csm.observability.CsmObservability; |
| 29 | +import io.opentelemetry.exporter.logging.LoggingMetricExporter; |
| 30 | +import io.opentelemetry.exporter.prometheus.PrometheusHttpServer; |
| 31 | +import io.opentelemetry.sdk.OpenTelemetrySdk; |
| 32 | +import io.opentelemetry.sdk.metrics.SdkMeterProvider; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 35 | +import java.util.logging.Level; |
| 36 | +import java.util.logging.Logger; |
| 37 | + |
| 38 | +/** |
| 39 | + * A simple CSM observability client that requests a greeting from the {@link HelloWorldServer} and |
| 40 | + * generates CSM telemetry data based on the configuration. |
| 41 | + */ |
| 42 | +public class CsmObservabilityClient { |
| 43 | + private static final Logger logger = Logger.getLogger(CsmObservabilityClient.class.getName()); |
| 44 | + |
| 45 | + private final GreeterGrpc.GreeterBlockingStub blockingStub; |
| 46 | + |
| 47 | + /** Construct client for accessing HelloWorld server using the existing channel. */ |
| 48 | + public CsmObservabilityClient(Channel channel) { |
| 49 | + blockingStub = GreeterGrpc.newBlockingStub(channel); |
| 50 | + } |
| 51 | + |
| 52 | + /** Say hello to server. */ |
| 53 | + public void greet(String name) { |
| 54 | + logger.info("Will try to greet " + name + " ..."); |
| 55 | + HelloRequest request = HelloRequest.newBuilder().setName(name).build(); |
| 56 | + HelloReply response; |
| 57 | + try { |
| 58 | + response = blockingStub.sayHello(request); |
| 59 | + } catch (StatusRuntimeException e) { |
| 60 | + logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); |
| 61 | + return; |
| 62 | + } |
| 63 | + logger.info("Greeting: " + response.getMessage()); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Greet server. If provided, the first element of {@code args} is the name to use in the |
| 68 | + * greeting. The second argument is the target server. |
| 69 | + */ |
| 70 | + public static void main(String[] args) throws Exception { |
| 71 | + String user = "world"; |
| 72 | + String target = "xds:///helloworld:50051"; |
| 73 | + int prometheusPort = 9464; |
| 74 | + AtomicBoolean sendRpcs = new AtomicBoolean(true); |
| 75 | + if (args.length > 0) { |
| 76 | + if ("--help".equals(args[0])) { |
| 77 | + System.err.println("Usage: [name [target [prometheusPort]]]"); |
| 78 | + System.err.println(""); |
| 79 | + System.err.println(" name The name you wish to be greeted by. Defaults to " + user); |
| 80 | + System.err.println(" target The server to connect to. Defaults to " + target); |
| 81 | + System.err.println(" prometheusPort The port to expose prometheus metrics. Defaults to " + prometheusPort); |
| 82 | + System.exit(1); |
| 83 | + } |
| 84 | + user = args[0]; |
| 85 | + } |
| 86 | + if (args.length > 1) { |
| 87 | + target = args[1]; |
| 88 | + } |
| 89 | + if (args.length > 2) { |
| 90 | + prometheusPort = Integer.parseInt(args[2]); |
| 91 | + } |
| 92 | + |
| 93 | + Thread mainThread = Thread.currentThread(); |
| 94 | + |
| 95 | + Runtime.getRuntime().addShutdownHook(new Thread() { |
| 96 | + @Override |
| 97 | + public void run() { |
| 98 | + // Use stderr here since the logger may have been reset by its JVM shutdown hook. |
| 99 | + System.err.println("*** shutting down gRPC client since JVM is shutting down"); |
| 100 | + |
| 101 | + sendRpcs.set(false); |
| 102 | + try { |
| 103 | + mainThread.join(); |
| 104 | + } catch (InterruptedException e) { |
| 105 | + e.printStackTrace(System.err); |
| 106 | + } |
| 107 | + System.err.println("*** client shut down"); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + // Adds a PrometheusHttpServer to convert OpenTelemetry metrics to Prometheus format and |
| 112 | + // expose these via a HttpServer exporter to the SdkMeterProvider. |
| 113 | + SdkMeterProvider sdkMeterProvider = SdkMeterProvider.builder() |
| 114 | + .registerMetricReader( |
| 115 | + PrometheusHttpServer.builder().setPort(prometheusPort).build()) |
| 116 | + .build(); |
| 117 | + |
| 118 | + // Initialize OpenTelemetry SDK with MeterProvider configured with Prometeheus. |
| 119 | + OpenTelemetrySdk openTelemetrySdk = |
| 120 | + OpenTelemetrySdk.builder().setMeterProvider(sdkMeterProvider).build(); |
| 121 | + |
| 122 | + // Initialize CSM Observability. |
| 123 | + CsmObservability observability = CsmObservability.newBuilder() |
| 124 | + .sdk(openTelemetrySdk) |
| 125 | + .build(); |
| 126 | + // Registers CSM observabiity globally. |
| 127 | + observability.registerGlobal(); |
| 128 | + |
| 129 | + // Create a communication channel to the server, known as a Channel. |
| 130 | + ManagedChannel channel = Grpc.newChannelBuilder(target, InsecureChannelCredentials.create()) |
| 131 | + .build(); |
| 132 | + CsmObservabilityClient client = new CsmObservabilityClient(channel); |
| 133 | + |
| 134 | + try { |
| 135 | + // Run RPCs every second. |
| 136 | + while (sendRpcs.get()) { |
| 137 | + client.greet(user); |
| 138 | + // Sleep for a bit before sending the next RPC. |
| 139 | + Thread.sleep(1000); |
| 140 | + } |
| 141 | + } finally { |
| 142 | + channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); |
| 143 | + // Shut down CSM Observability. |
| 144 | + observability.close(); |
| 145 | + // Shut down OpenTelemetry SDK. |
| 146 | + openTelemetrySdk.close(); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments