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 Environment Variable GRPC_CONFIG_OBSERVABILITY_JSON #5525
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,9 @@ package observability | |
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"os" | ||
"sync" | ||
|
@@ -683,78 +685,58 @@ func (s) TestRefuseStartWithInvalidPatterns(t *testing.T) { | |
} | ||
} | ||
|
||
// TestJSONEnvVarSet tests a valid observability configuration specified by the | ||
// GRPC_CONFIG_OBSERVABILITY_JSON environment variable, whose value represents a | ||
// file path pointing to a JSON encoded config. | ||
func (s) TestJSONEnvVarSet(t *testing.T) { | ||
config := &configpb.ObservabilityConfig{ | ||
EnableCloudLogging: true, | ||
DestinationProjectId: "fake", | ||
LogFilters: []*configpb.ObservabilityConfig_LogFilter{ | ||
{ | ||
Pattern: "*", | ||
HeaderBytes: infinitySizeBytes, | ||
MessageBytes: infinitySizeBytes, | ||
}, | ||
}, | ||
} | ||
|
||
configJSON, err := protojson.Marshal(config) | ||
if err != nil { | ||
t.Fatalf("failed to convert config to JSON: %v", err) | ||
} | ||
|
||
configJSONFile, err := os.Create("/tmp/configJSON") | ||
// createTmpConfigInFileSystem creates a random observability config at a random | ||
// place in the temporary portion of the file system dependent on system. It | ||
// also sets the environment variable GRPC_CONFIG_OBSERVABILITY_JSON to point to | ||
// this created config. | ||
func createTmpConfigInFileSystem(rawJSON json.RawMessage) (*os.File, error) { | ||
configJSONFile, err := ioutil.TempFile(os.TempDir(), "configJSON-") | ||
if err != nil { | ||
t.Fatalf("cannot create file /tmp/configJSON: %v", err) | ||
return nil, fmt.Errorf("cannot create file %v: %v", configJSONFile.Name(), err) | ||
} | ||
defer configJSONFile.Close() | ||
_, err = configJSONFile.Write(configJSON) | ||
_, err = configJSONFile.Write(rawJSON) | ||
if err != nil { | ||
t.Fatalf("cannot write marshalled JSON: %v", err) | ||
return nil, fmt.Errorf("cannot write marshalled JSON: %v", err) | ||
} | ||
os.Setenv(envObservabilityConfigJSON, "/tmp/configJSON") | ||
os.Setenv(envObservabilityConfigJSON, configJSONFile.Name()) | ||
return configJSONFile, nil /// do you even need to return this file if you're already closing it here? | ||
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. Should we delete it after the test? 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. Yeah good point. Stored this returned in a local variable at call sites and defered the Close(). |
||
} | ||
|
||
if err := Start(context.Background()); err != nil { | ||
// TestJSONEnvVarSet tests a valid observability configuration specified by the | ||
// GRPC_CONFIG_OBSERVABILITY_JSON environment variable, whose value represents a | ||
// file path pointing to a JSON encoded config. | ||
func (s) TestJSONEnvVarSet(t *testing.T) { | ||
configJSON := json.RawMessage(`{ | ||
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. Pass to 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. Switched. |
||
"destinationProjectId": "fake", | ||
"logFilters":[{"pattern":"*","headerBytes":1073741824,"messageBytes":1073741824}] | ||
}`) | ||
_, err := createTmpConfigInFileSystem(configJSON) | ||
if err != nil { | ||
t.Fatalf("failed to create config in file system: %v", err) | ||
} | ||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) | ||
defer cancel() | ||
if err := Start(ctx); err != nil { | ||
t.Fatalf("error starting observability with valid config through file system: %v", err) | ||
} | ||
defer End() | ||
} | ||
|
||
// TestBothConfigEnvVarsSet tests the scenario where both configuration | ||
// environment variables are set. The file system environment variable should | ||
// take precedence, and and error should return in the case of the file system | ||
// take precedence, and an error should return in the case of the file system | ||
// configuration being invalid, even if the direct configuration environment | ||
// variable is set and valid. | ||
func (s) TestBothConfigEnvVarsSet(t *testing.T) { | ||
invalidConfig := &configpb.ObservabilityConfig{ | ||
EnableCloudLogging: true, | ||
DestinationProjectId: "fake", | ||
LogFilters: []*configpb.ObservabilityConfig_LogFilter{ | ||
{ | ||
Pattern: ":-)", | ||
}, | ||
{ | ||
Pattern: "*", | ||
}, | ||
}, | ||
} | ||
invalidConfigJSON, err := protojson.Marshal(invalidConfig) | ||
if err != nil { | ||
t.Fatalf("failed to convert config to JSON: %v", err) | ||
} | ||
|
||
invalidConfigJSONFile, err := os.Create("/tmp/InvalidConfigJSON") | ||
configJSON := json.RawMessage(`{ | ||
"destinationProjectId":"fake", | ||
"logFilters":[{"pattern":":-)"}, {"pattern":"*"}] | ||
}`) | ||
_, err := createTmpConfigInFileSystem(configJSON) | ||
if err != nil { | ||
t.Fatalf("cannot create file /tmp/InvalidConfigJSON: %v", err) | ||
t.Fatalf("failed to create config in file system: %v", err) | ||
} | ||
defer invalidConfigJSONFile.Close() | ||
_, err = invalidConfigJSONFile.Write(invalidConfigJSON) | ||
if err != nil { | ||
t.Fatalf("cannot write marshalled JSON: %v", err) | ||
} | ||
os.Setenv(envObservabilityConfigJSON, "/tmp/InvalidConfigJSON") | ||
|
||
// This configuration should be ignored, as precedence 2. | ||
validConfig := &configpb.ObservabilityConfig{ | ||
EnableCloudLogging: true, | ||
|
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.
*its.
'
!= possessionThere 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.
Switched.