@@ -6,11 +6,11 @@ package server
6
6
7
7
import (
8
8
"bytes"
9
+ "errors"
10
+ "fmt"
9
11
"strconv"
10
12
"time"
11
13
12
- "github.com/pkg/errors"
13
-
14
14
"github.com/elastic/beats/v7/libbeat/common"
15
15
"github.com/elastic/beats/v7/metricbeat/helper/server"
16
16
"github.com/elastic/elastic-agent-libs/logp"
@@ -20,8 +20,7 @@ import (
20
20
var errInvalidPacket = errors .New ("invalid statsd packet" )
21
21
22
22
type metricProcessor struct {
23
- registry * registry
24
- reservoirSize int
23
+ registry * registry
25
24
}
26
25
27
26
type statsdMetric struct {
@@ -32,12 +31,12 @@ type statsdMetric struct {
32
31
tags map [string ]string
33
32
}
34
33
35
- func splitTags (rawTags [] byte , kvSep []byte ) map [string ]string {
34
+ func splitTags (rawTags , kvSep []byte ) map [string ]string {
36
35
tags := map [string ]string {}
37
36
for _ , kv := range bytes .Split (rawTags , []byte ("," )) {
38
37
kvSplit := bytes .SplitN (kv , kvSep , 2 )
39
38
if len (kvSplit ) != 2 {
40
- logger .Warnf ("could not parse tags" )
39
+ logger .Warn ("could not parse tags" )
41
40
continue
42
41
}
43
42
tags [string (kvSplit [0 ])] = string (kvSplit [1 ])
@@ -86,14 +85,16 @@ func parseSingle(b []byte) (statsdMetric, error) {
86
85
return s , nil
87
86
}
88
87
89
- // parse will parse a statsd metric into its components
88
+ // parse will parse statsd metrics into individual metric and then its components
90
89
func parse (b []byte ) ([]statsdMetric , error ) {
91
- metrics := []statsdMetric {}
92
- for _ , rawMetric := range bytes .Split (b , []byte ("\n " )) {
93
- if len (rawMetric ) > 0 {
94
- metric , err := parseSingle (rawMetric )
90
+ rawMetrics := bytes .Split (b , []byte ("\n " ))
91
+ metrics := make ([]statsdMetric , 0 , len (rawMetrics ))
92
+ for i := range rawMetrics {
93
+ if len (rawMetrics [i ]) > 0 {
94
+ metric , err := parseSingle (rawMetrics [i ])
95
95
if err != nil {
96
- return metrics , err
96
+ logger .Warnf ("invalid packet: %s" , err )
97
+ continue
97
98
}
98
99
metrics = append (metrics , metric )
99
100
}
@@ -120,13 +121,13 @@ func eventMapping(metricName string, metricValue interface{}, metricSetFields ma
120
121
// Not all labels match
121
122
// Skip and continue to next mapping
122
123
if len (res ) != (len (mapping .Labels ) + 1 ) {
123
- logger .Debugf ("not all labels match in statsd.mapping, skipped" )
124
+ logger .Debug ("not all labels match in statsd.mapping, skipped" )
124
125
continue
125
126
}
126
127
127
128
// Let's add the metric set fields from labels
128
129
names := mapping .regex .SubexpNames ()
129
- for i , _ := range res {
130
+ for i := range res {
130
131
for _ , label := range mapping .Labels {
131
132
if label .Attr != names [i ] {
132
133
continue
@@ -139,8 +140,6 @@ func eventMapping(metricName string, metricValue interface{}, metricSetFields ma
139
140
// Let's add the metric with the value field
140
141
metricSetFields [mapping .Value .Field ] = metricValue
141
142
}
142
-
143
- return
144
143
}
145
144
146
145
func newMetricProcessor (ttl time.Duration ) * metricProcessor {
@@ -162,10 +161,10 @@ func (p *metricProcessor) processSingle(m statsdMetric) error {
162
161
var err error
163
162
sampleRate , err = strconv .ParseFloat (m .sampleRate , 64 )
164
163
if err != nil {
165
- return errors . Wrapf ( err , "failed to process metric `%s` sample rate `%s`" , m .name , m .sampleRate )
164
+ return fmt . Errorf ( "failed to process metric `%s` sample rate `%s`: %w " , m .name , m .sampleRate , err )
166
165
}
167
166
if sampleRate <= 0.0 {
168
- return errors .Errorf ("sample rate of 0.0 is invalid for metric `%s`" , m .name )
167
+ return fmt .Errorf ("sample rate of 0.0 is invalid for metric `%s`: %w " , m .name , err )
169
168
}
170
169
}
171
170
@@ -174,7 +173,7 @@ func (p *metricProcessor) processSingle(m statsdMetric) error {
174
173
c := p .registry .GetOrNewCounter (m .name , m .tags )
175
174
v , err := strconv .ParseInt (m .value , 10 , 64 )
176
175
if err != nil {
177
- return errors . Wrapf ( err , "failed to process counter `%s` with value `%s`" , m .name , m .value )
176
+ return fmt . Errorf ( "failed to process counter `%s` with value `%s`: %w " , m .name , m .value , err )
178
177
}
179
178
// apply sample rate
180
179
v = int64 (float64 (v ) * (1.0 / sampleRate ))
@@ -183,9 +182,8 @@ func (p *metricProcessor) processSingle(m statsdMetric) error {
183
182
c := p .registry .GetOrNewGauge64 (m .name , m .tags )
184
183
v , err := strconv .ParseFloat (m .value , 64 )
185
184
if err != nil {
186
- return errors . Wrapf ( err , "failed to process gauge `%s` with value `%s`" , m .name , m .value )
185
+ return fmt . Errorf ( "failed to process gauge `%s` with value `%s`: %w " , m .name , m .value , err )
187
186
}
188
-
189
187
// inc/dec or set
190
188
if m .value [0 ] == '+' || m .value [0 ] == '-' {
191
189
c .Inc (v )
@@ -196,14 +194,14 @@ func (p *metricProcessor) processSingle(m statsdMetric) error {
196
194
c := p .registry .GetOrNewTimer (m .name , m .tags )
197
195
v , err := strconv .ParseFloat (m .value , 64 )
198
196
if err != nil {
199
- return errors . Wrapf ( err , "failed to process timer `%s` with value `%s`" , m .name , m .value )
197
+ return fmt . Errorf ( "failed to process timer `%s` with value `%s`: %w " , m .name , m .value , err )
200
198
}
201
199
c .SampledUpdate (time .Duration (v ), sampleRate )
202
200
case "h" : // TODO: can these be floats?
203
201
c := p .registry .GetOrNewHistogram (m .name , m .tags )
204
202
v , err := strconv .ParseInt (m .value , 10 , 64 )
205
203
if err != nil {
206
- return errors . Wrapf ( err , "failed to process histogram `%s` with value `%s`" , m .name , m .value )
204
+ return fmt . Errorf ( "failed to process histogram `%s` with value `%s`: %w " , m .name , m .value , err )
207
205
}
208
206
c .Update (v )
209
207
case "s" :
0 commit comments