Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add instrumentation to vinxi powered app? #311

Open
asterikx opened this issue Jun 17, 2024 · 1 comment
Open

How to add instrumentation to vinxi powered app? #311

asterikx opened this issue Jun 17, 2024 · 1 comment

Comments

@asterikx
Copy link

I would like to add Open Telemetry to a Solid Start application powered by vinxi for full-stack observability and performance tracing.

Is there something like an instrumentation.ts file like in Next.js (see here) or a special hook to register Open Telemetry with auto-instrumentation?

@nksaraf
Copy link
Owner

nksaraf commented Jun 17, 2024

I think we should try to make this something thats really simple to do as well. I haven't thought about it enough, but there are enough hooks in the system to do anything during the lifecycle. So its definetely possible to do. For eg. in my app, I added request/response logging like this during dev..

  app.hooks.hook("app:dev:server:created", ({ devApp }) => {
    let reqId = 0

    let regex = /(\/@)|(\/node_modules)|(\/(src|fonts|__)).*/
    devApp.h3App.options.onRequest = (event) => {
      if (regex.exec(event.path)) return
      let id = ++reqId
      console.log(
        `${new Date().toISOString()} 📞 ${id} ${event.method} ${event.path}`,
      )
      event.context.id = id
      event.context.requestStartTime = Date.now()
    }
    devApp.h3App.options.onAfterResponse = (event, response) => {
      if (regex.exec(event.path)) return
      let elapsed = Date.now() - event.context.requestStartTime
      console.log(
        `${new Date().toISOString()} 📬 ${event.context.id} ${event.method} ${event.path} ${event.node.res.statusCode} ${event.node.res.getHeader("content-type")} ${elapsed}ms`,
      )
    }
  })

This is just an example of what kind of hooks are available. During prod, you will be able to use a nitro plugin to do the same.

Actually maybe we should enable doing the same by playing the plugins during dev too which we have been planning to do.

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

No branches or pull requests

2 participants