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 2 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
19 changes: 19 additions & 0 deletions gcp/observability/observability.go
Expand Up @@ -29,7 +29,12 @@ import (
"context"
"fmt"

"contrib.go.opencensus.io/exporter/stackdriver"

"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/internal"
)

var logger = grpclog.Component("observability")
Expand Down Expand Up @@ -80,4 +85,18 @@ func Start(ctx context.Context) error {
// Note: this method should only be invoked once.
func End() {
defaultLogger.Close()
if exporter != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Let's create a stopOpenCensus function that does the cleanup needed due to startOpenCensus, and put it right next to it so we can be sure everything pairs up between start/end.

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. Thanks.

if sdExporter, ok := exporter.(*stackdriver.Exporter); ok {
sdExporter.Flush()
sdExporter.Close()
}

// Call these unconditionally, doesn't matter if not registered, will be
// a noop if not registered.
trace.UnregisterExporter(exporter)
view.UnregisterExporter(exporter)

internal.ClearExtraDialOptions()
internal.ClearExtraServerOptions()
}
}
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
5 changes: 4 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