Skip to content

Update logs #1062

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

Open
wants to merge 9 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ linters-settings:
- github.com/jmoiron/sqlx
sloglint:
# Enforce not mixing key-value pairs and attributes. Default: true
no-mixed-args: true
no-mixed-args: false
# Enforce using key-value pairs only (overrides no-mixed-args, incompatible with attr-only). Default: false
kv-only: false
# Enforce using attributes only (overrides no-mixed-args, incompatible with kv-only). Default: false
Expand Down
29 changes: 17 additions & 12 deletions internal/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type (
}
)

var logOrigin = slog.String("log_origin", "grpc.go")

var (
serviceConfig = `{
"healthCheckConfig": {
Expand Down Expand Up @@ -83,7 +85,7 @@ func NewGrpcConnection(ctx context.Context, agentConfig *config.Config) (*GrpcCo
fmt.Sprint(agentConfig.Command.Server.Port),
)

slog.InfoContext(ctx, "Dialing grpc server", "server_addr", serverAddr)
slog.InfoContext(ctx, "Dialing grpc server", "server_addr", serverAddr, logOrigin)

info := host.NewInfo()
resourceID := info.ResourceID(ctx)
Expand Down Expand Up @@ -120,7 +122,7 @@ func (gc *GrpcConnection) Close(ctx context.Context) error {
defer gc.mutex.Unlock()

if gc.conn != nil {
slog.InfoContext(ctx, "Closing grpc connection")
slog.InfoContext(ctx, "Closing grpc connection", logOrigin)
err := gc.conn.Close()
gc.conn = nil
if err != nil {
Expand Down Expand Up @@ -166,14 +168,14 @@ func GetDialOptions(agentConfig *config.Config, resourceID string) []grpc.DialOp

protoValidatorStreamClientInterceptor, err := ProtoValidatorStreamClientInterceptor()
if err != nil {
slog.Error("Unable to add proto validation stream interceptor", "error", err)
slog.Error("Unable to add proto validation stream interceptor", "error", err, logOrigin)
} else {
streamClientInterceptors = append(streamClientInterceptors, protoValidatorStreamClientInterceptor)
}

protoValidatorUnaryClientInterceptor, err := ProtoValidatorUnaryClientInterceptor()
if err != nil {
slog.Error("Unable to add proto validation unary interceptor", "error", err)
slog.Error("Unable to add proto validation unary interceptor", "error", err, logOrigin)
} else {
unaryClientInterceptors = append(unaryClientInterceptors, protoValidatorUnaryClientInterceptor)
}
Expand Down Expand Up @@ -223,15 +225,18 @@ func GetDialOptions(agentConfig *config.Config, resourceID string) []grpc.DialOp
func addTransportCredentials(agentConfig *config.Config, opts []grpc.DialOption) ([]grpc.DialOption, bool) {
transportCredentials, err := getTransportCredentials(agentConfig)
if err != nil {
slog.Error("Unable to add transport credentials to gRPC dial options, adding "+
"default transport credentials", "error", err)
slog.Error(
"Unable to add transport credentials to gRPC dial options, adding default transport credentials",
"error", err,
logOrigin,
)
opts = append(opts,
grpc.WithTransportCredentials(defaultCredentials),
)

return opts, true
}
slog.Debug("Adding transport credentials to gRPC dial options")
slog.Debug("Adding transport credentials to gRPC dial options", logOrigin)
opts = append(opts,
grpc.WithTransportCredentials(transportCredentials),
)
Expand All @@ -243,16 +248,16 @@ func addPerRPCCredentials(agentConfig *config.Config, resourceID string, opts []
token := agentConfig.Command.Auth.Token

if agentConfig.Command.Auth.TokenPath != "" {
slog.Debug("Reading token from file", "path", agentConfig.Command.Auth.TokenPath)
slog.Debug("Reading token from file", "path", agentConfig.Command.Auth.TokenPath, logOrigin)
tk, err := file.ReadFromFile(agentConfig.Command.Auth.TokenPath)
if err == nil {
token = tk
} else {
slog.Error("Unable to add token to gRPC dial options", "error", err)
slog.Error("Unable to add token to gRPC dial options", "error", err, logOrigin)
}
}

slog.Debug("Adding RPC credentials")
slog.Debug("Adding RPC credentials", logOrigin)
opts = append(opts,
grpc.WithPerRPCCredentials(
&PerRPCCredentials{
Expand Down Expand Up @@ -365,7 +370,7 @@ func getTransportCredentials(agentConfig *config.Config) (credentials.TransportC
}

if agentConfig.Command.TLS.SkipVerify {
slog.Warn("Verification of the server's certificate chain and host name is disabled")
slog.Warn("Verification of the server's certificate chain and host name is disabled", logOrigin)
}

tlsConfig := &tls.Config{
Expand All @@ -385,7 +390,7 @@ func getTransportCredentials(agentConfig *config.Config) (credentials.TransportC

err = appendRootCAs(tlsConfig, agentConfig.Command.TLS.Ca)
if err != nil {
slog.Debug("Unable to append root CA", "error", err)
slog.Debug("Unable to append root CA", "error", err, logOrigin)
}

return credentials.NewTLS(tlsConfig), nil
Expand Down
18 changes: 15 additions & 3 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (
CorrelationIDKey = "correlation_id"
)

var logOrigin = slog.String("log_origin", "logger.go")

var (
logLevels = map[string]slog.Level{
"debug": slog.LevelDebug,
Expand Down Expand Up @@ -73,7 +75,11 @@ func getLogWriter(logFile string) io.Writer {
if logFile != "" {
fileInfo, err := os.Stat(logPath)
if err != nil {
slog.Error("Error reading log directory, proceeding to log only to stdout/stderr", "error", err)
slog.Error(
"Error reading log directory, proceeding to log only to stdout/stderr",
"error", err,
logOrigin,
)

return os.Stderr
}
Expand All @@ -84,7 +90,11 @@ func getLogWriter(logFile string) io.Writer {

logFileHandle, err := os.OpenFile(logPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, filePermission)
if err != nil {
slog.Error("Failed to open log file, proceeding to log only to stdout/stderr", "error", err)
slog.Error(
"Failed to open log file, proceeding to log only to stdout/stderr",
"error", err,
logOrigin,
)

return os.Stderr
}
Expand Down Expand Up @@ -135,7 +145,9 @@ func GetCorrelationIDAttr(ctx context.Context) slog.Attr {
slog.Debug(
"Correlation ID not found in context, generating new correlation ID",
"correlation_id",
correlationID)
correlationID,
logOrigin,
)

return GenerateCorrelationID()
}
Expand Down
15 changes: 10 additions & 5 deletions internal/plugin/plugin_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/nginx/agent/v3/internal/watcher"
)

var logOrigin = slog.String("log_origin", "plugin_manager.go")

func LoadPlugins(ctx context.Context, agentConfig *config.Config) []bus.Plugin {
plugins := make([]bus.Plugin, 0)

Expand All @@ -44,7 +46,7 @@ func addCommandAndFilePlugins(ctx context.Context, plugins []bus.Plugin, agentCo
if agentConfig.IsGrpcClientConfigured() {
grpcConnection, err := grpc.NewGrpcConnection(ctx, agentConfig)
if err != nil {
slog.WarnContext(ctx, "Failed to create gRPC connection", "error", err)
slog.WarnContext(ctx, "Failed to create gRPC connection", "error", err, logOrigin)
} else {
commandPlugin := command.NewCommandPlugin(agentConfig, grpcConnection)
plugins = append(plugins, commandPlugin)
Expand All @@ -53,7 +55,7 @@ func addCommandAndFilePlugins(ctx context.Context, plugins []bus.Plugin, agentCo
}
} else {
slog.InfoContext(ctx, "Agent is not connected to a management plane. "+
"Configure a command server to establish a connection with a management plane.")
"Configure a command server to establish a connection with a management plane.", logOrigin)
}

return plugins
Expand All @@ -71,11 +73,14 @@ func addCollectorPlugin(ctx context.Context, agentConfig *config.Config, plugins
if err == nil {
plugins = append(plugins, oTelCollector)
} else {
slog.ErrorContext(ctx, "Failed to initialize collector plugin", "error", err)
slog.ErrorContext(ctx, "Failed to initialize collector plugin", "error", err, logOrigin)
}
} else {
slog.InfoContext(ctx, "Agent OTel collector isn't started. "+
"Configure a collector to begin collecting metrics.")
slog.InfoContext(
ctx,
"Agent OTel collector isn't started. Configure a collector to begin collecting metrics.",
logOrigin,
)
}

return plugins
Expand Down
31 changes: 21 additions & 10 deletions internal/resource/nginx_instance_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type NginxInstanceOperator struct {

var _ instanceOperator = (*NginxInstanceOperator)(nil)

var logOrigin = slog.String("log_origin", "nginx_instance_operator.go")

func NewInstanceOperator(agentConfig *config.Config) *NginxInstanceOperator {
return &NginxInstanceOperator{
executer: &exec.Exec{},
Expand All @@ -34,7 +36,7 @@ func NewInstanceOperator(agentConfig *config.Config) *NginxInstanceOperator {
}

func (i *NginxInstanceOperator) Validate(ctx context.Context, instance *mpi.Instance) error {
slog.DebugContext(ctx, "Validating NGINX config")
slog.DebugContext(ctx, "Validating NGINX config", logOrigin)
exePath := instance.GetInstanceRuntime().GetBinaryPath()

out, err := i.executer.RunCmd(ctx, exePath, "-t")
Expand All @@ -47,7 +49,7 @@ func (i *NginxInstanceOperator) Validate(ctx context.Context, instance *mpi.Inst
return err
}

slog.InfoContext(ctx, "NGINX config tested", "output", out)
slog.InfoContext(ctx, "NGINX config tested", "output", out, logOrigin)

return nil
}
Expand All @@ -68,10 +70,19 @@ func (i *NginxInstanceOperator) validateConfigCheckResponse(out []byte) error {

func (i *NginxInstanceOperator) Reload(ctx context.Context, instance *mpi.Instance) error {
var errorsFound error
slog.InfoContext(ctx, "Reloading NGINX PID", "pid",
instance.GetInstanceRuntime().GetProcessId())

slog.InfoContext(ctx, "NGINX reloaded", "processid", instance.GetInstanceRuntime().GetProcessId())
slog.InfoContext(
ctx,
"Reloading NGINX PID",
"pid", instance.GetInstanceRuntime().GetProcessId(),
logOrigin,
)

slog.InfoContext(
ctx,
"NGINX reloaded",
"processid", instance.GetInstanceRuntime().GetProcessId(),
logOrigin,
)

errorLogs := i.errorLogs(instance)

Expand All @@ -89,14 +100,14 @@ func (i *NginxInstanceOperator) Reload(ctx context.Context, instance *mpi.Instan

for i := 0; i < numberOfExpectedMessages; i++ {
logErr := <-logErrorChannel
slog.InfoContext(ctx, "Message received in logErrorChannel", "error", logErr)
slog.InfoContext(ctx, "Message received in logErrorChannel", "error", logErr, logOrigin)
if logErr != nil {
errorsFound = errors.Join(errorsFound, logErr)
slog.Info("Errors Found", "", errorsFound)
slog.Info("Errors Found", "", errorsFound, logOrigin)
}
}

slog.InfoContext(ctx, "Finished monitoring post reload")
slog.InfoContext(ctx, "Finished monitoring post reload", logOrigin)

if errorsFound != nil {
return errorsFound
Expand All @@ -117,7 +128,7 @@ func (i *NginxInstanceOperator) errorLogs(instance *mpi.Instance) (errorLogs []s

func (i *NginxInstanceOperator) monitorLogs(ctx context.Context, errorLogs []string, errorChannel chan error) {
if len(errorLogs) == 0 {
slog.InfoContext(ctx, "No NGINX error logs found to monitor")
slog.InfoContext(ctx, "No NGINX error logs found to monitor", logOrigin)
return
}

Expand Down
8 changes: 6 additions & 2 deletions internal/resource/nginx_log_tailer_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type NginxLogTailerOperator struct {

var _ logTailerOperator = (*NginxLogTailerOperator)(nil)

var tailerLogOrigin = slog.String("log_origin", "nginx_log_tailer_operator.go")

var (
// Line is over 120 characters long, regex needs to be on one line so needs to be ignored by linter
// nolint: lll
Expand All @@ -41,7 +43,8 @@ func NewLogTailerOperator(agentConfig *config.Config) *NginxLogTailerOperator {
func (l *NginxLogTailerOperator) Tail(ctx context.Context, errorLog string, errorChannel chan error) {
t, err := nginx.NewTailer(errorLog)
if err != nil {
slog.ErrorContext(ctx, "Unable to tail error log after NGINX reload", "log_file", errorLog, "error", err)
slog.ErrorContext(ctx, "Unable to tail error log after NGINX reload", "log_file", errorLog,
"error", err, tailerLogOrigin)
// this is not an error in the logs, ignoring tailing
errorChannel <- nil

Expand All @@ -51,7 +54,8 @@ func (l *NginxLogTailerOperator) Tail(ctx context.Context, errorLog string, erro
ctxWithTimeout, cancel := context.WithTimeout(ctx, l.agentConfig.DataPlaneConfig.Nginx.ReloadMonitoringPeriod)
defer cancel()

slog.DebugContext(ctxWithTimeout, "Monitoring NGINX error log file for any errors", "file", errorLog)
slog.DebugContext(ctxWithTimeout, "Monitoring NGINX error log file for any errors",
"file", errorLog, tailerLogOrigin)

data := make(chan string, logTailerChannelSize)
go t.Tail(ctxWithTimeout, data)
Expand Down
Loading