forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.go
56 lines (47 loc) · 1.99 KB
/
converter.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package cloudfoundryreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/cloudfoundryreceiver"
import (
"time"
"code.cloudfoundry.org/go-loggregator/rpc/loggregator_v2"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
)
const (
attributeNamePrefix = "org.cloudfoundry."
)
func convertEnvelopeToMetrics(envelope *loggregator_v2.Envelope, metricSlice pmetric.MetricSlice, startTime time.Time) {
namePrefix := envelope.Tags["origin"] + "."
switch message := envelope.Message.(type) {
case *loggregator_v2.Envelope_Log:
case *loggregator_v2.Envelope_Counter:
metric := metricSlice.AppendEmpty()
metric.SetName(namePrefix + message.Counter.GetName())
dataPoint := metric.SetEmptySum().DataPoints().AppendEmpty()
dataPoint.SetDoubleValue(float64(message.Counter.GetTotal()))
dataPoint.SetTimestamp(pcommon.Timestamp(envelope.GetTimestamp()))
dataPoint.SetStartTimestamp(pcommon.NewTimestampFromTime(startTime))
copyEnvelopeAttributes(dataPoint.Attributes(), envelope)
case *loggregator_v2.Envelope_Gauge:
for name, value := range message.Gauge.GetMetrics() {
metric := metricSlice.AppendEmpty()
metric.SetName(namePrefix + name)
dataPoint := metric.SetEmptyGauge().DataPoints().AppendEmpty()
dataPoint.SetDoubleValue(value.Value)
dataPoint.SetTimestamp(pcommon.Timestamp(envelope.GetTimestamp()))
dataPoint.SetStartTimestamp(pcommon.NewTimestampFromTime(startTime))
copyEnvelopeAttributes(dataPoint.Attributes(), envelope)
}
}
}
func copyEnvelopeAttributes(attributes pcommon.Map, envelope *loggregator_v2.Envelope) {
for key, value := range envelope.Tags {
attributes.PutStr(attributeNamePrefix+key, value)
}
if envelope.SourceId != "" {
attributes.PutStr(attributeNamePrefix+"source_id", envelope.SourceId)
}
if envelope.InstanceId != "" {
attributes.PutStr(attributeNamePrefix+"instance_id", envelope.InstanceId)
}
}