GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add support for Next.js cache instrumentation · Issue #12888 · getsentry/sentry-javascript

Add support for Next.js cache instrumentation #12888

New issue
Copy link
New issue
Copy link
Open
Open
Add support for Next.js cache instrumentation#12888
Copy link
Labels
ImprovementNext.jsSpans

Description

@AbhiPrasad
AbhiPrasad
opened on Jul 11, 2024
Issue body actions

Problem Statement

We support manually instrumenting caching, but this is a pain to figure out: https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing/instrumentation/custom-instrumentation/caches-module/

Solution Brainstorm

We should either
a) auto-instrument the Next.js cache via https://nextjs.org/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath
b) update the cache module docs if auto-instrumentation is not possible.

Reacted by Benjamin E. Coe and Augie

Activity

  1. AbhiPrasad
    added
    Type: Improvement
    Package: nextjs
    on Jul 11, 2024
  2. AbhiPrasad commented on Jul 11, 2024

    @AbhiPrasad
    AbhiPrasad
    on Jul 11, 2024
    ContributorAuthor
    More actions

    ref https://twitter.com/nhducit/status/1811459207847416085

  3. AbhiPrasad commented on Jul 11, 2024

    @AbhiPrasad
    AbhiPrasad
    on Jul 11, 2024
    ContributorAuthor
    More actions

    asking in nextjs discussions about extending default cache handler: vercel/next.js#67677

  4. AbhiPrasad
    added
    Feature: Performance
    on Jul 17, 2024
  5. AbhiPrasad
    assigned
    timfish
    on Jul 25, 2024
  6. timfish commented on Jul 29, 2024

    @timfish
    timfish
    on Jul 29, 2024
    Collaborator
    More actions

    So some initial thoughts after a brief look...

    We need to create some kind of cache wrapper that adds the instrumentation but this will only work for self-hosted. I guess hosted on Vercel is out of scope for this issue?

    Passing the cacheHandler via require.resolve feels like a strange choice since it means that you can't just pass an already configured class instance 🤔. I need to check how the existing cache implementations accept their config and get to the bottom of why cacheHandler is a path before I can work out how wrapping this might work!

    module.exports = {
      cacheHandler: require.resolve('./cache-handler.js'),
      cacheMaxMemorySize: 0, // disable default in-memory caching
    }

    I guess the default cache could be instrumented via require-in-the-middle of FileSystemCache 🤔

    Reacted by Abhijeet Prasad
  7. timfish commented on Jul 29, 2024

    @timfish
    timfish
    on Jul 29, 2024
    Collaborator
    More actions

    ...and whereas the Sentry docs warn about possible performance issues with JSON.stringify(data).length, the next.js code doesn't seem to have a problem with doing this, at least for the fetch cache:
    https://github.com/vercel/next.js/blob/a0f83f8271a34cbd3fb531cffb7107aa8adb5e52/packages/next/src/server/lib/incremental-cache/index.ts#L504-L505

  8. timfish commented on Jul 29, 2024

    @timfish
    timfish
    on Jul 29, 2024
    Collaborator
    More actions

    Ok so it loads the cache implementation here:
    https://github.com/vercel/next.js/blob/a0f83f8271a34cbd3fb531cffb7107aa8adb5e52/packages/next/src/export/helpers/create-incremental-cache.ts#L30-L34

      if (cacheHandler) {
        CacheHandler = interopDefault(
          await import(formatDynamicImportPath(dir, cacheHandler)).then(
            (mod) => mod.default || mod
          )
        )
      }

    mod.default || mod isn't actually required because that's exactly what interopDefault does.

    Passing around paths to modules like this is very inflexible so I'm hoping there's a good reason for it!

    So if we offered a solution that wraps the handler at this config point:

    • It would need to return a path*
    • We'd need to find a way to pass the users cache handler path to us*
    • When we resolve the the users cache handler, we'd need to have the same absolute/relative resolution
      (* in a way the bundler picks up if that's required)

    Assuming no bundler issues, the best I've got at this late hour is passing the user handler path via an environment variable 😂

    module.exports = {
      cacheHandler: sentryCacheWrapper(require.resolve('./cache-handler.js')),
      cacheMaxMemorySize: 0, // disable default in-memory caching
    }
    export function sentryCacheWrapper(userHandler: string): string {
      process.env.__SENTRY_USER_CACHE_HANDLER_PATH = userHandler;
      return require.resolve('./sentry-cache-handler.cjs');
    }

    sentry-cache-handler.cjs

    const userHandler = require(process.env.__SENTRY_USER_CACHE_HANDLER_PATH);
    
    export default class SentryCacheHandler {
      function get(key) {
        // do instrumentation here, etc
        // return userHandler.get(key);
      }
    }

    It might be easier to use require-in-the-middle and wrap the IncrementalCache which is what wraps both the default cache, or any user supplied cache. I guess these are all internals so they might not be stable enough to do this.

  9. AbhiPrasad commented on Jul 30, 2024

    @AbhiPrasad
    AbhiPrasad
    on Jul 30, 2024
    ContributorAuthor
    More actions

    JSON.stringify(data).length, the next.js code doesn't seem to have a problem with doing this, at least for the fetch cache

    This would be worrying with arbitrary data (especially since stringifying certain primitives like booleans are not a good for the real world size), but because data is constrained to be IncrementalCacheValue, which seem to objects of fixed-key number, so should actually be pretty decently fast.

    Though looking at https://github.com/vercel/next.js/blob/fd5581d7ef58e4e925485b8b3a2b30ea8a482732/packages/next/src/server/response-cache/types.ts#L87, I guess stringifying some of these fields buffer can get expensive. Maybe an LRU here? Would be interesting to profile IMO.

    It might be easier to use require-in-the-middle and wrap the IncrementalCache which is what wraps both the default cache, or any user supplied cache. I guess these are all internals so they might not be stable enough to do this.

    I'm leaning toward this option as long as we only wrap get and set - I don't think that part of the public API will change given this seems to be the public API surface of custom caches as well.

  10. lforst commented on Jul 30, 2024

    @lforst
    lforst
    on Jul 30, 2024
    Contributor
    More actions

    I think I like the approach of using require-in-the-middle but I would probably prefer wrapping the user's cache handler directly over wrapping IncrementalCache.

  11. timfish commented on Jul 30, 2024

    @timfish
    timfish
    on Jul 30, 2024
    Collaborator
    More actions

    I would probably prefer wrapping the user's cache handler directly over wrapping IncrementalCache

    Yep, probably less prone to breakage and it looks like IncrementalCache is also used for the fetch cache

    Reacted by Luca Forstner, Abhijeet Prasad and Augie
  12. AbhiPrasad
    removed
    Type: Improvement
    on Nov 1, 2024
  13. linear
    added
    Migrated
    on Jan 9, 2025
  14. stephanie-anderson
    removed
    Migrated
    on Mar 13, 2025
  15. ghost added
    getsentry/sentry-javascript
    on Apr 7, 2025
  16. linear
    added
    Improvement
    SDKs (New)
    on Apr 7, 2025
  17. stephanie-anderson
    removed
    SDKs (New)
    getsentry/sentry-javascript
    on Apr 8, 2025
  18. stephanie-anderson
    added
    Next.js
    and removed
    Package: nextjs
    on Apr 16, 2025
  19. stephanie-anderson
    added
    Spans
    and removed
    Feature: Spans
    on Apr 30, 2025
  20. stephanie-anderson
    removed the
    Improvement
    issue type on Aug 12, 2025
  21. s1gr1d
    added a commit that references this issue on Sep 11, 2026

    feat(nextjs): Record cache spans for `use cache` functions (#24302)

    4977731
  22. timfish
    removed their assignment
    on Oct 5, 2026
  23. s1gr1d
    added a commit that references this issue on Oct 20, 2026

    feat(nextjs): Link `use cache` hits to their fill across instances (#…

    60c12e8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ImprovementNext.jsSpans

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.