-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
metric.go
206 lines (187 loc) · 5.54 KB
/
metric.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2022-present Datadog, Inc.
package goflowlib
import (
"fmt"
"github.com/DataDog/datadog-agent/pkg/metrics"
promClient "github.com/prometheus/client_model/go"
)
type remapperType func(string) string
type mappedMetric struct {
name string
allowedTagKeys []string
valueRemapper map[string]remapperType
keyRemapper map[string]string
extraTags []string
}
func (m mappedMetric) isAllowedTagKey(tagKey string) bool {
for _, allowedTagKey := range m.allowedTagKeys {
if tagKey == allowedTagKey {
return true
}
}
return false
}
var collectorTypeMapper = map[string]string{
"NetFlowV5": "netflow5",
"NetFlow": "netflow",
"sFlow": "sflow",
}
var flowsetMapper = map[string]string{
"DataFlowSet": "data_flow_set",
"TemplateFlowSet": "template_flow_set",
"OptionsTemplateFlowSet": "options_template_flow_set",
"OptionsDataFlowSet": "options_data_flow_set",
}
// metricNameMapping maps goflow prometheus metrics to datadog netflow telemetry metrics
var metricNameMapping = map[string]mappedMetric{
"flow_decoder_count": {
name: "decoder.messages",
allowedTagKeys: []string{"name", "worker"},
valueRemapper: map[string]remapperType{
"name": remapCollectorType,
},
keyRemapper: map[string]string{
"name": "collector_type",
},
},
"flow_decoder_error_count": {
name: "decoder.errors",
allowedTagKeys: []string{"name", "worker"},
valueRemapper: map[string]remapperType{
"name": remapCollectorType,
},
keyRemapper: map[string]string{
"name": "collector_type",
},
},
"flow_process_nf_count": {
name: "processor.processed",
allowedTagKeys: []string{"router", "version"},
keyRemapper: map[string]string{
"router": "exporter_ip",
},
extraTags: []string{"flow_protocol:netflow"},
},
"flow_process_nf_flowset_sum": {
name: "processor.flowsets",
allowedTagKeys: []string{"router", "type", "version"},
valueRemapper: map[string]remapperType{
"type": remapFlowset,
},
keyRemapper: map[string]string{
"router": "exporter_ip",
},
extraTags: []string{"flow_protocol:netflow"},
},
"flow_process_nf_errors_count": {
name: "processor.errors",
allowedTagKeys: []string{"router", "error"},
keyRemapper: map[string]string{
"router": "exporter_ip",
},
extraTags: []string{"flow_protocol:netflow"},
},
"flow_traffic_bytes": {
name: "traffic.bytes",
allowedTagKeys: []string{"local_port", "remote_ip", "type"},
keyRemapper: map[string]string{
"local_port": "listener_port",
"remote_ip": "exporter_ip",
"type": "collector_type",
},
valueRemapper: map[string]remapperType{
"type": remapCollectorType,
},
},
"flow_traffic_packets": {
name: "traffic.packets",
allowedTagKeys: []string{"local_port", "remote_ip", "type"},
keyRemapper: map[string]string{
"local_port": "listener_port",
"remote_ip": "exporter_ip",
"type": "collector_type",
},
valueRemapper: map[string]remapperType{
"type": remapCollectorType,
},
},
"flow_process_sf_count": {
name: "processor.processed",
allowedTagKeys: []string{"router", "version"},
keyRemapper: map[string]string{
"router": "exporter_ip",
},
extraTags: []string{"flow_protocol:sflow"},
},
"flow_process_sf_errors_count": {
name: "processor.errors",
allowedTagKeys: []string{"router", "error"},
keyRemapper: map[string]string{
"router": "exporter_ip",
},
extraTags: []string{"flow_protocol:sflow"},
},
}
func remapCollectorType(goflowType string) string {
return collectorTypeMapper[goflowType]
}
func remapFlowset(flowset string) string {
return flowsetMapper[flowset]
}
// ConvertMetric converts prometheus metric to datadog compatible metrics
func ConvertMetric(metric *promClient.Metric, metricFamily *promClient.MetricFamily) (
metrics.MetricType, // metric type
string, // metric name
float64, // metric value
[]string, // metric tags
error,
) {
var ddMetricType metrics.MetricType
var floatValue float64
var tags []string
origMetricName := metricFamily.GetName()
aMappedMetric, ok := metricNameMapping[origMetricName]
if !ok {
return 0, "", 0, nil, fmt.Errorf("metric mapping not found for %s", origMetricName)
}
promMetricType := metricFamily.GetType()
switch promMetricType {
case promClient.MetricType_COUNTER:
floatValue = metric.GetCounter().GetValue()
ddMetricType = metrics.MonotonicCountType
case promClient.MetricType_GAUGE:
floatValue = metric.GetGauge().GetValue()
ddMetricType = metrics.GaugeType
default:
name := promClient.MetricType_name[int32(promMetricType)]
return 0, "", 0, nil, fmt.Errorf("metric type `%s` (%d) not supported", name, promMetricType)
}
for _, labelPair := range metric.GetLabel() {
tagKey := labelPair.GetName()
// check is allowed tag key
if !aMappedMetric.isAllowedTagKey(tagKey) {
continue
}
tagVal := labelPair.GetValue()
// remap metric value
valueRemapperFn, ok := aMappedMetric.valueRemapper[tagKey]
if ok {
tagVal = valueRemapperFn(tagVal)
}
// remap metric key
newKey, ok := aMappedMetric.keyRemapper[tagKey]
if ok {
tagKey = newKey
}
if tagKey != "" && tagVal != "" {
tags = append(tags, tagKey+":"+tagVal)
}
}
if len(aMappedMetric.extraTags) > 0 {
tags = append(tags, aMappedMetric.extraTags...)
}
return ddMetricType, aMappedMetric.name, floatValue, tags, nil
}