Skip to content

Commit 08961d9

Browse files
committed
Update some internal logs
1 parent d13a27c commit 08961d9

File tree

17 files changed

+232
-135
lines changed

17 files changed

+232
-135
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ linters-settings:
764764
- github.com/jmoiron/sqlx
765765
sloglint:
766766
# Enforce not mixing key-value pairs and attributes. Default: true
767-
no-mixed-args: true
767+
no-mixed-args: false
768768
# Enforce using key-value pairs only (overrides no-mixed-args, incompatible with attr-only). Default: false
769769
kv-only: false
770770
# Enforce using attributes only (overrides no-mixed-args, incompatible with kv-only). Default: false

cmd/agent/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"github.com/nginx/agent/v3/internal/config"
1919
)
2020

21+
var logOrigin = slog.String("log_origin", "main.go")
22+
2123
var (
2224
// set at buildtime
2325
commit = ""
@@ -32,7 +34,7 @@ func main() {
3234
go func() {
3335
select {
3436
case <-sigChan:
35-
slog.WarnContext(ctx, "NGINX Agent exiting")
37+
slog.WarnContext(ctx, "NGINX Agent exiting", logOrigin)
3638
cancel()
3739

3840
time.Sleep(config.DefGracefulShutdownPeriod)
@@ -41,6 +43,7 @@ func main() {
4143
"Failed to gracefully shutdown within timeout of %v. Exiting",
4244
config.DefGracefulShutdownPeriod,
4345
),
46+
logOrigin,
4447
)
4548
os.Exit(1)
4649
case <-ctx.Done():
@@ -51,6 +54,6 @@ func main() {
5154

5255
err := app.Run(ctx)
5356
if err != nil {
54-
slog.ErrorContext(ctx, "NGINX Agent exiting due to error", "error", err)
57+
slog.ErrorContext(ctx, "NGINX Agent exiting due to error", "error", err, logOrigin)
5558
}
5659
}

internal/app.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/spf13/cobra"
1717
)
1818

19+
var logOrigin = slog.String("log_origin", "app.go")
20+
1921
const (
2022
defaultMessagePipeChannelSize = 100
2123
defaultQueueSize = 100
@@ -36,13 +38,13 @@ func (a *App) Run(ctx context.Context) error {
3638
config.RegisterRunner(func(_ *cobra.Command, _ []string) {
3739
err := config.RegisterConfigFile()
3840
if err != nil {
39-
slog.ErrorContext(ctx, "Failed to load configuration file", "error", err)
41+
slog.ErrorContext(ctx, "Failed to load configuration file", "error", err, logOrigin)
4042
return
4143
}
4244

4345
agentConfig, err := config.ResolveConfig()
4446
if err != nil {
45-
slog.ErrorContext(ctx, "Invalid config", "error", err)
47+
slog.ErrorContext(ctx, "Invalid config", "error", err, logOrigin)
4648
return
4749
}
4850

@@ -52,12 +54,13 @@ func (a *App) Run(ctx context.Context) error {
5254
slog.InfoContext(ctx, "Starting NGINX Agent",
5355
slog.String("version", a.version),
5456
slog.String("commit", a.commit),
57+
logOrigin,
5558
)
5659

5760
messagePipe := bus.NewMessagePipe(defaultMessagePipeChannelSize)
5861
err = messagePipe.Register(defaultQueueSize, plugin.LoadPlugins(ctx, agentConfig))
5962
if err != nil {
60-
slog.ErrorContext(ctx, "Failed to register plugins", "error", err)
63+
slog.ErrorContext(ctx, "Failed to register plugins", "error", err, logOrigin)
6164
return
6265
}
6366

internal/bus/message_pipe.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
messagebus "github.com/vardius/message-bus"
1414
)
1515

16+
var logOrigin = slog.String("log_origin", "message_pipe.go")
17+
1618
type (
1719
Payload interface{}
1820

@@ -81,7 +83,7 @@ func (p *MessagePipe) Register(size int, plugins []Plugin) error {
8183
pluginsRegistered = append(pluginsRegistered, plugin.Info().Name)
8284
}
8385

84-
slog.Info("Finished registering plugins", "plugins", pluginsRegistered)
86+
slog.Info("Finished registering plugins", "plugins", pluginsRegistered, logOrigin)
8587

8688
return nil
8789
}
@@ -195,7 +197,7 @@ func (p *MessagePipe) initPlugins(ctx context.Context) {
195197
for index, plugin := range p.plugins {
196198
err := plugin.Init(ctx, p)
197199
if err != nil {
198-
slog.ErrorContext(ctx, "Failed to initialize plugin", "plugin", plugin.Info().Name, "error", err)
200+
slog.ErrorContext(ctx, "Failed to initialize plugin", "plugin", plugin.Info().Name, "error", err, logOrigin)
199201

200202
unsubscribeError := p.unsubscribePlugin(ctx, index, plugin)
201203
if unsubscribeError != nil {
@@ -204,6 +206,7 @@ func (p *MessagePipe) initPlugins(ctx context.Context) {
204206
"Failed to unsubscribe plugin",
205207
"plugin", plugin.Info().Name,
206208
"error", unsubscribeError,
209+
logOrigin,
207210
)
208211
}
209212
}

internal/collector/otel_collector_plugin.go

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ type (
5353
}
5454
)
5555

56+
var pluginLogOrigin = slog.String("log_origin", "otel_collector_plugin.go")
57+
5658
var (
5759
_ bus.Plugin = (*Collector)(nil)
5860
initMutex = &sync.Mutex{}
@@ -101,14 +103,16 @@ func (oc *Collector) GetState() otelcol.State {
101103

102104
// Init initializes and starts the plugin
103105
func (oc *Collector) Init(ctx context.Context, mp bus.MessagePipeInterface) error {
104-
slog.InfoContext(ctx, "Starting OTel Collector plugin")
106+
slog.InfoContext(ctx, "Starting OTel Collector plugin", pluginLogOrigin)
105107

106108
var runCtx context.Context
107109
runCtx, oc.cancel = context.WithCancel(ctx)
108110

109111
if !oc.config.AreReceiversConfigured() {
110112
slog.InfoContext(runCtx, "No receivers configured for OTel Collector. "+
111-
"Waiting to discover a receiver before starting OTel collector.")
113+
"Waiting to discover a receiver before starting OTel collector.",
114+
pluginLogOrigin,
115+
)
112116

113117
return nil
114118
}
@@ -128,7 +132,7 @@ func (oc *Collector) Init(ctx context.Context, mp bus.MessagePipeInterface) erro
128132

129133
bootErr := oc.bootup(runCtx)
130134
if bootErr != nil {
131-
slog.ErrorContext(runCtx, "Unable to start OTel Collector", "error", bootErr)
135+
slog.ErrorContext(runCtx, "Unable to start OTel Collector", "error", bootErr, pluginLogOrigin)
132136
}
133137

134138
return nil
@@ -138,37 +142,40 @@ func (oc *Collector) Init(ctx context.Context, mp bus.MessagePipeInterface) erro
138142
func (oc *Collector) processReceivers(ctx context.Context, receivers []config.OtlpReceiver) {
139143
for _, receiver := range receivers {
140144
if receiver.OtlpTLSConfig == nil {
141-
slog.WarnContext(ctx, "OTel receiver is configured without TLS. Connections are unencrypted.")
145+
slog.WarnContext(ctx, "OTel receiver is configured without TLS. Connections are unencrypted.",
146+
pluginLogOrigin)
147+
142148
continue
143149
}
144150

145151
if receiver.OtlpTLSConfig.GenerateSelfSignedCert {
146152
slog.WarnContext(ctx,
147153
"Self-signed certificate for OTel receiver requested, "+
148154
"this is not recommended for production environments.",
155+
pluginLogOrigin,
149156
)
150157

151158
if receiver.OtlpTLSConfig.ExistingCert {
152159
slog.WarnContext(ctx,
153-
"Certificate file already exists, skipping self-signed certificate generation",
154-
)
160+
"Certificate file already exists, skipping self-signed certificate generation", pluginLogOrigin)
155161
}
156162
} else {
157-
slog.WarnContext(ctx, "OTel receiver is configured without TLS. Connections are unencrypted.")
163+
slog.WarnContext(ctx, "OTel receiver is configured without TLS. Connections are unencrypted.",
164+
pluginLogOrigin)
158165
}
159166
}
160167
}
161168

162169
func (oc *Collector) bootup(ctx context.Context) error {
163-
slog.InfoContext(ctx, "Starting OTel collector")
170+
slog.InfoContext(ctx, "Starting OTel collector", pluginLogOrigin)
164171
errChan := make(chan error)
165172

166173
go func() {
167174
appErr := oc.service.Run(ctx)
168175
if appErr != nil {
169176
errChan <- appErr
170177
}
171-
slog.InfoContext(ctx, "OTel collector run finished")
178+
slog.InfoContext(ctx, "OTel collector run finished", pluginLogOrigin)
172179
}()
173180

174181
for {
@@ -204,10 +211,10 @@ func (oc *Collector) Info() *bus.Info {
204211

205212
// Close the plugin.
206213
func (oc *Collector) Close(ctx context.Context) error {
207-
slog.InfoContext(ctx, "Closing OTel Collector plugin")
214+
slog.InfoContext(ctx, "Closing OTel Collector plugin", pluginLogOrigin)
208215

209216
if !oc.stopped {
210-
slog.InfoContext(ctx, "Shutting down OTel Collector", "state", oc.service.GetState())
217+
slog.InfoContext(ctx, "Shutting down OTel Collector", "state", oc.service.GetState(), pluginLogOrigin)
211218
oc.service.Shutdown()
212219
oc.cancel()
213220

@@ -222,9 +229,12 @@ func (oc *Collector) Close(ctx context.Context) error {
222229
})
223230

224231
if err != nil {
225-
slog.ErrorContext(ctx, "Failed to shutdown OTel Collector", "error", err, "state", oc.service.GetState())
232+
slog.ErrorContext(ctx, "Failed to shutdown OTel Collector",
233+
"error", err, "state", oc.service.GetState(),
234+
pluginLogOrigin,
235+
)
226236
} else {
227-
slog.InfoContext(ctx, "OTel Collector shutdown", "state", oc.service.GetState())
237+
slog.InfoContext(ctx, "OTel Collector shutdown", "state", oc.service.GetState(), pluginLogOrigin)
228238
oc.stopped = true
229239
}
230240
}
@@ -240,7 +250,7 @@ func (oc *Collector) Process(ctx context.Context, msg *bus.Message) {
240250
case bus.ResourceUpdateTopic:
241251
oc.handleResourceUpdate(ctx, msg)
242252
default:
243-
slog.DebugContext(ctx, "OTel collector plugin unknown topic", "topic", msg.Topic)
253+
slog.DebugContext(ctx, "OTel collector plugin unknown topic", "topic", msg.Topic, pluginLogOrigin)
244254
}
245255
}
246256

@@ -258,17 +268,19 @@ func (oc *Collector) handleNginxConfigUpdate(ctx context.Context, msg *bus.Messa
258268

259269
nginxConfigContext, ok := msg.Data.(*model.NginxConfigContext)
260270
if !ok {
261-
slog.ErrorContext(ctx, "Unable to cast message payload to *model.NginxConfigContext", "payload", msg.Data)
271+
slog.ErrorContext(ctx, "Unable to cast message payload to *model.NginxConfigContext",
272+
"payload", msg.Data, pluginLogOrigin)
273+
262274
return
263275
}
264276

265277
reloadCollector := oc.checkForNewReceivers(nginxConfigContext)
266278

267279
if reloadCollector {
268-
slog.InfoContext(ctx, "Reloading OTel collector config")
280+
slog.InfoContext(ctx, "Reloading OTel collector config", pluginLogOrigin)
269281
err := writeCollectorConfig(oc.config.Collector)
270282
if err != nil {
271-
slog.ErrorContext(ctx, "Failed to write OTel Collector config", "error", err)
283+
slog.ErrorContext(ctx, "Failed to write OTel Collector config", "error", err, pluginLogOrigin)
272284
return
273285
}
274286

@@ -282,18 +294,18 @@ func (oc *Collector) handleResourceUpdate(ctx context.Context, msg *bus.Message)
282294

283295
resourceUpdateContext, ok := msg.Data.(*v1.Resource)
284296
if !ok {
285-
slog.ErrorContext(ctx, "Unable to cast message payload to *v1.Resource", "payload", msg.Data)
297+
slog.ErrorContext(ctx, "Unable to cast message payload to *v1.Resource", "payload", msg.Data, pluginLogOrigin)
286298
return
287299
}
288300

289301
resourceProcessorUpdated := oc.updateResourceProcessor(resourceUpdateContext)
290302
headersSetterExtensionUpdated := oc.updateHeadersSetterExtension(ctx, resourceUpdateContext)
291303

292304
if resourceProcessorUpdated || headersSetterExtensionUpdated {
293-
slog.InfoContext(ctx, "Reloading OTel collector config")
305+
slog.InfoContext(ctx, "Reloading OTel collector config", pluginLogOrigin)
294306
err := writeCollectorConfig(oc.config.Collector)
295307
if err != nil {
296-
slog.ErrorContext(ctx, "Failed to write OTel Collector config", "error", err)
308+
slog.ErrorContext(ctx, "Failed to write OTel Collector config", "error", err, pluginLogOrigin)
297309
return
298310
}
299311

@@ -343,9 +355,9 @@ func (oc *Collector) updateHeadersSetterExtension(
343355
}
344356

345357
if !isUUIDHeaderSet {
346-
slog.DebugContext(
347-
ctx, "Adding uuid header to OTel collector",
358+
slog.DebugContext(ctx, "Adding uuid header to OTel collector",
348359
"uuid", resourceUpdateContext.GetResourceId(),
360+
pluginLogOrigin,
349361
)
350362
oc.config.Collector.Extensions.HeadersSetter.Headers = append(
351363
oc.config.Collector.Extensions.HeadersSetter.Headers,
@@ -366,14 +378,14 @@ func (oc *Collector) updateHeadersSetterExtension(
366378
func (oc *Collector) restartCollector(ctx context.Context) {
367379
err := oc.Close(ctx)
368380
if err != nil {
369-
slog.ErrorContext(ctx, "Failed to shutdown OTel Collector", "error", err)
381+
slog.ErrorContext(ctx, "Failed to shutdown OTel Collector", "error", err, pluginLogOrigin)
370382
return
371383
}
372384

373385
settings := OTelCollectorSettings(oc.config)
374386
oTelCollector, err := otelcol.NewCollector(settings)
375387
if err != nil {
376-
slog.ErrorContext(ctx, "Failed to create OTel Collector", "error", err)
388+
slog.ErrorContext(ctx, "Failed to create OTel Collector", "error", err, pluginLogOrigin)
377389
return
378390
}
379391
oc.service = oTelCollector
@@ -382,13 +394,14 @@ func (oc *Collector) restartCollector(ctx context.Context) {
382394
runCtx, oc.cancel = context.WithCancel(ctx)
383395

384396
if !oc.stopped {
385-
slog.ErrorContext(ctx, "Unable to restart OTel collector, failed to stop collector")
397+
slog.ErrorContext(ctx, "Unable to restart OTel collector, failed to stop collector", pluginLogOrigin)
398+
386399
return
387400
}
388401

389402
bootErr := oc.bootup(runCtx)
390403
if bootErr != nil {
391-
slog.ErrorContext(runCtx, "Unable to start OTel Collector", "error", bootErr)
404+
slog.ErrorContext(runCtx, "Unable to start OTel Collector", "error", bootErr, pluginLogOrigin)
392405
}
393406
}
394407

internal/collector/settings.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const (
2626
configFilePermission = 0o600
2727
)
2828

29+
var settingsLogOrigin = slog.String("log_origin", "settings.go")
30+
2931
//go:embed otelcol.tmpl
3032
var otelcolTemplate string
3133

@@ -115,7 +117,7 @@ func writeCollectorConfig(conf *config.Collector) error {
115117
defer func() {
116118
err = file.Close()
117119
if err != nil {
118-
slog.Warn("Failed to close file", "file_path", confPath)
120+
slog.Warn("Failed to close file", "file_path", confPath, settingsLogOrigin)
119121
}
120122
}()
121123
if err != nil {

0 commit comments

Comments
 (0)