forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.go
163 lines (141 loc) · 4.15 KB
/
connector.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package grafanacloudconnector // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/grafanacloudconnector"
import (
"context"
"sync"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/otel/metric"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/connector/grafanacloudconnector/internal/metadata"
)
const (
hostInfoMetric = "traces_host_info"
hostIdentifierAttr = "grafana.host.id"
)
var _ connector.Traces = (*connectorImp)(nil)
type connectorImp struct {
config Config
logger *zap.Logger
started bool
done chan struct{}
shutdownOnce sync.Once
metricsConsumer consumer.Metrics
hostMetrics *hostMetrics
metricHostCount metric.Int64ObservableGauge
metricFlushCount metric.Int64Counter
metricDatapointCount metric.Int64Counter
}
func newConnector(logger *zap.Logger, set component.TelemetrySettings, config component.Config) (*connectorImp, error) {
hm := newHostMetrics()
mHostCount, err := metadata.Meter(set).Int64ObservableGauge(
"grafanacloud_host_count",
metric.WithDescription("Number of unique hosts"),
metric.WithUnit("1"),
metric.WithInt64Callback(func(_ context.Context, result metric.Int64Observer) error {
result.Observe(int64(hm.count()))
return nil
}),
)
if err != nil {
return nil, err
}
mFlushCount, err := metadata.Meter(set).Int64Counter(
"grafanacloud_flush_count",
metric.WithDescription("Number of metrics flushes"),
metric.WithUnit("1"),
)
if err != nil {
return nil, err
}
mDatapointCount, err := metadata.Meter(set).Int64Counter(
"grafanacloud_datapoint_count",
metric.WithDescription("Number of datapoints sent to Grafana Cloud"),
metric.WithUnit("1"),
)
if err != nil {
return nil, err
}
cfg := config.(*Config)
return &connectorImp{
config: *cfg,
logger: logger,
done: make(chan struct{}),
hostMetrics: hm,
metricHostCount: mHostCount,
metricFlushCount: mFlushCount,
metricDatapointCount: mDatapointCount,
}, nil
}
// Capabilities implements connector.Traces.
func (c *connectorImp) Capabilities() consumer.Capabilities {
return consumer.Capabilities{MutatesData: false}
}
// ConsumeTraces implements connector.Traces.
func (c *connectorImp) ConsumeTraces(_ context.Context, td ptrace.Traces) error {
for i := 0; i < td.ResourceSpans().Len(); i++ {
resourceSpan := td.ResourceSpans().At(i)
attrs := resourceSpan.Resource().Attributes()
mapping := attrs.AsRaw()
for _, attrName := range c.config.HostIdentifiers {
if val, ok := mapping[attrName]; ok {
if v, ok := val.(string); ok {
c.hostMetrics.add(v)
}
break
}
}
}
return nil
}
// Start implements connector.Traces.
func (c *connectorImp) Start(ctx context.Context, _ component.Host) error {
c.logger.Info("Starting Grafana Cloud connector")
c.started = true
ticker := time.NewTicker(c.config.MetricsFlushInterval)
go func() {
for {
select {
case <-c.done:
ticker.Stop()
return
case <-ticker.C:
if err := c.flush(ctx); err != nil {
c.logger.Error("Error consuming metrics", zap.Error(err))
}
}
}
}()
return nil
}
// Shutdown implements connector.Traces.
func (c *connectorImp) Shutdown(ctx context.Context) error {
c.shutdownOnce.Do(func() {
c.logger.Info("Stopping Grafana Cloud connector")
if c.started {
// flush metrics on shutdown
if err := c.flush(ctx); err != nil {
c.logger.Error("Error consuming metrics", zap.Error(err))
}
c.done <- struct{}{}
c.started = false
}
})
return nil
}
func (c *connectorImp) flush(ctx context.Context) error {
var err error
metrics, count := c.hostMetrics.metrics()
if count > 0 {
c.hostMetrics.reset()
c.logger.Debug("Flushing metrics", zap.Int("count", count))
c.metricDatapointCount.Add(ctx, int64(metrics.DataPointCount()))
err = c.metricsConsumer.ConsumeMetrics(ctx, *metrics)
}
c.metricFlushCount.Add(ctx, int64(1))
return err
}