Skip to content

Commit e599957

Browse files
authored
Refine scraperhelper.NewScraper to take component.Type directly (#11082)
#### Description Create new function to enable to pass `component.Type` to `scraperhelper.NewScraper` Because many contrib scrapers name the scraper with the same name as the component itself, create a function to take `component.Type`. For example, https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/0e2bea5ac2763421e1a3943093f67184e109265f/receiver/activedirectorydsreceiver/factory_windows.go#L35 passes `metadata.Type.String()` as a name of scraper. With this change, you can pass `metadata.Type` instead <!-- Issue number if applicable --> #### Link to tracking issue Fixes #9473
1 parent 6928951 commit e599957

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

.chloggen/refine-new-scraper.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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: deprecation
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: scraperhelper
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: deprecate NewScraper, should use NewScraperWithComponentType
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [11082]
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+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]

receiver/scraperhelper/scraper.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,21 @@ func (b *baseScraper) ID() component.ID {
6161

6262
// NewScraper creates a Scraper that calls Scrape at the specified collection interval,
6363
// reports observability information, and passes the scraped metrics to the next consumer.
64+
//
65+
// Deprecated: [v0.109.0] use NewScraperWithComponentType instead.
6466
func NewScraper(name string, scrape ScrapeFunc, options ...ScraperOption) (Scraper, error) {
67+
return NewScraperWithComponentType(component.MustNewType(name), scrape, options...)
68+
}
69+
70+
// NewScraperWithComponentType creates a Scraper that calls Scrape at the specified collection interval,
71+
// reports observability information, and passes the scraped metrics to the next consumer.
72+
func NewScraperWithComponentType(t component.Type, scrape ScrapeFunc, options ...ScraperOption) (Scraper, error) {
6573
if scrape == nil {
6674
return nil, errNilFunc
6775
}
6876
bs := &baseScraper{
6977
ScrapeFunc: scrape,
70-
id: component.NewID(component.MustNewType(name)),
78+
id: component.NewID(t),
7179
}
7280
for _, op := range options {
7381
op(bs)

receiver/scraperhelper/scrapercontroller_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func configureMetricOptions(t *testing.T, test metricsTestCase, initializeChs []
223223

224224
scrapeMetricsChs[i] = make(chan int)
225225
tsm := &testScrapeMetrics{ch: scrapeMetricsChs[i], err: test.scrapeErr}
226-
scp, err := NewScraper("scraper", tsm.scrape, scraperOptions...)
226+
scp, err := NewScraperWithComponentType(component.MustNewType("scraper"), tsm.scrape, scraperOptions...)
227227
assert.NoError(t, err)
228228

229229
metricOptions = append(metricOptions, AddScraper(scp))
@@ -325,7 +325,7 @@ func TestSingleScrapePerInterval(t *testing.T) {
325325

326326
tickerCh := make(chan time.Time)
327327

328-
scp, err := NewScraper("scaper", tsm.scrape)
328+
scp, err := NewScraperWithComponentType(component.MustNewType("scaper"), tsm.scrape)
329329
assert.NoError(t, err)
330330

331331
receiver, err := NewScraperControllerReceiver(
@@ -367,7 +367,7 @@ func TestScrapeControllerStartsOnInit(t *testing.T) {
367367
ch: make(chan int, 1),
368368
}
369369

370-
scp, err := NewScraper("scraper", tsm.scrape)
370+
scp, err := NewScraperWithComponentType(component.MustNewType("scraper"), tsm.scrape)
371371
require.NoError(t, err, "Must not error when creating scraper")
372372

373373
r, err := NewScraperControllerReceiver(
@@ -403,7 +403,7 @@ func TestScrapeControllerInitialDelay(t *testing.T) {
403403
}
404404
)
405405

406-
scp, err := NewScraper("timed", func(context.Context) (pmetric.Metrics, error) {
406+
scp, err := NewScraperWithComponentType(component.MustNewType("timed"), func(context.Context) (pmetric.Metrics, error) {
407407
elapsed <- time.Now()
408408
return pmetric.NewMetrics(), nil
409409
})

0 commit comments

Comments
 (0)