Skip to main content

Logging and Monitoring

Vercel provides runtime logs, which include all logs generated by the Serverless (basically the pages/api directory) and Edge Function (currently not used by this starter kit) invocations of our application. However, Vercel's logging limitations may be insufficient for proper monitoring and alerts.

There may be a need to augment the runtime logs with structured logs from your application with Datadog for aggregation and analysis.

This document will run through the following:

Viewing runtime logs (in Vercel)

To view runtime logs, click the Logs tab from your project overview in the dashboard. From here you can view, filter, and search through the runtime logs. Each log row shares basic info about the request like execution, domain name, HTTP status, function type, and RequestId.

You can view runtime logs for both preview and production deployments.

Example of runtime logs

For more information on how to parse, filter, and search through runtime logs, see Vercel's documentation.

Limitations

However, there are some limitations to the runtime logs in Vercel that may affect our ability to monitor the application. Runtime logs are only stored for the following limits:

PlanRetention timeLog entries
Hobby1 hour of logs4000 rows of log data
Pro1 hour of logs4000 rows of log data
Enterprise3 days of logsUp to 60 million rows of log data

If the log entry limits are exceeded, only the most recent logs can be queried, and therefore the logs may need to be piped to another service such as Datadog for longer retention.

Datadog

Datadog enables you to ingest logs from your application for analysis and storage, which may be useful for monitoring and alerts and bypasses Vercel's anemic retention time. This section will describe how to set up Datadog to ingest logs from your application.

Set up Datadog-Vercel integration

Datadog provides an integration to pipe logs generated by your Vercel-hosted application into Datadog.

Integrate Vercel with Datadog to:

  • View and parse your application logs using Datadog’s Log Management
  • See the number of requests and 4xx/5xx HTTP errors to your serverless applications and APIs running on Vercel
  • Monitor frontend performance with Datadog Synthetics

To set up the integration, follow the steps below:

  1. Generate a Datadog API key.
    note

    If you do not have proper permissions, reach out to someone who does to generate the API key

Add API key image

  1. Take note of the API key to be used in the next steps.

Add API key image

  1. Go to Datadog integration page in Vercel marketplace and add the integration

Vercel integration

  1. In the integration set up page, enter the API key generated in step 1 and click "Add Integration"

Add integration

The integration should have been added successfully. Now we head to Datadog to set up the dashboard for your application.

  1. Go to Datadog dashboards and search for "Vercel", or click here. The default Vercel dashboard should show up as an option. You should clone the dashboard and add the specific service for ease of modification for other items you may want to monitor.

Alternatively, you can create a new dashboard and add the widgets you want to monitor. We have created a powerpack for you to use as a starting point. You can use the powerpack by:

  1. Going to your dashboard and clicking the "Add Widgets or Powerpack" button,
  2. Searching for "Starter Kit Vercel Requests",
  3. Then drag and drop the powerpack into your dashboard.

Add powerpack

With the dashboards, you can proceed to add basic alerts and monitors for your application.

Below is an example of a Datadog monitor that tracks errors on the Vercel application and alerts a configured Slack channel. The monitor can also be found in by searching for "Starter Kit" on Datadog's monitor page.

note

To add a Slack channel, or BetterUptime integration, follow their respective documentation that can be found in the Useful Resources section of this document.

Datadog monitor

Useful Resources

ResourceLink
Vercel Runtime Logshttps://vercel.com/docs/concepts/observability/runtime-logs
Vercel Serverless Functionshttps://vercel.com/docs/concepts/functions/serverless-functions
Vercel Datadog integrationhttps://vercel.com/integrations/datadog
Adding Slack channel to Datadoghttps://docs.datadoghq.com/integrations/slack/
Adding BetterUptime integration to Datadoghttps://betterstack.com/docs/uptime/datadog/

loggerMiddleware usage

A loggerMiddleware has been set on all procedures with a path key which reflects the trpc method that was invoked.

The path context is especially useful for method-level observability purposes.

Example usage

Simply get logger object from the procedure's context.

post.router.ts

export const postRouter = router({
listMany: protectedProcedure
.input(z.string())
.query(async ({ input, ctx: { logger } }) => {
logger.info("your input is", { input });
}),
});

The log statement above will output the following when listMany has been invoked with an input of 1.

{
"message": "your input is",
"input": "1",
"path": "post.listMany"
}

Default loggerMiddleware logged statements

The following actions are logged by default (see loggerMiddleware for more info):

  • Per path completion time with durationInMs
  • Error logs on any error
    • Errors will contain the following:
      • message attribute of failure
      • error attribute containing the error thrown