-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathopamp_agent.go
More file actions
715 lines (609 loc) · 20.7 KB
/
opamp_agent.go
File metadata and controls
715 lines (609 loc) · 20.7 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package opampextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/opampextension"
import (
"context"
"crypto/sha256"
"errors"
"fmt"
"maps"
"net/http"
"os"
"runtime"
"sort"
"strings"
"sync"
"syscall"
"github.com/google/uuid"
"github.com/oklog/ulid/v2"
"github.com/open-telemetry/opamp-go/client"
"github.com/open-telemetry/opamp-go/client/types"
"github.com/open-telemetry/opamp-go/protobufs"
"github.com/shirou/gopsutil/v4/host"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componentstatus"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/extension"
"go.opentelemetry.io/collector/extension/extensioncapabilities"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/service"
"go.opentelemetry.io/collector/service/hostcapabilities"
conventions "go.opentelemetry.io/otel/semconv/v1.38.0"
"go.uber.org/zap"
expmaps "golang.org/x/exp/maps"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"gopkg.in/yaml.v3"
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/opampcustommessages"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/status"
)
type statusAggregator interface {
Subscribe(scope status.Scope, verbosity status.Verbosity) (<-chan *status.AggregateStatus, status.UnsubscribeFunc)
RecordStatus(source *componentstatus.InstanceID, event *componentstatus.Event)
}
type eventSourcePair struct {
source *componentstatus.InstanceID
event *componentstatus.Event
}
type opampAgent struct {
cfg *Config
logger *zap.Logger
agentType string
agentVersion string
agentInstanceID string
resourceAttrs map[string]string
instanceID uuid.UUID
eclk sync.RWMutex
effectiveConfig *confmap.Conf
// lifetimeCtx is canceled on Stop of the component
lifetimeCtx context.Context
lifetimeCtxCancel context.CancelFunc
reportFunc func(*componentstatus.Event)
capabilities Capabilities
agentDescription *protobufs.AgentDescription
availableComponents *protobufs.AvailableComponents
opampClient client.OpAMPClient
customCapabilityRegistry *customCapabilityRegistry
statusAggregator statusAggregator
statusSubscriptionWg *sync.WaitGroup
componentHealthWg *sync.WaitGroup
startTimeUnixNano uint64
componentStatusCh chan *eventSourcePair
readyCh chan struct{}
}
var (
_ opampcustommessages.CustomCapabilityRegistry = (*opampAgent)(nil)
_ extensioncapabilities.Dependent = (*opampAgent)(nil)
_ extensioncapabilities.ConfigWatcher = (*opampAgent)(nil)
_ extensioncapabilities.PipelineWatcher = (*opampAgent)(nil)
_ componentstatus.Watcher = (*opampAgent)(nil)
// identifyingAttributes is the list of semantic convention keys that are used
// for the agent description's identifying attributes.
identifyingAttributes = map[string]struct{}{
string(conventions.ServiceNameKey): {},
string(conventions.ServiceVersionKey): {},
string(conventions.ServiceInstanceIDKey): {},
}
)
func (o *opampAgent) Start(ctx context.Context, host component.Host) error {
o.reportFunc = func(event *componentstatus.Event) {
componentstatus.ReportStatus(host, event)
}
header := http.Header{}
for k, v := range o.cfg.Server.GetHeaders() {
header.Set(k, string(v))
}
tls, err := o.cfg.Server.GetTLSConfig(ctx)
if err != nil {
return err
}
if o.cfg.PPID != 0 {
go monitorPPID(o.lifetimeCtx, o.cfg.PPIDPollInterval, o.cfg.PPID, o.reportFunc)
}
headerFunc, err := makeHeadersFunc(o.logger, o.cfg.Server, host)
if err != nil {
return err
}
settings := types.StartSettings{
Header: header,
HeaderFunc: headerFunc,
TLSConfig: tls,
OpAMPServerURL: o.cfg.Server.GetEndpoint(),
InstanceUid: types.InstanceUid(o.instanceID),
Callbacks: types.Callbacks{
OnConnect: func(_ context.Context) {
o.logger.Debug("Connected to the OpAMP server")
},
OnConnectFailed: func(_ context.Context, err error) {
o.logger.Error("Failed to connect to the OpAMP server", zap.Error(err))
},
OnError: func(_ context.Context, err *protobufs.ServerErrorResponse) {
o.logger.Error("OpAMP server returned an error response", zap.String("message", err.ErrorMessage))
},
GetEffectiveConfig: func(_ context.Context) (*protobufs.EffectiveConfig, error) {
return o.composeEffectiveConfig(), nil
},
OnMessage: o.onMessage,
OnCommand: o.onCommand,
},
}
if err := o.createAgentDescription(); err != nil {
return err
}
if err := o.opampClient.SetAgentDescription(o.agentDescription); err != nil {
return err
}
if mi, ok := host.(hostcapabilities.ModuleInfo); ok {
o.initAvailableComponents(mi.GetModuleInfos())
} else if o.capabilities.ReportsAvailableComponents {
// init empty availableComponents to not get an error when starting the opampClient
o.initAvailableComponents(service.ModuleInfos{})
}
if o.availableComponents != nil {
if err := o.opampClient.SetAvailableComponents(o.availableComponents); err != nil {
return err
}
}
capabilities := o.capabilities.toAgentCapabilities()
if err := o.opampClient.SetCapabilities(&capabilities); err != nil {
return err
}
o.logger.Debug("Starting OpAMP client...")
if err := o.opampClient.Start(context.Background(), settings); err != nil {
return err
}
o.logger.Debug("OpAMP client started")
return nil
}
func (o *opampAgent) Shutdown(ctx context.Context) error {
if o.lifetimeCtxCancel != nil {
o.lifetimeCtxCancel()
}
o.statusSubscriptionWg.Wait()
o.componentHealthWg.Wait()
if o.componentStatusCh != nil {
close(o.componentStatusCh)
}
o.logger.Debug("OpAMP agent shutting down...")
if o.opampClient == nil {
return nil
}
o.logger.Debug("Stopping OpAMP client...")
err := o.opampClient.Stop(ctx)
// Opamp-go considers this an error, but the collector does not.
// https://github.com/open-telemetry/opamp-go/issues/255
if err != nil && strings.EqualFold(err.Error(), "cannot stop because not started") {
return nil
}
return err
}
// Dependencies implements extensioncapabilities.Dependent
func (o *opampAgent) Dependencies() []component.ID {
if o.cfg.Server == nil {
return nil
}
var emptyComponentID component.ID
authID := o.cfg.Server.GetAuthExtensionID()
if authID == emptyComponentID {
return nil
}
return []component.ID{authID}
}
func (o *opampAgent) NotifyConfig(ctx context.Context, conf *confmap.Conf) error {
if o.capabilities.ReportsEffectiveConfig {
o.updateEffectiveConfig(conf)
return o.opampClient.UpdateEffectiveConfig(ctx)
}
return nil
}
func (o *opampAgent) Register(capability string, opts ...opampcustommessages.CustomCapabilityRegisterOption) (opampcustommessages.CustomCapabilityHandler, error) {
return o.customCapabilityRegistry.Register(capability, opts...)
}
func (o *opampAgent) Ready() error {
o.setHealth(&protobufs.ComponentHealth{Healthy: true})
close(o.readyCh)
return nil
}
func (o *opampAgent) NotReady() error {
o.setHealth(&protobufs.ComponentHealth{Healthy: false})
return nil
}
// ComponentStatusChanged implements the componentstatus.Watcher interface.
func (o *opampAgent) ComponentStatusChanged(
source *componentstatus.InstanceID,
event *componentstatus.Event,
) {
// There can be late arriving events after shutdown. We need to close
// the event channel so that this function doesn't block and we release all
// goroutines, but attempting to write to a closed channel will panic; log
// and recover.
defer func() {
if r := recover(); r != nil {
o.logger.Info(
"discarding event received after shutdown",
zap.Any("source", source),
zap.Any("event", event),
)
}
}()
o.componentStatusCh <- &eventSourcePair{source: source, event: event}
}
func (o *opampAgent) updateEffectiveConfig(conf *confmap.Conf) {
o.eclk.Lock()
defer o.eclk.Unlock()
o.effectiveConfig = conf
}
func newOpampAgent(cfg *Config, set extension.Settings) (*opampAgent, error) {
agentType := set.BuildInfo.Command
sn, ok := set.Resource.Attributes().Get(string(conventions.ServiceNameKey))
if ok {
agentType = sn.AsString()
}
agentVersion := set.BuildInfo.Version
sv, ok := set.Resource.Attributes().Get(string(conventions.ServiceVersionKey))
if ok {
agentVersion = sv.AsString()
}
agentInstanceID := ""
if sid, ok := set.Resource.Attributes().Get(string(conventions.ServiceInstanceIDKey)); ok {
agentInstanceID = sid.Str()
}
var uid uuid.UUID
if cfg.InstanceUID != "" {
var err error
uid, err = parseInstanceIDString(cfg.InstanceUID)
if err != nil {
return nil, fmt.Errorf("could not parse configured instance id: %w", err)
}
} else {
var err error
uid, err = uuid.NewV7()
if err != nil {
return nil, fmt.Errorf("could not generate uuidv7: %w", err)
}
}
resourceAttrs := make(map[string]string, set.Resource.Attributes().Len())
set.Resource.Attributes().Range(func(k string, v pcommon.Value) bool {
resourceAttrs[k] = v.Str()
return true
})
opampClient := cfg.Server.GetClient(set.Logger)
agent := &opampAgent{
cfg: cfg,
logger: set.Logger,
agentType: agentType,
agentVersion: agentVersion,
agentInstanceID: agentInstanceID,
instanceID: uid,
capabilities: cfg.Capabilities,
opampClient: opampClient,
resourceAttrs: resourceAttrs,
statusSubscriptionWg: &sync.WaitGroup{},
componentHealthWg: &sync.WaitGroup{},
readyCh: make(chan struct{}),
customCapabilityRegistry: newCustomCapabilityRegistry(set.Logger, opampClient),
}
agent.lifetimeCtx, agent.lifetimeCtxCancel = context.WithCancel(context.Background())
if agent.capabilities.ReportsHealth {
agent.initHealthReporting()
}
return agent, nil
}
func parseInstanceIDString(instanceUID string) (uuid.UUID, error) {
parsedUUID, uuidParseErr := uuid.Parse(instanceUID)
if uuidParseErr == nil {
return parsedUUID, nil
}
parsedULID, ulidParseErr := ulid.Parse(instanceUID)
if ulidParseErr == nil {
return uuid.UUID(parsedULID), nil
}
return uuid.Nil, errors.Join(uuidParseErr, ulidParseErr)
}
func stringKeyValue(key, value string) *protobufs.KeyValue {
return &protobufs.KeyValue{
Key: key,
Value: &protobufs.AnyValue{
Value: &protobufs.AnyValue_StringValue{StringValue: value},
},
}
}
func (o *opampAgent) createAgentDescription() error {
hostname, err := os.Hostname()
if err != nil {
return err
}
description := getOSDescription(o.logger)
ident := []*protobufs.KeyValue{
stringKeyValue(string(conventions.ServiceInstanceIDKey), o.agentInstanceID),
stringKeyValue(string(conventions.ServiceNameKey), o.agentType),
stringKeyValue(string(conventions.ServiceVersionKey), o.agentVersion),
}
// Initially construct using a map to properly deduplicate any keys that
// are both automatically determined and defined in the config
nonIdentifyingAttributeMap := map[string]string{}
nonIdentifyingAttributeMap[string(conventions.OSTypeKey)] = runtime.GOOS
nonIdentifyingAttributeMap[string(conventions.HostArchKey)] = runtime.GOARCH
nonIdentifyingAttributeMap[string(conventions.HostNameKey)] = hostname
nonIdentifyingAttributeMap[string(conventions.OSDescriptionKey)] = description
maps.Copy(nonIdentifyingAttributeMap, o.cfg.AgentDescription.NonIdentifyingAttributes)
if o.cfg.AgentDescription.IncludeResourceAttributes {
for k, v := range o.resourceAttrs {
// skip the attributes that are being used in the identifying attributes.
if _, ok := identifyingAttributes[k]; ok {
continue
}
nonIdentifyingAttributeMap[k] = v
}
}
// Sort the non identifying attributes to give them a stable order for tests
keys := expmaps.Keys(nonIdentifyingAttributeMap)
sort.Strings(keys)
nonIdent := make([]*protobufs.KeyValue, 0, len(nonIdentifyingAttributeMap))
for _, k := range keys {
v := nonIdentifyingAttributeMap[k]
nonIdent = append(nonIdent, stringKeyValue(k, v))
}
o.agentDescription = &protobufs.AgentDescription{
IdentifyingAttributes: ident,
NonIdentifyingAttributes: nonIdent,
}
return nil
}
func (o *opampAgent) updateAgentIdentity(instanceID uuid.UUID) {
o.logger.Debug("OpAMP agent identity is being changed",
zap.String("old_id", o.instanceID.String()),
zap.String("new_id", instanceID.String()))
o.instanceID = instanceID
}
func (o *opampAgent) composeEffectiveConfig() *protobufs.EffectiveConfig {
o.eclk.RLock()
defer o.eclk.RUnlock()
if !o.capabilities.ReportsEffectiveConfig || o.effectiveConfig == nil {
return nil
}
m := o.effectiveConfig.ToStringMap()
conf, err := yaml.Marshal(m)
if err != nil {
o.logger.Error("cannot unmarshal effectiveConfig", zap.Any("conf", o.effectiveConfig), zap.Error(err))
return nil
}
return &protobufs.EffectiveConfig{
ConfigMap: &protobufs.AgentConfigMap{
ConfigMap: map[string]*protobufs.AgentConfigFile{
"": {
Body: conf,
ContentType: "text/yaml",
},
},
},
}
}
func (o *opampAgent) onMessage(_ context.Context, msg *types.MessageData) {
if msg.AgentIdentification != nil {
instanceID, err := uuid.FromBytes(msg.AgentIdentification.NewInstanceUid)
if err != nil {
o.logger.Error("Invalid agent ID provided as new instance UID", zap.Error(err))
} else {
o.updateAgentIdentity(instanceID)
}
}
if msg.CustomMessage != nil {
o.customCapabilityRegistry.ProcessMessage(msg.CustomMessage)
}
}
func (o *opampAgent) onCommand(_ context.Context, command *protobufs.ServerToAgentCommand) error {
if command.GetType() != protobufs.CommandType_CommandType_Restart {
o.logger.Debug("ignoring non-restart command")
return nil
}
if o.capabilities.AcceptsRestartCommand {
o.logger.Info("received restart command, sending SIGHUP to reload")
collectorProcess, err := os.FindProcess(os.Getpid())
if err != nil {
return fmt.Errorf("finding current process from pid: %w", err)
}
return collectorProcess.Signal(syscall.SIGHUP)
}
return nil
}
func (o *opampAgent) setHealth(ch *protobufs.ComponentHealth) {
if o.capabilities.ReportsHealth && o.opampClient != nil {
if ch.Healthy && o.startTimeUnixNano == 0 {
ch.StartTimeUnixNano = ch.StatusTimeUnixNano
} else {
ch.StartTimeUnixNano = o.startTimeUnixNano
}
if err := o.opampClient.SetHealth(ch); err != nil {
o.logger.Error("Could not report health to OpAMP server", zap.Error(err))
}
}
}
func getOSDescription(logger *zap.Logger) string {
info, err := host.Info()
if err != nil {
logger.Error("failed getting host info", zap.Error(err))
return runtime.GOOS
}
switch runtime.GOOS {
case "darwin":
return "macOS " + info.PlatformVersion
case "linux":
return cases.Title(language.English).String(info.Platform) + " " + info.PlatformVersion
case "windows":
return info.Platform + " " + info.PlatformVersion
default:
return runtime.GOOS
}
}
func (o *opampAgent) initHealthReporting() {
if !o.capabilities.ReportsHealth {
return
}
o.setHealth(&protobufs.ComponentHealth{Healthy: false})
if o.statusAggregator == nil {
o.statusAggregator = status.NewAggregator(status.PriorityPermanent)
}
statusChan, unsubscribeFunc := o.statusAggregator.Subscribe(status.ScopeAll, status.Verbose)
o.statusSubscriptionWg.Add(1)
go o.statusAggregatorEventLoop(unsubscribeFunc, statusChan)
// Start processing events in the background so that our status watcher doesn't
// block others before the extension starts.
o.componentStatusCh = make(chan *eventSourcePair)
o.componentHealthWg.Add(1)
go o.componentHealthEventLoop()
}
func (o *opampAgent) initAvailableComponents(moduleInfos service.ModuleInfos) {
if !o.capabilities.ReportsAvailableComponents {
return
}
o.availableComponents = &protobufs.AvailableComponents{
Hash: generateAvailableComponentsHash(moduleInfos),
Components: map[string]*protobufs.ComponentDetails{
"receivers": {
SubComponentMap: createComponentTypeAvailableComponentDetails(moduleInfos.Receiver),
},
"processors": {
SubComponentMap: createComponentTypeAvailableComponentDetails(moduleInfos.Processor),
},
"exporters": {
SubComponentMap: createComponentTypeAvailableComponentDetails(moduleInfos.Exporter),
},
"extensions": {
SubComponentMap: createComponentTypeAvailableComponentDetails(moduleInfos.Extension),
},
"connectors": {
SubComponentMap: createComponentTypeAvailableComponentDetails(moduleInfos.Connector),
},
},
}
}
func generateAvailableComponentsHash(moduleInfos service.ModuleInfos) []byte {
var builder strings.Builder
addComponentTypeComponentsToStringBuilder(&builder, moduleInfos.Receiver, "receiver")
addComponentTypeComponentsToStringBuilder(&builder, moduleInfos.Processor, "processor")
addComponentTypeComponentsToStringBuilder(&builder, moduleInfos.Exporter, "exporter")
addComponentTypeComponentsToStringBuilder(&builder, moduleInfos.Extension, "extension")
addComponentTypeComponentsToStringBuilder(&builder, moduleInfos.Connector, "connector")
// Compute the SHA-256 hash of the serialized representation.
hash := sha256.Sum256([]byte(builder.String()))
return hash[:]
}
func addComponentTypeComponentsToStringBuilder(builder *strings.Builder, componentTypeComponents map[component.Type]service.ModuleInfo, componentType string) {
// Collect components and sort them to ensure deterministic ordering.
components := make([]component.Type, 0, len(componentTypeComponents))
for k := range componentTypeComponents {
components = append(components, k)
}
sort.Slice(components, func(i, j int) bool {
return components[i].String() < components[j].String()
})
// Append the component type and its sorted key-value pairs.
builder.WriteString(componentType + ":")
for _, k := range components {
builder.WriteString(k.String() + "=" + componentTypeComponents[k].BuilderRef + ";")
}
}
func createComponentTypeAvailableComponentDetails(componentTypeComponents map[component.Type]service.ModuleInfo) map[string]*protobufs.ComponentDetails {
availableComponentDetails := map[string]*protobufs.ComponentDetails{}
for componentType, r := range componentTypeComponents {
availableComponentDetails[componentType.String()] = &protobufs.ComponentDetails{
Metadata: []*protobufs.KeyValue{
{
Key: "code.namespace",
Value: &protobufs.AnyValue{
Value: &protobufs.AnyValue_StringValue{
StringValue: r.BuilderRef,
},
},
},
},
}
}
return availableComponentDetails
}
func (o *opampAgent) componentHealthEventLoop() {
// Record events with component.StatusStarting, but queue other events until
// PipelineWatcher.Ready is called. This prevents aggregate statuses from
// flapping between StatusStarting and StatusOK as components are started
// individually by the service.
var eventQueue []*eventSourcePair
defer o.componentHealthWg.Done()
for loop := true; loop; {
select {
case esp, ok := <-o.componentStatusCh:
if !ok {
return
}
if esp.event.Status() != componentstatus.StatusStarting {
eventQueue = append(eventQueue, esp)
continue
}
o.statusAggregator.RecordStatus(esp.source, esp.event)
case <-o.readyCh:
for _, esp := range eventQueue {
o.statusAggregator.RecordStatus(esp.source, esp.event)
}
eventQueue = nil
loop = false
case <-o.lifetimeCtx.Done():
return
}
}
// After PipelineWatcher.Ready, record statuses as they are received.
for {
select {
case esp, ok := <-o.componentStatusCh:
if !ok {
return
}
o.statusAggregator.RecordStatus(esp.source, esp.event)
case <-o.lifetimeCtx.Done():
return
}
}
}
func (o *opampAgent) statusAggregatorEventLoop(unsubscribeFunc status.UnsubscribeFunc, statusChan <-chan *status.AggregateStatus) {
defer func() {
unsubscribeFunc()
o.statusSubscriptionWg.Done()
}()
for {
select {
case <-o.lifetimeCtx.Done():
return
case statusUpdate, ok := <-statusChan:
if !ok {
return
}
if statusUpdate == nil || statusUpdate.Status() == componentstatus.StatusNone {
continue
}
componentHealth := convertComponentHealth(statusUpdate)
o.setHealth(componentHealth)
}
}
}
func convertComponentHealth(statusUpdate *status.AggregateStatus) *protobufs.ComponentHealth {
var isHealthy bool
if statusUpdate.Status() == componentstatus.StatusOK {
isHealthy = true
} else {
isHealthy = false
}
componentHealth := &protobufs.ComponentHealth{
Healthy: isHealthy,
Status: statusUpdate.Status().String(),
StatusTimeUnixNano: uint64(statusUpdate.Timestamp().UnixNano()),
}
if statusUpdate.Err() != nil {
componentHealth.LastError = statusUpdate.Err().Error()
}
if len(statusUpdate.ComponentStatusMap) > 0 {
componentHealth.ComponentHealthMap = map[string]*protobufs.ComponentHealth{}
for comp, compState := range statusUpdate.ComponentStatusMap {
componentHealth.ComponentHealthMap[comp] = convertComponentHealth(compState)
}
}
return componentHealth
}