1 /* 2 * Copyright 2023 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.gcpobservability; 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.StatusRuntimeException; 24 import io.grpc.examples.helloworld.GreeterGrpc; 25 import io.grpc.examples.helloworld.HelloReply; 26 import io.grpc.examples.helloworld.HelloRequest; 27 import io.grpc.gcp.observability.GcpObservability; 28 import java.util.concurrent.TimeUnit; 29 import java.util.logging.Level; 30 import java.util.logging.Logger; 31 32 /** 33 * A simple observability client that requests a greeting from the {@link HelloWorldServer} and 34 * generates logs, metrics and traces based on the configuration. 35 */ 36 public class GcpObservabilityClient { 37 private static final Logger logger = Logger.getLogger(GcpObservabilityClient.class.getName()); 38 39 private final GreeterGrpc.GreeterBlockingStub blockingStub; 40 41 /** Construct client for accessing HelloWorld server using the existing channel. */ GcpObservabilityClient(Channel channel)42 public GcpObservabilityClient(Channel channel) { 43 blockingStub = GreeterGrpc.newBlockingStub(channel); 44 } 45 46 /** Say hello to server. */ greet(String name)47 public void greet(String name) { 48 logger.info("Will try to greet " + name + " ..."); 49 HelloRequest request = HelloRequest.newBuilder().setName(name).build(); 50 HelloReply response; 51 try { 52 response = blockingStub.sayHello(request); 53 } catch (StatusRuntimeException e) { 54 logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); 55 return; 56 } 57 logger.info("Greeting: " + response.getMessage()); 58 } 59 60 /** 61 * Greet server. If provided, the first element of {@code args} is the name to use in the 62 * greeting. The second argument is the target server. 63 */ main(String[] args)64 public static void main(String[] args) throws Exception { 65 String user = "world"; 66 String target = "localhost:50051"; 67 if (args.length > 0) { 68 if ("--help".equals(args[0])) { 69 System.err.println("Usage: [name [target]]"); 70 System.err.println(""); 71 System.err.println(" name The name you wish to be greeted by. Defaults to " + user); 72 System.err.println(" target The server to connect to. Defaults to " + target); 73 System.exit(1); 74 } 75 user = args[0]; 76 } 77 if (args.length > 1) { 78 target = args[1]; 79 } 80 81 // Initialize observability 82 try (GcpObservability observability = GcpObservability.grpcInit()) { 83 ManagedChannel channel = Grpc.newChannelBuilder(target, InsecureChannelCredentials.create()) 84 .build(); 85 try { 86 GcpObservabilityClient client = new GcpObservabilityClient(channel); 87 client.greet(user); 88 } finally { 89 channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); 90 } 91 } // observability.close() called implicitly 92 } 93 } 94