github.com/thanos-io/thanos@v0.32.5/pkg/extgrpc/client.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package extgrpc
     5  
     6  import (
     7  	"math"
     8  
     9  	"github.com/go-kit/log"
    10  	"github.com/go-kit/log/level"
    11  	grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
    12  	grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
    13  	"github.com/opentracing/opentracing-go"
    14  	"github.com/prometheus/client_golang/prometheus"
    15  	"google.golang.org/grpc"
    16  	"google.golang.org/grpc/credentials"
    17  	"google.golang.org/grpc/credentials/insecure"
    18  
    19  	"github.com/thanos-io/thanos/pkg/tls"
    20  	"github.com/thanos-io/thanos/pkg/tracing"
    21  )
    22  
    23  // EndpointGroupGRPCOpts creates gRPC dial options for connecting to endpoint groups.
    24  // For details on retry capabilities, see https://github.com/grpc/proposal/blob/master/A6-client-retries.md#retry-policy-capabilities
    25  func EndpointGroupGRPCOpts() []grpc.DialOption {
    26  	serviceConfig := `
    27  {
    28    "loadBalancingPolicy":"round_robin",
    29    "retryPolicy": {
    30      "maxAttempts": 3,
    31      "initialBackoff": "0.1s",
    32      "backoffMultiplier": 2,
    33      "retryableStatusCodes": [
    34    	  "UNAVAILABLE"
    35      ]
    36    }
    37  }`
    38  
    39  	return []grpc.DialOption{
    40  		grpc.WithDefaultServiceConfig(serviceConfig),
    41  	}
    42  }
    43  
    44  // StoreClientGRPCOpts creates gRPC dial options for connecting to a store client.
    45  func StoreClientGRPCOpts(logger log.Logger, reg *prometheus.Registry, tracer opentracing.Tracer, secure, skipVerify bool, cert, key, caCert, serverName string) ([]grpc.DialOption, error) {
    46  	grpcMets := grpc_prometheus.NewClientMetrics()
    47  	grpcMets.EnableClientHandlingTimeHistogram(
    48  		grpc_prometheus.WithHistogramBuckets([]float64{0.001, 0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120, 240, 360, 720}),
    49  	)
    50  	dialOpts := []grpc.DialOption{
    51  		// We want to make sure that we can receive huge gRPC messages from storeAPI.
    52  		// On TCP level we can be fine, but the gRPC overhead for huge messages could be significant.
    53  		// Current limit is ~2GB.
    54  		// TODO(bplotka): Split sent chunks on store node per max 4MB chunks if needed.
    55  		grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(math.MaxInt32)),
    56  		grpc.WithUnaryInterceptor(
    57  			grpc_middleware.ChainUnaryClient(
    58  				grpcMets.UnaryClientInterceptor(),
    59  				tracing.UnaryClientInterceptor(tracer),
    60  			),
    61  		),
    62  		grpc.WithStreamInterceptor(
    63  			grpc_middleware.ChainStreamClient(
    64  				grpcMets.StreamClientInterceptor(),
    65  				tracing.StreamClientInterceptor(tracer),
    66  			),
    67  		),
    68  	}
    69  	if reg != nil {
    70  		reg.MustRegister(grpcMets)
    71  	}
    72  
    73  	if !secure {
    74  		return append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials())), nil
    75  	}
    76  
    77  	level.Info(logger).Log("msg", "enabling client to server TLS")
    78  
    79  	tlsCfg, err := tls.NewClientConfig(logger, cert, key, caCert, serverName, skipVerify)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	return append(dialOpts, grpc.WithTransportCredentials(credentials.NewTLS(tlsCfg))), nil
    84  }