forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.go
161 lines (135 loc) · 4.86 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
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package cloudfoundryreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/cloudfoundryreceiver"
import (
"context"
"errors"
"fmt"
"sync"
"time"
"code.cloudfoundry.org/go-loggregator"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/receiverhelper"
)
const (
transport = "http"
dataFormat = "cloudfoundry"
instrumentationLibName = "otelcol/cloudfoundry"
)
var _ receiver.Metrics = (*cloudFoundryReceiver)(nil)
// newCloudFoundryReceiver implements the receiver.Metrics for Cloud Foundry protocol.
type cloudFoundryReceiver struct {
settings component.TelemetrySettings
cancel context.CancelFunc
config Config
nextConsumer consumer.Metrics
obsrecv *receiverhelper.ObsReport
goroutines sync.WaitGroup
receiverStartTime time.Time
}
// newCloudFoundryReceiver creates the Cloud Foundry receiver with the given parameters.
func newCloudFoundryReceiver(
settings receiver.CreateSettings,
config Config,
nextConsumer consumer.Metrics) (receiver.Metrics, error) {
obsrecv, err := receiverhelper.NewObsReport(receiverhelper.ObsReportSettings{
ReceiverID: settings.ID,
Transport: transport,
ReceiverCreateSettings: settings,
})
if err != nil {
return nil, err
}
return &cloudFoundryReceiver{
settings: settings.TelemetrySettings,
config: config,
nextConsumer: nextConsumer,
obsrecv: obsrecv,
receiverStartTime: time.Now(),
}, nil
}
func (cfr *cloudFoundryReceiver) Start(ctx context.Context, host component.Host) error {
tokenProvider, tokenErr := newUAATokenProvider(cfr.settings.Logger, cfr.config.UAA.LimitedClientConfig, cfr.config.UAA.Username, string(cfr.config.UAA.Password))
if tokenErr != nil {
return fmt.Errorf("create cloud foundry UAA token provider: %w", tokenErr)
}
streamFactory, streamErr := newEnvelopeStreamFactory(
ctx,
cfr.settings,
tokenProvider,
cfr.config.RLPGateway.ClientConfig,
host,
)
if streamErr != nil {
return fmt.Errorf("creating cloud foundry RLP envelope stream factory: %w", streamErr)
}
innerCtx, cancel := context.WithCancel(ctx)
cfr.cancel = cancel
cfr.goroutines.Add(1)
go func() {
defer cfr.goroutines.Done()
cfr.settings.Logger.Debug("cloud foundry receiver starting")
_, tokenErr = tokenProvider.ProvideToken()
if tokenErr != nil {
cfr.settings.ReportStatus(component.NewFatalErrorEvent(fmt.Errorf("cloud foundry receiver failed to fetch initial token from UAA: %w", tokenErr)))
return
}
envelopeStream, err := streamFactory.CreateStream(innerCtx, cfr.config.RLPGateway.ShardID)
if err != nil {
cfr.settings.ReportStatus(component.NewFatalErrorEvent(fmt.Errorf("creating RLP gateway envelope stream: %w", err)))
return
}
cfr.streamMetrics(innerCtx, envelopeStream)
cfr.settings.Logger.Debug("cloudfoundry metrics streamer stopped")
}()
return nil
}
func (cfr *cloudFoundryReceiver) Shutdown(_ context.Context) error {
if cfr.cancel == nil {
return nil
}
cfr.cancel()
cfr.goroutines.Wait()
return nil
}
func (cfr *cloudFoundryReceiver) streamMetrics(
ctx context.Context,
stream loggregator.EnvelopeStream) {
for {
// Blocks until non-empty result or context is cancelled (returns nil in that case)
envelopes := stream()
if envelopes == nil {
// If context has not been cancelled, then nil means the shutdown was due to an error within stream
if ctx.Err() == nil {
cfr.settings.ReportStatus(component.NewFatalErrorEvent(errors.New("RLP gateway streamer shut down due to an error")))
}
break
}
metrics := pmetric.NewMetrics()
libraryMetrics := createLibraryMetricsSlice(metrics)
for _, envelope := range envelopes {
if envelope != nil {
// There is no concept of startTime in CF loggregator, and we do not know the uptime of the component
// from which the metric originates, so just provide receiver start time as metric start time
convertEnvelopeToMetrics(envelope, libraryMetrics, cfr.receiverStartTime)
}
}
if libraryMetrics.Len() > 0 {
obsCtx := cfr.obsrecv.StartMetricsOp(ctx)
err := cfr.nextConsumer.ConsumeMetrics(ctx, metrics)
cfr.obsrecv.EndMetricsOp(obsCtx, dataFormat, metrics.DataPointCount(), err)
}
}
}
func createLibraryMetricsSlice(metrics pmetric.Metrics) pmetric.MetricSlice {
resourceMetrics := metrics.ResourceMetrics()
resourceMetric := resourceMetrics.AppendEmpty()
resourceMetric.Resource().Attributes()
libraryMetricsSlice := resourceMetric.ScopeMetrics()
libraryMetrics := libraryMetricsSlice.AppendEmpty()
libraryMetrics.Scope().SetName(instrumentationLibName)
return libraryMetrics.Metrics()
}