Skip to content

Commit

Permalink
[receiver/dockerstats] Fix leaking goroutines (open-telemetry#31668)
Browse files Browse the repository at this point in the history
**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
The dockerstats receiver starts a go routine that isn't shutdown until
its context is done. This adds a shutdown method to trigger the end of
this running goroutine in a timely manner. This also enables goleak to
check for leaking goroutines on this receiver.

**Link to tracking Issue:** <Issue number if applicable>
open-telemetry#30438

**Testing:** <Describe what testing was performed and which tests were
added.>
All existing and added checks are passing.
  • Loading branch information
crobert-1 authored Mar 11, 2024
1 parent 71e6c55 commit c0b5a1d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
27 changes: 27 additions & 0 deletions .chloggen/goleak_dockerstatsreceiver.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: dockerstatsreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add shutdown method to fix leaking goroutines

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

# (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: []
2 changes: 1 addition & 1 deletion receiver/dockerstatsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func createMetricsReceiver(
dockerConfig := config.(*Config)
dsr := newMetricsReceiver(params, dockerConfig)

scrp, err := scraperhelper.NewScraper(metadata.Type.String(), dsr.scrapeV2, scraperhelper.WithStart(dsr.start))
scrp, err := scraperhelper.NewScraper(metadata.Type.String(), dsr.scrapeV2, scraperhelper.WithStart(dsr.start), scraperhelper.WithShutdown(dsr.shutdown))
if err != nil {
return nil, err
}
Expand Down
14 changes: 14 additions & 0 deletions receiver/dockerstatsreceiver/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package dockerstatsreceiver

import (
"testing"

"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
13 changes: 12 additions & 1 deletion receiver/dockerstatsreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type metricsReceiver struct {
settings receiver.CreateSettings
client *docker.Client
mb *metadata.MetricsBuilder
cancel context.CancelFunc
}

func newMetricsReceiver(set receiver.CreateSettings, config *Config) *metricsReceiver {
Expand All @@ -65,7 +66,17 @@ func (r *metricsReceiver) start(ctx context.Context, _ component.Host) error {
return err
}

go r.client.ContainerEventLoop(ctx)
cctx, cancel := context.WithCancel(ctx)
r.cancel = cancel

go r.client.ContainerEventLoop(cctx)
return nil
}

func (r *metricsReceiver) shutdown(context.Context) error {
if r.cancel != nil {
r.cancel()
}
return nil
}

Expand Down
1 change: 1 addition & 0 deletions receiver/dockerstatsreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ func TestScrapeV2(t *testing.T) {
receivertest.NewNopCreateSettings(), tc.cfgBuilder.withEndpoint(mockDockerEngine.URL).build())
err := receiver.start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err)
defer func() { require.NoError(t, receiver.shutdown(context.Background())) }()

actualMetrics, err := receiver.scrapeV2(context.Background())
require.NoError(t, err)
Expand Down

0 comments on commit c0b5a1d

Please sign in to comment.