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