-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathconverter.go
144 lines (132 loc) · 4.64 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
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package cloudfoundryreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/cloudfoundryreceiver"
import (
"fmt"
"slices"
"time"
"code.cloudfoundry.org/go-loggregator/rpc/loggregator_v2"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
)
const (
attributeNamePrefix = "org.cloudfoundry."
)
var ResourceAttributesKeys = []string{
"index",
"ip",
"deployment",
"id",
"job",
"product",
"instance_group",
"instance_id",
"origin",
"system_domain",
"source_id",
"source_type",
"process_type",
"process_id",
"process_instance_id",
}
var allowResourceAttributes = featuregate.GlobalRegistry().MustRegister(
"cloudfoundry.resourceAttributes.allow",
featuregate.StageAlpha,
featuregate.WithRegisterDescription("When enabled, envelope tags are copied to the metrics as resource attributes instead of datapoint attributes"),
featuregate.WithRegisterFromVersion("v0.117.0"),
)
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_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))
if allowResourceAttributes.IsEnabled() {
attrs := getEnvelopeDataAttributes(envelope)
attrs.CopyTo(dataPoint.Attributes())
} else {
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))
if allowResourceAttributes.IsEnabled() {
attrs := getEnvelopeDataAttributes(envelope)
attrs.CopyTo(dataPoint.Attributes())
} else {
copyEnvelopeAttributes(dataPoint.Attributes(), envelope)
}
}
}
}
func convertEnvelopeToLogs(envelope *loggregator_v2.Envelope, logSlice plog.LogRecordSlice, startTime time.Time) error {
log := logSlice.AppendEmpty()
log.SetTimestamp(pcommon.Timestamp(envelope.GetTimestamp()))
log.SetObservedTimestamp(pcommon.NewTimestampFromTime(startTime))
logLine := string(envelope.GetLog().GetPayload())
log.Body().SetStr(logLine)
//exhaustive:enforce
switch envelope.GetLog().GetType() {
case loggregator_v2.Log_OUT:
log.SetSeverityText(plog.SeverityNumberInfo.String())
log.SetSeverityNumber(plog.SeverityNumberInfo)
case loggregator_v2.Log_ERR:
log.SetSeverityText(plog.SeverityNumberError.String())
log.SetSeverityNumber(plog.SeverityNumberError)
default:
return fmt.Errorf("unsupported envelope log type: %s", envelope.GetLog().GetType())
}
if allowResourceAttributes.IsEnabled() {
attrs := getEnvelopeDataAttributes(envelope)
attrs.CopyTo(log.Attributes())
} else {
copyEnvelopeAttributes(log.Attributes(), envelope)
}
return nil
}
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)
}
}
func getEnvelopeDataAttributes(envelope *loggregator_v2.Envelope) pcommon.Map {
attrs := pcommon.NewMap()
for key, value := range envelope.Tags {
if !slices.Contains(ResourceAttributesKeys, key) {
attrs.PutStr(attributeNamePrefix+key, value)
}
}
return attrs
}
func getEnvelopeResourceAttributes(envelope *loggregator_v2.Envelope) pcommon.Map {
attrs := pcommon.NewMap()
for key, value := range envelope.Tags {
if slices.Contains(ResourceAttributesKeys, key) {
attrs.PutStr(attributeNamePrefix+key, value)
}
}
if envelope.SourceId != "" {
attrs.PutStr(attributeNamePrefix+"source_id", envelope.SourceId)
}
if envelope.InstanceId != "" {
attrs.PutStr(attributeNamePrefix+"instance_id", envelope.InstanceId)
}
return attrs
}