1
1
// Copyright The OpenTelemetry Authors
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
- package protocol // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/protocol "
4
+ package parser // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/parser "
5
5
6
6
import (
7
7
"errors"
@@ -18,18 +18,16 @@ import (
18
18
"go.opentelemetry.io/collector/pdata/pmetric"
19
19
semconv "go.opentelemetry.io/collector/semconv/v1.22.0"
20
20
"go.opentelemetry.io/otel/attribute"
21
+
22
+ "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/protocol"
21
23
)
22
24
23
25
var (
24
26
errEmptyMetricName = errors .New ("empty metric name" )
25
27
errEmptyMetricValue = errors .New ("empty metric value" )
26
28
)
27
29
28
- type (
29
- MetricType string // From the statsd line e.g., "c", "g", "h"
30
- TypeName string // How humans describe the MetricTypes ("counter", "gauge")
31
- ObserverType string // How the server will aggregate histogram and timings ("gauge", "summary")
32
- )
30
+ type MetricType string // From the statsd line e.g., "c", "g", "h"
33
31
34
32
const (
35
33
tagMetricType = "metric_type"
@@ -40,46 +38,17 @@ const (
40
38
TimingType MetricType = "ms"
41
39
DistributionType MetricType = "d"
42
40
43
- CounterTypeName TypeName = "counter"
44
- GaugeTypeName TypeName = "gauge"
45
- HistogramTypeName TypeName = "histogram"
46
- TimingTypeName TypeName = "timing"
47
- TimingAltTypeName TypeName = "timer"
48
- DistributionTypeName TypeName = "distribution"
49
-
50
- GaugeObserver ObserverType = "gauge"
51
- SummaryObserver ObserverType = "summary"
52
- HistogramObserver ObserverType = "histogram"
53
- DisableObserver ObserverType = "disabled"
54
-
55
- DefaultObserverType = DisableObserver
56
-
57
41
receiverName = "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver"
58
42
)
59
43
60
- type TimerHistogramMapping struct {
61
- StatsdType TypeName `mapstructure:"statsd_type"`
62
- ObserverType ObserverType `mapstructure:"observer_type"`
63
- Histogram HistogramConfig `mapstructure:"histogram"`
64
- Summary SummaryConfig `mapstructure:"summary"`
65
- }
66
-
67
- type HistogramConfig struct {
68
- MaxSize int32 `mapstructure:"max_size"`
69
- }
70
-
71
- type SummaryConfig struct {
72
- Percentiles []float64 `mapstructure:"percentiles"`
73
- }
74
-
75
44
type ObserverCategory struct {
76
- method ObserverType
45
+ method protocol. ObserverType
77
46
histogramConfig structure.Config
78
47
summaryPercentiles []float64
79
48
}
80
49
81
50
var defaultObserverCategory = ObserverCategory {
82
- method : DefaultObserverType ,
51
+ method : protocol . DefaultObserverType ,
83
52
}
84
53
85
54
// StatsDParser supports the Parse method for parsing StatsD messages with Tags.
@@ -146,28 +115,28 @@ type statsDMetricDescription struct {
146
115
attrs attribute.Set
147
116
}
148
117
149
- func (t MetricType ) FullName () TypeName {
118
+ func (t MetricType ) FullName () protocol. TypeName {
150
119
switch t {
151
120
case GaugeType :
152
- return GaugeTypeName
121
+ return protocol . GaugeTypeName
153
122
case CounterType :
154
- return CounterTypeName
123
+ return protocol . CounterTypeName
155
124
case TimingType :
156
- return TimingTypeName
125
+ return protocol . TimingTypeName
157
126
case HistogramType :
158
- return HistogramTypeName
127
+ return protocol . HistogramTypeName
159
128
case DistributionType :
160
- return DistributionTypeName
129
+ return protocol . DistributionTypeName
161
130
}
162
- return TypeName (fmt .Sprintf ("unknown(%s)" , t ))
131
+ return protocol . TypeName (fmt .Sprintf ("unknown(%s)" , t ))
163
132
}
164
133
165
134
func (p * StatsDParser ) resetState (when time.Time ) {
166
135
p .lastIntervalTime = when
167
136
p .instrumentsByAddress = make (map [netAddr ]* instruments )
168
137
}
169
138
170
- func (p * StatsDParser ) Initialize (enableMetricType bool , enableSimpleTags bool , isMonotonicCounter bool , enableIPOnlyAggregation bool , sendTimerHistogram []TimerHistogramMapping ) error {
139
+ func (p * StatsDParser ) Initialize (enableMetricType bool , enableSimpleTags bool , isMonotonicCounter bool , enableIPOnlyAggregation bool , sendTimerHistogram []protocol. TimerHistogramMapping ) error {
171
140
p .resetState (timeNowFunc ())
172
141
173
142
p .histogramEvents = defaultObserverCategory
@@ -180,21 +149,21 @@ func (p *StatsDParser) Initialize(enableMetricType bool, enableSimpleTags bool,
180
149
// Note: validation occurs in ("../".Config).validate()
181
150
for _ , eachMap := range sendTimerHistogram {
182
151
switch eachMap .StatsdType {
183
- case HistogramTypeName , DistributionTypeName :
152
+ case protocol . HistogramTypeName , protocol . DistributionTypeName :
184
153
p .histogramEvents .method = eachMap .ObserverType
185
154
p .histogramEvents .histogramConfig = expoHistogramConfig (eachMap .Histogram )
186
155
p .histogramEvents .summaryPercentiles = eachMap .Summary .Percentiles
187
- case TimingTypeName , TimingAltTypeName :
156
+ case protocol . TimingTypeName , protocol . TimingAltTypeName :
188
157
p .timerEvents .method = eachMap .ObserverType
189
158
p .timerEvents .histogramConfig = expoHistogramConfig (eachMap .Histogram )
190
159
p .timerEvents .summaryPercentiles = eachMap .Summary .Percentiles
191
- case CounterTypeName , GaugeTypeName :
160
+ case protocol . CounterTypeName , protocol . GaugeTypeName :
192
161
}
193
162
}
194
163
return nil
195
164
}
196
165
197
- func expoHistogramConfig (opts HistogramConfig ) structure.Config {
166
+ func expoHistogramConfig (opts protocol. HistogramConfig ) structure.Config {
198
167
var r []structure.Option
199
168
if opts .MaxSize >= structure .MinSize {
200
169
r = append (r , structure .WithMaxSize (opts .MaxSize ))
@@ -331,9 +300,9 @@ func (p *StatsDParser) Aggregate(line string, addr net.Addr) error {
331
300
case TimingType , HistogramType , DistributionType :
332
301
category := p .observerCategoryFor (parsedMetric .description .metricType )
333
302
switch category .method {
334
- case GaugeObserver :
303
+ case protocol . GaugeObserver :
335
304
instrument .timersAndDistributions = append (instrument .timersAndDistributions , buildGaugeMetric (parsedMetric , timeNowFunc ()))
336
- case SummaryObserver :
305
+ case protocol . SummaryObserver :
337
306
raw := parsedMetric .sampleValue ()
338
307
if existing , ok := instrument .summaries [parsedMetric .description ]; ! ok {
339
308
instrument .summaries [parsedMetric .description ] = summaryMetric {
@@ -348,7 +317,7 @@ func (p *StatsDParser) Aggregate(line string, addr net.Addr) error {
348
317
percentiles : category .summaryPercentiles ,
349
318
}
350
319
}
351
- case HistogramObserver :
320
+ case protocol . HistogramObserver :
352
321
raw := parsedMetric .sampleValue ()
353
322
var agg * histogramStructure
354
323
if existing , ok := instrument .histograms [parsedMetric .description ]; ok {
@@ -366,7 +335,7 @@ func (p *StatsDParser) Aggregate(line string, addr net.Addr) error {
366
335
uint64 (raw .count ), // Note! Rounding float64 to uint64 here.
367
336
)
368
337
369
- case DisableObserver :
338
+ case protocol . DisableObserver :
370
339
// No action.
371
340
}
372
341
}
0 commit comments