-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathheartbeat.go
135 lines (116 loc) · 3.81 KB
/
heartbeat.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package splunkhecexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/splunkhecexporter"
import (
"context"
"fmt"
"os"
"runtime"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
)
const (
metricsPrefix = "otelcol_exporter_splunkhec_"
defaultHBSentMetricsName = metricsPrefix + "heartbeats_sent"
defaultHBFailedMetricsName = metricsPrefix + "heartbeats_failed"
)
type heartbeater struct {
hbDoneChan chan struct{}
}
func getMetricsName(overrides map[string]string, metricName string) string {
if name, ok := overrides[metricName]; ok {
return name
}
return metricName
}
func newHeartbeater(config *Config, buildInfo component.BuildInfo, pushLogFn func(ctx context.Context, ld plog.Logs) error, meter metric.Meter) *heartbeater {
interval := config.Heartbeat.Interval
if interval == 0 {
return nil
}
var heartbeatsSent, heartbeatsFailed metric.Int64Counter
var attrs attribute.Set
if config.Telemetry.Enabled {
overrides := config.Telemetry.OverrideMetricsNames
extraAttributes := config.Telemetry.ExtraAttributes
var tags []attribute.KeyValue
for key, val := range extraAttributes {
tags = append(tags, attribute.String(key, val))
}
attrs = attribute.NewSet(tags...)
var err error
heartbeatsSent, err = meter.Int64Counter(
getMetricsName(overrides, defaultHBSentMetricsName),
metric.WithDescription("number of heartbeats sent"),
metric.WithUnit("1"),
)
if err != nil {
return nil
}
heartbeatsFailed, err = meter.Int64Counter(
getMetricsName(overrides, defaultHBFailedMetricsName),
metric.WithDescription("number of heartbeats failed"),
metric.WithUnit("1"),
)
if err != nil {
return nil
}
}
hbter := &heartbeater{
hbDoneChan: make(chan struct{}),
}
go func() {
ticker := time.NewTicker(interval)
for {
select {
case <-hbter.hbDoneChan:
return
case <-ticker.C:
err := hbter.sendHeartbeat(config, buildInfo, pushLogFn)
if config.Telemetry.Enabled {
observe(heartbeatsSent, heartbeatsFailed, attrs, err)
}
}
}
}()
return hbter
}
func (h *heartbeater) shutdown() {
close(h.hbDoneChan)
}
func (h *heartbeater) sendHeartbeat(config *Config, buildInfo component.BuildInfo, pushLogFn func(ctx context.Context, ld plog.Logs) error) error {
return pushLogFn(context.Background(), generateHeartbeatLog(config.HecToOtelAttrs, buildInfo))
}
// there is only use case for open census metrics recording for now. Extend to use open telemetry in the future.
func observe(heartbeatsSent, heartbeatsFailed metric.Int64Counter, attrs attribute.Set, err error) {
if err == nil {
heartbeatsSent.Add(context.Background(), 1, metric.WithAttributeSet(attrs))
} else {
heartbeatsFailed.Add(context.Background(), 1, metric.WithAttributeSet(attrs))
}
}
func generateHeartbeatLog(hecToOtelAttrs splunk.HecToOtelAttrs, buildInfo component.BuildInfo) plog.Logs {
host, err := os.Hostname()
if err != nil {
host = "unknownhost"
}
ret := plog.NewLogs()
resourceLogs := ret.ResourceLogs().AppendEmpty()
resourceAttrs := resourceLogs.Resource().Attributes()
resourceAttrs.PutStr(hecToOtelAttrs.Index, "_internal")
resourceAttrs.PutStr(hecToOtelAttrs.Source, "otelcol")
resourceAttrs.PutStr(hecToOtelAttrs.SourceType, "heartbeat")
resourceAttrs.PutStr(hecToOtelAttrs.Host, host)
logRecord := resourceLogs.ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()
logRecord.Body().SetStr(fmt.Sprintf(
"HeartbeatInfo version=%s description=%s os=%s arch=%s",
buildInfo.Version,
buildInfo.Description,
runtime.GOOS,
runtime.GOARCH))
return ret
}