Skip to content
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

[receiver/chrony] generated lifecycle tests #31524

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/chronyreceiver_lifecycle.yaml
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: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: chronyreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: move initialization of the chrony client to the start function

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [27849]

# (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: []
12 changes: 7 additions & 5 deletions receiver/chronyreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ func newMetricsReceiver(
return nil, fmt.Errorf("wrong config provided: %w", errInvalidValue)
}

chronyc, err := chrony.New(cfg.Endpoint, cfg.Timeout)
if err != nil {
return nil, err
}
s := newScraper(ctx, cfg, set)
scraper, err := scraperhelper.NewScraper(
metadata.Type.String(),
newScraper(ctx, chronyc, cfg, set).scrape,
s.scrape,
scraperhelper.WithStart(func(ctx context.Context, host component.Host) error {
chronyc, err := chrony.New(cfg.Endpoint, cfg.Timeout)
s.client = chronyc
return err
dmitryax marked this conversation as resolved.
Show resolved Hide resolved
}),
)
if err != nil {
return nil, err
Expand Down
68 changes: 68 additions & 0 deletions receiver/chronyreceiver/generated_component_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions receiver/chronyreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,7 @@ metrics:
value_type: double
attributes:
- leap.status

tests:
config:
skip_lifecycle: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you try to start the receiver, it fails looking for the chrony socket file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So why do we need to do the functional changes if the test fails anyway? That can be done separately right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generated tests test two scenarios:

  • lifecycle, meaning we start and stop the receiver with a configuration, and make sure it is able to run. This is skipped.
  • shutdown: we create the receiver and call .Shutdown on it. We check the receiver doesn't panic on this unexpected call.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know. so the shutdown part is failing without functional changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, the current behavior of the chrony receiver is to create a chrony client before creating the receiver. This client immediately looks up a file on disk and fails. Moving the chrony client creation to start helps.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying 👍

3 changes: 1 addition & 2 deletions receiver/chronyreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ type chronyScraper struct {
mb *metadata.MetricsBuilder
}

func newScraper(ctx context.Context, client chrony.Client, cfg *Config, set receiver.CreateSettings) *chronyScraper {
func newScraper(ctx context.Context, cfg *Config, set receiver.CreateSettings) *chronyScraper {
return &chronyScraper{
client: client,
mb: metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, set,
metadata.WithStartTime(pcommon.NewTimestampFromTime(clock.FromContext(ctx).Now())),
),
Expand Down
3 changes: 2 additions & 1 deletion receiver/chronyreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ func TestChronyScraper(t *testing.T) {
chronym.On("GetTrackingData").Return(tc.mockTracking, tc.mockErr)

ctx := clock.Context(context.Background(), clck)
scraper := newScraper(ctx, chronym, tc.conf, receivertest.NewNopCreateSettings())
scraper := newScraper(ctx, tc.conf, receivertest.NewNopCreateSettings())
scraper.client = chronym

metrics, err := scraper.scrape(ctx)

Expand Down
Loading