Skip to content

Commit b357a3b

Browse files
authored
[receiver/purefa] Fix the receiver failing to start due to an invalid internal Prometheus scrape configuration (#48850)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description The receiver used a `*discovery.StaticConfig` pointer when building the Prometheus scrape configs. Only the value type `discovery.StaticConfig` is registered for YAML marshaling, so the prometheus receiver failed on startup with `cannot marshal unregistered Config type: *discovery.StaticConfig`. The config now uses the value type. <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes #48847 <!--Describe what testing was performed and which tests were added.--> #### Testing Added unit tests to cover this issue. <!--Authorship attestation. See AGENTS.md for details. AI agents must not check this box on behalf of the user; the human author must check it themselves before the PR is ready for review.--> #### Authorship - [X] I, a human, wrote this pull request description myself. <!--Please delete paragraphs that you did not use before submitting.--> --------- Signed-off-by: Paulo Dias <paulodias.gm@gmail.com>
1 parent cfef760 commit b357a3b

4 files changed

Lines changed: 55 additions & 2 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
change_type: bug_fix
2+
component: receiver/purefa
3+
note: Fix the receiver failing to start due to an invalid internal Prometheus scrape configuration.
4+
issues: [48847]
5+
subtext: |
6+
The receiver used a `*discovery.StaticConfig` pointer when building the Prometheus scrape
7+
configs. Only the value type `discovery.StaticConfig` is registered for YAML marshaling, so
8+
the prometheus receiver failed on startup with "cannot marshal unregistered Config type:
9+
*discovery.StaticConfig". The config now uses the value type.
10+
change_logs: [user]

receiver/purefareceiver/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
go.opentelemetry.io/collector/receiver/receivertest v0.154.1-0.20260611154946-8ae0933981eb
2424
go.uber.org/goleak v1.3.0
2525
go.uber.org/multierr v1.11.0
26+
gopkg.in/yaml.v2 v2.4.0
2627
)
2728

2829
require (
@@ -260,7 +261,6 @@ require (
260261
gopkg.in/evanphx/json-patch.v4 v4.13.0 // indirect
261262
gopkg.in/inf.v0 v0.9.1 // indirect
262263
gopkg.in/ini.v1 v1.67.2 // indirect
263-
gopkg.in/yaml.v2 v2.4.0 // indirect
264264
gopkg.in/yaml.v3 v3.0.1 // indirect
265265
k8s.io/api v0.35.3 // indirect
266266
k8s.io/apimachinery v0.35.3 // indirect

receiver/purefareceiver/internal/scraper.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ func (h *scraper) ToPrometheusReceiverConfig(host component.Host, _ receiver.Fac
101101
},
102102

103103
ServiceDiscoveryConfigs: discovery.Configs{
104-
&discovery.StaticConfig{
104+
// Must be a value, not a pointer: only discovery.StaticConfig is
105+
// registered for YAML marshaling, which the prometheus receiver
106+
// performs on startup.
107+
discovery.StaticConfig{
105108
{
106109
Targets: []model.LabelSet{
107110
{model.AddressLabel: model.LabelValue(u.Host)},

receiver/purefareceiver/internal/scraper_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"go.opentelemetry.io/collector/config/configauth"
1515
"go.opentelemetry.io/collector/config/configtls"
1616
"go.opentelemetry.io/collector/extension/extensiontest"
17+
"gopkg.in/yaml.v2"
1718

1819
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/bearertokenauthextension"
1920
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver"
@@ -71,3 +72,42 @@ func TestToPrometheusConfig(t *testing.T) {
7172
assert.EqualValues(t, interval, scCfgs[0].ScrapeTimeout)
7273
assert.EqualValues(t, interval, scCfgs[0].ScrapeInterval)
7374
}
75+
76+
// TestToPrometheusConfigMarshalsToYAML guards against using a *discovery.StaticConfig
77+
// pointer, which is unregistered and fails the YAML marshal the prometheus receiver
78+
// runs on startup.
79+
func TestToPrometheusConfigMarshalsToYAML(t *testing.T) {
80+
prFactory := prometheusreceiver.NewFactory()
81+
baFactory := bearertokenauthextension.NewFactory()
82+
83+
baCfg := baFactory.CreateDefaultConfig().(*bearertokenauthextension.Config)
84+
baCfg.BearerToken = "the-token"
85+
86+
baExt, err := baFactory.Create(t.Context(), extensiontest.NewNopSettings(baFactory.Type()), baCfg)
87+
require.NoError(t, err)
88+
89+
host := &mockHost{
90+
extensions: map[component.ID]component.Component{
91+
component.MustNewIDWithName("bearertokenauth", "array01"): baExt,
92+
},
93+
}
94+
95+
cfgs := []ScraperConfig{
96+
{
97+
Address: "array01",
98+
Auth: configauth.Config{
99+
AuthenticatorID: component.MustNewIDWithName("bearertokenauth", "array01"),
100+
},
101+
},
102+
}
103+
104+
scraper := NewScraper(t.Context(), "hosts", "http://example.com", "purefa", configtls.ClientConfig{}, cfgs, 15*time.Second, model.LabelSet{})
105+
106+
scCfgs, err := scraper.ToPrometheusReceiverConfig(host, prFactory)
107+
require.NoError(t, err)
108+
require.Len(t, scCfgs, 1)
109+
110+
// Mirrors the marshaling the prometheus receiver performs on startup.
111+
_, err = yaml.Marshal(scCfgs[0])
112+
require.NoError(t, err)
113+
}

0 commit comments

Comments
 (0)