Skip to content

Commit 0a9a95e

Browse files
authored
Add correlation_id to logs (#4506)
Problem: Agent logs a correlation ID when it calls the control plane. The control plane doesn't log the ID, making it hard to cross-reference and debug the connections. Solution: For relevant logs, add the correlation ID.
1 parent eb83cdb commit 0a9a95e

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

internal/controller/nginx/agent/command.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ func (cs *commandService) CreateConnection(
8484

8585
resource := req.GetResource()
8686
podName := resource.GetContainerInfo().GetHostname()
87-
cs.logger.Info(fmt.Sprintf("Creating connection for nginx pod: %s", podName))
87+
cs.logger.Info(
88+
fmt.Sprintf("Creating connection for nginx pod: %s", podName),
89+
"correlation_id", req.GetMessageMeta().GetCorrelationId(),
90+
)
8891

8992
name, depType := getAgentDeploymentNameAndType(resource.GetInstances())
9093
if name == (types.NamespacedName{}) || depType == "" {
@@ -96,7 +99,7 @@ func (cs *commandService) CreateConnection(
9699
Error: err.Error(),
97100
},
98101
}
99-
cs.logger.Error(err, "error getting pod owner")
102+
cs.logger.Error(err, "error getting pod owner", "correlation_id", req.GetMessageMeta().GetCorrelationId())
100103
return response, grpcStatus.Errorf(codes.InvalidArgument, "error getting pod owner: %s", err.Error())
101104
}
102105

internal/controller/nginx/agent/file.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ func (fs *fileService) GetFileStream(
106106
sizeUint32 = uint32(size) //nolint:gosec // validation check performed on previous line
107107
hash := req.GetFileMeta().GetHash()
108108

109-
fs.logger.V(1).Info("Sending chunked file to agent", "file", req.GetFileMeta().GetName())
109+
fs.logger.V(1).Info(
110+
"Sending chunked file to agent",
111+
"file", req.GetFileMeta().GetName(),
112+
"correlation_id", req.GetMessageMeta().GetCorrelationId(),
113+
)
110114

111115
if err := files.SendChunkedFile(
112116
req.GetMessageMeta(),
@@ -145,20 +149,28 @@ func (fs *fileService) getFileContents(req *pb.GetFileRequest, connKey string) (
145149
filename := req.GetFileMeta().GetName()
146150
contents, fileFoundHash := deployment.GetFile(filename, req.GetFileMeta().GetHash())
147151
if len(contents) == 0 {
148-
fs.logger.V(1).Info("Error getting file for agent", "file", filename)
152+
fs.logger.V(1).Info(
153+
"Error getting file for agent",
154+
"file", filename,
155+
"correlation_id", req.GetMessageMeta().GetCorrelationId(),
156+
)
149157
if fileFoundHash != "" {
150158
fs.logger.V(1).Info(
151159
"File found had wrong hash",
152-
"hashWanted",
153-
req.GetFileMeta().GetHash(),
154-
"hashFound",
155-
fileFoundHash,
160+
"hash_wanted", req.GetFileMeta().GetHash(),
161+
"hash_found", fileFoundHash,
162+
"correlation_id", req.GetMessageMeta().GetCorrelationId(),
156163
)
157164
}
158165
return nil, status.Errorf(codes.NotFound, "file not found")
159166
}
160167

161-
fs.logger.V(1).Info("Getting file for agent", "file", filename, "fileHash", fileFoundHash)
168+
fs.logger.V(1).Info(
169+
"Getting file for agent",
170+
"file", filename,
171+
"file_hash", fileFoundHash,
172+
"correlation_id", req.GetMessageMeta().GetCorrelationId(),
173+
)
162174

163175
return contents, nil
164176
}

0 commit comments

Comments
 (0)