2

I am building a monolithic NextJS WebApp hosted on Vercel and have been trying to implement some form of APM to the backend logic of the application. I am using the built-in NextJS internal REST API. While reading through some posts, I came across This Stack Overflow Post explaining that it is best practice in NextJS to separate business logic from the API routes into their own separate functions, export them, and then call them directly in getServerSideProps() on non-static pages. This brings me to the issue I am running into.

Let's say I have a GET API route that grabs a blogpost from a db:

// /pages/api/blog/[slug].ts

export const getBlogPostViaSlug = async (slug: string): Promise<BlogPost> => {
  return await blogService.getOne({ slug });
};

handler.get(async (req: NextApiRequest, res: NextApiResponse<any>) => {
  const { slug } = req.query;

  const blogPost = await getBlogPostViaSlug(slugStr);

  res.status(200).json(blogPost)
});

export default handler;

I also have a page that depends on the blogpost to render

const BlogPostPage: NextPage = (props: any) => {
  return (
    <div className="container">
      <BlogPostContainer {...props} />
    </div>
  );
};

export const getServerSideProps: GetServerSideProps = async (context) => {
  const mongoBlogPost = await getBlogPostViaSlug(slugStr);
}

export default BlogPostPage;

If I am primarily calling this getBlogPostViaSlug directly, what would be the best way to monitor the performance of the function without calling the API route from the frontend? This seems like it would be a common problem, yet I can't find examples of what other people have done without using the API and some sort of middleware.

What I have looked into so far

I was originally planning on using Datadog as an APM, but have found that while they do support serverless APM, it needs to be directly connected to the Lambda's that are being created under the hood via Vercel. Vercel does not expose any of those details to the user, so this is not an option.

note: I also found this workaround, but this doesn't work on Vercel (would need to host elsewhere)

I also looked into Vercel's in-house APM offerings, but this is defined at the top-layer of the application and does not seem to have a lot of granularity.

Jake Myers
  • 21
  • 1

0 Answers0