Skip to content

fix: [#726] scope log handler cache to the application - #1482

Merged
krishankumar01 merged 2 commits into
masterfrom
kkumar-gcc/#726-otel-log-restart
Jun 10, 2026
Merged

fix: [#726] scope log handler cache to the application#1482
krishankumar01 merged 2 commits into
masterfrom
kkumar-gcc/#726-otel-log-restart

Conversation

@krishankumar01

Copy link
Copy Markdown
Member

📑 Description

RelatedTo goravel/goravel#726

The log module caches channel handlers in a package level sync.Map, so they survive facades.App().Restart(). Handlers that hold per container resources keep using the old ones after the restart; for the otel channel this means emitting into a shut down logger provider, which silently drops every record.

Scope the cache to the Application instead: instances derived via WithContext/Channel/Stack share it, and it dies with the container. Also removes the clearChannelCache() test helper that existed to work around the global.

@coderabbitai summary

✅ Checks

  • Added test cases for my code

@krishankumar01
krishankumar01 marked this pull request as ready for review June 9, 2026 20:54
@krishankumar01
krishankumar01 requested a review from a team as a code owner June 9, 2026 20:54
Copilot AI review requested due to automatic review settings June 9, 2026 20:54
@codecov

codecov Bot commented Jun 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.40%. Comparing base (9e0d1e9) to head (885b2ae).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1482      +/-   ##
==========================================
- Coverage   69.40%   69.40%   -0.01%     
==========================================
  Files         377      378       +1     
  Lines       30190    30199       +9     
==========================================
+ Hits        20954    20959       +5     
- Misses       8256     8258       +2     
- Partials      980      982       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an application-restart correctness issue in the logging subsystem by scoping the log channel handler cache to a log.Application instance, rather than using a package-level global cache that could outlive the container and retain stale telemetry resources.

Changes:

  • Replaced the package-level handler cache with an Application-scoped handlerCache shared across WithContext/Channel/Stack derived instances.
  • Updated getHandlers to take an explicit cache parameter and thread it through stack recursion.
  • Removed the prior global-cache test helper and added a test ensuring cache sharing/leakage behavior matches the new scoping.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
log/application.go Introduces per-Application handler cache and ensures derived instances share it while new root apps don’t.
log/application_test.go Removes global cache helper; adds coverage verifying cache scope and updates getHandlers call sites.
log/writer_test.go Removes now-unneeded cache clearing calls that were compensating for the prior global cache.

Comment thread log/application.go Outdated
config config.Config
json foundation.Json
telemetryResolver contractstelemetry.Resolver
handlerCache *sync.Map

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is a pointer required?

Suggested change
handlerCache *sync.Map
channelToHandlers sync.Map

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is required, we want an application level cache here. Channel/WithContext/Stack create a new Application each call and all of them should share the same cache. If we make it a value, each instance gets a copy of the map, so if someone calls Channel multiple times it will create the handlers each time since whatever was cached stays in the copy. Also vet doesn't allow copying sync.Map by value, it has a mutex inside.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

But I have renamed handlerCache -> channelToHandlers

@hwbrzzl hwbrzzl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, LGTM

@krishankumar01
krishankumar01 merged commit 8940870 into master Jun 10, 2026
17 of 19 checks passed
@krishankumar01
krishankumar01 deleted the kkumar-gcc/#726-otel-log-restart branch June 10, 2026 07:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants