This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
generated from yandex-praktikum/go-musthave-devops-tpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
144 lines (115 loc) · 2.98 KB
/
grpc.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package exporter
import (
"context"
"fmt"
"github.com/alkurbatov/metrics-collector/internal/entity"
"github.com/alkurbatov/metrics-collector/internal/security"
"github.com/alkurbatov/metrics-collector/pkg/grpcapi"
"github.com/alkurbatov/metrics-collector/pkg/metrics"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
)
// GRPCExporter sends collected metrics to metrics collector in single batch request.
type GRPCExporter struct {
// Address and port of server providing gRPC API.
endpoint entity.NetAddress
conn *grpc.ClientConn
// Entity to sign requests.
// If set to nil, requests will not be signed.
signer *security.Signer
// Internal buffer to store requests.
buffer []*grpcapi.MetricReq
// Error happened during one of previous method calls.
// If at least one error occurred, further calls are noop.
err error
}
func NewGRPCExporter(
endpoint entity.NetAddress,
secret security.Secret,
) *GRPCExporter {
var signer *security.Signer
if len(secret) > 0 {
signer = security.NewSigner(secret)
}
return &GRPCExporter{
endpoint: endpoint,
signer: signer,
}
}
// Add a metric to internal buffer.
func (g *GRPCExporter) Add(name string, value metrics.Metric) Exporter {
if g.err != nil {
return g
}
var req *grpcapi.MetricReq
switch v := value.(type) {
case metrics.Counter:
req = grpcapi.NewUpdateCounterReq(name, v)
case metrics.Gauge:
req = grpcapi.NewUpdateGaugeReq(name, v)
default:
g.err = entity.MetricNotImplementedError(value.Kind())
return g
}
if g.signer != nil {
hash, err := g.signer.CalculateSignature(name, value)
if err != nil {
g.err = err
return g
}
req.Hash = hash
}
g.buffer = append(g.buffer, req)
return g
}
func (g *GRPCExporter) Error() error {
if g.err == nil {
return nil
}
return fmt.Errorf("metrics export failed: %w", g.err)
}
// Send metrics stored in internal buffer to metrics collector in single batch request.
func (g *GRPCExporter) Send(ctx context.Context) Exporter {
if g.err != nil {
return g
}
if g.conn == nil {
g.conn, g.err = grpc.DialContext(
ctx,
g.endpoint.String(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if g.err != nil {
return g
}
}
if len(g.buffer) == 0 {
g.err = entity.ErrIncompleteRequest
return g
}
clientIP, err := getOutboundIP()
if err != nil {
g.err = err
return g
}
md := metadata.New(map[string]string{"x-real-ip": clientIP.String()})
ctx = metadata.NewOutgoingContext(ctx, md)
client := grpcapi.NewMetricsClient(g.conn)
req := &grpcapi.BatchUpdateRequest{Data: g.buffer}
_, g.err = client.BatchUpdate(ctx, req)
return g
}
// Reset reset state of exporter to initial.
// This doesn't affected the underlying connection.
func (g *GRPCExporter) Reset() {
g.buffer = make([]*grpcapi.MetricReq, 0)
g.err = nil
}
// Close gracefully finishes gRPC client connection.
func (g *GRPCExporter) Close() error {
if g.conn == nil {
return nil
}
return g.conn.Close()
}