-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
connector.go
133 lines (114 loc) · 3.4 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
// 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.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
telemetryBuilder *metadata.TelemetryBuilder
}
func newConnector(logger *zap.Logger, set component.TelemetrySettings, config component.Config) (*connectorImp, error) {
hm := newHostMetrics()
telemetryBuilder, err := metadata.NewTelemetryBuilder(set,
metadata.WithGrafanacloudHostCountCallback(func() int64 { return int64(hm.count()) }),
)
if err != nil {
return nil, err
}
cfg := config.(*Config)
return &connectorImp{
config: *cfg,
logger: logger,
done: make(chan struct{}),
hostMetrics: hm,
telemetryBuilder: telemetryBuilder,
}, 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.telemetryBuilder.GrafanacloudDatapointCount.Add(ctx, int64(metrics.DataPointCount()))
err = c.metricsConsumer.ConsumeMetrics(ctx, *metrics)
}
c.telemetryBuilder.GrafanacloudFlushCount.Add(ctx, int64(1))
return err
}