Skip to content

Commit 054cdda

Browse files
committed
Switch internal configuration representation from proto to struct
1 parent a930d0b commit 054cdda

File tree

7 files changed

+121
-535
lines changed

7 files changed

+121
-535
lines changed

gcp/observability/config.go

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ import (
2828

2929
gcplogging "cloud.google.com/go/logging"
3030
"golang.org/x/oauth2/google"
31-
configpb "google.golang.org/grpc/gcp/observability/internal/config"
32-
"google.golang.org/protobuf/encoding/protojson"
3331
)
3432

3533
const (
@@ -41,6 +39,69 @@ const (
4139

4240
var logFilterPatternRegexp = regexp.MustCompile(logFilterPatternRegexpStr)
4341

42+
// LogFilter represents a method logging configuration.
43+
type LogFilter struct {
44+
// Pattern is a string which can select a group of method names. By
45+
// default, the Pattern is an empty string, matching no methods.
46+
//
47+
// Only "*" Wildcard is accepted for Pattern. A Pattern is in the form
48+
// of <service>/<method> or just a character "*" .
49+
//
50+
// If the Pattern is "*", it specifies the defaults for all the
51+
// services; If the Pattern is <service>/*, it specifies the defaults
52+
// for all methods in the specified service <service>; If the Pattern is
53+
// */<method>, this is not supported.
54+
//
55+
// Examples:
56+
// - "Foo/Bar" selects only the method "Bar" from service "Foo"
57+
// - "Foo/*" selects all methods from service "Foo"
58+
// - "*" selects all methods from all services.
59+
Pattern string `json:"pattern,omitempty"`
60+
// HeaderBytes is the number of bytes of each header to log. If the size of
61+
// the header is greater than the defined limit, content past the limit will
62+
// be truncated. The default value is 0.
63+
HeaderBytes int32 `json:"header_bytes,omitempty"`
64+
// MessageBytes is the number of bytes of each message to log. If the size
65+
// of the message is greater than the defined limit, content pass the limit
66+
// will be truncated. The default value is 0.
67+
MessageBytes int32 `json:"message_bytes,omitempty"`
68+
}
69+
70+
// ObvConfig is configuration for observability behaviors. By default, no
71+
// configuration is required for tracing/metrics/logging to function. This
72+
// config captures the most common knobs for gRPC users. It's always possible to
73+
// override with explicit config in code.
74+
type ObvConfig struct {
75+
// EnableCloudTrace represents whether the tracing data upload to
76+
// CloudTrace should be enabled or not.
77+
EnableCloudTrace bool `json:"enable_cloud_trace,omitempty"`
78+
// EnableCloudMonitoring represents whether the metrics data upload to
79+
// CloudMonitoring should be enabled or not.
80+
EnableCloudMonitoring bool `json:"enable_cloud_monitoring,omitempty"`
81+
// EnableCloudLogging represents Whether the logging data upload to
82+
// CloudLogging should be enabled or not.
83+
EnableCloudLogging bool `json:"enable_cloud_logging,omitempty"`
84+
// DestinationProjectID is the destination GCP project identifier for the
85+
// uploading log entries. If empty, the gRPC Observability plugin will
86+
// attempt to fetch the project_id from the GCP environment variables, or
87+
// from the default credentials.
88+
DestinationProjectID string `json:"destination_project_id,omitempty"`
89+
// LogFilters is a list of method config. The order matters here - the first
90+
// Pattern which matches the current method will apply the associated config
91+
// options in the LogFilter. Any other LogFilter that also matches that
92+
// comes later will be ignored. So a LogFilter of "*/*" should appear last
93+
// in this list.
94+
LogFilters []LogFilter `json:"log_filters,omitempty"`
95+
// GlobalTraceSamplingRate is the global setting that controls the
96+
// probability of a RPC being traced. For example, 0.05 means there is a 5%
97+
// chance for a RPC to be traced, 1.0 means trace every call, 0 means don’t
98+
// start new traces.
99+
GlobalTraceSamplingRate float64 `json:"global_trace_sampling_rate,omitempty"`
100+
// CustomTags a list of custom tags that will be attached to every log
101+
// entry.
102+
CustomTags map[string]string `json:"custom_tags,omitempty"`
103+
}
104+
44105
// fetchDefaultProjectID fetches the default GCP project id from environment.
45106
func fetchDefaultProjectID(ctx context.Context) string {
46107
// Step 1: Check ENV var
@@ -62,25 +123,25 @@ func fetchDefaultProjectID(ctx context.Context) string {
62123
return credentials.ProjectID
63124
}
64125

65-
func validateFilters(config *configpb.ObservabilityConfig) error {
66-
for _, filter := range config.GetLogFilters() {
126+
func validateFilters(config *ObvConfig) error {
127+
for _, filter := range config.LogFilters {
67128
if filter.Pattern == "*" {
68129
continue
69130
}
70131
match := logFilterPatternRegexp.FindStringSubmatch(filter.Pattern)
71132
if match == nil {
72-
return fmt.Errorf("invalid log filter pattern: %v", filter.Pattern)
133+
return fmt.Errorf("invalid log filter Pattern: %v", filter.Pattern)
73134
}
74135
}
75136
return nil
76137
}
77138

78139
// unmarshalAndVerifyConfig unmarshals a json string representing an
79-
// observability config into its protobuf format, and also verifies the
140+
// observability config into its internal go format, and also verifies the
80141
// configuration's fields for validity.
81-
func unmarshalAndVerifyConfig(rawJSON json.RawMessage) (*configpb.ObservabilityConfig, error) {
82-
var config configpb.ObservabilityConfig
83-
if err := protojson.Unmarshal(rawJSON, &config); err != nil {
142+
func unmarshalAndVerifyConfig(rawJSON json.RawMessage) (*ObvConfig, error) {
143+
var config ObvConfig
144+
if err := json.Unmarshal(rawJSON, &config); err != nil {
84145
return nil, fmt.Errorf("error parsing observability config: %v", err)
85146
}
86147
if err := validateFilters(&config); err != nil {
@@ -93,7 +154,7 @@ func unmarshalAndVerifyConfig(rawJSON json.RawMessage) (*configpb.ObservabilityC
93154
return &config, nil
94155
}
95156

96-
func parseObservabilityConfig() (*configpb.ObservabilityConfig, error) {
157+
func parseObservabilityConfig() (*ObvConfig, error) {
97158
if fileSystemPath := os.Getenv(envObservabilityConfigJSON); fileSystemPath != "" {
98159
content, err := ioutil.ReadFile(fileSystemPath) // TODO: Switch to os.ReadFile once dropped support for go 1.15
99160
if err != nil {
@@ -107,14 +168,14 @@ func parseObservabilityConfig() (*configpb.ObservabilityConfig, error) {
107168
return nil, nil
108169
}
109170

110-
func ensureProjectIDInObservabilityConfig(ctx context.Context, config *configpb.ObservabilityConfig) error {
111-
if config.GetDestinationProjectId() == "" {
171+
func ensureProjectIDInObservabilityConfig(ctx context.Context, config *ObvConfig) error {
172+
if config.DestinationProjectID == "" {
112173
// Try to fetch the GCP project id
113174
projectID := fetchDefaultProjectID(ctx)
114175
if projectID == "" {
115176
return fmt.Errorf("empty destination project ID")
116177
}
117-
config.DestinationProjectId = projectID
178+
config.DestinationProjectID = projectID
118179
}
119180
return nil
120181
}

gcp/observability/exporting.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"fmt"
2525

2626
gcplogging "cloud.google.com/go/logging"
27-
configpb "google.golang.org/grpc/gcp/observability/internal/config"
2827
grpclogrecordpb "google.golang.org/grpc/gcp/observability/internal/logging"
2928
"google.golang.org/protobuf/encoding/protojson"
3029
)
@@ -45,8 +44,8 @@ type cloudLoggingExporter struct {
4544
logger *gcplogging.Logger
4645
}
4746

48-
func newCloudLoggingExporter(ctx context.Context, config *configpb.ObservabilityConfig) (*cloudLoggingExporter, error) {
49-
c, err := gcplogging.NewClient(ctx, fmt.Sprintf("projects/%v", config.DestinationProjectId))
47+
func newCloudLoggingExporter(ctx context.Context, config *ObvConfig) (*cloudLoggingExporter, error) {
48+
c, err := gcplogging.NewClient(ctx, fmt.Sprintf("projects/%v", config.DestinationProjectID))
5049
if err != nil {
5150
return nil, fmt.Errorf("failed to create cloudLoggingExporter: %v", err)
5251
}
@@ -55,7 +54,7 @@ func newCloudLoggingExporter(ctx context.Context, config *configpb.Observability
5554
logger.Infof("Adding custom tags: %+v", config.CustomTags)
5655
}
5756
return &cloudLoggingExporter{
58-
projectID: config.DestinationProjectId,
57+
projectID: config.DestinationProjectID,
5958
client: c,
6059
logger: c.Logger("microservices.googleapis.com/observability/grpc", gcplogging.CommonLabels(config.CustomTags)),
6160
}, nil

0 commit comments

Comments
 (0)