Skip to content

Commit 7064f21

Browse files
authored
refactor: use constants for structured logging field names (#488)
Signed-off-by: bypark <[email protected]>
1 parent 39ccbac commit 7064f21

File tree

23 files changed

+1543
-51
lines changed

23 files changed

+1543
-51
lines changed

agent/agent.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/argoproj-labs/argocd-agent/internal/event"
3030
"github.com/argoproj-labs/argocd-agent/internal/informer"
3131
"github.com/argoproj-labs/argocd-agent/internal/kube"
32+
"github.com/argoproj-labs/argocd-agent/internal/logging"
3233
"github.com/argoproj-labs/argocd-agent/internal/manager"
3334
"github.com/argoproj-labs/argocd-agent/internal/manager/application"
3435
"github.com/argoproj-labs/argocd-agent/internal/manager/appproject"
@@ -396,7 +397,7 @@ func (a *Agent) SetConnected(connected bool) {
396397
}
397398

398399
func log() *logrus.Entry {
399-
return logrus.WithField("module", "Agent")
400+
return logging.ModuleLogger("Agent")
400401
}
401402

402403
func (a *Agent) healthzHandler(w http.ResponseWriter, r *http.Request) {

agent/connection.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/argoproj-labs/argocd-agent/internal/event"
2222
"github.com/argoproj-labs/argocd-agent/internal/grpcutil"
2323
"github.com/argoproj-labs/argocd-agent/internal/kube"
24+
"github.com/argoproj-labs/argocd-agent/internal/logging/logfields"
2425
"github.com/argoproj-labs/argocd-agent/internal/manager"
2526
"github.com/argoproj-labs/argocd-agent/internal/resync"
2627
"github.com/argoproj-labs/argocd-agent/pkg/api/grpc/eventstreamapi"
@@ -63,9 +64,9 @@ func (a *Agent) maintainConnection() error {
6364

6465
func (a *Agent) sender(stream eventstreamapi.EventStream_SubscribeClient) error {
6566
logCtx := log().WithFields(logrus.Fields{
66-
"module": "StreamEvent",
67-
"direction": "Send",
68-
"client_addr": grpcutil.AddressFromContext(stream.Context()),
67+
logfields.Module: "StreamEvent",
68+
logfields.Direction: "Send",
69+
logfields.ClientAddr: grpcutil.AddressFromContext(stream.Context()),
6970
})
7071

7172
q := a.queues.SendQ(defaultQueueName)
@@ -101,9 +102,9 @@ func (a *Agent) sender(stream eventstreamapi.EventStream_SubscribeClient) error
101102
// will block until an event has been received, or an error has occurred.
102103
func (a *Agent) receiver(stream eventstreamapi.EventStream_SubscribeClient) error {
103104
logCtx := log().WithFields(logrus.Fields{
104-
"module": "StreamEvent",
105-
"direction": "Recv",
106-
"client_addr": grpcutil.AddressFromContext(stream.Context()),
105+
logfields.Module: "StreamEvent",
106+
logfields.Direction: "Recv",
107+
logfields.ClientAddr: grpcutil.AddressFromContext(stream.Context()),
107108
})
108109
rcvd, err := stream.Recv()
109110
if err != nil {
@@ -172,8 +173,8 @@ func (a *Agent) handleStreamEvents() error {
172173
go a.eventWriter.SendWaitingEvents(a.context)
173174

174175
logCtx := log().WithFields(logrus.Fields{
175-
"module": "StreamEvent",
176-
"server_addr": grpcutil.AddressFromContext(stream.Context()),
176+
logfields.Module: "StreamEvent",
177+
logfields.ServerAddr: grpcutil.AddressFromContext(stream.Context()),
177178
})
178179

179180
if err := a.resyncOnStart(logCtx); err != nil {
@@ -239,7 +240,7 @@ func (a *Agent) handleStreamEvents() error {
239240
}
240241
}
241242

242-
log().WithField("component", "EventHandler").Info("Stream closed")
243+
log().WithField(logfields.Component, "EventHandler").Info("Stream closed")
243244

244245
return nil
245246
}

agent/inbound_redis.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"time"
2525

2626
"github.com/argoproj-labs/argocd-agent/internal/event"
27+
"github.com/argoproj-labs/argocd-agent/internal/logging/logfields"
2728
rediscache "github.com/go-redis/cache/v9"
2829
"github.com/redis/go-redis/v9"
2930
"github.com/sirupsen/logrus"
@@ -182,7 +183,7 @@ func (a *Agent) forwardRedisSubscribeNotificationsToPrincipal(pubsub *redis.PubS
182183

183184
ch := pubsub.Channel()
184185

185-
logCtx = logCtx.WithField("channel-name", channelName)
186+
logCtx = logCtx.WithField(logfields.ChannelName, channelName)
186187
for {
187188
select {
188189

@@ -207,7 +208,7 @@ func (a *Agent) forwardRedisSubscribeNotificationsToPrincipal(pubsub *redis.PubS
207208
// If the connection has not been active for X minutes, close the connection
208209
if time.Since(*lastPing) >= principalRedisConnectionTimeout {
209210
pubsub.Close()
210-
logCtx.WithField("lastPing", lastPing).Trace("closing redis connection due to inactivity")
211+
logCtx.WithField(logfields.LastPing, lastPing).Trace("closing redis connection due to inactivity")
211212
return
212213
}
213214

agent/outbound.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package agent
1616

1717
import (
1818
"github.com/argoproj-labs/argocd-agent/internal/event"
19+
"github.com/argoproj-labs/argocd-agent/internal/logging/logfields"
1920
"github.com/argoproj-labs/argocd-agent/internal/resources"
2021
"github.com/argoproj-labs/argocd-agent/pkg/types"
2122
"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
@@ -26,7 +27,7 @@ import (
2627
// addAppCreationToQueue processes a new application event originating from the
2728
// AppInformer and puts it in the send queue.
2829
func (a *Agent) addAppCreationToQueue(app *v1alpha1.Application) {
29-
logCtx := log().WithField("event", "NewApp").WithField("application", app.QualifiedName())
30+
logCtx := log().WithField(logfields.Event, "NewApp").WithField(logfields.Application, app.QualifiedName())
3031
logCtx.Debugf("New app event")
3132

3233
a.resources.Add(resources.NewResourceKeyFromApp(app))
@@ -55,13 +56,13 @@ func (a *Agent) addAppCreationToQueue(app *v1alpha1.Application) {
5556
}
5657

5758
q.Add(a.emitter.ApplicationEvent(event.Create, app))
58-
logCtx.WithField("sendq_len", q.Len()).WithField("sendq_name", defaultQueueName).Debugf("Added app create event to send queue")
59+
logCtx.WithField(logfields.SendQueueLen, q.Len()).WithField(logfields.SendQueueName, defaultQueueName).Debugf("Added app create event to send queue")
5960
}
6061

6162
// addAppUpdateToQueue processes an application update event originating from
6263
// the AppInformer and puts it in the agent's send queue.
6364
func (a *Agent) addAppUpdateToQueue(old *v1alpha1.Application, new *v1alpha1.Application) {
64-
logCtx := log().WithField("event", "UpdateApp").WithField("application", old.QualifiedName())
65+
logCtx := log().WithField(logfields.Event, "UpdateApp").WithField(logfields.Application, old.QualifiedName())
6566
a.watchLock.Lock()
6667
defer a.watchLock.Unlock()
6768
if a.appManager.IsChangeIgnored(new.QualifiedName(), new.ResourceVersion) {
@@ -101,15 +102,15 @@ func (a *Agent) addAppUpdateToQueue(old *v1alpha1.Application, new *v1alpha1.App
101102
q.Add(a.emitter.ApplicationEvent(eventType, new))
102103
// q.Add(ev)
103104
logCtx.
104-
WithField("sendq_len", q.Len()).
105-
WithField("sendq_name", defaultQueueName).
105+
WithField(logfields.SendQueueLen, q.Len()).
106+
WithField(logfields.SendQueueName, defaultQueueName).
106107
Debugf("Added event of type %s to send queue", eventType)
107108
}
108109

109110
// addAppDeletionToQueue processes an application delete event originating from
110111
// the AppInformer and puts it in the send queue.
111112
func (a *Agent) addAppDeletionToQueue(app *v1alpha1.Application) {
112-
logCtx := log().WithField("event", "DeleteApp").WithField("application", app.QualifiedName())
113+
logCtx := log().WithField(logfields.Event, "DeleteApp").WithField(logfields.Application, app.QualifiedName())
113114
logCtx.Debugf("Delete app event")
114115

115116
a.resources.Remove(resources.NewResourceKeyFromApp(app))
@@ -128,13 +129,13 @@ func (a *Agent) addAppDeletionToQueue(app *v1alpha1.Application) {
128129
}
129130

130131
q.Add(a.emitter.ApplicationEvent(event.Delete, app))
131-
logCtx.WithField("sendq_len", q.Len()).Debugf("Added app delete event to send queue")
132+
logCtx.WithField(logfields.SendQueueLen, q.Len()).Debugf("Added app delete event to send queue")
132133
}
133134

134135
// deleteNamespaceCallback is called when the user deletes the agent namespace.
135136
// Since there is no namespace we can remove the queue associated with this agent.
136137
func (a *Agent) deleteNamespaceCallback(outbound *corev1.Namespace) {
137-
logCtx := log().WithField("event", "DeleteNamespace").WithField("agent", outbound.Name)
138+
logCtx := log().WithField(logfields.Event, "DeleteNamespace").WithField(logfields.Agent, outbound.Name)
138139

139140
if !a.queues.HasQueuePair(outbound.Name) {
140141
return
@@ -185,7 +186,7 @@ func (a *Agent) addAppProjectCreationToQueue(appProject *v1alpha1.AppProject) {
185186
}
186187

187188
q.Add(a.emitter.AppProjectEvent(event.Create, appProject))
188-
logCtx.WithField("sendq_len", q.Len()).Debugf("Added appProject create event to send queue")
189+
logCtx.WithField(logfields.SendQueueLen, q.Len()).Debugf("Added appProject create event to send queue")
189190
}
190191

191192
// addAppProjectUpdateToQueue processes an appProject update event originating from the
@@ -223,7 +224,7 @@ func (a *Agent) addAppProjectUpdateToQueue(old *v1alpha1.AppProject, new *v1alph
223224
}
224225

225226
q.Add(a.emitter.AppProjectEvent(event.SpecUpdate, new))
226-
logCtx.WithField("sendq_len", q.Len()).Debugf("Added appProject spec update event to send queue")
227+
logCtx.WithField(logfields.SendQueueLen, q.Len()).Debugf("Added appProject spec update event to send queue")
227228
}
228229

229230
// addAppProjectDeletionToQueue processes an appProject delete event originating from the
@@ -258,5 +259,5 @@ func (a *Agent) addAppProjectDeletionToQueue(appProject *v1alpha1.AppProject) {
258259
}
259260

260261
q.Add(a.emitter.AppProjectEvent(event.Delete, appProject))
261-
logCtx.WithField("sendq_len", q.Len()).Debugf("Added appProject delete event to send queue")
262+
logCtx.WithField(logfields.SendQueueLen, q.Len()).Debugf("Added appProject delete event to send queue")
262263
}

0 commit comments

Comments
 (0)