-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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: Add support for custom tags #5565
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ce98283
Rewrote observability tests for exporters to use global func
zasweq a930d0b
Added support for custom tags and deleted old implementation
zasweq 054cdda
Switch internal configuration representation from proto to struct
zasweq b44b856
Responded to Doug's comments
zasweq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -28,8 +28,6 @@ import ( | |
|
||
gcplogging "cloud.google.com/go/logging" | ||
"golang.org/x/oauth2/google" | ||
configpb "google.golang.org/grpc/gcp/observability/internal/config" | ||
"google.golang.org/protobuf/encoding/protojson" | ||
) | ||
|
||
const ( | ||
|
@@ -41,6 +39,69 @@ const ( | |
|
||
var logFilterPatternRegexp = regexp.MustCompile(logFilterPatternRegexpStr) | ||
|
||
// LogFilter represents a method logging configuration. | ||
type LogFilter struct { | ||
// Pattern is a string which can select a group of method names. By | ||
// default, the Pattern is an empty string, matching no methods. | ||
// | ||
// Only "*" Wildcard is accepted for Pattern. A Pattern is in the form | ||
// of <service>/<method> or just a character "*" . | ||
// | ||
// If the Pattern is "*", it specifies the defaults for all the | ||
// services; If the Pattern is <service>/*, it specifies the defaults | ||
// for all methods in the specified service <service>; If the Pattern is | ||
// */<method>, this is not supported. | ||
// | ||
// Examples: | ||
// - "Foo/Bar" selects only the method "Bar" from service "Foo" | ||
// - "Foo/*" selects all methods from service "Foo" | ||
// - "*" selects all methods from all services. | ||
Pattern string `json:"pattern,omitempty"` | ||
// HeaderBytes is the number of bytes of each header to log. If the size of | ||
// the header is greater than the defined limit, content past the limit will | ||
// be truncated. The default value is 0. | ||
HeaderBytes int32 `json:"header_bytes,omitempty"` | ||
// MessageBytes is the number of bytes of each message to log. If the size | ||
// of the message is greater than the defined limit, content pass the limit | ||
// will be truncated. The default value is 0. | ||
MessageBytes int32 `json:"message_bytes,omitempty"` | ||
} | ||
|
||
// ObvConfig is configuration for observability behaviors. By default, no | ||
// configuration is required for tracing/metrics/logging to function. This | ||
// config captures the most common knobs for gRPC users. It's always possible to | ||
// override with explicit config in code. | ||
type ObvConfig struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Chose config. |
||
// EnableCloudTrace represents whether the tracing data upload to | ||
// CloudTrace should be enabled or not. | ||
EnableCloudTrace bool `json:"enable_cloud_trace,omitempty"` | ||
// EnableCloudMonitoring represents whether the metrics data upload to | ||
// CloudMonitoring should be enabled or not. | ||
EnableCloudMonitoring bool `json:"enable_cloud_monitoring,omitempty"` | ||
// EnableCloudLogging represents Whether the logging data upload to | ||
// CloudLogging should be enabled or not. | ||
EnableCloudLogging bool `json:"enable_cloud_logging,omitempty"` | ||
// DestinationProjectID is the destination GCP project identifier for the | ||
// uploading log entries. If empty, the gRPC Observability plugin will | ||
// attempt to fetch the project_id from the GCP environment variables, or | ||
// from the default credentials. | ||
DestinationProjectID string `json:"destination_project_id,omitempty"` | ||
// LogFilters is a list of method config. The order matters here - the first | ||
// Pattern which matches the current method will apply the associated config | ||
// options in the LogFilter. Any other LogFilter that also matches that | ||
// comes later will be ignored. So a LogFilter of "*/*" should appear last | ||
// in this list. | ||
LogFilters []LogFilter `json:"log_filters,omitempty"` | ||
// GlobalTraceSamplingRate is 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. | ||
GlobalTraceSamplingRate float64 `json:"global_trace_sampling_rate,omitempty"` | ||
// CustomTags a list of custom tags that will be attached to every log | ||
// entry. | ||
CustomTags map[string]string `json:"custom_tags,omitempty"` | ||
} | ||
|
||
// fetchDefaultProjectID fetches the default GCP project id from environment. | ||
func fetchDefaultProjectID(ctx context.Context) string { | ||
// Step 1: Check ENV var | ||
|
@@ -62,25 +123,25 @@ func fetchDefaultProjectID(ctx context.Context) string { | |
return credentials.ProjectID | ||
} | ||
|
||
func validateFilters(config *configpb.ObservabilityConfig) error { | ||
for _, filter := range config.GetLogFilters() { | ||
func validateFilters(config *ObvConfig) error { | ||
for _, filter := range config.LogFilters { | ||
if filter.Pattern == "*" { | ||
continue | ||
} | ||
match := logFilterPatternRegexp.FindStringSubmatch(filter.Pattern) | ||
if match == nil { | ||
return fmt.Errorf("invalid log filter pattern: %v", filter.Pattern) | ||
return fmt.Errorf("invalid log filter Pattern: %v", filter.Pattern) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// unmarshalAndVerifyConfig unmarshals a json string representing an | ||
// observability config into its protobuf format, and also verifies the | ||
// observability config into its internal go format, and also verifies the | ||
// configuration's fields for validity. | ||
func unmarshalAndVerifyConfig(rawJSON json.RawMessage) (*configpb.ObservabilityConfig, error) { | ||
var config configpb.ObservabilityConfig | ||
if err := protojson.Unmarshal(rawJSON, &config); err != nil { | ||
func unmarshalAndVerifyConfig(rawJSON json.RawMessage) (*ObvConfig, error) { | ||
var config ObvConfig | ||
if err := json.Unmarshal(rawJSON, &config); err != nil { | ||
return nil, fmt.Errorf("error parsing observability config: %v", err) | ||
} | ||
if err := validateFilters(&config); err != nil { | ||
|
@@ -93,7 +154,7 @@ func unmarshalAndVerifyConfig(rawJSON json.RawMessage) (*configpb.ObservabilityC | |
return &config, nil | ||
} | ||
|
||
func parseObservabilityConfig() (*configpb.ObservabilityConfig, error) { | ||
func parseObservabilityConfig() (*ObvConfig, error) { | ||
if fileSystemPath := os.Getenv(envObservabilityConfigJSON); fileSystemPath != "" { | ||
content, err := ioutil.ReadFile(fileSystemPath) // TODO: Switch to os.ReadFile once dropped support for go 1.15 | ||
if err != nil { | ||
|
@@ -107,14 +168,14 @@ func parseObservabilityConfig() (*configpb.ObservabilityConfig, error) { | |
return nil, nil | ||
} | ||
|
||
func ensureProjectIDInObservabilityConfig(ctx context.Context, config *configpb.ObservabilityConfig) error { | ||
if config.GetDestinationProjectId() == "" { | ||
func ensureProjectIDInObservabilityConfig(ctx context.Context, config *ObvConfig) error { | ||
if config.DestinationProjectID == "" { | ||
// Try to fetch the GCP project id | ||
projectID := fetchDefaultProjectID(ctx) | ||
if projectID == "" { | ||
return fmt.Errorf("empty destination project ID") | ||
} | ||
config.DestinationProjectId = projectID | ||
config.DestinationProjectID = projectID | ||
} | ||
return nil | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type should not be exported, right? Same below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.