Giter Club home page Giter Club logo

agent-apm-nextjs's Introduction

Getting Started

agent-apm-nextjs

Description: Agent APM for Next.js

Prerequisites

Make sure you have installed the latest version of Next.js or a version greater than 13.4+, as Vercel introduced their experimental feature in that release.

Before proceeding with the Next.js APM setup, make sure you have the @opentelemetry/api package installed. If it's not already installed, run the following command:

npm install @opentelemetry/api@">=1.3.0 <1.5.0"

Guides

You can use this APM to track your project, either deployed on Vercel platform or hosted on own server. Run follow steps:

Step 1: Install Next.js APM package

Run the command below in your terminal to install Middleware’s Next.js APM package:

npm install @middleware.io/agent-apm-nextjs

Step 2: Modify the next.config.js file

As this feature is experimental, you need to explicitly opt-in by providing below thing into your next.config.js file.

const nextConfig = {
    // ...
    // Your existing code
    
     experimental: {
         instrumentationHook: true
     }
     
    // ...
    // Your existing code
}
module.exports = nextConfig

Step 3: Create an Instrumentation file

Create a custom instrumentation.ts file in your project root directory, and add following code as per your choice:

  • If you are using Vercel platform to deploy your projects, then use the code snippet below for serverless functions:
// @ts-ignore
import tracker from '@middleware.io/agent-apm-nextjs';

export function register() {
    tracker.track({
        projectName: "<PROJECT-NAME>",
        serviceName: "<SERVICE-NAME>",
        accessToken: "<ACCESS-TOKEN>",
        target: "vercel",
    });
}

Note: You can find your <ACCOUNT-KEY> on the Installation screen for NextJs / Vercel.

Note: After Deploying your project on Vercel, you need to integrate the Middleware from the marketplace. You can find more details here. To get a better idea, you can clone the sample project from the GitHub repository.

// @ts-ignore
import tracker from '@middleware.io/agent-apm-nextjs';

export function register() {
    tracker.track({
        projectName: "<PROJECT-NAME>",
        serviceName: "<SERVICE-NAME>",
        accessToken: "<ACCESS-TOKEN>",
    });
}
  • If you want to instrument your project without installing any host then use below code snippet:
// @ts-ignore
import tracker from '@middleware.io/agent-apm-nextjs';

export function register() {
    tracker.track({
        projectName: "<PROJECT-NAME>",
        serviceName: "<SERVICE-NAME>",
        accessToken: "<ACCESS-TOKEN>",
        target: "https://<ACCOUNT-UID>.middleware.io:443"
    });
}

Step 4: Enable Logging

To enable logging in your project, you need to add the following code in your file:

// @ts-ignore
import tracker from '@middleware.io/agent-apm-nextjs';

export default async function handler(req, res) {
    // ...
    // Your existing code

    tracker.info("Info Sample");
    tracker.warn("Warn Sample", {
        "tester": "Alex",
    });
    tracker.debug("Debug Sample");
    tracker.error("Error Sample");

    // ...
    // Your existing code
}

Note: You can find these details in your Middleware's Installation page.

That's it.

agent-apm-nextjs's People

Contributors

sanjoyment avatar

Watchers

Meghraj Choudhary avatar Raju Ram avatar Meenal Mathur avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.