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

gcp/observability: fix End() to cleanup global state correctly #5623

Merged
merged 5 commits into from Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Expand Up @@ -109,7 +109,7 @@ jobs:
cd "${GITHUB_WORKSPACE}"
for MOD_FILE in $(find . -name 'go.mod' | grep -Ev '^\./go\.mod'); do
pushd "$(dirname ${MOD_FILE})"
go test ${{ matrix.testflags }} -timeout 2m ./...
go test ${{ matrix.testflags }} -cpu 1,4 -timeout 2m ./...
popd
done

Expand Down
1 change: 1 addition & 0 deletions gcp/observability/observability.go
Expand Up @@ -80,4 +80,5 @@ func Start(ctx context.Context) error {
// Note: this method should only be invoked once.
func End() {
defaultLogger.Close()
stopOpenCensus()
}
7 changes: 0 additions & 7 deletions gcp/observability/observability_test.go
Expand Up @@ -35,7 +35,6 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
grpclogrecordpb "google.golang.org/grpc/gcp/observability/internal/logging"
"google.golang.org/grpc/internal"
iblog "google.golang.org/grpc/internal/binarylog"
"google.golang.org/grpc/internal/grpctest"
"google.golang.org/grpc/internal/leakcheck"
Expand Down Expand Up @@ -875,12 +874,6 @@ func (s) TestCustomTagsTracingMetrics(t *testing.T) {
cleanup, err := createTmpConfigInFileSystem(configJSON)
defer cleanup()

// To clear globally registered tracing and metrics exporters.
defer func() {
internal.ClearExtraDialOptions()
internal.ClearExtraServerOptions()
}()

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
err = Start(ctx)
Expand Down
24 changes: 23 additions & 1 deletion gcp/observability/opencensus.go
Expand Up @@ -58,6 +58,8 @@ type tracingMetricsExporter interface {
view.Exporter
}

var exporter tracingMetricsExporter

// global to stub out in tests
var newExporter = newStackdriverExporter

Expand Down Expand Up @@ -87,7 +89,8 @@ func startOpenCensus(config *config) error {
return nil
}

exporter, err := newExporter(config)
var err error
exporter, err = newExporter(config)
dfawley marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down Expand Up @@ -118,3 +121,22 @@ func startOpenCensus(config *config) error {

return nil
}

// stopOpenCensus flushes the exporter's and cleans up globals across all
// packages if exporter was created.
func stopOpenCensus() {
if exporter != nil {
internal.ClearExtraDialOptions()
internal.ClearExtraServerOptions()

// Call these unconditionally, doesn't matter if not registered, will be
// a noop if not registered.
trace.UnregisterExporter(exporter)
view.UnregisterExporter(exporter)
dfawley marked this conversation as resolved.
Show resolved Hide resolved

if sdExporter, ok := exporter.(*stackdriver.Exporter); ok {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not define Flush and Close methods on tracingMetricsExporter instead? Does it make the tests unnecessarily complex?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

sdExporter.Flush()
sdExporter.Close()
}
}
}