1/* 2 * 3 * Copyright 2016 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19// Binary metrics_client is a client to retrieve metrics from the server. 20package main 21 22import ( 23 "context" 24 "flag" 25 "fmt" 26 "io" 27 28 "google.golang.org/grpc" 29 "google.golang.org/grpc/grpclog" 30 metricspb "google.golang.org/grpc/stress/grpc_testing" 31) 32 33var ( 34 metricsServerAddress = flag.String("metrics_server_address", "", "The metrics server addresses in the format <hostname>:<port>") 35 totalOnly = flag.Bool("total_only", false, "If true, this prints only the total value of all gauges") 36) 37 38func printMetrics(client metricspb.MetricsServiceClient, totalOnly bool) { 39 stream, err := client.GetAllGauges(context.Background(), &metricspb.EmptyMessage{}) 40 if err != nil { 41 grpclog.Fatalf("failed to call GetAllGauges: %v", err) 42 } 43 44 var ( 45 overallQPS int64 46 rpcStatus error 47 ) 48 for { 49 gaugeResponse, err := stream.Recv() 50 if err != nil { 51 rpcStatus = err 52 break 53 } 54 if _, ok := gaugeResponse.GetValue().(*metricspb.GaugeResponse_LongValue); !ok { 55 panic(fmt.Sprintf("gauge %s is not a long value", gaugeResponse.Name)) 56 } 57 v := gaugeResponse.GetLongValue() 58 if !totalOnly { 59 grpclog.Infof("%s: %d", gaugeResponse.Name, v) 60 } 61 overallQPS += v 62 } 63 if rpcStatus != io.EOF { 64 grpclog.Fatalf("failed to finish server streaming: %v", rpcStatus) 65 } 66 grpclog.Infof("overall qps: %d", overallQPS) 67} 68 69func main() { 70 flag.Parse() 71 if *metricsServerAddress == "" { 72 grpclog.Fatalf("Metrics server address is empty.") 73 } 74 75 conn, err := grpc.Dial(*metricsServerAddress, grpc.WithInsecure()) 76 if err != nil { 77 grpclog.Fatalf("cannot connect to metrics server: %v", err) 78 } 79 defer conn.Close() 80 81 c := metricspb.NewMetricsServiceClient(conn) 82 printMetrics(c, *totalOnly) 83} 84