> ## Documentation Index
> Fetch the complete documentation index at: https://docs.infrastack.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrate OpenTelemetry for Next.js with infrastack.ai

> Learn how to instrument your Next.js application with infrastack.ai and OpenTelemetry for comprehensive observability and performance insights.

export const VercelIcon = ({size = "24"}) => <svg width={size} height={size} viewBox="0 0 256 256" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid">
    <g>
      <polygon fill="#708090" points="128 0 256 221.705007 0 221.705007">

      </polygon>
    </g>
  </svg>;

export const RenderIcon = ({size = "24"}) => <svg xmlns="http://www.w3.org/2000/svg" width={size} height={size} fill="#708090" version="1.1">
    <path d="M15.6491 0.00582604C12.9679 -0.120371 10.7133 1.81847 10.3286 4.373C10.3134 4.49154 10.2905 4.60627 10.2715 4.72099C9.67356 7.90268 6.88955 10.3119 3.5457 10.3119C2.35364 10.3119 1.23395 10.006 0.258977 9.47058C0.140914 9.40557 0 9.4897 0 9.62354V10.3081V20.6218H10.2677V12.8894C10.2677 11.4668 11.4178 10.3119 12.8346 10.3119H15.4015C18.3074 10.3119 20.6458 7.89121 20.5315 4.94662C20.4287 2.29649 18.2884 0.132023 15.6491 0.00582604Z" />
  </svg>;

export const JsIcon = ({size = "24"}) => <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50" width={size} height={size} fill="#708090"><path d="M 43.335938 4 L 6.667969 4 C 5.195313 4 4 5.195313 4 6.667969 L 4 43.332031 C 4 44.804688 5.195313 46 6.667969 46 L 43.332031 46 C 44.804688 46 46 44.804688 46 43.335938 L 46 6.667969 C 46 5.195313 44.804688 4 43.335938 4 Z M 27 36.183594 C 27 40.179688 24.65625 42 21.234375 42 C 18.140625 42 15.910156 39.925781 15 38 L 18.144531 36.097656 C 18.75 37.171875 19.671875 38 21 38 C 22.269531 38 23 37.503906 23 35.574219 L 23 23 L 27 23 Z M 35.675781 42 C 32.132813 42 30.121094 40.214844 29 38 L 32 36 C 32.816406 37.335938 33.707031 38.613281 35.589844 38.613281 C 37.171875 38.613281 38 37.824219 38 36.730469 C 38 35.425781 37.140625 34.960938 35.402344 34.199219 L 34.449219 33.789063 C 31.695313 32.617188 29.863281 31.148438 29.863281 28.039063 C 29.863281 25.179688 32.046875 23 35.453125 23 C 37.878906 23 39.621094 23.84375 40.878906 26.054688 L 37.910156 27.964844 C 37.253906 26.789063 36.550781 26.328125 35.453125 26.328125 C 34.335938 26.328125 33.628906 27.039063 33.628906 27.964844 C 33.628906 29.109375 34.335938 29.570313 35.972656 30.28125 L 36.925781 30.691406 C 40.171875 32.078125 42 33.496094 42 36.683594 C 42 40.117188 39.300781 42 35.675781 42 Z" /></svg>;

export const GithubIcon = ({size = "24"}) => <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width={size} height={size} fill="#708090"><path fill-rule="evenodd" clip-rule="evenodd" d="M48.854 0C21.839 0 0 22 0 49.217c0 21.756 13.993 40.172 33.405 46.69 2.427.49 3.316-1.059 3.316-2.362 0-1.141-.08-5.052-.08-9.127-13.59 2.934-16.42-5.867-16.42-5.867-2.184-5.704-5.42-7.17-5.42-7.17-4.448-3.015.324-3.015.324-3.015 4.934.326 7.523 5.052 7.523 5.052 4.367 7.496 11.404 5.378 14.235 4.074.404-3.178 1.699-5.378 3.074-6.6-10.839-1.141-22.243-5.378-22.243-24.283 0-5.378 1.94-9.778 5.014-13.2-.485-1.222-2.184-6.275.486-13.038 0 0 4.125-1.304 13.426 5.052a46.97 46.97 0 0 1 12.214-1.63c4.125 0 8.33.571 12.213 1.63 9.302-6.356 13.427-5.052 13.427-5.052 2.67 6.763.97 11.816.485 13.038 3.155 3.422 5.015 7.822 5.015 13.2 0 18.905-11.404 23.06-22.324 24.283 1.78 1.548 3.316 4.481 3.316 9.126 0 6.6-.08 11.897-.08 13.526 0 1.304.89 2.853 3.316 2.364 19.412-6.52 33.405-24.935 33.405-46.691C97.707 22 75.788 0 48.854 0z" /></svg>;

export const DockerIcon = ({size = "24"}) => <svg xmlns="http://www.w3.org/2000/svg" role="img" viewBox="0 0 512 512"><path stroke="#708090" stroke-width="38" d="M296 226h42m-92 0h42m-91 0h42m-91 0h41m-91 0h42m8-46h41m8 0h42m7 0h42m-42-46h42" /><path fill="#708090" d="m472 228s-18-17-55-11c-4-29-35-46-35-46s-29 35-8 74c-6 3-16 7-31 7H68c-5 19-5 145 133 145 99 0 173-46 208-130 52 4 63-39 63-39" /></svg>;

#### What you will learn

* How to set up your environment
* How to <Tooltip tip="To make an application observable, it must be instrumented: That is, code from the system's components must emit traces, metrics, and logs."> **instrument** </Tooltip> your application using the infrastack.ai SDK or the official OpenTelemetry SDK

#### Prerequisites

* An [infrastack.ai](https://app.infrastack.ai) account to get your API key
* A Next.js application

<CardGroup cols={1}>
  <Card title="Follow the example project on GitHub" href="https://github.com/infrastackai/infrastack/tree/main/examples/nextjs" icon={<GithubIcon />} />
</CardGroup>

### Getting started

<Tabs>
  <Tab title="Using the Infrastack SDK">
    You can use the Infrastack SDK to instrument your Next.js application with a single line of code.

    <Steps>
      <Step title="Install the NPM package">
        <CodeGroup>
          ```bash npm theme={null}
          npm install @infrastack/otel
          ```

          ```bash yarn theme={null}
          yarn add @infrastack/otel
          ```

          ```bash pnpm theme={null}
          pnpm add @infrastack/otel
          ```
        </CodeGroup>
      </Step>

      <Step title="Prepare your environment">
        Create an `.env` file in the root of your project and add your API key.

        ```properties .env theme={null}
        INFRASTACK_API_KEY=sk-1*************************f5af
        ```
      </Step>

      <Step title="Configure your Next.js application">
        This is step is required in order to enable instrumentation. Right now, `instrumentationHook` and `serverComponentsExternalPackages` are expermiental. Please follow the [Official Next.js Documentation for instrumentationHook](https://nextjs.org/docs/app/api-reference/next-config-js/instrumentationHook) if this document is outdated.

        ```javascript next.config.mjs theme={null}
        const nextConfig = {
            experimental: {
                instrumentationHook: true, // To enable instrumentation
            },
        };

        export default nextConfig;
        ```
      </Step>

      <Step title="Create instrumentation.ts">
        <Info>If you are using a `src` folder in your Next.js application, create an `instrumentation.ts` file there. If not, create the file in the root of your project.</Info>

        ```javascript instrumentation.ts theme={null}
        import { Infrastack, Protocol } from "@infrastack/otel";
        export async function register() {
            Infrastack.init({
                serviceName: "YOUR_SERVICE_NAME",
                protocol: Protocol.HTTP, // Recommended for Vercel
            });
        }
        ```

        <Info>If you don't provide a service name, we will create a random name for you.</Info>
      </Step>

      <Step title="Run your application">
        Run your Next.js application with the default command.

        ```bash theme={null}
        npm run dev
        ```

        <Tip>You can check your data from the [infrastack.ai](https://app.infrastack.ai) dashboard.</Tip>
      </Step>
    </Steps>
  </Tab>

  <Tab title="Using the OpenTelemetry SDK">
    You can use the [OpenTelemetry Javascript SDK](https://opentelemetry.io/docs/languages/js/) to instrument your Next.js application.

    <Steps>
      <Step title="Install the NPM packages">
        <CodeGroup>
          ```bash npm theme={null}
          npm install @opentelemetry/api @opentelemetry/sdk-node @opentelemetry/sdk-trace-node
          npm install @opentelemetry/auto-instrumentations-node @opentelemetry/exporter-trace-otlp-grpc
          npm install @opentelemetry/resources @opentelemetry/semantic-conventions
          ```

          ```bash yarn theme={null}
          yarn add @opentelemetry/api @opentelemetry/sdk-node @opentelemetry/sdk-trace-node
          yarn add @opentelemetry/auto-instrumentations-node @opentelemetry/exporter-trace-otlp-grpc
          yarn add @opentelemetry/resources @opentelemetry/semantic-conventions
          ```

          ```bash pnpm theme={null}
          pnpm add @opentelemetry/api @opentelemetry/sdk-node @opentelemetry/sdk-trace-node
          pnpm add @opentelemetry/auto-instrumentations-node @opentelemetry/exporter-trace-otlp-grpc
          pnpm add @opentelemetry/resources @opentelemetry/semantic-conventions
          ```
        </CodeGroup>
      </Step>

      <Step title="Prepare your environment">
        ```properties .env theme={null}
        OTEL_EXPORTER_OTLP_ENDPOINT="https://collector.infrastack.ai"
        OTEL_EXPORTER_OTLP_HEADERS="infrastack-api-key={{YOUR_INFRASTACK_API_KEY}}"
        ```
      </Step>

      <Step title="Configure your Next.js application">
        This is step is required in order to enable instrumentation. Right now, `instrumentationHook` and `serverComponentsExternalPackages` are experimental. Please follow the [Official Next.js Documentation for instrumentationHook](https://nextjs.org/docs/app/api-reference/next-config-js/instrumentationHook) if this document is outdated.

        ```javascript next.config.mjs theme={null}
        const nextConfig = {
            experimental: {
                instrumentationHook: true, // To enable instrumentation
                serverComponentsExternalPackages: [ // To utilize node.js specific features in a next.js environment
                    "@opentelemetry/auto-instrumentations-node",
                    "@opentelemetry/sdk-node",
                ],
            },
        };

        export default nextConfig;
        ```
      </Step>

      <Step title="Create instrumentation-node.ts">
        Here, you will create a file that will be used to instrument your application. You will have lower level control over the instrumentation process.

        ```javascript instrumentation-node.ts theme={null}
        'use strict'
        import { NodeSDK } from '@opentelemetry/sdk-node';
        import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
        import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
        import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';
        import { Resource } from '@opentelemetry/resources';

        const traceExporter = new OTLPTraceExporter();

        const sdk = new NodeSDK({
            resource: new Resource({
                [ATTR_SERVICE_NAME]: "{{YOUR_SERVICE_NAME}}",
                [ATTR_SERVICE_VERSION]: "{{YOUR_SERVICE_VERSION}}",
            }),
            traceExporter,
            instrumentations: [
                getNodeAutoInstrumentations({
                    '@opentelemetry/instrumentation-fs': {
                        requireParentSpan: true,
                    },
                })
            ],
        });

        sdk.start(); 
        ```
      </Step>

      <Step title="Create instrumentation.ts">
        <Info>If you are using a `src` folder in your Next.js application, create an `instrumentation.ts` file there. If not, create the file in the root of your project.</Info>

        ```javascript instrumentation.ts theme={null}
        export async function register() {
            if (process.env.NEXT_RUNTIME === "nodejs") {
                await import('./instrumentation-node');
            }
        }
        ```
      </Step>

      <Step title="Run your application">
        Run your Next.js application with the default command.

        ```bash theme={null}
        npm run dev
        ```

        <Tip>You can check your data from the [infrastack.ai](https://app.infrastack.ai) dashboard.</Tip>
      </Step>
    </Steps>
  </Tab>
</Tabs>

### Next steps

<CardGroup cols={2}>
  <Card title="Deploy Next.js with OpenTelemetry to Vercel" href="/guides/deploy-nextjs-with-opentelemetry-to-vercel" icon={<VercelIcon />} />

  <Card title="Deploy Next.js with OpenTelemetry to Render" href="/guides/deploy-nextjs-with-opentelemetry-to-render" icon={<RenderIcon />} />

  <Card title="Dockerize Next.js with OpenTelemetry" href="/guides/dockerize-nextjs-with-opentelemetry-with-infrastack" icon={<DockerIcon />} />

  <Card title="Javascript SDK Reference" href="/sdk-reference/javascript/configuration" icon={<JsIcon />} />

  <Card title="Explore the Next.js example project" href="https://github.com/infrastackai/infrastack/tree/main/examples/nextjs" icon={<GithubIcon />} />
</CardGroup>
