Skip to content

fix(httpserver): replace stdlib log with structured logr in memory handler#1773

Merged
EItanya merged 4 commits intokagent-dev:mainfrom
mesutoezdil:fix/memory-handler-structured-logging
Apr 29, 2026
Merged

fix(httpserver): replace stdlib log with structured logr in memory handler#1773
EItanya merged 4 commits intokagent-dev:mainfrom
mesutoezdil:fix/memory-handler-structured-logging

Conversation

@mesutoezdil
Copy link
Copy Markdown
Contributor

@mesutoezdil mesutoezdil commented Apr 28, 2026

The memory handler was the only handler in the package still using the stdlib log package. Every other handler uses go-logr/logr. The logging middleware already injects a logr logger into the request context via ctrllog.IntoContext, so switching to logr.FromContextOrDiscard(r.Context()) is a straight drop-in.

This makes memory handler logs consistent with the rest: same format, same log level control, same structured key/value fields.

…ndler

Signed-off-by: mesutoezdil <mesudozdil@gmail.com>
Copilot AI review requested due to automatic review settings April 28, 2026 19:02
@github-actions github-actions Bot added bug Something isn't working and removed bug Something isn't working labels Apr 28, 2026
@github-actions github-actions Bot added the bug Something isn't working label Apr 28, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the MemoryHandler logging to use structured request-scoped logging instead of the stdlib log, aligning the memory endpoints with the logging approach used across the httpserver package.

Changes:

  • Replace log.Printf calls with structured Info/Error logging.
  • Retrieve a logger from the request context in memory handler endpoints.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}

log.Printf("Successfully added %d memory items", len(memories))
log.Info("added memory batch", "count", len(memories))
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a consistent log key naming convention for structured fields (most handlers use camelCase keys like count, userID, agentName). Using count is fine, but if you add more fields later, stick to the established key style to keep log queries consistent.

Copilot uses AI. Check for mistakes.

// List handles GET /api/memories and returns all memories for an agent+user, ranked by access frequency
func (h *MemoryHandler) List(w ErrorResponseWriter, r *http.Request) {
log := logr.FromContextOrDiscard(r.Context())
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using logr.FromContextOrDiscard(r.Context()) will discard logs if the handler is invoked without the logging middleware attaching a logger to the context. To keep behavior consistent with other handlers (and with the previous stdlib logger always emitting), prefer ctrllog.FromContext(r.Context()) and add an operation/name as needed.

Copilot uses AI. Check for mistakes.

// Delete handles DELETE /api/memories
func (h *MemoryHandler) Delete(w ErrorResponseWriter, r *http.Request) {
log := logr.FromContextOrDiscard(r.Context())
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using logr.FromContextOrDiscard(r.Context()) here can drop logs when no logger is present in context. Align with other handlers by using ctrllog.FromContext(r.Context()) (and optionally setting .WithName("memory-handler") / .WithValues("operation", "delete")) so logs still emit outside the middleware path (including unit tests).

Copilot uses AI. Check for mistakes.

// AddSession handles POST /api/memories/sessions
func (h *MemoryHandler) AddSession(w ErrorResponseWriter, r *http.Request) {
log := logr.FromContextOrDiscard(r.Context())
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using logr.FromContextOrDiscard(r.Context()) will silently drop logs when the request context doesn't have a logger (e.g., handler methods called directly in unit tests or if middleware is bypassed). Other handlers consistently use ctrllog.FromContext(r.Context()) (controller-runtime) which falls back to the global logger and composes correctly with ctrllog.IntoContext in the logging middleware. Consider switching to ctrllog.FromContext(...).WithName("memory-handler").WithValues("operation", "add-session") (and similarly for other operations) to avoid losing logs and to match handler logging patterns.

Copilot uses AI. Check for mistakes.
Comment thread go/core/internal/httpserver/handlers/memory.go Outdated

// AddSessionBatch handles POST /api/memories/sessions/batch
func (h *MemoryHandler) AddSessionBatch(w ErrorResponseWriter, r *http.Request) {
log := logr.FromContextOrDiscard(r.Context())
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using logr.FromContextOrDiscard(r.Context()) here can discard logs when the context isn't populated by the logging middleware. To match the rest of the handlers and preserve fallback behavior, prefer ctrllog.FromContext(r.Context()) (optionally adding .WithName("memory-handler") / .WithValues("operation", "add-session-batch")).

Copilot uses AI. Check for mistakes.
EItanya and others added 2 commits April 29, 2026 13:07
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Eitan Yarmush <eitan.yarmush@solo.io>
Copy link
Copy Markdown
Contributor

@EItanya EItanya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please take a look at the copilot comments. Changes make sense overall though

Replaced logr.FromContextOrDiscard with ctrllog.FromContext in all four
handler functions. FromContextOrDiscard silently drops logs when no logger
is in the context; ctrllog.FromContext falls back to the global logger so
logs still emit if middleware is bypassed.

Also aligned log field keys with the rest of the handlers: user_id ->
userID, agent -> agentName.

Signed-off-by: mesutoezdil <mesudozdil@gmail.com>
@mesutoezdil
Copy link
Copy Markdown
Contributor Author

mesutoezdil commented Apr 29, 2026

Fixed in 2c9ab6a. Replaced logr.FromContextOrDiscard with ctrllog.FromContext in all four handler functions, and updated the log field keys to match the rest of the handlers (user_id -> userID, agent -> agentName) @EItanya .

@EItanya EItanya merged commit 8c58631 into kagent-dev:main Apr 29, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants