Skip to content

Commit e8a0def

Browse files
[receiver/statsd] Make full config structure public (#38186)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Currently some parts of the config struct are in an internal package, which makes using the exported types from the receiver's module insufficient for programmatically generating a config for the receiver. Since the config types are shared with the parsing code, I had to move the types into a non-top-level package. I chose to rename the `internal/protocol` subpackage to `internal/parser` since most code in there appears to be related to parsing, and make a public `protocol` package that contains types related to configuring things related to the StatsD protocol. I don't have a strong opinions on the names for any of the packages and am open to suggestions. --------- Co-authored-by: Antoine Toulme <[email protected]>
1 parent 1b86776 commit e8a0def

File tree

12 files changed

+124
-85
lines changed

12 files changed

+124
-85
lines changed

.chloggen/statsd-public-config.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: receiver/statsd
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Make all types within the config struct public
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [38186]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: This allows programmatically generating the receiver's config using the module's public types.
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [api]

receiver/statsdreceiver/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"go.opentelemetry.io/collector/config/confignet"
1313
"go.uber.org/multierr"
1414

15-
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/protocol"
15+
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/protocol"
1616
)
1717

1818
// Config defines configuration for StatsD receiver.

receiver/statsdreceiver/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"go.opentelemetry.io/collector/confmap/xconfmap"
1919

2020
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/metadata"
21-
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/protocol"
21+
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/protocol"
2222
)
2323

2424
func TestLoadConfig(t *testing.T) {

receiver/statsdreceiver/factory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"go.opentelemetry.io/collector/receiver"
1414

1515
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/metadata"
16-
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/protocol"
16+
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/protocol"
1717
)
1818

1919
const (

receiver/statsdreceiver/internal/protocol/metric_translator.go renamed to receiver/statsdreceiver/internal/parser/metric_translator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

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"
55

66
import (
77
"sort"

receiver/statsdreceiver/internal/protocol/metric_translator_test.go renamed to receiver/statsdreceiver/internal/parser/metric_translator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package protocol
4+
package parser
55

66
import (
77
"testing"

receiver/statsdreceiver/internal/protocol/package_test.go renamed to receiver/statsdreceiver/internal/parser/package_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package protocol
4+
package parser
55

66
import (
77
"testing"

receiver/statsdreceiver/internal/protocol/parser.go renamed to receiver/statsdreceiver/internal/parser/parser.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

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"
55

66
import (
77
"net"
88

99
"go.opentelemetry.io/collector/client"
1010
"go.opentelemetry.io/collector/pdata/pmetric"
11+
12+
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/protocol"
1113
)
1214

1315
// Parser is something that can map input StatsD strings to OTLP Metric representations.
1416
type Parser interface {
15-
Initialize(enableMetricType bool, enableSimpleTags bool, isMonotonicCounter bool, enableIPOnlyAggregation bool, sendTimerHistogram []TimerHistogramMapping) error
17+
Initialize(enableMetricType bool, enableSimpleTags bool, isMonotonicCounter bool, enableIPOnlyAggregation bool, sendTimerHistogram []protocol.TimerHistogramMapping) error
1618
GetMetrics() []BatchMetrics
1719
Aggregate(line string, addr net.Addr) error
1820
}

receiver/statsdreceiver/internal/protocol/statsd_parser.go renamed to receiver/statsdreceiver/internal/parser/statsd_parser.go

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

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"
55

66
import (
77
"errors"
@@ -18,18 +18,16 @@ import (
1818
"go.opentelemetry.io/collector/pdata/pmetric"
1919
semconv "go.opentelemetry.io/collector/semconv/v1.22.0"
2020
"go.opentelemetry.io/otel/attribute"
21+
22+
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/protocol"
2123
)
2224

2325
var (
2426
errEmptyMetricName = errors.New("empty metric name")
2527
errEmptyMetricValue = errors.New("empty metric value")
2628
)
2729

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"
3331

3432
const (
3533
tagMetricType = "metric_type"
@@ -40,46 +38,17 @@ const (
4038
TimingType MetricType = "ms"
4139
DistributionType MetricType = "d"
4240

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-
5741
receiverName = "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver"
5842
)
5943

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-
7544
type ObserverCategory struct {
76-
method ObserverType
45+
method protocol.ObserverType
7746
histogramConfig structure.Config
7847
summaryPercentiles []float64
7948
}
8049

8150
var defaultObserverCategory = ObserverCategory{
82-
method: DefaultObserverType,
51+
method: protocol.DefaultObserverType,
8352
}
8453

8554
// StatsDParser supports the Parse method for parsing StatsD messages with Tags.
@@ -146,28 +115,28 @@ type statsDMetricDescription struct {
146115
attrs attribute.Set
147116
}
148117

149-
func (t MetricType) FullName() TypeName {
118+
func (t MetricType) FullName() protocol.TypeName {
150119
switch t {
151120
case GaugeType:
152-
return GaugeTypeName
121+
return protocol.GaugeTypeName
153122
case CounterType:
154-
return CounterTypeName
123+
return protocol.CounterTypeName
155124
case TimingType:
156-
return TimingTypeName
125+
return protocol.TimingTypeName
157126
case HistogramType:
158-
return HistogramTypeName
127+
return protocol.HistogramTypeName
159128
case DistributionType:
160-
return DistributionTypeName
129+
return protocol.DistributionTypeName
161130
}
162-
return TypeName(fmt.Sprintf("unknown(%s)", t))
131+
return protocol.TypeName(fmt.Sprintf("unknown(%s)", t))
163132
}
164133

165134
func (p *StatsDParser) resetState(when time.Time) {
166135
p.lastIntervalTime = when
167136
p.instrumentsByAddress = make(map[netAddr]*instruments)
168137
}
169138

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 {
171140
p.resetState(timeNowFunc())
172141

173142
p.histogramEvents = defaultObserverCategory
@@ -180,21 +149,21 @@ func (p *StatsDParser) Initialize(enableMetricType bool, enableSimpleTags bool,
180149
// Note: validation occurs in ("../".Config).validate()
181150
for _, eachMap := range sendTimerHistogram {
182151
switch eachMap.StatsdType {
183-
case HistogramTypeName, DistributionTypeName:
152+
case protocol.HistogramTypeName, protocol.DistributionTypeName:
184153
p.histogramEvents.method = eachMap.ObserverType
185154
p.histogramEvents.histogramConfig = expoHistogramConfig(eachMap.Histogram)
186155
p.histogramEvents.summaryPercentiles = eachMap.Summary.Percentiles
187-
case TimingTypeName, TimingAltTypeName:
156+
case protocol.TimingTypeName, protocol.TimingAltTypeName:
188157
p.timerEvents.method = eachMap.ObserverType
189158
p.timerEvents.histogramConfig = expoHistogramConfig(eachMap.Histogram)
190159
p.timerEvents.summaryPercentiles = eachMap.Summary.Percentiles
191-
case CounterTypeName, GaugeTypeName:
160+
case protocol.CounterTypeName, protocol.GaugeTypeName:
192161
}
193162
}
194163
return nil
195164
}
196165

197-
func expoHistogramConfig(opts HistogramConfig) structure.Config {
166+
func expoHistogramConfig(opts protocol.HistogramConfig) structure.Config {
198167
var r []structure.Option
199168
if opts.MaxSize >= structure.MinSize {
200169
r = append(r, structure.WithMaxSize(opts.MaxSize))
@@ -331,9 +300,9 @@ func (p *StatsDParser) Aggregate(line string, addr net.Addr) error {
331300
case TimingType, HistogramType, DistributionType:
332301
category := p.observerCategoryFor(parsedMetric.description.metricType)
333302
switch category.method {
334-
case GaugeObserver:
303+
case protocol.GaugeObserver:
335304
instrument.timersAndDistributions = append(instrument.timersAndDistributions, buildGaugeMetric(parsedMetric, timeNowFunc()))
336-
case SummaryObserver:
305+
case protocol.SummaryObserver:
337306
raw := parsedMetric.sampleValue()
338307
if existing, ok := instrument.summaries[parsedMetric.description]; !ok {
339308
instrument.summaries[parsedMetric.description] = summaryMetric{
@@ -348,7 +317,7 @@ func (p *StatsDParser) Aggregate(line string, addr net.Addr) error {
348317
percentiles: category.summaryPercentiles,
349318
}
350319
}
351-
case HistogramObserver:
320+
case protocol.HistogramObserver:
352321
raw := parsedMetric.sampleValue()
353322
var agg *histogramStructure
354323
if existing, ok := instrument.histograms[parsedMetric.description]; ok {
@@ -366,7 +335,7 @@ func (p *StatsDParser) Aggregate(line string, addr net.Addr) error {
366335
uint64(raw.count), // Note! Rounding float64 to uint64 here.
367336
)
368337

369-
case DisableObserver:
338+
case protocol.DisableObserver:
370339
// No action.
371340
}
372341
}

0 commit comments

Comments
 (0)