-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
80 lines (68 loc) · 2.99 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package test
import (
"context"
"net/http"
"github.com/alexfalkowski/go-service/client"
"github.com/alexfalkowski/go-service/telemetry/tracer"
"github.com/alexfalkowski/go-service/transport"
tgrpc "github.com/alexfalkowski/go-service/transport/grpc"
gtracer "github.com/alexfalkowski/go-service/transport/grpc/telemetry/tracer"
shttp "github.com/alexfalkowski/go-service/transport/http"
htracer "github.com/alexfalkowski/go-service/transport/http/telemetry/tracer"
"go.opentelemetry.io/otel/metric"
"go.uber.org/fx"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
// NewHTTPClient for test.
func NewHTTPClient(lc fx.Lifecycle, logger *zap.Logger, cfg *tracer.Config, tcfg *transport.Config, meter metric.Meter) *http.Client {
return NewHTTPClientWithRoundTripper(lc, logger, cfg, tcfg, nil, meter)
}
// NewHTTPClientWithRoundTripper for test.
func NewHTTPClientWithRoundTripper(lc fx.Lifecycle, logger *zap.Logger, cfg *tracer.Config, tcfg *transport.Config, rt http.RoundTripper, meter metric.Meter) *http.Client {
tracer, _ := htracer.NewTracer(htracer.Params{Lifecycle: lc, Config: cfg, Version: Version})
client, _ := shttp.NewClient(
shttp.WithClientLogger(logger),
shttp.WithClientRoundTripper(rt), shttp.WithClientBreaker(),
shttp.WithClientTracer(tracer), shttp.WithClientRetry(&tcfg.HTTP.Retry),
shttp.WithClientMetrics(meter), shttp.WithClientUserAgent(tcfg.HTTP.UserAgent),
)
return client
}
// NewGRPCClient for test.
func NewGRPCClient(
ctx context.Context, lc fx.Lifecycle, logger *zap.Logger,
tcfg *transport.Config, ocfg *tracer.Config,
cred credentials.PerRPCCredentials,
meter metric.Meter,
) *grpc.ClientConn {
tracer, _ := gtracer.NewTracer(gtracer.Params{Lifecycle: lc, Config: ocfg, Version: Version}) //nolint:contextcheck
dialOpts := []grpc.DialOption{grpc.WithBlock()}
if cred != nil {
dialOpts = append(dialOpts, grpc.WithPerRPCCredentials(cred))
}
cl := &client.Config{Host: "127.0.0.1:" + tcfg.GRPC.Port, Retry: tcfg.GRPC.Retry, UserAgent: tcfg.GRPC.UserAgent}
conn, _ := tgrpc.NewClient(ctx, cl.Host,
tgrpc.WithClientLogger(logger), tgrpc.WithClientTracer(tracer),
tgrpc.WithClientBreaker(), tgrpc.WithClientRetry(&cl.Retry),
tgrpc.WithClientDialOption(dialOpts...), tgrpc.WithClientMetrics(meter),
tgrpc.WithClientUserAgent(cl.UserAgent),
)
return conn
}
// NewSecureGRPCClient for test.
func NewSecureGRPCClient(
ctx context.Context, lc fx.Lifecycle, logger *zap.Logger,
tcfg *transport.Config, ocfg *tracer.Config,
meter metric.Meter,
) *grpc.ClientConn {
tracer, _ := gtracer.NewTracer(gtracer.Params{Lifecycle: lc, Config: ocfg, Version: Version}) //nolint:contextcheck
sec, _ := tgrpc.WithClientSecure(NewSecureClientConfig())
conn, _ := tgrpc.NewClient(ctx, "localhost:"+tcfg.GRPC.Port,
tgrpc.WithClientLogger(logger), tgrpc.WithClientTracer(tracer),
tgrpc.WithClientBreaker(), tgrpc.WithClientRetry(&tcfg.GRPC.Retry),
tgrpc.WithClientMetrics(meter), tgrpc.WithClientUserAgent(tcfg.GRPC.UserAgent), sec,
)
return conn
}