Skip to content

Commit

Permalink
modify simpleprometheus receiver to use httpclientsetting (open-telem…
Browse files Browse the repository at this point in the history
…etry#11553)

* modify simpleprometheus receiver to use httpclientsetting

Signed-off-by: Ziqi Zhao <[email protected]>

* add back old setting

Signed-off-by: Ziqi Zhao <[email protected]>

* fix readme

Signed-off-by: Ziqi Zhao <[email protected]>

* fix reviews

Signed-off-by: Ziqi Zhao <[email protected]>

* add changelog

Signed-off-by: Ziqi Zhao <[email protected]>

* fix for lints

Signed-off-by: Ziqi Zhao <[email protected]>

* fix for deadlink

Signed-off-by: Ziqi Zhao <[email protected]>

* fix default config and add more test

Signed-off-by: Ziqi Zhao <[email protected]>

* fix bugs

Signed-off-by: Ziqi Zhao <[email protected]>

* fix for lint

Signed-off-by: Ziqi Zhao <[email protected]>
  • Loading branch information
fatsheep9146 authored Jul 1, 2022
1 parent 83e0baf commit 6b72286
Show file tree
Hide file tree
Showing 11 changed files with 329 additions and 56 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

### 🚩 Deprecations 🚩

- `simpleprometheusreceiver`: Announcing `tls_enable`, `tls_config` will be deprecated and use `confighttp.HTTPClientSettings` instead. (#11553)
- `logzioexporter`: Announcing `custom_endpoint`, `drain_interval`, `queue_capacity`, `queue_max_length` configuration options will be deprecated in upcoming releases (#10821)
- `hostmetricsreceiver`: remove direction attribute. The feature gate: `receiver.hostmetricsreceiver.removeDirectionAttribute` can be set to apply the following (#11820)
- `system.network.dropped` will become:
Expand Down
17 changes: 9 additions & 8 deletions receiver/simpleprometheusreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ be emitted by this receiver.
Kubernetes Pod service account for authentication.
- `tls_enabled` (default = `false`): Whether or not to use TLS. Only if
`tls_enabled` is set to `true`, the values under `tls_config` are accounted
for.
for. This setting will be deprecated. Please use `tls` instead.

The `tls_config` section supports the following options:
The `tls_config` section supports the following options. This setting will be deprecated. Please use `tls` instead:

- `ca_file` (no default): Path to the CA cert that has signed the TLS
certificate.
Expand All @@ -43,6 +43,8 @@ connections.
- `insecure_skip_verify` (default = `false`): Whether or not to skip
certificate verification.

- `tls`: see [TLS Configuration Settings](https://github.com/open-telemetry/opentelemetry-collector/blob/main/config/configtls/README.md#tls-configuration-settings) for the full set of available options.

Example:

```yaml
Expand All @@ -51,12 +53,11 @@ Example:
collection_interval: 10s
use_service_account: true
endpoint: "172.17.0.5:9153"
tls_enabled: true
tls_config:
ca_file: "/path/to/ca"
cert_file: "/path/to/cert"
key_file: "/path/to/key"
insecure_skip_verify: true
tls:
ca_file: "/path/to/ca"
cert_file: "/path/to/cert"
key_file: "/path/to/key"
insecure_skip_verify: true
exporters:
signalfx:
access_token: <SIGNALFX_ACCESS_TOKEN>
Expand Down
9 changes: 5 additions & 4 deletions receiver/simpleprometheusreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import (
"time"

"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/confighttp"
)

// Config defines configuration for simple prometheus receiver.
type Config struct {
config.ReceiverSettings `mapstructure:",squash"`
httpConfig `mapstructure:",squash"`
confignet.TCPAddr `mapstructure:",squash"`
config.ReceiverSettings `mapstructure:",squash"`
confighttp.HTTPClientSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.
// Deprecated: [v0.55.0] Use confighttp.HTTPClientSettings instead.
httpConfig `mapstructure:",squash"`
// CollectionInterval is the interval at which metrics should be collected
CollectionInterval time.Duration `mapstructure:"collection_interval"`
// MetricsPath the path to the metrics endpoint.
Expand Down
28 changes: 14 additions & 14 deletions receiver/simpleprometheusreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/service/servicetest"
)

Expand All @@ -48,15 +49,14 @@ func TestLoadConfig(t *testing.T) {
assert.Equal(t, r2,
&Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentIDWithName(typeStr, "all_settings")),
TCPAddr: confignet.TCPAddr{
HTTPClientSettings: confighttp.HTTPClientSettings{
Endpoint: "localhost:1234",
},
httpConfig: httpConfig{
TLSEnabled: true,
TLSConfig: tlsConfig{
CAFile: "path",
CertFile: "path",
KeyFile: "path",
TLSSetting: configtls.TLSClientSetting{
TLSSetting: configtls.TLSSetting{
CAFile: "path",
CertFile: "path",
KeyFile: "path",
},
InsecureSkipVerify: true,
},
},
Expand All @@ -70,8 +70,11 @@ func TestLoadConfig(t *testing.T) {
assert.Equal(t, r3,
&Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentIDWithName(typeStr, "partial_settings")),
TCPAddr: confignet.TCPAddr{
HTTPClientSettings: confighttp.HTTPClientSettings{
Endpoint: "localhost:1234",
TLSSetting: configtls.TLSClientSetting{
Insecure: true,
},
},
CollectionInterval: 30 * time.Second,
MetricsPath: "/metrics",
Expand All @@ -81,12 +84,9 @@ func TestLoadConfig(t *testing.T) {
assert.Equal(t, r4,
&Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentIDWithName(typeStr, "partial_tls_settings")),
TCPAddr: confignet.TCPAddr{
HTTPClientSettings: confighttp.HTTPClientSettings{
Endpoint: "localhost:1234",
},
httpConfig: httpConfig{
TLSEnabled: true,
},
CollectionInterval: 30 * time.Second,
MetricsPath: "/metrics",
})
Expand Down
8 changes: 6 additions & 2 deletions receiver/simpleprometheusreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/consumer"
)

Expand All @@ -46,8 +47,11 @@ func NewFactory() component.ReceiverFactory {
func createDefaultConfig() config.Receiver {
return &Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(typeStr)),
TCPAddr: confignet.TCPAddr{
HTTPClientSettings: confighttp.HTTPClientSettings{
Endpoint: defaultEndpoint,
TLSSetting: configtls.TLSClientSetting{
Insecure: true,
},
},
MetricsPath: defaultMetricsPath,
CollectionInterval: defaultCollectionInterval,
Expand Down
6 changes: 6 additions & 0 deletions receiver/simpleprometheusreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ require (
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1 // indirect
github.com/envoyproxy/protoc-gen-validate v0.6.7 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
Expand All @@ -55,6 +57,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/go-querystring v1.1.0 // indirect
Expand All @@ -80,6 +83,7 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.6 // indirect
github.com/knadh/koanf v1.4.2 // indirect
github.com/kolo/xmlrpc v0.0.0-20201022064351-38db28db192b // indirect
github.com/kr/pretty v0.3.0 // indirect
Expand All @@ -106,13 +110,15 @@ require (
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common/sigv4 v0.1.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rs/cors v1.8.2 // indirect
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/vultr/govultr/v2 v2.17.0 // indirect
go.opencensus.io v0.23.0 // indirect
go.opentelemetry.io/collector/pdata v0.54.0 // indirect
go.opentelemetry.io/collector/semconv v0.54.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.32.0 // indirect
go.opentelemetry.io/otel v1.7.0 // indirect
go.opentelemetry.io/otel/metric v0.30.0 // indirect
go.opentelemetry.io/otel/trace v1.7.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions receiver/simpleprometheusreceiver/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 28 additions & 6 deletions receiver/simpleprometheusreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/discovery"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/consumer"
"k8s.io/client-go/rest"

Expand All @@ -46,7 +47,7 @@ func new(params component.ReceiverCreateSettings, cfg *Config, consumer consumer
func (prw *prometheusReceiverWrapper) Start(ctx context.Context, host component.Host) error {
pFactory := prometheusreceiver.NewFactory()

pConfig, err := getPrometheusConfig(prw.config)
pConfig, err := getPrometheusConfigWrapper(prw.config, prw.params)
if err != nil {
return fmt.Errorf("failed to create prometheus receiver config: %w", err)
}
Expand All @@ -60,6 +61,23 @@ func (prw *prometheusReceiverWrapper) Start(ctx context.Context, host component.
return prw.prometheusRecever.Start(ctx, host)
}

// Deprecated: [v0.55.0] Use getPrometheusConfig instead.
func getPrometheusConfigWrapper(cfg *Config, params component.ReceiverCreateSettings) (*prometheusreceiver.Config, error) {
if cfg.TLSEnabled {
params.Logger.Warn("the `tls_config` and 'tls_enabled' settings are deprecated, please use `tls` instead")
cfg.HTTPClientSettings.TLSSetting = configtls.TLSClientSetting{
TLSSetting: configtls.TLSSetting{
CAFile: cfg.TLSConfig.CAFile,
CertFile: cfg.TLSConfig.CertFile,
KeyFile: cfg.TLSConfig.KeyFile,
},
Insecure: false,
InsecureSkipVerify: cfg.TLSConfig.InsecureSkipVerify,
}
}
return getPrometheusConfig(cfg)
}

func getPrometheusConfig(cfg *Config) (*prometheusreceiver.Config, error) {
var bearerToken string
if cfg.UseServiceAccount {
Expand All @@ -78,13 +96,17 @@ func getPrometheusConfig(cfg *Config) (*prometheusreceiver.Config, error) {

scheme := "http"

if cfg.TLSEnabled {
tlsConfig, err := cfg.TLSSetting.LoadTLSConfig()
if err != nil {
return nil, fmt.Errorf("tls config is not valid: %w", err)
}
if tlsConfig != nil {
scheme = "https"
httpConfig.TLSConfig = configutil.TLSConfig{
CAFile: cfg.TLSConfig.CAFile,
CertFile: cfg.TLSConfig.CertFile,
KeyFile: cfg.TLSConfig.KeyFile,
InsecureSkipVerify: cfg.TLSConfig.InsecureSkipVerify,
CAFile: cfg.TLSSetting.CAFile,
CertFile: cfg.TLSSetting.CertFile,
KeyFile: cfg.TLSSetting.KeyFile,
InsecureSkipVerify: cfg.TLSSetting.InsecureSkipVerify,
}
}

Expand Down
Loading

0 comments on commit 6b72286

Please sign in to comment.