Skip to content

Commit

Permalink
gcp/observability: Implement tracing/metrics via OpenCensus (#5372)
Browse files Browse the repository at this point in the history
  • Loading branch information
lidizheng committed Jun 7, 2022
1 parent 34e4fc3 commit 9ee2f14
Show file tree
Hide file tree
Showing 8 changed files with 411 additions and 77 deletions.
3 changes: 3 additions & 0 deletions gcp/observability/config.go
Expand Up @@ -82,6 +82,9 @@ func parseObservabilityConfig() (*configpb.ObservabilityConfig, error) {
if err := validateFilters(&config); err != nil {
return nil, fmt.Errorf("error parsing observability config: %v", err)
}
if config.GlobalTraceSamplingRate > 1 || config.GlobalTraceSamplingRate < 0 {
return nil, fmt.Errorf("error parsing observability config: invalid global trace sampling rate %v", config.GlobalTraceSamplingRate)
}
logger.Infof("Parsed ObservabilityConfig: %+v", &config)
return &config, nil
}
Expand Down
2 changes: 2 additions & 0 deletions gcp/observability/go.mod
Expand Up @@ -4,8 +4,10 @@ go 1.14

require (
cloud.google.com/go/logging v1.4.2
contrib.go.opencensus.io/exporter/stackdriver v0.13.12
github.com/golang/protobuf v1.5.2
github.com/google/uuid v1.3.0
go.opencensus.io v0.23.0
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
google.golang.org/grpc v1.46.0
google.golang.org/protobuf v1.27.1
Expand Down
111 changes: 101 additions & 10 deletions gcp/observability/go.sum

Large diffs are not rendered by default.

117 changes: 78 additions & 39 deletions gcp/observability/internal/config/config.pb.go

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

29 changes: 20 additions & 9 deletions gcp/observability/internal/config/config.proto
@@ -1,16 +1,16 @@
// Copyright 2022 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

// Observability Config is used by gRPC Observability plugin to control provided
// observability features. It contains parameters to enable/disable certain
Expand All @@ -34,8 +34,14 @@ option go_package = "google.golang.org/grpc/gcp/observability/internal/config";
// most common knobs for gRPC users. It's always possible to override with
// explicit config in code.
message ObservabilityConfig {
// Whether the logging data uploading to CloudLogging should be enabled or
// not. The default value is true.
// Whether the tracing data upload to CloudTrace should be enabled or not.
bool enable_cloud_trace = 4;

// Whether the metrics data upload to CloudMonitoring should be enabled or
// not.
bool enable_cloud_monitoring = 5;

// Whether the logging data upload to CloudLogging should be enabled or not.
bool enable_cloud_logging = 1;

// The destination GCP project identifier for the uploading log entries. If
Expand Down Expand Up @@ -75,4 +81,9 @@ message ObservabilityConfig {
// the LogFilter. Any other LogFilter that also matches that comes later
// will be ignored. So a LogFilter of "*/*" should appear last in this list.
repeated LogFilter log_filters = 3;

// The global setting that controls the probability of a RPC being traced.
// For example, 0.05 means there is a 5% chance for a RPC to be traced, 1.0
// means trace every call, 0 means don’t start new traces.
double global_trace_sampling_rate = 6;
}
9 changes: 6 additions & 3 deletions gcp/observability/observability.go
Expand Up @@ -45,11 +45,9 @@ func init() {
//
// - it loads observability config from environment;
// - it registers default exporters if not disabled by the config;
// - it sets up binary logging sink against the logging exporter.
// - it sets up telemetry collectors (binary logging sink or StatsHandlers).
//
// Note: this method should only be invoked once.
// Note: currently, the binarylog module only supports one sink, so using the
// "observability" module will conflict with existing binarylog usage.
// Note: handle the error
func Start(ctx context.Context) error {
config, err := parseObservabilityConfig()
Expand All @@ -65,6 +63,11 @@ func Start(ctx context.Context) error {
return err
}

// Enabling tracing and metrics via OpenCensus
if err := startOpenCensus(config, nil); err != nil {
return fmt.Errorf("failed to instrument OpenCensus: %v", err)
}

// Logging is controlled by the config at methods level.
return defaultLogger.Start(ctx, config)
}
Expand Down

0 comments on commit 9ee2f14

Please sign in to comment.