-
Notifications
You must be signed in to change notification settings - Fork 43
/
server_client.go
275 lines (236 loc) · 8.49 KB
/
server_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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package opamp
import (
"context"
"fmt"
"math/rand"
"os"
"runtime"
"sync"
"time"
"github.com/SigNoz/signoz-otel-collector/constants"
"github.com/SigNoz/signoz-otel-collector/signozcol"
"github.com/oklog/ulid"
"github.com/open-telemetry/opamp-go/client"
"github.com/open-telemetry/opamp-go/client/types"
"github.com/open-telemetry/opamp-go/protobufs"
"go.uber.org/multierr"
"go.uber.org/zap"
)
// serverClient is the implementation of the Opamp client
// that connects to the Opamp server and manages the agent configuration
// and the collector lifecycle.
// It implements the client.OpAMPClient interface.
// It is responsible for:
// 1. Connecting to the Opamp server
// 2. Sending the current agent configuration to the Opamp server
// 3. Receiving the remote configuration from the Opamp server
// 4. Applying the remote configuration to the agent
// 5. Sending the updated agent configuration to the Opamp server
type serverClient struct {
baseClient
logger *zap.Logger
opampClient client.OpAMPClient
configManager *agentConfigManager
managerConfig AgentManagerConfig
instanceId ulid.ULID
receivedInitialConfig bool
mux sync.Mutex
}
type NewServerClientOpts struct {
Logger *zap.Logger
Config *AgentManagerConfig
WrappedCollector *signozcol.WrappedCollector
CollectorConfgPath string
}
// NewServerClient creates a new OpAmp client
func NewServerClient(args *NewServerClientOpts) (Client, error) {
clientLogger := args.Logger.With(zap.String("component", "opamp-server-client"))
configManager := NewAgentConfigManager(args.Logger)
svrClient := &serverClient{
baseClient: baseClient{
coll: args.WrappedCollector,
err: make(chan error, 1),
stopped: make(chan bool),
logger: clientLogger,
},
logger: clientLogger,
configManager: configManager,
managerConfig: *args.Config,
mux: sync.Mutex{},
}
svrClient.createInstanceId()
dynamicConfig, err := NewDynamicConfig(args.CollectorConfgPath, svrClient.reload, clientLogger)
if err != nil {
return nil, fmt.Errorf("failed to create collector config: %v", err)
}
svrClient.configManager.Set(dynamicConfig)
svrClient.opampClient = client.NewWebSocket(clientLogger.Sugar())
return svrClient, nil
}
func keyVal(key, val string) *protobufs.KeyValue {
return &protobufs.KeyValue{
Key: key,
Value: &protobufs.AnyValue{
Value: &protobufs.AnyValue_StringValue{StringValue: val},
},
}
}
func (s *serverClient) createInstanceId() {
entropy := ulid.Monotonic(rand.New(rand.NewSource(0)), 0)
s.instanceId = ulid.MustNew(ulid.Timestamp(time.Now()), entropy)
}
func (s *serverClient) createAgentDescription() *protobufs.AgentDescription {
hostname, _ := os.Hostname()
// Create Agent description.
return &protobufs.AgentDescription{
IdentifyingAttributes: []*protobufs.KeyValue{
keyVal("service.name", "signoz-otel-collector"),
keyVal("service.version", constants.Version),
},
NonIdentifyingAttributes: []*protobufs.KeyValue{
keyVal("os.family", runtime.GOOS),
keyVal("host.name", hostname),
keyVal("capabilities.lbexporter", constants.SupportLbExporterConfig),
},
}
}
// Start starts the Opamp client
// It connects to the Opamp server and starts the Opamp client
func (s *serverClient) Start(ctx context.Context) error {
if err := s.opampClient.SetAgentDescription(s.createAgentDescription()); err != nil {
s.logger.Error("error while setting agent description", zap.Error(err))
return err
}
settings := types.StartSettings{
OpAMPServerURL: s.managerConfig.ServerEndpoint,
InstanceUid: s.instanceId.String(),
Callbacks: types.CallbacksStruct{
OnConnectFunc: func() {
s.logger.Info("Connected to the server.")
},
OnConnectFailedFunc: func(err error) {
s.logger.Error("Failed to connect to the server: %v", zap.Error(err))
},
OnErrorFunc: func(err *protobufs.ServerErrorResponse) {
s.logger.Error("Server returned an error response: %v", zap.String("", err.ErrorMessage))
},
GetEffectiveConfigFunc: func(ctx context.Context) (*protobufs.EffectiveConfig, error) {
cfg, err := s.configManager.CreateEffectiveConfigMsg()
if err != nil {
return nil, err
}
return cfg, nil
},
OnMessageFunc: s.onMessageFuncHandler,
},
Capabilities: protobufs.AgentCapabilities_AgentCapabilities_ReportsStatus |
protobufs.AgentCapabilities_AgentCapabilities_AcceptsRemoteConfig |
protobufs.AgentCapabilities_AgentCapabilities_ReportsRemoteConfig |
protobufs.AgentCapabilities_AgentCapabilities_ReportsEffectiveConfig |
protobufs.AgentCapabilities_AgentCapabilities_ReportsHealth,
}
err := s.opampClient.SetHealth(&protobufs.AgentHealth{Healthy: false})
if err != nil {
return err
}
err = s.opampClient.Start(ctx, settings)
if err != nil {
s.logger.Error("Error while starting opamp client", zap.Error(err))
return err
}
// Wait for the initial remote config to be received
// before starting the collector.
s.waitForInitialRemoteConfig()
// Watch for any async errors from the collector and initiate a shutdown
go s.ensureRunning()
return nil
}
func (s *serverClient) waitForInitialRemoteConfig() {
for {
s.logger.Info("Waiting for initial remote config")
time.Sleep(1 * time.Second)
s.mux.Lock()
if s.receivedInitialConfig {
s.mux.Unlock()
break
}
s.mux.Unlock()
}
}
// Stop stops the Opamp client
// It stops the Opamp client and disconnects from the Opamp server
func (s *serverClient) Stop(ctx context.Context) error {
s.logger.Info("Stopping OpAMP server client")
close(s.stopped)
s.coll.Shutdown()
opampErr := s.opampClient.Stop(ctx)
collErr := <-s.coll.ErrorChan()
return multierr.Combine(opampErr, collErr)
}
// onMessageFuncHandler is the callback function that is called when the Opamp client receives a message from the Opamp server
func (s *serverClient) onMessageFuncHandler(ctx context.Context, msg *types.MessageData) {
if msg.RemoteConfig != nil {
s.mux.Lock()
s.receivedInitialConfig = true
s.mux.Unlock()
if err := s.onRemoteConfigHandler(ctx, msg.RemoteConfig); err != nil {
s.logger.Error("error while onRemoteConfigHandler", zap.Error(err))
}
}
// TODO: Handle other message types.
}
// onRemoteConfigHandler is the callback function that is called when the Opamp client receives a remote configuration from the Opamp server
func (s *serverClient) onRemoteConfigHandler(ctx context.Context, remoteConfig *protobufs.AgentRemoteConfig) error {
changed, err := s.configManager.Apply(remoteConfig)
remoteCfgStatus := &protobufs.RemoteConfigStatus{
LastRemoteConfigHash: remoteConfig.GetConfigHash(),
Status: protobufs.RemoteConfigStatuses_RemoteConfigStatuses_APPLIED,
}
if err != nil {
s.logger.Error("failed to apply config", zap.Error(err))
remoteCfgStatus.Status = protobufs.RemoteConfigStatuses_RemoteConfigStatuses_FAILED
remoteCfgStatus.ErrorMessage = fmt.Sprintf("failed to apply config changes: %s", err.Error())
}
if err := s.opampClient.SetRemoteConfigStatus(remoteCfgStatus); err != nil {
return fmt.Errorf("failed to set remote config status: %w", err)
}
if changed {
if err := s.opampClient.UpdateEffectiveConfig(ctx); err != nil {
return fmt.Errorf("failed to update effective config: %w", err)
}
}
return nil
}
// reload is the callback function that is called when the agent configuration file changes
func (s *serverClient) reload(contents []byte) error {
s.reloadMux.Lock()
s.isReloading = true
defer func() {
s.isReloading = false
s.reloadMux.Unlock()
}()
collectorConfigPath := s.configManager.agentConfig.path
rollbackPath := fmt.Sprintf("%s.rollback", collectorConfigPath)
err := copy(collectorConfigPath, rollbackPath)
if err != nil {
return fmt.Errorf("failed to create backup of collector config: %w", err)
}
// Create rollback func
rollbackFunc := func() error {
return copy(rollbackPath, collectorConfigPath)
}
if err := os.WriteFile(collectorConfigPath, contents, 0600); err != nil {
return fmt.Errorf("failed to update config file %s: %w", collectorConfigPath, err)
}
if err := s.coll.Restart(context.Background()); err != nil {
if rollbackErr := rollbackFunc(); rollbackErr != nil {
s.logger.Error("Failed to rollbakc the config", zap.Error(rollbackErr))
}
// Restart collector with original file
if rollbackErr := s.coll.Restart(context.Background()); rollbackErr != nil {
s.logger.Error("Collector failed for restart during rollback", zap.Error(rollbackErr))
}
return fmt.Errorf("collector failed to restart: %w", err)
}
return nil
}