Skip to content
This repository has been archived by the owner on Oct 9, 2024. It is now read-only.

Commit

Permalink
keep order of log fields
Browse files Browse the repository at this point in the history
  • Loading branch information
gi8lino committed Jul 4, 2024
1 parent b44c813 commit c1102b6
Showing 1 changed file with 9 additions and 22 deletions.
31 changes: 9 additions & 22 deletions cmd/wait-for-tcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"
)

const version = "0.0.12"
const version = "0.0.13"

// Vars holds the environment variables required for the target checker.
type Vars struct {
Expand All @@ -23,15 +23,9 @@ type Vars struct {
}

// logMessage logs a message in structured key-value pairs format.
func logMessage(output io.Writer, level string, message string, fields map[string]string) {
var logEntry strings.Builder
logEntry.WriteString(fmt.Sprintf("ts=%s level=%s msg=%q", time.Now().Format(time.RFC3339), level, message))

for k, v := range fields {
logEntry.WriteString(fmt.Sprintf(" %s=%q", k, v))
}

fmt.Fprintln(output, logEntry.String())
func logMessage(output io.Writer, level string, message string, fields string) {
logEntry := fmt.Sprintf("ts=%s level=%s msg=%q %s", time.Now().Format(time.RFC3339), level, message, fields)
fmt.Fprintln(output, logEntry)
}

// parseEnv retrieves and validates the environment variables required for the target checker.
Expand Down Expand Up @@ -94,15 +88,9 @@ func runLoop(ctx context.Context, envVars Vars, stdErr, stdOut io.Writer) error
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer cancel()

fields := map[string]string{
"target_name": envVars.TargetName,
"target_address": envVars.TargetAddress,
"interval": envVars.Interval.String(),
"dial_timeout": envVars.DialTimeout.String(),
"version": version,
}
baseFields := fmt.Sprintf("target_name=%q target_address=%q interval=%q dial_timeout=%q version=%q", envVars.TargetName, envVars.TargetAddress, envVars.Interval.String(), envVars.DialTimeout.String(), version)

logMessage(stdOut, "info", fmt.Sprintf("Waiting for %s to become ready...", envVars.TargetName), fields)
logMessage(stdOut, "info", fmt.Sprintf("Waiting for %s to become ready...", envVars.TargetName), baseFields)

dialer := &net.Dialer{
Timeout: envVars.DialTimeout,
Expand All @@ -111,13 +99,12 @@ func runLoop(ctx context.Context, envVars Vars, stdErr, stdOut io.Writer) error
for {
err := checkConnection(ctx, dialer, envVars.TargetAddress)
if err == nil {
delete(fields, "error") // remove posible errors from before
logMessage(stdOut, "info", "Target is ready ✓", fields)
logMessage(stdOut, "info", "Target is ready ✓", baseFields)
return nil
}

fields["error"] = err.Error()
logMessage(stdErr, "warn", "Target is not ready ✗", fields)
errorFields := fmt.Sprintf("%s error=%q", baseFields, err.Error())
logMessage(stdErr, "warn", "Target is not ready ✗", errorFields)

select {
case <-time.After(envVars.Interval):
Expand Down

0 comments on commit c1102b6

Please sign in to comment.