Skip to content

Commit 2e0d436

Browse files
authored
[receiver/prometheus] Use confighttp for target allocator client (#31452)
**Description:** Use confighttp for target allocator http client in prometheus receiver. **Link to tracking Issue:** #31449 **Testing:** Added some simple config tests. **Documentation:** Added a note to the README.
1 parent 135d723 commit 2e0d436

File tree

7 files changed

+52
-7
lines changed

7 files changed

+52
-7
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: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: prometheusreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Use confighttp for target allocator client
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: [31449]
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/prometheusreceiver/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ receivers:
141141
interval: 30s
142142
collector_id: collector-1
143143
```
144+
145+
The `target_allocator` section embeds the full [confighttp client configuration][confighttp].
146+
147+
[confighttp]: https://github.com/open-telemetry/opentelemetry-collector/tree/main/config/confighttp#client-configuration
148+
144149
## Exemplars
145150
This receiver accepts exemplars coming in Prometheus format and converts it to OTLP format.
146151
1. Value is expected to be received in `float64` format

receiver/prometheusreceiver/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
promconfig "github.com/prometheus/prometheus/config"
1717
promHTTP "github.com/prometheus/prometheus/discovery/http"
1818
"github.com/prometheus/prometheus/discovery/kubernetes"
19+
"go.opentelemetry.io/collector/config/confighttp"
1920
"go.opentelemetry.io/collector/confmap"
2021
"gopkg.in/yaml.v2"
2122
)
@@ -52,10 +53,10 @@ func (cfg *Config) Validate() error {
5253
}
5354

5455
type TargetAllocator struct {
55-
Endpoint string `mapstructure:"endpoint"`
56-
Interval time.Duration `mapstructure:"interval"`
57-
CollectorID string `mapstructure:"collector_id"`
58-
HTTPSDConfig *PromHTTPSDConfig `mapstructure:"http_sd_config"`
56+
confighttp.ClientConfig `mapstructure:",squash"`
57+
Interval time.Duration `mapstructure:"interval"`
58+
CollectorID string `mapstructure:"collector_id"`
59+
HTTPSDConfig *PromHTTPSDConfig `mapstructure:"http_sd_config"`
5960
}
6061

6162
func (cfg *TargetAllocator) Validate() error {

receiver/prometheusreceiver/config_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@ func TestLoadTargetAllocatorConfig(t *testing.T) {
7171
r0 := cfg.(*Config)
7272
assert.NotNil(t, r0.PrometheusConfig)
7373
assert.Equal(t, "http://localhost:8080", r0.TargetAllocator.Endpoint)
74+
assert.Equal(t, 5*time.Second, r0.TargetAllocator.Timeout)
75+
assert.Equal(t, "client.crt", r0.TargetAllocator.TLSSetting.CertFile)
7476
assert.Equal(t, 30*time.Second, r0.TargetAllocator.Interval)
7577
assert.Equal(t, "collector-1", r0.TargetAllocator.CollectorID)
78+
assert.NotNil(t, r0.PrometheusConfig)
7679

7780
sub, err = cm.Sub(component.NewIDWithName(metadata.Type, "withScrape").String())
7881
require.NoError(t, err)

receiver/prometheusreceiver/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/prometheus/prometheus v0.48.1
1515
github.com/stretchr/testify v1.9.0
1616
go.opentelemetry.io/collector/component v0.96.1-0.20240306115632-b2693620eff6
17+
go.opentelemetry.io/collector/config/confighttp v0.96.1-0.20240306115632-b2693620eff6
1718
go.opentelemetry.io/collector/confmap v0.96.1-0.20240306115632-b2693620eff6
1819
go.opentelemetry.io/collector/confmap/provider/fileprovider v0.96.1-0.20240306115632-b2693620eff6
1920
go.opentelemetry.io/collector/consumer v0.96.1-0.20240306115632-b2693620eff6
@@ -162,7 +163,6 @@ require (
162163
go.opentelemetry.io/collector v0.96.1-0.20240306115632-b2693620eff6 // indirect
163164
go.opentelemetry.io/collector/config/configauth v0.96.1-0.20240306115632-b2693620eff6 // indirect
164165
go.opentelemetry.io/collector/config/configcompression v0.96.1-0.20240306115632-b2693620eff6 // indirect
165-
go.opentelemetry.io/collector/config/confighttp v0.96.1-0.20240306115632-b2693620eff6 // indirect
166166
go.opentelemetry.io/collector/config/configopaque v1.3.1-0.20240306115632-b2693620eff6 // indirect
167167
go.opentelemetry.io/collector/config/configretry v0.96.1-0.20240306115632-b2693620eff6 // indirect
168168
go.opentelemetry.io/collector/config/configtelemetry v0.96.1-0.20240306115632-b2693620eff6 // indirect

receiver/prometheusreceiver/metrics_receiver.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type pReceiver struct {
5050
settings receiver.CreateSettings
5151
scrapeManager *scrape.Manager
5252
discoveryManager *discovery.Manager
53+
httpClient *http.Client
5354
}
5455

5556
// New creates a new prometheus.Receiver reference.
@@ -66,7 +67,7 @@ func newPrometheusReceiver(set receiver.CreateSettings, cfg *Config, next consum
6667

6768
// Start is the method that starts Prometheus scraping. It
6869
// is controlled by having previously defined a Configuration using perhaps New.
69-
func (r *pReceiver) Start(_ context.Context, _ component.Host) error {
70+
func (r *pReceiver) Start(_ context.Context, host component.Host) error {
7071
discoveryCtx, cancel := context.WithCancel(context.Background())
7172
r.cancelFunc = cancel
7273

@@ -89,6 +90,11 @@ func (r *pReceiver) Start(_ context.Context, _ component.Host) error {
8990

9091
allocConf := r.cfg.TargetAllocator
9192
if allocConf != nil {
93+
r.httpClient, err = r.cfg.TargetAllocator.ToClient(host, r.settings.TelemetrySettings)
94+
if err != nil {
95+
r.settings.Logger.Error("Failed to create http client", zap.Error(err))
96+
return err
97+
}
9298
err = r.startTargetAllocator(allocConf, baseCfg)
9399
if err != nil {
94100
return err
@@ -197,7 +203,7 @@ func (r *pReceiver) getScrapeConfigsResponse(baseURL string) (map[string]*config
197203
return nil, err
198204
}
199205

200-
resp, err := http.Get(scrapeConfigsURL) //nolint
206+
resp, err := r.httpClient.Get(scrapeConfigsURL)
201207
if err != nil {
202208
return nil, err
203209
}

receiver/prometheusreceiver/testdata/config_target_allocator.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
prometheus:
22
target_allocator:
33
endpoint: http://localhost:8080
4+
timeout: 5s
5+
tls:
6+
cert_file: "client.crt"
47
interval: 30s
58
collector_id: collector-1
69
prometheus/withScrape:

0 commit comments

Comments
 (0)