forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore][exporter/googlecloudpubsub] Fix goroutines leak (open-telemet…
…ry#36591) #### Description Fixes goroutines leak by properly closing the underlying gRPC client which is only when we're using an insecure custom endpoint. Enables goleak in tests. #### Link to tracking issue Related to open-telemetry#30438
- Loading branch information
1 parent
1a0679f
commit 05ec876
Showing
9 changed files
with
242 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
change_type: bug_fix | ||
component: googlecloudpubsubexporter | ||
note: Fix a goroutine leak during shutdown. | ||
issues: [30438] | ||
subtext: | | ||
A goroutine leak was found in the googlecloudpubsubexporter. | ||
The goroutine leak was caused by the exporter not closing the underlying created gRPC client when using an insecure custom endpoint. | ||
change_logs: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package googlecloudpubsubexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/googlecloudpubsubexporter" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
pubsub "cloud.google.com/go/pubsub/apiv1" | ||
"cloud.google.com/go/pubsub/apiv1/pubsubpb" | ||
"github.com/googleapis/gax-go/v2" | ||
"google.golang.org/api/option" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
) | ||
|
||
// publisherClient subset of `pubsub.PublisherClient` | ||
type publisherClient interface { | ||
Close() error | ||
Publish(ctx context.Context, req *pubsubpb.PublishRequest, opts ...gax.CallOption) (*pubsubpb.PublishResponse, error) | ||
} | ||
|
||
// wrappedPublisherClient allows to override the close function | ||
type wrappedPublisherClient struct { | ||
publisherClient | ||
closeFn func() error | ||
} | ||
|
||
func (c *wrappedPublisherClient) Close() error { | ||
if c.closeFn != nil { | ||
return c.closeFn() | ||
} | ||
return c.publisherClient.Close() | ||
} | ||
|
||
func newPublisherClient(ctx context.Context, config *Config, userAgent string) (publisherClient, error) { | ||
clientOptions, closeFn, err := generateClientOptions(config, userAgent) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed preparing the gRPC client options to PubSub: %w", err) | ||
} | ||
|
||
client, err := pubsub.NewPublisherClient(ctx, clientOptions...) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed creating the gRPC client to PubSub: %w", err) | ||
} | ||
|
||
if closeFn == nil { | ||
return client, nil | ||
} | ||
|
||
return &wrappedPublisherClient{ | ||
publisherClient: client, | ||
closeFn: closeFn, | ||
}, nil | ||
} | ||
|
||
func generateClientOptions(config *Config, userAgent string) ([]option.ClientOption, func() error, error) { | ||
var copts []option.ClientOption | ||
var closeFn func() error | ||
|
||
if userAgent != "" { | ||
copts = append(copts, option.WithUserAgent(userAgent)) | ||
} | ||
if config.Endpoint != "" { | ||
if config.Insecure { | ||
var dialOpts []grpc.DialOption | ||
if userAgent != "" { | ||
dialOpts = append(dialOpts, grpc.WithUserAgent(userAgent)) | ||
} | ||
client, err := grpc.NewClient(config.Endpoint, append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))...) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
copts = append(copts, option.WithGRPCConn(client)) | ||
closeFn = client.Close // we need to be able to properly close the grpc client otherwise it'll leak goroutines | ||
} else { | ||
copts = append(copts, option.WithEndpoint(config.Endpoint)) | ||
} | ||
} | ||
return copts, closeFn, nil | ||
} |
126 changes: 126 additions & 0 deletions
126
exporter/googlecloudpubsubexporter/publisher_client_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package googlecloudpubsubexporter | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
pubsub "cloud.google.com/go/pubsub/apiv1" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/api/option" | ||
) | ||
|
||
func TestGenerateClientOptions(t *testing.T) { | ||
factory := NewFactory() | ||
|
||
t.Run("defaults", func(t *testing.T) { | ||
cfg := factory.CreateDefaultConfig().(*Config) | ||
cfg.ProjectID = "my-project" | ||
cfg.Topic = "projects/my-project/topics/otlp" | ||
|
||
require.NoError(t, cfg.Validate()) | ||
|
||
gotOptions, closeConnFn, err := generateClientOptions(cfg, "test-user-agent 6789") | ||
assert.NoError(t, err) | ||
assert.Empty(t, closeConnFn) | ||
|
||
expectedOptions := []option.ClientOption{ | ||
option.WithUserAgent("test-user-agent 6789"), | ||
} | ||
assert.ElementsMatch(t, expectedOptions, gotOptions) | ||
}) | ||
|
||
t.Run("secure custom endpoint", func(t *testing.T) { | ||
cfg := factory.CreateDefaultConfig().(*Config) | ||
cfg.ProjectID = "my-project" | ||
cfg.Topic = "projects/my-project/topics/otlp" | ||
cfg.Endpoint = "defg" | ||
|
||
require.NoError(t, cfg.Validate()) | ||
|
||
gotOptions, closeConnFn, err := generateClientOptions(cfg, "test-user-agent 4321") | ||
assert.NoError(t, err) | ||
assert.Empty(t, closeConnFn) | ||
|
||
expectedOptions := []option.ClientOption{ | ||
option.WithUserAgent("test-user-agent 4321"), | ||
option.WithEndpoint("defg"), | ||
} | ||
assert.ElementsMatch(t, expectedOptions, gotOptions) | ||
}) | ||
|
||
t.Run("insecure endpoint", func(t *testing.T) { | ||
cfg := factory.CreateDefaultConfig().(*Config) | ||
cfg.ProjectID = "my-project" | ||
cfg.Topic = "projects/my-project/topics/otlp" | ||
cfg.Endpoint = "abcd" | ||
cfg.Insecure = true | ||
|
||
require.NoError(t, cfg.Validate()) | ||
|
||
gotOptions, closeConnFn, err := generateClientOptions(cfg, "test-user-agent 1234") | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, closeConnFn) | ||
assert.NoError(t, closeConnFn()) | ||
|
||
require.Len(t, gotOptions, 2) | ||
assert.Equal(t, option.WithUserAgent("test-user-agent 1234"), gotOptions[0]) | ||
assert.IsType(t, option.WithGRPCConn(nil), gotOptions[1]) | ||
}) | ||
} | ||
|
||
func TestNewPublisherClient(t *testing.T) { | ||
// The publisher client checks for credentials during init | ||
t.Setenv("GOOGLE_APPLICATION_CREDENTIALS", "testdata/gcp-fake-creds.json") | ||
|
||
ctx := context.Background() | ||
factory := NewFactory() | ||
|
||
t.Run("defaults", func(t *testing.T) { | ||
cfg := factory.CreateDefaultConfig().(*Config) | ||
cfg.ProjectID = "my-project" | ||
cfg.Topic = "projects/my-project/topics/otlp" | ||
|
||
require.NoError(t, cfg.Validate()) | ||
|
||
client, err := newPublisherClient(ctx, cfg, "test-user-agent 6789") | ||
assert.NoError(t, err) | ||
require.NotEmpty(t, client) | ||
assert.IsType(t, &pubsub.PublisherClient{}, client) | ||
assert.NoError(t, client.Close()) | ||
}) | ||
|
||
t.Run("secure custom endpoint", func(t *testing.T) { | ||
cfg := factory.CreateDefaultConfig().(*Config) | ||
cfg.ProjectID = "my-project" | ||
cfg.Topic = "projects/my-project/topics/otlp" | ||
cfg.Endpoint = "xyz" | ||
|
||
require.NoError(t, cfg.Validate()) | ||
|
||
client, err := newPublisherClient(ctx, cfg, "test-user-agent 6789") | ||
assert.NoError(t, err) | ||
require.NotEmpty(t, client) | ||
assert.IsType(t, &pubsub.PublisherClient{}, client) | ||
assert.NoError(t, client.Close()) | ||
}) | ||
|
||
t.Run("insecure endpoint", func(t *testing.T) { | ||
cfg := factory.CreateDefaultConfig().(*Config) | ||
cfg.ProjectID = "my-project" | ||
cfg.Topic = "projects/my-project/topics/otlp" | ||
cfg.Endpoint = "abc" | ||
cfg.Insecure = true | ||
|
||
require.NoError(t, cfg.Validate()) | ||
|
||
client, err := newPublisherClient(ctx, cfg, "test-user-agent 6789") | ||
assert.NoError(t, err) | ||
require.NotEmpty(t, client) | ||
assert.IsType(t, &wrappedPublisherClient{}, client) | ||
assert.NoError(t, client.Close()) | ||
}) | ||
} |
9 changes: 9 additions & 0 deletions
9
exporter/googlecloudpubsubexporter/testdata/gcp-fake-creds.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"type": "service_account", | ||
"private_key_id": "abc", | ||
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDY3E8o1NEFcjMM\nHW/5ZfFJw29/8NEqpViNjQIx95Xx5KDtJ+nWn9+OW0uqsSqKlKGhAdAo+Q6bjx2c\nuXVsXTu7XrZUY5Kltvj94DvUa1wjNXs606r/RxWTJ58bfdC+gLLxBfGnB6CwK0YQ\nxnfpjNbkUfVVzO0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t+/zrDMDCPiSLX1NAvAoGBAN1T\nVLJYdjvIMxf1bm59VYcepbK7HLHFkRq6xMJMZbtG0ryraZjUzYvB4q4VjHk2UDiC\nlhx13tXWDZH7MJtABzjyg+AI7XWSEQs2cBXACos0M4Myc6lU+eL+iA+OuoUOhmrh\nqmT8YYGu76/IBWUSqWuvcpHPpwl7871i4Ga/I3qnAoGBANNkKAcMoeAbJQK7a/Rn\nwPEJB+dPgNDIaboAsh1nZhVhN5cvdvCWuEYgOGCPQLYQF0zmTLcM+sVxOYgfy8mV\nfbNgPgsP5xmu6dw2COBKdtozw0HrWSRjACd1N4yGu75+wPCcX/gQarcjRcXXZeEa\nNtBLSfcqPULqD+h7br9lEJio\n-----END PRIVATE KEY-----\n", | ||
"client_email": "[email protected]", | ||
"client_id": "123-abc.apps.googleusercontent.com", | ||
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | ||
"token_uri": "http://localhost:8080/token" | ||
} |