From fa6cd22cce2f0049e0332004d7b87060ef955289 Mon Sep 17 00:00:00 2001 From: mrasu Date: Sun, 8 Sep 2024 22:31:03 +0900 Subject: [PATCH 1/2] Refine scraperhelper.NewScraper to take component.Type directly --- .chloggen/refine-new-scraper.yaml | 25 +++++++++++++++++++ receiver/scraperhelper/scraper.go | 10 +++++++- .../scraperhelper/scrapercontroller_test.go | 8 +++--- 3 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 .chloggen/refine-new-scraper.yaml diff --git a/.chloggen/refine-new-scraper.yaml b/.chloggen/refine-new-scraper.yaml new file mode 100644 index 00000000000..de926f76fee --- /dev/null +++ b/.chloggen/refine-new-scraper.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: deprecation + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: scraperhelper + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: deprecate NewScraper, should use NewScraperWithComponentType + +# One or more tracking issues or pull requests related to the change +issues: [] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [api] diff --git a/receiver/scraperhelper/scraper.go b/receiver/scraperhelper/scraper.go index f97eb65a403..143be6b06c9 100644 --- a/receiver/scraperhelper/scraper.go +++ b/receiver/scraperhelper/scraper.go @@ -61,13 +61,21 @@ func (b *baseScraper) ID() component.ID { // NewScraper creates a Scraper that calls Scrape at the specified collection interval, // reports observability information, and passes the scraped metrics to the next consumer. +// +// Deprecated: [v0.109.0] use NewScraperWithComponentType instead. func NewScraper(name string, scrape ScrapeFunc, options ...ScraperOption) (Scraper, error) { + return NewScraperWithComponentType(component.MustNewType(name), scrape, options...) +} + +// NewScraperWithComponentType creates a Scraper that calls Scrape at the specified collection interval, +// reports observability information, and passes the scraped metrics to the next consumer. +func NewScraperWithComponentType(t component.Type, scrape ScrapeFunc, options ...ScraperOption) (Scraper, error) { if scrape == nil { return nil, errNilFunc } bs := &baseScraper{ ScrapeFunc: scrape, - id: component.NewID(component.MustNewType(name)), + id: component.NewID(t), } for _, op := range options { op(bs) diff --git a/receiver/scraperhelper/scrapercontroller_test.go b/receiver/scraperhelper/scrapercontroller_test.go index f056a7eb8d2..28965a10caa 100644 --- a/receiver/scraperhelper/scrapercontroller_test.go +++ b/receiver/scraperhelper/scrapercontroller_test.go @@ -223,7 +223,7 @@ func configureMetricOptions(t *testing.T, test metricsTestCase, initializeChs [] scrapeMetricsChs[i] = make(chan int) tsm := &testScrapeMetrics{ch: scrapeMetricsChs[i], err: test.scrapeErr} - scp, err := NewScraper("scraper", tsm.scrape, scraperOptions...) + scp, err := NewScraperWithComponentType(component.MustNewType("scraper"), tsm.scrape, scraperOptions...) assert.NoError(t, err) metricOptions = append(metricOptions, AddScraper(scp)) @@ -325,7 +325,7 @@ func TestSingleScrapePerInterval(t *testing.T) { tickerCh := make(chan time.Time) - scp, err := NewScraper("scaper", tsm.scrape) + scp, err := NewScraperWithComponentType(component.MustNewType("scaper"), tsm.scrape) assert.NoError(t, err) receiver, err := NewScraperControllerReceiver( @@ -367,7 +367,7 @@ func TestScrapeControllerStartsOnInit(t *testing.T) { ch: make(chan int, 1), } - scp, err := NewScraper("scraper", tsm.scrape) + scp, err := NewScraperWithComponentType(component.MustNewType("scraper"), tsm.scrape) require.NoError(t, err, "Must not error when creating scraper") r, err := NewScraperControllerReceiver( @@ -403,7 +403,7 @@ func TestScrapeControllerInitialDelay(t *testing.T) { } ) - scp, err := NewScraper("timed", func(context.Context) (pmetric.Metrics, error) { + scp, err := NewScraperWithComponentType(component.MustNewType("timed"), func(context.Context) (pmetric.Metrics, error) { elapsed <- time.Now() return pmetric.NewMetrics(), nil }) From f01bd12852814833a24d5365566ad6610de8b654 Mon Sep 17 00:00:00 2001 From: mrasu Date: Sun, 8 Sep 2024 23:12:14 +0900 Subject: [PATCH 2/2] Set chloggen's issue number --- .chloggen/refine-new-scraper.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chloggen/refine-new-scraper.yaml b/.chloggen/refine-new-scraper.yaml index de926f76fee..c9457552c40 100644 --- a/.chloggen/refine-new-scraper.yaml +++ b/.chloggen/refine-new-scraper.yaml @@ -10,7 +10,7 @@ component: scraperhelper note: deprecate NewScraper, should use NewScraperWithComponentType # One or more tracking issues or pull requests related to the change -issues: [] +issues: [11082] # (Optional) One or more lines of additional information to render under the primary note. # These lines will be padded with 2 spaces and then inserted directly into the document.