/
reporter.go
152 lines (131 loc) · 3.6 KB
/
reporter.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
145
146
147
148
149
150
151
152
package registry
import (
"context"
"encoding/base64"
"fmt"
"net"
"net/url"
"strings"
"time"
"github.com/pomerium/pomerium/config"
"github.com/pomerium/pomerium/internal/log"
"github.com/pomerium/pomerium/pkg/grpc"
pb "github.com/pomerium/pomerium/pkg/grpc/registry"
"github.com/cenkalti/backoff/v4"
)
// Reporter periodically submits a list of services available on this instance to the service registry
type Reporter struct {
cancel func()
outboundGRPCConnection *grpc.CachedOutboundGRPClientConn
}
// NewReporter creates a new Reporter.
func NewReporter() *Reporter {
return &Reporter{
outboundGRPCConnection: new(grpc.CachedOutboundGRPClientConn),
}
}
// OnConfigChange applies configuration changes to the reporter
func (r *Reporter) OnConfigChange(ctx context.Context, cfg *config.Config) {
if r.cancel != nil {
r.cancel()
}
services, err := getReportedServices(cfg)
if err != nil {
log.Warn(ctx).Err(err).Msg("metrics announce to service registry is disabled")
}
sharedKey, err := cfg.Options.GetSharedKey()
if err != nil {
log.Error(ctx).Err(err).Msg("decoding shared key")
return
}
registryConn, err := r.outboundGRPCConnection.Get(ctx, &grpc.OutboundOptions{
OutboundPort: cfg.OutboundPort,
InstallationID: cfg.Options.InstallationID,
ServiceName: cfg.Options.Services,
SignedJWTKey: sharedKey,
})
if err != nil {
log.Error(ctx).Err(err).Msg("connecting to registry")
return
}
if len(services) > 0 {
ctx, cancel := context.WithCancel(context.TODO())
go runReporter(ctx, pb.NewRegistryClient(registryConn), services)
r.cancel = cancel
}
}
func getReportedServices(cfg *config.Config) ([]*pb.Service, error) {
if cfg.Options.MetricsAddr == "" {
return nil, nil
}
mu, err := metricsURL(*cfg.Options)
if err != nil {
return nil, err
}
return []*pb.Service{
{Kind: pb.ServiceKind_PROMETHEUS_METRICS, Endpoint: mu.String()},
}, nil
}
func metricsURL(o config.Options) (*url.URL, error) {
host, port, err := net.SplitHostPort(o.MetricsAddr)
if err != nil {
return nil, fmt.Errorf("invalid metrics address %q: %w", o.MetricsAddr, err)
}
if port == "" {
return nil, fmt.Errorf("invalid metrics value %q: port is required", o.MetricsAddr)
}
if host == "" {
if host, err = getHostOrIP(); err != nil {
return nil, fmt.Errorf("could not guess hostname: %w", err)
}
}
u := url.URL{
Scheme: "http",
Host: net.JoinHostPort(host, port),
Path: defaultMetricsPath,
}
if o.MetricsBasicAuth != "" {
txt, err := base64.StdEncoding.DecodeString(o.MetricsBasicAuth)
if err != nil {
return nil, fmt.Errorf("metrics basic auth: %w", err)
}
parts := strings.SplitN(string(txt), ":", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("expected username:password for basic auth")
}
u.User = url.UserPassword(parts[0], parts[1])
}
if o.MetricsCertificate != "" || o.MetricsCertificateFile != "" {
u.Scheme = "https"
}
if o.MetricsAddr == "" {
return nil, fmt.Errorf("no metrics address provided")
}
return &u, nil
}
func runReporter(
ctx context.Context,
client pb.RegistryClient,
services []*pb.Service,
) {
backoff := backoff.NewExponentialBackOff()
backoff.MaxElapsedTime = 0
req := &pb.RegisterRequest{Services: services}
after := minTTL
for {
select {
case <-time.After(after):
resp, err := client.Report(ctx, req)
if err != nil {
log.Ctx(ctx).Error().Err(err).Msg("grpc.service_registry.Report")
after = backoff.NextBackOff()
continue
}
after = resp.CallBackAfter.AsDuration()
backoff.Reset()
case <-ctx.Done():
log.Info(ctx).Msg("service registry reporter stopping")
return
}
}
}