Skip to content

Commit

Permalink
[receiver/chrony] generated lifecycle tests (open-telemetry#31524)
Browse files Browse the repository at this point in the history
**Description:**
Add generated tests for chrony receiver; move initialization of the
chrony client to the start function

**Link to tracking Issue:**
Relates to open-telemetry#27849
  • Loading branch information
atoulme authored and XinRanZhAWS committed Mar 13, 2024
1 parent 1770c86 commit ee375a6
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 8 deletions.
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
}),
)
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
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

0 comments on commit ee375a6

Please sign in to comment.