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

[service] Fix memory leaks and enable goleak check in tests #9241

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions .chloggen/goleak_service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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. otlpreceiver)
component: service

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix memory leaks during service package shutdown

# One or more tracking issues or pull requests related to the change
issues: [9165]

# (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:

# 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 service/generated_package_test.go

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

23 changes: 15 additions & 8 deletions service/internal/proctelemetry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"net/url"
"os"
"strings"
"sync"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -63,9 +64,9 @@
errNoValidMetricExporter = errors.New("no valid metric exporter")
)

func InitMetricReader(ctx context.Context, reader config.MetricReader, asyncErrorChannel chan error) (sdkmetric.Reader, *http.Server, error) {
func InitMetricReader(ctx context.Context, reader config.MetricReader, asyncErrorChannel chan error, serverWG *sync.WaitGroup) (sdkmetric.Reader, *http.Server, error) {
if reader.Pull != nil {
return initPullExporter(reader.Pull.Exporter, asyncErrorChannel)
return initPullExporter(reader.Pull.Exporter, asyncErrorChannel, serverWG)
}
if reader.Periodic != nil {
var opts []sdkmetric.PeriodicReaderOption
Expand Down Expand Up @@ -97,16 +98,22 @@
), nil
}

func InitPrometheusServer(registry *prometheus.Registry, address string, asyncErrorChannel chan error) *http.Server {
func InitPrometheusServer(registry *prometheus.Registry, address string, asyncErrorChannel chan error, serverWG *sync.WaitGroup) *http.Server {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
server := &http.Server{
Addr: address,
Handler: mux,
}

serverWG.Add(1)
go func() {
defer serverWG.Done()
if serveErr := server.ListenAndServe(); serveErr != nil && !errors.Is(serveErr, http.ErrServerClosed) {
asyncErrorChannel <- serveErr
select {
case asyncErrorChannel <- serveErr:
case <-time.After(1 * time.Second):

Check warning on line 115 in service/internal/proctelemetry/config.go

View check run for this annotation

Codecov / codecov/patch

service/internal/proctelemetry/config.go#L113-L115

Added lines #L113 - L115 were not covered by tests
}
}
}()
return server
Expand Down Expand Up @@ -155,7 +162,7 @@
}
}

func initPrometheusExporter(prometheusConfig *config.Prometheus, asyncErrorChannel chan error) (sdkmetric.Reader, *http.Server, error) {
func initPrometheusExporter(prometheusConfig *config.Prometheus, asyncErrorChannel chan error, serverWG *sync.WaitGroup) (sdkmetric.Reader, *http.Server, error) {
promRegistry := prometheus.NewRegistry()
if prometheusConfig.Host == nil {
return nil, nil, fmt.Errorf("host must be specified")
Expand All @@ -182,12 +189,12 @@
return nil, nil, fmt.Errorf("error creating otel prometheus exporter: %w", err)
}

return exporter, InitPrometheusServer(promRegistry, net.JoinHostPort(*prometheusConfig.Host, fmt.Sprintf("%d", *prometheusConfig.Port)), asyncErrorChannel), nil
return exporter, InitPrometheusServer(promRegistry, net.JoinHostPort(*prometheusConfig.Host, fmt.Sprintf("%d", *prometheusConfig.Port)), asyncErrorChannel, serverWG), nil
}

func initPullExporter(exporter config.MetricExporter, asyncErrorChannel chan error) (sdkmetric.Reader, *http.Server, error) {
func initPullExporter(exporter config.MetricExporter, asyncErrorChannel chan error, serverWG *sync.WaitGroup) (sdkmetric.Reader, *http.Server, error) {
if exporter.Prometheus != nil {
return initPrometheusExporter(exporter.Prometheus, asyncErrorChannel)
return initPrometheusExporter(exporter.Prometheus, asyncErrorChannel, serverWG)
}
return nil, nil, errNoValidMetricExporter
}
Expand Down
3 changes: 2 additions & 1 deletion service/internal/proctelemetry/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"net/url"
"sync"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -339,7 +340,7 @@ func TestMetricReader(t *testing.T) {
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
reader, server, err := InitMetricReader(context.Background(), tt.reader, make(chan error))
reader, server, err := InitMetricReader(context.Background(), tt.reader, make(chan error), &sync.WaitGroup{})
defer func() {
if reader != nil {
assert.NoError(t, reader.Shutdown(context.Background()))
Expand Down
1 change: 0 additions & 1 deletion service/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ tests:
top:
# See https://github.com/census-instrumentation/opencensus-go/issues/1191 for more information.
- "go.opencensus.io/stats/view.(*worker).start"
- "go.opentelemetry.io/collector/service/internal/proctelemetry.InitPrometheusServer.func1"

telemetry:
metrics:
Expand Down
20 changes: 13 additions & 7 deletions service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ func TestServiceTelemetryRestart(t *testing.T) {
assert.NoError(t, err)
assert.NoError(t, resp.Body.Close())
assert.Equal(t, http.StatusOK, resp.StatusCode)
// Response body must be closed now instead of defer as the test
// restarts the server on the same port. Leaving response open
// leaks a goroutine.
resp.Body.Close()

// Shutdown the service
require.NoError(t, srvOne.Shutdown(context.Background()))
Expand All @@ -362,6 +366,7 @@ func TestServiceTelemetryRestart(t *testing.T) {
100*time.Millisecond,
"Must get a valid response from the service",
)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)

// Shutdown the new service
Expand Down Expand Up @@ -536,13 +541,14 @@ func assertZPages(t *testing.T, zpagesAddr string) {

func newNopSettings() Settings {
return Settings{
BuildInfo: component.NewDefaultBuildInfo(),
CollectorConf: confmap.New(),
Receivers: receivertest.NewNopBuilder(),
Processors: processortest.NewNopBuilder(),
Exporters: exportertest.NewNopBuilder(),
Connectors: connectortest.NewNopBuilder(),
Extensions: extensiontest.NewNopBuilder(),
BuildInfo: component.NewDefaultBuildInfo(),
CollectorConf: confmap.New(),
Receivers: receivertest.NewNopBuilder(),
Processors: processortest.NewNopBuilder(),
Exporters: exportertest.NewNopBuilder(),
Connectors: connectortest.NewNopBuilder(),
Extensions: extensiontest.NewNopBuilder(),
AsyncErrorChannel: make(chan error),
}
}

Expand Down
11 changes: 8 additions & 3 deletions service/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"net/http"
"strconv"
"sync"

"go.opentelemetry.io/contrib/config"
"go.opentelemetry.io/otel/metric"
Expand All @@ -29,7 +30,8 @@ const (

type meterProvider struct {
*sdkmetric.MeterProvider
servers []*http.Server
servers []*http.Server
serverWG sync.WaitGroup
}

type meterProviderSettings struct {
Expand Down Expand Up @@ -71,7 +73,7 @@ func newMeterProvider(set meterProviderSettings, disableHighCardinality bool) (m
var opts []sdkmetric.Option
for _, reader := range set.cfg.Readers {
// https://github.com/open-telemetry/opentelemetry-collector/issues/8045
r, server, err := proctelemetry.InitMetricReader(context.Background(), reader, set.asyncErrorChannel)
r, server, err := proctelemetry.InitMetricReader(context.Background(), reader, set.asyncErrorChannel, &mp.serverWG)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -110,5 +112,8 @@ func (mp *meterProvider) Shutdown(ctx context.Context) error {
errs = multierr.Append(errs, server.Close())
}
}
return multierr.Append(errs, mp.MeterProvider.Shutdown(ctx))
errs = multierr.Append(errs, mp.MeterProvider.Shutdown(ctx))
mp.serverWG.Wait()

return errs
}
Loading