github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/ingester/client/client.go (about)

     1  package client
     2  
     3  import (
     4  	"flag"
     5  
     6  	"github.com/go-kit/log"
     7  	"github.com/grafana/dskit/grpcclient"
     8  	"github.com/prometheus/client_golang/prometheus"
     9  	"github.com/prometheus/client_golang/prometheus/promauto"
    10  	"google.golang.org/grpc"
    11  	"google.golang.org/grpc/health/grpc_health_v1"
    12  )
    13  
    14  var ingesterClientRequestDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
    15  	Namespace: "cortex",
    16  	Name:      "ingester_client_request_duration_seconds",
    17  	Help:      "Time spent doing Ingester requests.",
    18  	Buckets:   prometheus.ExponentialBuckets(0.001, 4, 6),
    19  }, []string{"operation", "status_code"})
    20  
    21  // HealthAndIngesterClient is the union of IngesterClient and grpc_health_v1.HealthClient.
    22  type HealthAndIngesterClient interface {
    23  	IngesterClient
    24  	grpc_health_v1.HealthClient
    25  	Close() error
    26  }
    27  
    28  type closableHealthAndIngesterClient struct {
    29  	IngesterClient
    30  	grpc_health_v1.HealthClient
    31  	conn *grpc.ClientConn
    32  }
    33  
    34  // MakeIngesterClient makes a new IngesterClient
    35  func MakeIngesterClient(addr string, cfg Config) (HealthAndIngesterClient, error) {
    36  	dialOpts, err := cfg.GRPCClientConfig.DialOption(grpcclient.Instrument(ingesterClientRequestDuration))
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	conn, err := grpc.Dial(addr, dialOpts...)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	return &closableHealthAndIngesterClient{
    45  		IngesterClient: NewIngesterClient(conn),
    46  		HealthClient:   grpc_health_v1.NewHealthClient(conn),
    47  		conn:           conn,
    48  	}, nil
    49  }
    50  
    51  func (c *closableHealthAndIngesterClient) Close() error {
    52  	return c.conn.Close()
    53  }
    54  
    55  // Config is the configuration struct for the ingester client
    56  type Config struct {
    57  	GRPCClientConfig grpcclient.Config `yaml:"grpc_client_config"`
    58  }
    59  
    60  // RegisterFlags registers configuration settings used by the ingester client config.
    61  func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
    62  	cfg.GRPCClientConfig.RegisterFlagsWithPrefix("ingester.client", f)
    63  }
    64  
    65  func (cfg *Config) Validate(log log.Logger) error {
    66  	return cfg.GRPCClientConfig.Validate(log)
    67  }