...

Source file src/google.golang.org/grpc/interop/stress/metrics_client/main.go

Documentation: google.golang.org/grpc/interop/stress/metrics_client

     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.
    20  package main
    21  
    22  import (
    23  	"context"
    24  	"flag"
    25  	"fmt"
    26  	"io"
    27  
    28  	"google.golang.org/grpc"
    29  	"google.golang.org/grpc/credentials/insecure"
    30  	"google.golang.org/grpc/grpclog"
    31  	metricspb "google.golang.org/grpc/interop/stress/grpc_testing"
    32  )
    33  
    34  var (
    35  	metricsServerAddress = flag.String("metrics_server_address", "", "The metrics server addresses in the format <hostname>:<port>")
    36  	totalOnly            = flag.Bool("total_only", false, "If true, this prints only the total value of all gauges")
    37  
    38  	logger = grpclog.Component("stress")
    39  )
    40  
    41  func printMetrics(client metricspb.MetricsServiceClient, totalOnly bool) {
    42  	stream, err := client.GetAllGauges(context.Background(), &metricspb.EmptyMessage{})
    43  	if err != nil {
    44  		logger.Fatalf("failed to call GetAllGauges: %v", err)
    45  	}
    46  
    47  	var (
    48  		overallQPS int64
    49  		rpcStatus  error
    50  	)
    51  	for {
    52  		gaugeResponse, err := stream.Recv()
    53  		if err != nil {
    54  			rpcStatus = err
    55  			break
    56  		}
    57  		if _, ok := gaugeResponse.GetValue().(*metricspb.GaugeResponse_LongValue); !ok {
    58  			panic(fmt.Sprintf("gauge %s is not a long value", gaugeResponse.Name))
    59  		}
    60  		v := gaugeResponse.GetLongValue()
    61  		if !totalOnly {
    62  			logger.Infof("%s: %d", gaugeResponse.Name, v)
    63  		}
    64  		overallQPS += v
    65  	}
    66  	if rpcStatus != io.EOF {
    67  		logger.Fatalf("failed to finish server streaming: %v", rpcStatus)
    68  	}
    69  	logger.Infof("overall qps: %d", overallQPS)
    70  }
    71  
    72  func main() {
    73  	flag.Parse()
    74  	if *metricsServerAddress == "" {
    75  		logger.Fatal("-metrics_server_address is unset")
    76  	}
    77  
    78  	conn, err := grpc.Dial(*metricsServerAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
    79  	if err != nil {
    80  		logger.Fatalf("cannot connect to metrics server: %v", err)
    81  	}
    82  	defer conn.Close()
    83  
    84  	c := metricspb.NewMetricsServiceClient(conn)
    85  	printMetrics(c, *totalOnly)
    86  }
    87  

View as plain text