From 0f0a1c77b89483651c2c9dc5f0cf38978eb38a7a Mon Sep 17 00:00:00 2001 From: Curtis Robert Date: Tue, 30 Jan 2024 09:54:13 -0800 Subject: [PATCH 1/8] [exporter/signalfx] Fix goleak failures --- .chloggen/goleak_signalfx_correlations.yaml | 27 +++++++++++++++++++ .../internal/apm/correlations/client.go | 7 +++++ .../internal/apm/correlations/client_test.go | 23 +++++++++------- .../internal/apm/correlations/package_test.go | 17 ++++++++++++ .../internal/correlation/correlation.go | 1 + .../internal/correlation/correlation_test.go | 4 +++ .../internal/correlation/package_test.go | 17 ++++++++++++ 7 files changed, 86 insertions(+), 10 deletions(-) create mode 100755 .chloggen/goleak_signalfx_correlations.yaml create mode 100644 exporter/signalfxexporter/internal/apm/correlations/package_test.go create mode 100644 exporter/signalfxexporter/internal/correlation/package_test.go diff --git a/.chloggen/goleak_signalfx_correlations.yaml b/.chloggen/goleak_signalfx_correlations.yaml new file mode 100755 index 000000000000..d20b4d2ccc73 --- /dev/null +++ b/.chloggen/goleak_signalfx_correlations.yaml @@ -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: signalfxexporter + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix memory leak in shutdown + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [30864, 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: [] diff --git a/exporter/signalfxexporter/internal/apm/correlations/client.go b/exporter/signalfxexporter/internal/apm/correlations/client.go index 427c0abb685f..ba86baf9a6c2 100644 --- a/exporter/signalfxexporter/internal/apm/correlations/client.go +++ b/exporter/signalfxexporter/internal/apm/correlations/client.go @@ -44,6 +44,7 @@ type CorrelationClient interface { Delete(*Correlation, SuccessfulDeleteCB) Get(dimName string, dimValue string, cb SuccessfulGetCB) Start() + Shutdown() } type request struct { @@ -387,3 +388,9 @@ func (cc *Client) Start() { go cc.processChan() go cc.processRetryChan() } + +// Shutdown the client. This will block until the context's cancel +// function is complete. +func (cc *Client) Shutdown() { + cc.wg.Wait() +} diff --git a/exporter/signalfxexporter/internal/apm/correlations/client_test.go b/exporter/signalfxexporter/internal/apm/correlations/client_test.go index 48cb24b996de..5cf6cd2a1d0a 100644 --- a/exporter/signalfxexporter/internal/apm/correlations/client_test.go +++ b/exporter/signalfxexporter/internal/apm/correlations/client_test.go @@ -124,7 +124,7 @@ func makeHandler(t *testing.T, corCh chan<- *request, forcedRespCode *atomic.Val }) } -func setup(t *testing.T) (CorrelationClient, chan *request, *atomic.Value, *atomic.Value, context.CancelFunc) { +func setup(t *testing.T) (CorrelationClient, *httptest.Server, chan *request, *atomic.Value, *atomic.Value, context.CancelFunc, context.Context) { serverCh := make(chan *request, 100) var forcedRespCode atomic.Value @@ -132,10 +132,6 @@ func setup(t *testing.T) (CorrelationClient, chan *request, *atomic.Value, *atom server := httptest.NewServer(makeHandler(t, serverCh, &forcedRespCode, &forcedRespPayload)) ctx, cancel := context.WithCancel(context.Background()) - go func() { - <-ctx.Done() - server.Close() - }() serverURL, err := url.Parse(server.URL) if err != nil { @@ -176,13 +172,20 @@ func setup(t *testing.T) (CorrelationClient, chan *request, *atomic.Value, *atom } client.Start() - return client, serverCh, &forcedRespCode, &forcedRespPayload, cancel + return client, server, serverCh, &forcedRespCode, &forcedRespPayload, cancel, ctx +} + +func teardown(client CorrelationClient, server *httptest.Server, serverCh chan *request, ctx context.Context, cancel context.CancelFunc) { + close(serverCh) + cancel() + <-ctx.Done() + client.Shutdown() + server.Close() } func TestCorrelationClient(t *testing.T) { - client, serverCh, forcedRespCode, forcedRespPayload, cancel := setup(t) - defer close(serverCh) - defer cancel() + client, server, serverCh, forcedRespCode, forcedRespPayload, cancel, ctx := setup(t) + defer teardown(client, server, serverCh, ctx, cancel) for _, correlationType := range []Type{Service, Environment} { for _, op := range []string{http.MethodPut, http.MethodDelete} { @@ -242,7 +245,7 @@ func TestCorrelationClient(t *testing.T) { client.Correlate(testData, CorrelateCB(func(_ *Correlation, _ error) {})) // sending the testData twice tests deduplication, since the 500 status // will trigger retries, and the requests should be deduped and the - // TotalRertriedUpdates should still only be 5 + // TotalRetriedUpdates should still only be 5 client.Correlate(testData, CorrelateCB(func(_ *Correlation, _ error) {})) cors := waitForCors(serverCh, 1, 4) diff --git a/exporter/signalfxexporter/internal/apm/correlations/package_test.go b/exporter/signalfxexporter/internal/apm/correlations/package_test.go new file mode 100644 index 000000000000..3aa20e78afd4 --- /dev/null +++ b/exporter/signalfxexporter/internal/apm/correlations/package_test.go @@ -0,0 +1,17 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package correlations + +import ( + "testing" + + "go.uber.org/goleak" +) + +// The IgnoreTopFunction call prevents catching the leak generated by opencensus +// defaultWorker.Start which at this time is part of the package's init call. +// See https://github.com/census-instrumentation/opencensus-go/issues/1191 for more information. +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m, goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start")) +} diff --git a/exporter/signalfxexporter/internal/correlation/correlation.go b/exporter/signalfxexporter/internal/correlation/correlation.go index ee00b0a470d0..281339e1df6c 100644 --- a/exporter/signalfxexporter/internal/correlation/correlation.go +++ b/exporter/signalfxexporter/internal/correlation/correlation.go @@ -137,6 +137,7 @@ func (cor *Tracker) Shutdown(_ context.Context) error { if cor != nil { if cor.correlation != nil { cor.correlation.cancel() + cor.correlation.CorrelationClient.Shutdown() } if cor.pTicker != nil { diff --git a/exporter/signalfxexporter/internal/correlation/correlation_test.go b/exporter/signalfxexporter/internal/correlation/correlation_test.go index d1e9db2f7c03..5bf62732a83d 100644 --- a/exporter/signalfxexporter/internal/correlation/correlation_test.go +++ b/exporter/signalfxexporter/internal/correlation/correlation_test.go @@ -86,6 +86,10 @@ func TestTrackerStart(t *testing.T) { } else { require.NoError(t, err) } + + if tracker != nil { + assert.NoError(t, tracker.Shutdown(context.Background())) + } }) } } diff --git a/exporter/signalfxexporter/internal/correlation/package_test.go b/exporter/signalfxexporter/internal/correlation/package_test.go new file mode 100644 index 000000000000..0d55bd5d83fe --- /dev/null +++ b/exporter/signalfxexporter/internal/correlation/package_test.go @@ -0,0 +1,17 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package correlation + +import ( + "testing" + + "go.uber.org/goleak" +) + +// The IgnoreTopFunction call prevents catching the leak generated by opencensus +// defaultWorker.Start which at this time is part of the package's init call. +// See https://github.com/census-instrumentation/opencensus-go/issues/1191 for more information. +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m, goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start")) +} From 0af31c991fe27b2cd6be443301e0d47ad838f6df Mon Sep 17 00:00:00 2001 From: Curtis Robert Date: Tue, 30 Jan 2024 14:29:42 -0800 Subject: [PATCH 2/8] Add missing Shutdown method --- .../signalfxexporter/internal/apm/tracetracker/tracker_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/exporter/signalfxexporter/internal/apm/tracetracker/tracker_test.go b/exporter/signalfxexporter/internal/apm/tracetracker/tracker_test.go index 43c638ac0948..b7fb27695550 100644 --- a/exporter/signalfxexporter/internal/apm/tracetracker/tracker_test.go +++ b/exporter/signalfxexporter/internal/apm/tracetracker/tracker_test.go @@ -85,7 +85,8 @@ type correlationTestClient struct { correlateCounter int64 } -func (c *correlationTestClient) Start() { /*no-op*/ } +func (c *correlationTestClient) Start() { /*no-op*/ } +func (c *correlationTestClient) Shutdown() { /*no-op*/ } func (c *correlationTestClient) Get(_ string, dimValue string, cb correlations.SuccessfulGetCB) { atomic.AddInt64(&c.getCounter, 1) go func() { From b7a904e4e591fef84e0591cb8e04aab5e8296928 Mon Sep 17 00:00:00 2001 From: Curtis Robert Date: Tue, 30 Jan 2024 14:37:56 -0800 Subject: [PATCH 3/8] Fix argument order --- .../signalfxexporter/internal/apm/correlations/client_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exporter/signalfxexporter/internal/apm/correlations/client_test.go b/exporter/signalfxexporter/internal/apm/correlations/client_test.go index 5cf6cd2a1d0a..87ff12866cb6 100644 --- a/exporter/signalfxexporter/internal/apm/correlations/client_test.go +++ b/exporter/signalfxexporter/internal/apm/correlations/client_test.go @@ -175,7 +175,7 @@ func setup(t *testing.T) (CorrelationClient, *httptest.Server, chan *request, *a return client, server, serverCh, &forcedRespCode, &forcedRespPayload, cancel, ctx } -func teardown(client CorrelationClient, server *httptest.Server, serverCh chan *request, ctx context.Context, cancel context.CancelFunc) { +func teardown(ctx context.Context, client CorrelationClient, server *httptest.Server, serverCh chan *request, cancel context.CancelFunc) { close(serverCh) cancel() <-ctx.Done() @@ -185,7 +185,7 @@ func teardown(client CorrelationClient, server *httptest.Server, serverCh chan * func TestCorrelationClient(t *testing.T) { client, server, serverCh, forcedRespCode, forcedRespPayload, cancel, ctx := setup(t) - defer teardown(client, server, serverCh, ctx, cancel) + defer teardown(ctx, client, server, serverCh, cancel) for _, correlationType := range []Type{Service, Environment} { for _, op := range []string{http.MethodPut, http.MethodDelete} { From 276d6ffb3e5cca92fded87eae64e89e06afb4dc4 Mon Sep 17 00:00:00 2001 From: Curtis Robert Date: Thu, 29 Feb 2024 14:53:28 -0800 Subject: [PATCH 4/8] Remove nil check --- .../signalfxexporter/internal/correlation/correlation_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/exporter/signalfxexporter/internal/correlation/correlation_test.go b/exporter/signalfxexporter/internal/correlation/correlation_test.go index 5bf62732a83d..4c2b7db6c83a 100644 --- a/exporter/signalfxexporter/internal/correlation/correlation_test.go +++ b/exporter/signalfxexporter/internal/correlation/correlation_test.go @@ -87,9 +87,7 @@ func TestTrackerStart(t *testing.T) { require.NoError(t, err) } - if tracker != nil { - assert.NoError(t, tracker.Shutdown(context.Background())) - } + assert.NoError(t, tracker.Shutdown(context.Background())) }) } } From 548ae0867c35d89a3e863ec4e494998055e297f6 Mon Sep 17 00:00:00 2001 From: Curtis Robert Date: Thu, 14 Mar 2024 14:56:22 -0700 Subject: [PATCH 5/8] Remove failing goleak check --- .../internal/correlation/package_test.go | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 exporter/signalfxexporter/internal/correlation/package_test.go diff --git a/exporter/signalfxexporter/internal/correlation/package_test.go b/exporter/signalfxexporter/internal/correlation/package_test.go deleted file mode 100644 index 0d55bd5d83fe..000000000000 --- a/exporter/signalfxexporter/internal/correlation/package_test.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -package correlation - -import ( - "testing" - - "go.uber.org/goleak" -) - -// The IgnoreTopFunction call prevents catching the leak generated by opencensus -// defaultWorker.Start which at this time is part of the package's init call. -// See https://github.com/census-instrumentation/opencensus-go/issues/1191 for more information. -func TestMain(m *testing.M) { - goleak.VerifyTestMain(m, goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start")) -} From af232c9b8b654f15eb630ade73ba0b341de96368 Mon Sep 17 00:00:00 2001 From: Curtis Robert Date: Thu, 14 Mar 2024 14:58:31 -0700 Subject: [PATCH 6/8] Remove goleak check --- .../internal/apm/correlations/package_test.go | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 exporter/signalfxexporter/internal/apm/correlations/package_test.go diff --git a/exporter/signalfxexporter/internal/apm/correlations/package_test.go b/exporter/signalfxexporter/internal/apm/correlations/package_test.go deleted file mode 100644 index 3aa20e78afd4..000000000000 --- a/exporter/signalfxexporter/internal/apm/correlations/package_test.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -package correlations - -import ( - "testing" - - "go.uber.org/goleak" -) - -// The IgnoreTopFunction call prevents catching the leak generated by opencensus -// defaultWorker.Start which at this time is part of the package's init call. -// See https://github.com/census-instrumentation/opencensus-go/issues/1191 for more information. -func TestMain(m *testing.M) { - goleak.VerifyTestMain(m, goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start")) -} From 518854d46f9c12dc47db8ada9eea9ffb7a210857 Mon Sep 17 00:00:00 2001 From: Curtis Robert Date: Thu, 14 Mar 2024 16:16:14 -0700 Subject: [PATCH 7/8] Only block when context is still alive --- .../internal/apm/correlations/package_test.go | 14 ++++++++++++++ .../internal/apm/requests/sender.go | 9 +++++++-- .../internal/correlation/package_test.go | 14 ++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 exporter/signalfxexporter/internal/apm/correlations/package_test.go create mode 100644 exporter/signalfxexporter/internal/correlation/package_test.go diff --git a/exporter/signalfxexporter/internal/apm/correlations/package_test.go b/exporter/signalfxexporter/internal/apm/correlations/package_test.go new file mode 100644 index 000000000000..334bbbdc8b54 --- /dev/null +++ b/exporter/signalfxexporter/internal/apm/correlations/package_test.go @@ -0,0 +1,14 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package correlations + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} diff --git a/exporter/signalfxexporter/internal/apm/requests/sender.go b/exporter/signalfxexporter/internal/apm/requests/sender.go index e06e649ea33a..119669a548a6 100644 --- a/exporter/signalfxexporter/internal/apm/requests/sender.go +++ b/exporter/signalfxexporter/internal/apm/requests/sender.go @@ -48,8 +48,13 @@ func (rs *ReqSender) Send(req *http.Request) { go rs.processRequests() } - // Block until we can get through a request - rs.requests <- req + // Block until we can get through a request, until context is cancelled, as the request processor + // also shuts down when the context has been cancelled. + select { + case <-rs.ctx.Done(): + return + case rs.requests <- req: + } } } diff --git a/exporter/signalfxexporter/internal/correlation/package_test.go b/exporter/signalfxexporter/internal/correlation/package_test.go new file mode 100644 index 000000000000..80d3ec2c9232 --- /dev/null +++ b/exporter/signalfxexporter/internal/correlation/package_test.go @@ -0,0 +1,14 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package correlation + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} From af5b95ab7515ebccc0977902c8ed73a7bf821cf9 Mon Sep 17 00:00:00 2001 From: Curtis Robert Date: Thu, 14 Mar 2024 16:21:15 -0700 Subject: [PATCH 8/8] Update comment --- exporter/signalfxexporter/internal/apm/requests/sender.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/exporter/signalfxexporter/internal/apm/requests/sender.go b/exporter/signalfxexporter/internal/apm/requests/sender.go index 119669a548a6..708f00571655 100644 --- a/exporter/signalfxexporter/internal/apm/requests/sender.go +++ b/exporter/signalfxexporter/internal/apm/requests/sender.go @@ -48,8 +48,9 @@ func (rs *ReqSender) Send(req *http.Request) { go rs.processRequests() } - // Block until we can get through a request, until context is cancelled, as the request processor - // also shuts down when the context has been cancelled. + // Block until we can get the request through, or until the context is cancelled. The request processor + // shuts down when the context has been cancelled, so there's no value added to keep blocking. Blocking + // forever results in Shutdown never completing. select { case <-rs.ctx.Done(): return