-
Notifications
You must be signed in to change notification settings - Fork 35
/
extension.go
350 lines (300 loc) · 8.77 KB
/
extension.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package opampextension
import (
"bytes"
"context"
"fmt"
"github.com/google/uuid"
"github.com/open-telemetry/opamp-go/client"
"github.com/open-telemetry/opamp-go/protobufs"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.uber.org/zap"
"io/ioutil"
"os"
"os/exec"
"syscall"
"time"
)
const (
yamlContentType = "text/yaml"
)
type OpAMPExtension struct {
logger *zap.Logger
conf *Config
endpoint string
name string
version string
instanceid string
currentHash []byte
client client.OpAMPClient
cancel context.CancelFunc
}
func newOpAMPExtension(conf *Config, logger *zap.Logger, name string, version string) (*OpAMPExtension, error) {
// Unfortunately, we don't seem to have access to original instance uuid so need to use another one
instanceuuid, err := uuid.NewRandom()
if err != nil {
return nil, err
}
return &OpAMPExtension{
conf: conf,
logger: logger,
name: name,
version: version,
endpoint: conf.ServerEndpoint,
currentHash: []byte{},
instanceid: instanceuuid.String(),
}, nil
}
func (se *OpAMPExtension) Debugf(format string, v ...interface{}) {
se.logger.Debug(fmt.Sprintf(format, v...))
}
func (se *OpAMPExtension) Errorf(format string, v ...interface{}) {
se.logger.Error(fmt.Sprintf(format, v...))
}
func (se *OpAMPExtension) Start(ctx context.Context, host component.Host) error {
se.logger.Info("Starting OpAMP Extension")
settings := client.StartSettings{
OpAMPServerURL: se.endpoint,
InstanceUid: se.instanceid,
AgentType: se.name,
AgentVersion: se.version,
Callbacks: client.CallbacksStruct{
OnRemoteConfigFunc: se.onAgentRemoteConfig,
OnConnectFunc: func() {
se.logger.Debug("Connected")
},
OnConnectFailedFunc: func(err error) {
se.logger.Error("Failed connecting to OpAMP", zap.Error(err))
},
},
}
se.client = client.New(se)
var err error
err = se.client.SetEffectiveConfig(se.readCurrentConfig())
if err != nil {
return err
}
err = se.client.Start(settings)
se.startAgentAttrsUpdateLoop(ctx)
return err
}
func (se *OpAMPExtension) startAgentAttrsUpdateLoop(ctx context.Context) {
ctx, se.cancel = context.WithCancel(ctx)
go func() {
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
se.setAgentAttributes()
case <-ctx.Done():
return
}
}
}()
}
func (se *OpAMPExtension) setAgentAttributes() {
se.client.SetAgentAttributes(map[string]*protobufs.AnyValue{
"version": {
Value: &protobufs.AnyValue_StringValue{StringValue: se.version},
},
})
}
func (se *OpAMPExtension) readCurrentConfig() *protobufs.EffectiveConfig {
se.currentHash = se.readLastConfigHash()
if se.currentHash == nil {
return nil
}
currentConfig, err := ioutil.ReadFile(se.conf.ConfigPath)
if err != nil {
return nil
}
configMap := map[string]*protobufs.AgentConfigFile{}
configMap[""] = &protobufs.AgentConfigFile{
Body: currentConfig,
ContentType: yamlContentType,
}
return &protobufs.EffectiveConfig{
Hash: se.readLastConfigHash(),
ConfigMap: &protobufs.AgentConfigMap{ConfigMap: configMap},
}
}
// Shutdown is invoked during service shutdown.
func (se *OpAMPExtension) Shutdown(ctx context.Context) error {
se.cancel()
return se.client.Stop(ctx)
}
func (se *OpAMPExtension) onAgentRemoteConfig(ctx context.Context, config *protobufs.AgentRemoteConfig) (*protobufs.EffectiveConfig, error) {
//for k, v := range config.Config.ConfigMap {
// println("Received config key: ", k)
// println("Received content-type: ", v.ContentType)
// println("Received body: ", string(v.Body))
//}
if config == nil {
se.logger.Error("Empty remote config received, ignoring it")
return nil, nil
}
newConfig, err := se.buildConfig(config)
if err != nil {
se.logger.Error("Failed building config", zap.Error(err))
return nil, nil
}
if !se.validateConfig(newConfig) {
se.logger.Error("The new config is not valid, skipping it")
return nil, nil
}
if !bytes.Equal(config.ConfigHash, se.currentHash) || !se.usingConfiguredFile() {
changed, err := se.updateConfigIfDiffers(newConfig)
if err != nil {
se.logger.Error("failed updating config", zap.Error(err))
return nil, err
}
se.writeConfigHash(config.ConfigHash)
if changed {
se.logger.Info("New config detected. Going to shutdown the collector and restart it")
}
if !se.usingConfiguredFile() {
se.logger.Info("Detected different config path used by collector, going to restart the process")
}
go func() {
err := se.handleRestart(ctx)
if err != nil {
se.logger.Error("Restarting the agent failed", zap.Error(err))
}
}()
}
return nil, nil
}
func (se *OpAMPExtension) usingConfiguredFile() bool {
// This is supposed to solve bootstraping issue, to some extent
for i, arg := range os.Args {
if i > 0 && os.Args[i-1] == "--config" {
// todo: this requires better comparison
if arg == se.conf.ConfigPath {
return true
}
}
}
return false
}
func (se *OpAMPExtension) buildConfig(config *protobufs.AgentRemoteConfig) (string, error) {
var efficientConfig string
for _, v := range config.Config.ConfigMap {
if len(efficientConfig) > 0 {
efficientConfig += "\n"
}
switch v.ContentType {
case yamlContentType:
efficientConfig += string(v.Body)
default:
return "", fmt.Errorf("unsupported config content type encountered: %s", v.ContentType)
}
}
// TODO: look at the se.ensureopamp flag and use it to make sure we do not remove ourselves from opamp
// by accident
return efficientConfig, nil
}
func (se *OpAMPExtension) validateConfig(newConfig string) bool {
return true
}
func (se *OpAMPExtension) configHashPath() string {
return fmt.Sprintf("%s.hash", se.conf.ConfigPath)
}
func (se *OpAMPExtension) readLastConfigHash() []byte {
content, err := ioutil.ReadFile(se.configHashPath())
if err != nil {
se.logger.Warn("Could not read config hash", zap.Error(err))
return nil
}
return content
}
func (se *OpAMPExtension) writeConfigHash(configHash []byte) {
err := ioutil.WriteFile(se.configHashPath(), configHash, 0640)
if err != nil {
se.logger.Warn("Could not write config hash", zap.Error(err))
}
}
func (se *OpAMPExtension) updateConfigIfDiffers(newConfig string) (bool, error) {
content, err := ioutil.ReadFile(se.conf.ConfigPath)
if err == nil {
current := string(content)
if current == newConfig {
se.logger.Info("New config received but it does not differ from the existing one")
return false, nil
}
} else {
// TODO: check for various types of errors, but so far just assume the file could not be read
se.logger.Warn("Could not read existing config", zap.Error(err))
}
backupPath := se.conf.ConfigPath + ".old"
err = ioutil.WriteFile(backupPath, content, 0640)
if err != nil {
se.logger.Warn("Could not write backup config", zap.String("backupPath", backupPath))
}
err = ioutil.WriteFile(se.conf.ConfigPath, []byte(newConfig), 0640)
if err != nil {
return false, fmt.Errorf("could not write new config file to %s", se.conf.ConfigPath)
}
return true, nil
}
func (se *OpAMPExtension) handleRestart(ctx context.Context) error {
p, err := os.FindProcess(os.Getpid())
if err != nil {
return err
}
// NOTE: we could use wd and absolute paths instead
//wd, _ := os.Getwd()
newArgs := make([]string, len(os.Args))
for i, arg := range os.Args {
if i > 0 && os.Args[i-1] == "--config" {
// Update the config path with the new location
newArgs[i] = se.conf.ConfigPath
} else {
newArgs[i] = arg
}
}
argv0, err := lookPath()
if err != nil {
return err
}
se.logger.Info("Restarting the process since new configuration needs to be applied")
// TODO: some more graceful Stop() or so?
se.client.Stop(ctx)
time.Sleep(3 * time.Second)
se.logger.Info("Running new instance")
err = syscall.Exec(argv0, newArgs, os.Environ())
if err != nil {
return err
}
err = p.Signal(syscall.SIGTERM)
if err != nil {
return err
}
return nil
}
func lookPath() (argv0 string, err error) {
argv0, err = exec.LookPath(os.Args[0])
if nil != err {
return
}
if _, err = os.Stat(argv0); nil != err {
return
}
return
}
func (se *OpAMPExtension) ComponentID() config.ComponentID {
return se.conf.ExtensionSettings.ID()
}