forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreceiver.go
87 lines (70 loc) · 2.36 KB
/
receiver.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package hostmetricsreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver"
import (
"context"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/receiver"
"go.uber.org/zap"
metadataPkg "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata"
)
const entityType = "host"
type hostEntitiesReceiver struct {
cfg *Config
nextLogs consumer.Logs
cancel context.CancelFunc
settings *receiver.Settings
}
func (hmr *hostEntitiesReceiver) Start(ctx context.Context, _ component.Host) error {
ctx, hmr.cancel = context.WithCancel(ctx)
if hmr.nextLogs != nil {
hmr.sendEntityEvent(ctx)
if hmr.cfg.MetadataCollectionInterval != 0 {
ticker := time.NewTicker(hmr.cfg.MetadataCollectionInterval)
go func() {
for {
select {
case <-ticker.C:
hmr.sendEntityEvent(ctx)
case <-ctx.Done():
ticker.Stop()
return
}
}
}()
}
}
return nil
}
func (hmr *hostEntitiesReceiver) Shutdown(_ context.Context) error {
if hmr.cancel != nil {
hmr.cancel()
}
return nil
}
func (hmr *hostEntitiesReceiver) sendEntityEvent(ctx context.Context) {
timestamp := pcommon.NewTimestampFromTime(time.Now())
out := metadataPkg.NewEntityEventsSlice()
entityEvent := out.AppendEmpty()
entityEvent.SetTimestamp(timestamp)
state := entityEvent.SetEntityState()
state.SetEntityType(entityType)
if hmr.cfg.MetadataCollectionInterval != 0 {
state.SetInterval(hmr.cfg.MetadataCollectionInterval)
}
logs := out.ConvertAndMoveToLogs()
err := hmr.nextLogs.ConsumeLogs(ctx, logs)
if err != nil {
hmr.settings.Logger.Error("Error sending entity event to the consumer", zap.Error(err))
}
// Note: receiver contract says that we need to retry sending if the
// returned error is not Permanent. However, we are not doing it here.
// Instead, we rely on the fact the metadata is collected periodically
// and the entity events will be delivered on the next cycle. This is
// fine because we deliver cumulative entity state.
// This allows us to avoid stressing the Collector or its destination
// unnecessarily (typically non-Permanent errors happen in stressed conditions).
}