-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
manager.go
163 lines (137 loc) · 4.94 KB
/
manager.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
153
154
155
156
157
158
159
160
161
162
163
package telemetry
import (
"context"
"net/url"
"strings"
"time"
"github.com/pkg/errors"
"go.uber.org/multierr"
"github.com/smartcontractkit/libocr/commontypes"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink/v2/core/config"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore"
"github.com/smartcontractkit/chainlink/v2/core/services/synchronization"
)
type Manager struct {
services.StateMachine
bufferSize uint
endpoints []*telemetryEndpoint
ks keystore.CSA
lggr logger.Logger
logging bool
maxBatchSize uint
sendInterval time.Duration
sendTimeout time.Duration
uniConn bool
useBatchSend bool
MonitoringEndpointGenerator MonitoringEndpointGenerator
}
type telemetryEndpoint struct {
ChainID string
Network string
URL *url.URL
client synchronization.TelemetryService
PubKey string
}
// NewManager create a new telemetry manager that is responsible for configuring telemetry agents and generating the defined telemetry endpoints and monitoring endpoints
func NewManager(cfg config.TelemetryIngress, csaKeyStore keystore.CSA, lggr logger.Logger) *Manager {
m := &Manager{
bufferSize: cfg.BufferSize(),
endpoints: nil,
ks: csaKeyStore,
lggr: lggr.Named("TelemetryManager"),
logging: cfg.Logging(),
maxBatchSize: cfg.MaxBatchSize(),
sendInterval: cfg.SendInterval(),
sendTimeout: cfg.SendTimeout(),
uniConn: cfg.UniConn(),
useBatchSend: cfg.UseBatchSend(),
}
for _, e := range cfg.Endpoints() {
if err := m.addEndpoint(e); err != nil {
m.lggr.Error(err)
}
}
return m
}
func (m *Manager) Start(ctx context.Context) error {
return m.StartOnce("TelemetryManager", func() error {
var err error
for _, e := range m.endpoints {
err = multierr.Append(err, e.client.Start(ctx))
}
return err
})
}
func (m *Manager) Close() error {
return m.StopOnce("TelemetryManager", func() error {
var err error
for _, e := range m.endpoints {
err = multierr.Append(err, e.client.Close())
}
return err
})
}
func (m *Manager) Name() string {
return m.lggr.Name()
}
func (m *Manager) HealthReport() map[string]error {
hr := map[string]error{m.Name(): m.Healthy()}
for _, e := range m.endpoints {
services.CopyHealth(hr, e.client.HealthReport())
}
return hr
}
// GenMonitoringEndpoint creates a new monitoring endpoints based on the existing available endpoints defined in the core config TOML, if no endpoint for the network and chainID exists, a NOOP agent will be used and the telemetry will not be sent
func (m *Manager) GenMonitoringEndpoint(network string, chainID string, contractID string, telemType synchronization.TelemetryType) commontypes.MonitoringEndpoint {
e, found := m.getEndpoint(network, chainID)
if !found {
m.lggr.Warnf("no telemetry endpoint found for network %q chainID %q, telemetry %q for contactID %q will NOT be sent", network, chainID, telemType, contractID)
return &NoopAgent{}
}
if m.useBatchSend {
return NewIngressAgentBatch(e.client, network, chainID, contractID, telemType)
}
return NewIngressAgent(e.client, network, chainID, contractID, telemType)
}
func (m *Manager) addEndpoint(e config.TelemetryIngressEndpoint) error {
if e.Network() == "" {
return errors.New("cannot add telemetry endpoint, network cannot be empty")
}
if e.ChainID() == "" {
return errors.New("cannot add telemetry endpoint, chainID cannot be empty")
}
if e.URL() == nil {
return errors.New("cannot add telemetry endpoint, URL cannot be empty")
}
if e.ServerPubKey() == "" {
return errors.New("cannot add telemetry endpoint, ServerPubKey cannot be empty")
}
if _, found := m.getEndpoint(e.Network(), e.ChainID()); found {
return errors.Errorf("cannot add telemetry endpoint for network %q and chainID %q, endpoint already exists", e.Network(), e.ChainID())
}
var tClient synchronization.TelemetryService
if m.useBatchSend {
tClient = synchronization.NewTelemetryIngressBatchClient(e.URL(), e.ServerPubKey(), m.ks, m.logging, m.lggr, m.bufferSize, m.maxBatchSize, m.sendInterval, m.sendTimeout, m.uniConn, e.Network(), e.ChainID())
} else {
tClient = synchronization.NewTelemetryIngressClient(e.URL(), e.ServerPubKey(), m.ks, m.logging, m.lggr, m.bufferSize, e.Network(), e.ChainID())
}
te := telemetryEndpoint{
Network: strings.ToUpper(e.Network()),
ChainID: strings.ToUpper(e.ChainID()),
URL: e.URL(),
PubKey: e.ServerPubKey(),
client: tClient,
}
m.endpoints = append(m.endpoints, &te)
return nil
}
func (m *Manager) getEndpoint(network string, chainID string) (*telemetryEndpoint, bool) {
for _, e := range m.endpoints {
if e.Network == strings.ToUpper(network) && e.ChainID == strings.ToUpper(chainID) {
return e, true
}
}
return nil, false
}