| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package xscraper // import "go.opentelemetry.io/collector/scraper/xscraper" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
|
| 10 |
"go.opentelemetry.io/collector/component" |
| 11 |
) |
| 12 |
|
| 13 |
var errNilFunc = errors.New("nil scrape func") |
| 14 |
|
| 15 |
// ScrapeFunc scrapes data. |
| 16 |
type ScrapeFunc[T any] func(context.Context) (T, error) |
| 17 |
|
| 18 |
// Option apply changes to internal options. |
| 19 |
type Option interface { |
| 20 |
apply(*baseScraper) |
| 21 |
} |
| 22 |
|
| 23 |
type scraperOptionFunc func(*baseScraper) |
| 24 |
|
| 25 |
func (of scraperOptionFunc) apply(e *baseScraper) { |
| 26 |
of(e) |
| 27 |
} |
| 28 |
|
| 29 |
// WithStart sets the function that will be called on startup. |
| 30 |
func WithStart(start component.StartFunc) Option { |
| 31 |
return scraperOptionFunc(func(o *baseScraper) { |
| 32 |
o.StartFunc = start |
| 33 |
}) |
| 34 |
} |
| 35 |
|
| 36 |
// WithShutdown sets the function that will be called on shutdown. |
| 37 |
func WithShutdown(shutdown component.ShutdownFunc) Option { |
| 38 |
return scraperOptionFunc(func(o *baseScraper) { |
| 39 |
o.ShutdownFunc = shutdown |
| 40 |
}) |
| 41 |
} |
| 42 |
|
| 43 |
type baseScraper struct { |
| 44 |
component.StartFunc |
| 45 |
component.ShutdownFunc |
| 46 |
} |
| 47 |
|
| 48 |
// newBaseScraper returns the internal settings starting from the default and applying all options. |
| 49 |
func newBaseScraper(options []Option) baseScraper { |
| 50 |
// Start from the default options: |
| 51 |
bs := baseScraper{} |
| 52 |
|
| 53 |
for _, op := range options { |
| 54 |
op.apply(&bs) |
| 55 |
} |
| 56 |
|
| 57 |
return bs |
| 58 |
} |