Skip to content

Commit 8dfbdf1

Browse files
atoulmedmitryax
authored andcommitted
[receiver/collectd] Move to use HTTPServerSettings with collectdreceiver (open-telemetry#28812)
**Description:** Overhauls collectdreceiver to use the latest config helper features **Link to tracking Issue:** Fixes open-telemetry#28811 **Documentation:** No impact to docs. User interface remains the same. Separate changelog to notice API breaking changes, as the Config struct is changing. --------- Co-authored-by: Dmitrii Anoshin <[email protected]>
1 parent 3ddc6f6 commit 8dfbdf1

File tree

12 files changed

+238
-66
lines changed

12 files changed

+238
-66
lines changed
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: breaking
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: collectdreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Move to use confighttp.HTTPServerSettings
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: [28811]
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:
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]
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: collectdreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add support of confighttp.HTTPServerSettings
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: [28811]
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:
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: []

receiver/collectdreceiver/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ value `field[a=b, k=v]`, this receiver will extract `a` and `b` as label keys
2929
and, `k` and `v` as the respective label values.
3030

3131
## Configuration
32+
The configuration includes the Opentelemetry collector's server [confighttp](https://github.com/open-telemetry/opentelemetry-collector/tree/main/config/confighttp#server-configuration),
33+
which allows for a variety of settings. Only the most relevant ones will be discussed here, but all are available.
3234

3335
The following settings are required:
3436

35-
- `endpoint` (default = `localhost:8081`): Address to reach the desired Docker daemon.
37+
- `endpoint` (default = `localhost:8081`): Endpoint exposed by this receiver to send data.
3638

3739
The following settings are optional:
3840

receiver/collectdreceiver/config.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,29 @@
44
package collectdreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/collectdreceiver"
55

66
import (
7+
"fmt"
8+
"strings"
79
"time"
810

9-
"go.opentelemetry.io/collector/config/confignet"
11+
"go.opentelemetry.io/collector/config/confighttp"
1012
)
1113

1214
// Config defines configuration for Collectd receiver.
1315
type Config struct {
14-
confignet.TCPAddr `mapstructure:",squash"`
16+
confighttp.HTTPServerSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
17+
Timeout time.Duration `mapstructure:"timeout"`
18+
Encoding string `mapstructure:"encoding"`
19+
AttributesPrefix string `mapstructure:"attributes_prefix"`
20+
}
1521

16-
Timeout time.Duration `mapstructure:"timeout"`
17-
AttributesPrefix string `mapstructure:"attributes_prefix"`
18-
Encoding string `mapstructure:"encoding"`
22+
func (c *Config) Validate() error {
23+
// CollectD receiver only supports JSON encoding. We expose a config option
24+
// to make it explicit and obvious to the users.
25+
if strings.ToLower(c.Encoding) != defaultEncodingFormat {
26+
return fmt.Errorf(
27+
"CollectD only support JSON encoding format. %s is not supported",
28+
c.Encoding,
29+
)
30+
}
31+
return nil
1932
}

receiver/collectdreceiver/config_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
package collectdreceiver
55

66
import (
7+
"errors"
78
"path/filepath"
89
"testing"
910
"time"
1011

1112
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
1314
"go.opentelemetry.io/collector/component"
14-
"go.opentelemetry.io/collector/config/confignet"
15+
"go.opentelemetry.io/collector/config/confighttp"
1516
"go.opentelemetry.io/collector/confmap/confmaptest"
1617

1718
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/collectdreceiver/internal/metadata"
@@ -23,6 +24,7 @@ func TestLoadConfig(t *testing.T) {
2324
tests := []struct {
2425
id component.ID
2526
expected component.Config
27+
wantErr error
2628
}{
2729
{
2830
id: component.NewIDWithName(metadata.Type, ""),
@@ -31,13 +33,14 @@ func TestLoadConfig(t *testing.T) {
3133
{
3234
id: component.NewIDWithName(metadata.Type, "one"),
3335
expected: &Config{
34-
TCPAddr: confignet.TCPAddr{
36+
HTTPServerSettings: confighttp.HTTPServerSettings{
3537
Endpoint: "localhost:12345",
3638
},
37-
Timeout: time.Second * 50,
39+
Timeout: 50 * time.Second,
3840
AttributesPrefix: "dap_",
3941
Encoding: "command",
4042
},
43+
wantErr: errors.New("CollectD only support JSON encoding format. command is not supported"),
4144
},
4245
}
4346

@@ -53,7 +56,11 @@ func TestLoadConfig(t *testing.T) {
5356
require.NoError(t, err)
5457
require.NoError(t, component.UnmarshalConfig(sub, cfg))
5558

56-
assert.NoError(t, component.ValidateConfig(cfg))
59+
if tt.wantErr == nil {
60+
assert.NoError(t, component.ValidateConfig(cfg))
61+
} else {
62+
assert.Equal(t, tt.wantErr, component.ValidateConfig(cfg))
63+
}
5764
assert.Equal(t, tt.expected, cfg)
5865
})
5966
}

receiver/collectdreceiver/factory.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ package collectdreceiver // import "github.com/open-telemetry/opentelemetry-coll
55

66
import (
77
"context"
8-
"fmt"
9-
"strings"
108
"time"
119

1210
"go.opentelemetry.io/collector/component"
13-
"go.opentelemetry.io/collector/config/confignet"
11+
"go.opentelemetry.io/collector/config/confighttp"
1412
"go.opentelemetry.io/collector/consumer"
1513
"go.opentelemetry.io/collector/receiver"
1614

@@ -21,7 +19,6 @@ import (
2119

2220
const (
2321
defaultBindEndpoint = "localhost:8081"
24-
defaultTimeout = time.Second * 30
2522
defaultEncodingFormat = "json"
2623
)
2724

@@ -34,10 +31,10 @@ func NewFactory() receiver.Factory {
3431
}
3532
func createDefaultConfig() component.Config {
3633
return &Config{
37-
TCPAddr: confignet.TCPAddr{
34+
HTTPServerSettings: confighttp.HTTPServerSettings{
3835
Endpoint: defaultBindEndpoint,
3936
},
40-
Timeout: defaultTimeout,
37+
Timeout: 30 * time.Second,
4138
Encoding: defaultEncodingFormat,
4239
}
4340
}
@@ -49,14 +46,5 @@ func createMetricsReceiver(
4946
nextConsumer consumer.Metrics,
5047
) (receiver.Metrics, error) {
5148
c := cfg.(*Config)
52-
c.Encoding = strings.ToLower(c.Encoding)
53-
// CollectD receiver only supports JSON encoding. We expose a config option
54-
// to make it explicit and obvious to the users.
55-
if c.Encoding != defaultEncodingFormat {
56-
return nil, fmt.Errorf(
57-
"CollectD only support JSON encoding format. %s is not supported",
58-
c.Encoding,
59-
)
60-
}
61-
return newCollectdReceiver(cs.Logger, c.Endpoint, c.Timeout, c.AttributesPrefix, nextConsumer, cs)
49+
return newCollectdReceiver(cs.Logger, c, c.AttributesPrefix, nextConsumer, cs)
6250
}

receiver/collectdreceiver/go.mod

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.88.0
99
github.com/stretchr/testify v1.8.4
1010
go.opentelemetry.io/collector/component v0.88.1-0.20231026220224-6405e152a2d9
11-
go.opentelemetry.io/collector/config/confignet v0.88.1-0.20231026220224-6405e152a2d9
11+
go.opentelemetry.io/collector/config/confighttp v0.88.1-0.20231026220224-6405e152a2d9
1212
go.opentelemetry.io/collector/confmap v0.88.1-0.20231026220224-6405e152a2d9
1313
go.opentelemetry.io/collector/consumer v0.88.1-0.20231026220224-6405e152a2d9
1414
go.opentelemetry.io/collector/pdata v1.0.0-rcv0017.0.20231026220224-6405e152a2d9
@@ -19,9 +19,15 @@ require (
1919
require (
2020
github.com/cespare/xxhash/v2 v2.2.0 // indirect
2121
github.com/davecgh/go-spew v1.1.1 // indirect
22+
github.com/felixge/httpsnoop v1.0.3 // indirect
23+
github.com/fsnotify/fsnotify v1.7.0 // indirect
24+
github.com/go-logr/logr v1.2.4 // indirect
25+
github.com/go-logr/stdr v1.2.2 // indirect
2226
github.com/gogo/protobuf v1.3.2 // indirect
2327
github.com/golang/protobuf v1.5.3 // indirect
28+
github.com/golang/snappy v0.0.4 // indirect
2429
github.com/json-iterator/go v1.1.12 // indirect
30+
github.com/klauspost/compress v1.17.2 // indirect
2531
github.com/knadh/koanf/maps v0.1.1 // indirect
2632
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
2733
github.com/knadh/koanf/v2 v2.0.1 // indirect
@@ -32,10 +38,19 @@ require (
3238
github.com/modern-go/reflect2 v1.0.2 // indirect
3339
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.88.0 // indirect
3440
github.com/pmezard/go-difflib v1.0.0 // indirect
41+
github.com/rs/cors v1.10.1 // indirect
3542
go.opencensus.io v0.24.0 // indirect
3643
go.opentelemetry.io/collector v0.88.1-0.20231026220224-6405e152a2d9 // indirect
44+
go.opentelemetry.io/collector/config/configauth v0.88.1-0.20231026220224-6405e152a2d9 // indirect
45+
go.opentelemetry.io/collector/config/configcompression v0.88.1-0.20231026220224-6405e152a2d9 // indirect
46+
go.opentelemetry.io/collector/config/configopaque v0.88.1-0.20231026220224-6405e152a2d9 // indirect
3747
go.opentelemetry.io/collector/config/configtelemetry v0.88.1-0.20231026220224-6405e152a2d9 // indirect
48+
go.opentelemetry.io/collector/config/configtls v0.88.1-0.20231026220224-6405e152a2d9 // indirect
49+
go.opentelemetry.io/collector/config/internal v0.88.1-0.20231026220224-6405e152a2d9 // indirect
50+
go.opentelemetry.io/collector/extension v0.88.1-0.20231026220224-6405e152a2d9 // indirect
51+
go.opentelemetry.io/collector/extension/auth v0.88.1-0.20231026220224-6405e152a2d9 // indirect
3852
go.opentelemetry.io/collector/featuregate v1.0.0-rcv0017.0.20231026220224-6405e152a2d9 // indirect
53+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.45.0 // indirect
3954
go.opentelemetry.io/otel v1.19.0 // indirect
4055
go.opentelemetry.io/otel/metric v1.19.0 // indirect
4156
go.opentelemetry.io/otel/trace v1.19.0 // indirect

receiver/collectdreceiver/go.sum

Lines changed: 31 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)