-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[windowsservicereceiver] Initial implementation #42545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 26 commits
d700d32
9b32801
2caf6a8
e92563e
e8d7715
4193646
8ec62cc
e4ce831
e074798
314ac81
72fb072
203dc1c
00e60bc
b5341a6
3af4ef6
8f5bce0
4708906
b689b9c
c7bb867
927b023
448d532
f516436
35f02a7
1ddaabe
57d15ee
bdb4484
f655c70
91fc456
1231141
27f9ba9
0876798
9d28ac4
654c1ef
4de59c3
5c857a0
2c77f18
4399e54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| //go:build !windows | ||
|
pjanotti marked this conversation as resolved.
|
||
|
|
||
| package windowsservicereceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsservicereceiver" | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "time" | ||
|
|
||
| "go.opentelemetry.io/collector/component" | ||
| "go.opentelemetry.io/collector/consumer" | ||
| "go.opentelemetry.io/collector/receiver" | ||
| "go.opentelemetry.io/collector/scraper/scraperhelper" | ||
|
|
||
| "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsservicereceiver/internal/metadata" | ||
| ) | ||
|
|
||
| var errUnsupportedOS = errors.New("windowsservicereceiver: supported only on Windows") | ||
|
|
||
| func NewFactory() receiver.Factory { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At first I think this will be better if you have the same |
||
| return receiver.NewFactory( | ||
| metadata.Type, | ||
| createDefaultConfig, | ||
| receiver.WithMetrics(createUnsupported, metadata.MetricsStability), | ||
| ) | ||
| } | ||
|
|
||
| func createDefaultConfig() component.Config { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This same function is also defined in Side note: the suffix of the file names in go, e.g.:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is still an issue. |
||
| return &Config{ | ||
| ControllerConfig: scraperhelper.ControllerConfig{ | ||
| CollectionInterval: 1 * time.Minute, | ||
| }, | ||
| MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(), | ||
| IncludeServices: nil, | ||
| ExcludeServices: nil, | ||
| } | ||
| } | ||
|
|
||
| func createUnsupported( | ||
|
pjanotti marked this conversation as resolved.
Outdated
|
||
| _ context.Context, | ||
| _ receiver.Settings, | ||
| _ component.Config, | ||
| _ consumer.Metrics, | ||
| ) (receiver.Metrics, error) { | ||
| return nil, errUnsupportedOS | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,134 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //revive:disable:unused-parameter | ||
| //go:build windows | ||
|
|
||
| package windowsservicereceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsservicereceiver" | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "go.opentelemetry.io/collector/component" | ||
| "go.opentelemetry.io/collector/pdata/pcommon" | ||
| "go.opentelemetry.io/collector/pdata/pmetric" | ||
| "go.opentelemetry.io/collector/receiver" | ||
| "go.uber.org/multierr" | ||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsservicereceiver/internal/metadata" | ||
| ) | ||
|
|
||
| //nolint:unused | ||
| type windowsServiceScraper struct { | ||
| scm serviceManager | ||
| settings receiver.Settings | ||
| conf *Config | ||
| mb *metadata.MetricsBuilder | ||
| logger *zap.Logger | ||
| cfg *Config | ||
| mb *metadata.MetricsBuilder | ||
| mgr serviceManager | ||
| includeSet map[string]struct{} | ||
| excludeSet map[string]struct{} | ||
| } | ||
|
|
||
| func newWindowsServiceScraper(settings receiver.Settings, cfg *Config, mb *metadata.MetricsBuilder) *windowsServiceScraper { | ||
| ws := &windowsServiceScraper{ | ||
| logger: settings.Logger, | ||
| cfg: cfg, | ||
| mb: mb, | ||
| mgr: serviceManager{}, | ||
| } | ||
| if len(cfg.IncludeServices) > 0 { | ||
| ws.includeSet = make(map[string]struct{}, len(cfg.IncludeServices)) | ||
| for _, n := range cfg.IncludeServices { | ||
| ws.includeSet[n] = struct{}{} | ||
| } | ||
| } | ||
| if len(cfg.ExcludeServices) > 0 { | ||
| ws.excludeSet = make(map[string]struct{}, len(cfg.ExcludeServices)) | ||
| for _, n := range cfg.ExcludeServices { | ||
| ws.excludeSet[n] = struct{}{} | ||
| } | ||
| } | ||
| return ws | ||
| } | ||
|
|
||
| //nolint:unused | ||
| func newWindowsServiceScraper(settings receiver.Settings, _ *Config) windowsServiceScraper { | ||
| return windowsServiceScraper{ | ||
| settings: settings, | ||
| mb: metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), settings), | ||
| func mapStartTypeToAttr(st StartType) metadata.AttributeStartupMode { | ||
| switch st { | ||
| case StartBoot: | ||
| return metadata.AttributeStartupModeBootStart | ||
| case StartSystem: | ||
| return metadata.AttributeStartupModeSystemStart | ||
| case StartAutomatic: | ||
| return metadata.AttributeStartupModeAutoStart | ||
| case StartManual: | ||
| return metadata.AttributeStartupModeDemandStart | ||
| case StartDisabled: | ||
| return metadata.AttributeStartupModeDisabled | ||
| default: | ||
| return metadata.AttributeStartupModeDemandStart | ||
| } | ||
| } | ||
|
|
||
| //nolint:unused | ||
| func (*windowsServiceScraper) start(context.Context, component.Host) (err error) { | ||
| return nil | ||
| func (ws *windowsServiceScraper) start(_ context.Context, _ component.Host) error { | ||
| return ws.mgr.connect() | ||
| } | ||
|
|
||
| //nolint:unused | ||
| func (*windowsServiceScraper) shutdown(context.Context) (err error) { | ||
| return nil | ||
| func (ws *windowsServiceScraper) shutdown(_ context.Context) error { | ||
| return ws.mgr.disconnect() | ||
| } | ||
|
|
||
| //nolint:unused | ||
| func (ws *windowsServiceScraper) scrape(context.Context) (pmetric.Metrics, error) { | ||
| return ws.mb.Emit(), nil | ||
| func (ws *windowsServiceScraper) allowed(name string) bool { | ||
| if len(ws.includeSet) > 0 { | ||
| if _, ok := ws.includeSet[name]; !ok { | ||
| return false | ||
| } | ||
| } | ||
| if _, banned := ws.excludeSet[name]; banned { | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func (ws *windowsServiceScraper) scrape(_ context.Context) (pmetric.Metrics, error) { | ||
| ts := pcommon.NewTimestampFromTime(time.Now()) | ||
|
|
||
| names, err := ws.mgr.listServices() | ||
| if err != nil { | ||
| return ws.mb.Emit(), err | ||
| } | ||
|
|
||
| var scrapeErr error | ||
|
|
||
| for _, name := range names { | ||
|
atoulme marked this conversation as resolved.
|
||
| if !ws.allowed(name) { | ||
| continue | ||
| } | ||
|
|
||
| svc, err := updateService(&ws.mgr, name) | ||
| if err != nil { | ||
| scrapeErr = multierr.Append(scrapeErr, err) | ||
| continue | ||
| } | ||
|
|
||
| func() { | ||
|
pjanotti marked this conversation as resolved.
Outdated
|
||
| defer func() { _ = svc.close() }() | ||
|
|
||
| if err := svc.updateStatus(); err != nil { | ||
| scrapeErr = multierr.Append(scrapeErr, err) | ||
| return | ||
| } | ||
| if err := svc.updateConfig(); err != nil { | ||
| scrapeErr = multierr.Append(scrapeErr, err) | ||
| return | ||
| } | ||
|
|
||
| val := int64(svc.status.State) | ||
|
|
||
| if val < 1 || val > 7 { | ||
| val = 0 | ||
| } | ||
|
|
||
| startAttr := mapStartTypeToAttr(svc.config.StartType) | ||
| ws.mb.RecordWindowsServiceStatusDataPoint(ts, val, name, startAttr) | ||
| }() | ||
| } | ||
|
|
||
| return ws.mb.Emit(), scrapeErr | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since there is a file named
factory_windows.gothis file,factory.go, is expected to have code that is common to all platforms and this file should not need this compile directive.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This directive shouldn't be necessary anymore... I see that it is still needed because there is still a
NewFactoryimplementation infactory_others.go...