-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore][receiver/googlecloudpubsubreceiver] Fix goroutines leak (#37311)
#### Description Fixes goroutines leak by properly closing the underlying gRPC client which is only when we're using an insecure custom endpoint. Enables shutdown tests. #### Link to tracking issue Related to #30438 Co-authored-by: Kevin N. <6809505+kevinnoel-be@users.noreply.github.com>
- Loading branch information
1 parent
b026f73
commit 0708844
Showing
10 changed files
with
304 additions
and
47 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: googlecloudpubsubreceiver | ||
note: Fix a goroutine leak during shutdown. | ||
issues: [30438] | ||
subtext: | | ||
A goroutine leak was found in the googlecloudpubsubreceiver. | ||
The goroutine leak was caused by the receiver not closing the underlying created gRPC client when using an insecure custom endpoint. | ||
change_logs: [] |
53 changes: 53 additions & 0 deletions
53
receiver/googlecloudpubsubreceiver/generated_component_test.go
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
17 changes: 17 additions & 0 deletions
17
receiver/googlecloudpubsubreceiver/internal/subscriber_client.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,17 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/googlecloudpubsubreceiver/internal" | ||
|
||
import ( | ||
"context" | ||
|
||
"cloud.google.com/go/pubsub/apiv1/pubsubpb" | ||
"github.com/googleapis/gax-go/v2" | ||
) | ||
|
||
// subscriberClient subset of `pubsub.SubscriberClient` | ||
type SubscriberClient interface { | ||
Close() error | ||
StreamingPull(ctx context.Context, opts ...gax.CallOption) (pubsubpb.Subscriber_StreamingPullClient, error) | ||
} |
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
9 changes: 9 additions & 0 deletions
9
receiver/googlecloudpubsubreceiver/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\nxnfpjNbkUfVVzO0MQD7UP0Hl5ZcY0Puvxd/yHuONQn/rIAieTHH1pqgW+zrH/y3c\n59IGThC9PPtugI9ea8RSnVj3PWz1bX2UkCDpy9IRh9LzJLaYYX9RUd7++dULUlat\nAaXBh1U6emUDzhrIsgApjDVtimOPbmQWmX1S60mqQikRpVYZ8u+NDD+LNw+/Eovn\nxCj2Y3z1AgMBAAECggE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AoGBANNkKAcMoeAbJQK7a/Rn\nwPEJB+dPgNDIaboAsh1nZhVhN5cvdvCWuEYgOGCPQLYQF0zmTLcM+sVxOYgfy8mV\nfbNgPgsP5xmu6dw2COBKdtozw0HrWSRjACd1N4yGu75+wPCcX/gQarcjRcXXZeEa\nNtBLSfcqPULqD+h7br9lEJio\n-----END PRIVATE KEY-----\n", | ||
"client_email": "123-abc@developer.gserviceaccount.com", | ||
"client_id": "123-abc.apps.googleusercontent.com", | ||
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | ||
"token_uri": "http://localhost:8080/token" | ||
} |
76 changes: 76 additions & 0 deletions
76
receiver/googlecloudpubsubreceiver/wrapped_subscriber_client.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,76 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package googlecloudpubsubreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/googlecloudpubsubreceiver" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
pubsub "cloud.google.com/go/pubsub/apiv1" | ||
"google.golang.org/api/option" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/googlecloudpubsubreceiver/internal" | ||
) | ||
|
||
// wrappedSubscriberClient allows to override the close function | ||
type wrappedSubscriberClient struct { | ||
internal.SubscriberClient | ||
closeFn func() error | ||
} | ||
|
||
func (c *wrappedSubscriberClient) Close() error { | ||
if c.closeFn != nil { | ||
return c.closeFn() | ||
} | ||
return c.SubscriberClient.Close() | ||
} | ||
|
||
func newSubscriberClient(ctx context.Context, config *Config, userAgent string) (internal.SubscriberClient, 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.NewSubscriberClient(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 &wrappedSubscriberClient{ | ||
SubscriberClient: 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 | ||
} |
Oops, something went wrong.