forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/stanza] Cache event publishers: log warn once per provider (open…
…-telemetry#27658) **Description:** Cache the publisher event to: 1. Avoid logging the same error message every time one event from the given source is logged. 2. Avoid opening and closing the event publisher for every single event. **Link to tracking Issue:** [Item 4 described on the investigation](open-telemetry#21491 (comment)) for issue open-telemetry#21491. **Testing:** * Go tests for `pkg/stanza` and `receiver/windowseventlogreceiver` on Windows box. * Ran the contrib build locally to validate the change. * Can't run the full make locally: misspell is failing on Windows because the command line is too long. **Documentation:** Let me know if changing the severity of the log message requires a changelog update.
- Loading branch information
1 parent
3ee13b6
commit 9c9523d
Showing
6 changed files
with
182 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: pkg/stanza | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Log warning, instead of error, when Windows Event Log publisher metadata is not available and cache the successfully retrieved ones. | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [27658] | ||
|
||
# (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: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# 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: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//go:build windows | ||
// +build windows | ||
|
||
package windows // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/windows" | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
type publisherCache struct { | ||
cache map[string]Publisher | ||
} | ||
|
||
func newPublisherCache() publisherCache { | ||
return publisherCache{ | ||
cache: make(map[string]Publisher), | ||
} | ||
} | ||
|
||
func (c *publisherCache) get(provider string) (publisher Publisher, openPublisherErr error) { | ||
publisher, ok := c.cache[provider] | ||
if ok { | ||
return publisher, nil | ||
} | ||
|
||
publisher = NewPublisher() | ||
err := publisher.Open(provider) | ||
|
||
// Always store the publisher even if there was an error opening it. | ||
c.cache[provider] = publisher | ||
|
||
return publisher, err | ||
} | ||
|
||
func (c *publisherCache) evictAll() error { | ||
var errs error | ||
for _, publisher := range c.cache { | ||
if publisher.Valid() { | ||
if err := publisher.Close(); err != nil { | ||
errs = errors.Join(errs, err) | ||
} | ||
} | ||
} | ||
|
||
c.cache = make(map[string]Publisher) | ||
return errs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//go:build windows | ||
// +build windows | ||
|
||
package windows | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetValidPublisher(t *testing.T) { | ||
publisherCache := newPublisherCache() | ||
defer publisherCache.evictAll() | ||
|
||
// Provider "Application" exists in all Windows versions. | ||
publisher, openPublisherErr := publisherCache.get("Application") | ||
require.NoError(t, openPublisherErr) | ||
require.True(t, publisher.Valid()) | ||
|
||
// Get the same publisher again. | ||
publisher, openPublisherErr = publisherCache.get("Application") | ||
require.NoError(t, openPublisherErr) | ||
require.True(t, publisher.Valid()) | ||
} | ||
|
||
func TestGetInvalidPublisher(t *testing.T) { | ||
publisherCache := newPublisherCache() | ||
defer publisherCache.evictAll() | ||
|
||
// Provider "InvalidProvider" does not exist in any Windows version. | ||
publisher, openPublisherErr := publisherCache.get("InvalidProvider") | ||
require.Error(t, openPublisherErr, "%v", publisherCache) | ||
require.False(t, publisher.Valid()) | ||
|
||
// Get "InvalidProvider" publisher again. | ||
publisher, openPublisherErr = publisherCache.get("InvalidProvider") | ||
require.NoError(t, openPublisherErr) // It is cached, no error opening it. | ||
require.False(t, publisher.Valid()) | ||
} | ||
|
||
func TestValidAndInvalidPublishers(t *testing.T) { | ||
publisherCache := newPublisherCache() | ||
defer publisherCache.evictAll() | ||
|
||
// Provider "Application" exists in all Windows versions. | ||
publisher, openPublisherErr := publisherCache.get("Application") | ||
require.NoError(t, openPublisherErr) | ||
require.True(t, publisher.Valid()) | ||
|
||
// Provider "InvalidProvider" does not exist in any Windows version. | ||
publisher, openPublisherErr = publisherCache.get("InvalidProvider") | ||
require.Error(t, openPublisherErr, "%v", publisherCache) | ||
require.False(t, publisher.Valid()) | ||
|
||
// Get the existing publisher again. | ||
publisher, openPublisherErr = publisherCache.get("Application") | ||
require.NoError(t, openPublisherErr) | ||
require.True(t, publisher.Valid()) | ||
|
||
// Get "InvalidProvider" publisher again. | ||
publisher, openPublisherErr = publisherCache.get("InvalidProvider") | ||
require.NoError(t, openPublisherErr) // It is cached, no error opening it. | ||
require.False(t, publisher.Valid()) | ||
} |