MCPcopy
hub / github.com/grafana/dskit / NewClient

Function NewClient

httpgrpc/server/server.go:174–207  ·  view source on GitHub ↗

NewClient makes a new Client, given a kubernetes service address.

(address string)

Source from the content-addressed store, hash-verified

172
173// NewClient makes a new Client, given a kubernetes service address.
174func NewClient(address string) (*Client, error) {
175 kuberesolver.RegisterInCluster()
176
177 address, err := ParseURL(address)
178 if err != nil {
179 return nil, err
180 }
181 const grpcServiceConfig = `{"loadBalancingPolicy":"round_robin"}`
182
183 var unaryInterceptors []grpc.UnaryClientInterceptor
184 if opentracing.IsGlobalTracerRegistered() {
185 unaryInterceptors = append(unaryInterceptors, otgrpc.OpenTracingClientInterceptor(opentracing.GlobalTracer()))
186 }
187 unaryInterceptors = append(unaryInterceptors, middleware.ClientUserHeaderInterceptor)
188
189 dialOptions := []grpc.DialOption{
190 grpc.WithDefaultServiceConfig(grpcServiceConfig),
191 grpc.WithTransportCredentials(insecure.NewCredentials()),
192 grpc.WithChainUnaryInterceptor(unaryInterceptors...),
193 }
194 if !opentracing.IsGlobalTracerRegistered() { // Note: I'm not sure whether this condition is required, feel free to question it.
195 dialOptions = append(dialOptions, grpc.WithStatsHandler(otelgrpc.NewClientHandler()))
196 }
197
198 conn, err := grpc.NewClient(address, dialOptions...)
199 if err != nil {
200 return nil, err
201 }
202
203 return &Client{
204 client: httpgrpc.NewHTTPClient(conn),
205 conn: conn,
206 }, nil
207}
208
209// ServeHTTP implements http.Handler
210func (c *Client) ServeHTTP(w http.ResponseWriter, r *http.Request) {

Callers 3

TestBasicFunction · 0.70
TestErrorFunction · 0.70
TestTracePropagationFunction · 0.70

Calls 2

NewHTTPClientFunction · 0.92
ParseURLFunction · 0.85

Tested by 3

TestBasicFunction · 0.56
TestErrorFunction · 0.56
TestTracePropagationFunction · 0.56