Skip to content

Commit 71f1083

Browse files
authored
[exporter/loadbalancing] Fix memory leaks (#31050)
**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.--> This fixes a few goroutine leaks in the loadbalancing exporter. 1. `metrics`, `traces`, and `logs` exporters were starting their respective load balancers, but were not shutting them down. This adds each respective shutdown call. 2. The `loadbalancer` was starting the resolver but never shutting it down. This adds a shutdown call to the resolver. 3. The static resolver was starting resolvers for each passed in exporter, but never shut them down. This adds a shutdown call for each resolver in the static resolver. Also added a couple missing `Shutdown` calls from tests. **Link to tracking Issue:** <Issue number if applicable> #30438 **Testing:** <Describe what testing was performed and which tests were added.> All existing tests are passing as well as added goleak checks.
1 parent 97f685e commit 71f1083

10 files changed

+66
-9
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: loadbalancingexporter
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fix memory leaks on shutdown
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [31050]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

exporter/loadbalancingexporter/go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
go.opentelemetry.io/collector/semconv v0.96.1-0.20240306115632-b2693620eff6
1717
go.opentelemetry.io/otel/metric v1.24.0
1818
go.opentelemetry.io/otel/trace v1.24.0
19+
go.uber.org/goleak v1.3.0
1920
go.uber.org/multierr v1.11.0
2021
go.uber.org/zap v1.27.0
2122
k8s.io/api v0.29.2

exporter/loadbalancingexporter/loadbalancer.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,10 @@ func endpointFound(endpoint string, endpoints []string) bool {
168168
return false
169169
}
170170

171-
func (lb *loadBalancer) Shutdown(context.Context) error {
171+
func (lb *loadBalancer) Shutdown(ctx context.Context) error {
172+
err := lb.res.shutdown(ctx)
172173
lb.stopped = true
173-
return nil
174+
return err
174175
}
175176

176177
// exporterAndEndpoint returns the exporter and the endpoint for the given identifier.

exporter/loadbalancingexporter/loadbalancer_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func TestWithDNSResolverNoEndpoints(t *testing.T) {
134134

135135
err = p.Start(context.Background(), componenttest.NewNopHost())
136136
require.NoError(t, err)
137+
defer func() { assert.NoError(t, p.Shutdown(context.Background())) }()
137138

138139
// test
139140
_, e, _ := p.exporterAndEndpoint([]byte{128, 128, 0, 0})

exporter/loadbalancingexporter/log_exporter.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@ func (e *logExporterImp) Start(ctx context.Context, host component.Host) error {
5757
return e.loadBalancer.Start(ctx, host)
5858
}
5959

60-
func (e *logExporterImp) Shutdown(context.Context) error {
60+
func (e *logExporterImp) Shutdown(ctx context.Context) error {
6161
if !e.started {
6262
return nil
6363
}
64+
err := e.loadBalancer.Shutdown(ctx)
6465
e.started = false
6566
e.shutdownWg.Wait()
66-
return nil
67+
return err
6768
}
6869

6970
func (e *logExporterImp) ConsumeLogs(ctx context.Context, ld plog.Logs) error {

exporter/loadbalancingexporter/metrics_exporter.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ func (e *metricExporterImp) Start(ctx context.Context, host component.Host) erro
7474
return e.loadBalancer.Start(ctx, host)
7575
}
7676

77-
func (e *metricExporterImp) Shutdown(context.Context) error {
77+
func (e *metricExporterImp) Shutdown(ctx context.Context) error {
78+
err := e.loadBalancer.Shutdown(ctx)
7879
e.stopped = true
7980
e.shutdownWg.Wait()
80-
return nil
81+
return err
8182
}
8283

8384
func (e *metricExporterImp) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package loadbalancingexporter
5+
6+
import (
7+
"testing"
8+
9+
"go.uber.org/goleak"
10+
)
11+
12+
// The IgnoreTopFunction call prevents catching the leak generated by opencensus
13+
// defaultWorker.Start which at this time is part of the package's init call.
14+
// See https://github.com/census-instrumentation/opencensus-go/issues/1191 for more information.
15+
func TestMain(m *testing.M) {
16+
goleak.VerifyTestMain(m, goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"))
17+
}

exporter/loadbalancingexporter/resolver_dns_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func TestCantResolve(t *testing.T) {
107107

108108
// verify
109109
assert.NoError(t, err)
110+
assert.NoError(t, res.shutdown(context.Background()))
110111
}
111112

112113
func TestOnChange(t *testing.T) {

exporter/loadbalancingexporter/resolver_static.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ func (r *staticResolver) start(ctx context.Context) error {
4949
return err
5050
}
5151

52-
func (r *staticResolver) shutdown(_ context.Context) error {
52+
func (r *staticResolver) shutdown(context.Context) error {
53+
r.endpoints = nil
54+
55+
for _, callback := range r.onChangeCallbacks {
56+
callback(r.endpoints)
57+
}
58+
5359
return nil
5460
}
5561

exporter/loadbalancingexporter/trace_exporter.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ func (e *traceExporterImp) Start(ctx context.Context, host component.Host) error
7272
return e.loadBalancer.Start(ctx, host)
7373
}
7474

75-
func (e *traceExporterImp) Shutdown(context.Context) error {
75+
func (e *traceExporterImp) Shutdown(ctx context.Context) error {
76+
err := e.loadBalancer.Shutdown(ctx)
7677
e.stopped = true
7778
e.shutdownWg.Wait()
78-
return nil
79+
return err
7980
}
8081

8182
func (e *traceExporterImp) ConsumeTraces(ctx context.Context, td ptrace.Traces) error {

0 commit comments

Comments
 (0)