|
| 1 | +// Package client provides gRPC client implementation for distributor service. |
| 2 | +package client |
| 3 | + |
| 4 | +import ( |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/grafana/dskit/grpcclient" |
| 11 | + "github.com/grafana/dskit/middleware" |
| 12 | + "github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc" |
| 13 | + "github.com/opentracing/opentracing-go" |
| 14 | + "github.com/prometheus/client_golang/prometheus" |
| 15 | + "github.com/prometheus/client_golang/prometheus/promauto" |
| 16 | + "google.golang.org/grpc" |
| 17 | + "google.golang.org/grpc/health/grpc_health_v1" |
| 18 | + "google.golang.org/grpc/keepalive" |
| 19 | + |
| 20 | + "github.com/grafana/loki/v3/pkg/logproto" |
| 21 | + "github.com/grafana/loki/v3/pkg/util/server" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + GRPCLoadBalancingPolicyRoundRobin = "round_robin" |
| 26 | + |
| 27 | + grpcServiceConfigTemplate = `{"loadBalancingPolicy":"%s"}` |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + requestDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{ |
| 32 | + Name: "loki_distributor_client_request_duration_seconds", |
| 33 | + Help: "Time spent doing distributor requests.", |
| 34 | + Buckets: prometheus.ExponentialBuckets(0.001, 4, 6), |
| 35 | + }, []string{"operation", "status_code"}) |
| 36 | +) |
| 37 | + |
| 38 | +// Config contains the config for an ingest-limits client. |
| 39 | +type Config struct { |
| 40 | + Addr string `yaml:"addr"` |
| 41 | + GRPCClientConfig grpcclient.Config `yaml:"grpc_client_config"` |
| 42 | + GRPCUnaryClientInterceptors []grpc.UnaryClientInterceptor `yaml:"-"` |
| 43 | + GRCPStreamClientInterceptors []grpc.StreamClientInterceptor `yaml:"-"` |
| 44 | + |
| 45 | + // Internal is used to indicate that this client communicates on behalf of |
| 46 | + // a machine and not a user. When Internal = true, the client won't attempt |
| 47 | + // to inject an userid into the context. |
| 48 | + Internal bool `yaml:"-"` |
| 49 | +} |
| 50 | + |
| 51 | +func (cfg *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) { |
| 52 | + f.StringVar(&cfg.Addr, prefix+".addr", "", "The address of the distributor. Preferred 'dns:///distributor.namespace.svc.cluster.local:3100'") |
| 53 | + cfg.GRPCClientConfig.RegisterFlagsWithPrefix(prefix, f) |
| 54 | +} |
| 55 | + |
| 56 | +// Client is a gRPC client for the distributor. |
| 57 | +type Client struct { |
| 58 | + logproto.PusherClient |
| 59 | + grpc_health_v1.HealthClient |
| 60 | + io.Closer |
| 61 | +} |
| 62 | + |
| 63 | +// New returns a new Client for the specified ingest-limits. |
| 64 | +func New(cfg Config) (*Client, error) { |
| 65 | + opts := []grpc.DialOption{ |
| 66 | + grpc.WithDefaultCallOptions(cfg.GRPCClientConfig.CallOptions()...), |
| 67 | + } |
| 68 | + unaryInterceptors, streamInterceptors := getGRPCInterceptors(&cfg) |
| 69 | + dialOpts, err := cfg.GRPCClientConfig.DialOption(unaryInterceptors, streamInterceptors, middleware.NoOpInvalidClusterValidationReporter) |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + |
| 74 | + serviceConfig := fmt.Sprintf(grpcServiceConfigTemplate, GRPCLoadBalancingPolicyRoundRobin) |
| 75 | + |
| 76 | + dialOpts = append(dialOpts, |
| 77 | + grpc.WithBlock(), // nolint:staticcheck // grpc.WithBlock() has been deprecated; we'll address it before upgrading to gRPC 2 |
| 78 | + grpc.WithKeepaliveParams( |
| 79 | + keepalive.ClientParameters{ |
| 80 | + Time: time.Second * 10, |
| 81 | + Timeout: time.Second * 5, |
| 82 | + PermitWithoutStream: true, |
| 83 | + }, |
| 84 | + ), |
| 85 | + grpc.WithDefaultServiceConfig(serviceConfig), |
| 86 | + ) |
| 87 | + |
| 88 | + opts = append(opts, dialOpts...) |
| 89 | + // nolint:staticcheck // grpc.Dial() has been deprecated; we'll address it before upgrading to gRPC 2. |
| 90 | + conn, err := grpc.Dial(cfg.Addr, opts...) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + return &Client{ |
| 95 | + PusherClient: logproto.NewPusherClient(conn), |
| 96 | + HealthClient: grpc_health_v1.NewHealthClient(conn), |
| 97 | + Closer: conn, |
| 98 | + }, nil |
| 99 | +} |
| 100 | + |
| 101 | +// getInterceptors returns the gRPC interceptors for the given ClientConfig. |
| 102 | +func getGRPCInterceptors(cfg *Config) ([]grpc.UnaryClientInterceptor, []grpc.StreamClientInterceptor) { |
| 103 | + var ( |
| 104 | + unaryInterceptors []grpc.UnaryClientInterceptor |
| 105 | + streamInterceptors []grpc.StreamClientInterceptor |
| 106 | + ) |
| 107 | + |
| 108 | + unaryInterceptors = append(unaryInterceptors, cfg.GRPCUnaryClientInterceptors...) |
| 109 | + unaryInterceptors = append(unaryInterceptors, server.UnaryClientQueryTagsInterceptor) |
| 110 | + unaryInterceptors = append(unaryInterceptors, server.UnaryClientHTTPHeadersInterceptor) |
| 111 | + unaryInterceptors = append(unaryInterceptors, otgrpc.OpenTracingClientInterceptor(opentracing.GlobalTracer())) |
| 112 | + if !cfg.Internal { |
| 113 | + unaryInterceptors = append(unaryInterceptors, middleware.ClientUserHeaderInterceptor) |
| 114 | + } |
| 115 | + unaryInterceptors = append(unaryInterceptors, middleware.UnaryClientInstrumentInterceptor(requestDuration)) |
| 116 | + |
| 117 | + streamInterceptors = append(streamInterceptors, cfg.GRCPStreamClientInterceptors...) |
| 118 | + streamInterceptors = append(streamInterceptors, server.StreamClientQueryTagsInterceptor) |
| 119 | + streamInterceptors = append(streamInterceptors, server.StreamClientHTTPHeadersInterceptor) |
| 120 | + streamInterceptors = append(streamInterceptors, otgrpc.OpenTracingStreamClientInterceptor(opentracing.GlobalTracer())) |
| 121 | + if !cfg.Internal { |
| 122 | + streamInterceptors = append(streamInterceptors, middleware.StreamClientUserHeaderInterceptor) |
| 123 | + } |
| 124 | + streamInterceptors = append(streamInterceptors, middleware.StreamClientInstrumentInterceptor(requestDuration)) |
| 125 | + |
| 126 | + return unaryInterceptors, streamInterceptors |
| 127 | +} |
0 commit comments