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

Add flag to set GCP logging endpoint. #537

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Changes from 1 commit
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
23 changes: 20 additions & 3 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ var (
// Enable/Disable cloud logging
disableCloudLogging = flag.Bool("disable_cloud_logging", false, "Disable cloud logging.")

// Override the GCP cloud logging endpoint.
gcpLoggingEndpoint = flag.String("gcp_logging_endpoint", "", "GCP logging endpoint")

// LogPrefixEnvVar environment variable is used to determine the stackdriver
// log name prefix. Default prefix is "cloudprober".
LogPrefixEnvVar = "CLOUDPROBER_LOG_PREFIX"
Expand All @@ -56,10 +59,11 @@ var (
// EnvVars defines environment variables that can be used to modify the logging
// behavior.
var EnvVars = struct {
DisableCloudLogging, DebugLog string
DisableCloudLogging, DebugLog, GcpLoggingEndpoint string
}{
shellyvilenko marked this conversation as resolved.
Show resolved Hide resolved
"CLOUDPROBER_DISABLE_CLOUD_LOGGING",
"CLOUDPROBER_DEBUG_LOG",
"CLOUDPROBER_GCP_LOGGING_ENDPOINT",
}

const (
Expand Down Expand Up @@ -169,6 +173,7 @@ type Logger struct {
gcpLogger *logging.Logger
debugLog bool
disableCloudLogging bool
gcpLoggingEndpoint string
labels map[string]string
attrs []slog.Attr
writer io.Writer
Expand Down Expand Up @@ -196,6 +201,7 @@ func newLogger(opts ...Option) *Logger {
l := &Logger{
labels: make(map[string]string),
disableCloudLogging: *disableCloudLogging,
gcpLoggingEndpoint: *gcpLoggingEndpoint,
}
for _, opt := range opts {
opt(l)
Expand Down Expand Up @@ -289,7 +295,14 @@ func (l *Logger) EnableStackdriverLogging() {
return
}

l.gcpLogc, err = logging.NewClient(context.Background(), projectID, option.WithTokenSource(google.ComputeTokenSource("")))
// Create Client options for logging client
o := []option.ClientOption{option.WithTokenSource(google.ComputeTokenSource(""))}
if l.gcpLoggingEndpoint != "" {
l.Infof("Setting logging endpoint to %s", l.gcpLoggingEndpoint)
o = append(o, option.WithEndpoint(l.gcpLoggingEndpoint))
}

l.gcpLogc, err = logging.NewClient(context.Background(), projectID, o...)
if err != nil {
l.Warningf("Error creating client for google cloud logging: %v, will skip", err)
return
Expand Down Expand Up @@ -490,7 +503,7 @@ func (l *Logger) Criticalf(format string, args ...interface{}) {

func envVarSet(key string) bool {
v, ok := os.LookupEnv(key)
if ok && strings.ToUpper(v) != "NO" && strings.ToUpper(v) != "FALSE" {
if ok && strings.ToUpper(v) != "NO" && strings.ToUpper(v) != "FALSE" && strings.ToUpper(v) != "" {
shellyvilenko marked this conversation as resolved.
Show resolved Hide resolved
return true
}
return false
Expand All @@ -505,6 +518,10 @@ func init() {
*debugLog = true
}

if envVarSet(EnvVars.GcpLoggingEndpoint) {
*gcpLoggingEndpoint = os.Getenv(EnvVars.GcpLoggingEndpoint)
}

// Determine the base path for the cloudprober source code.
var pcs [1]uintptr
runtime.Callers(1, pcs[:])
Expand Down