> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> SDK do Next.js para o ClickStack - a stack de observabilidade do ClickHouse

# Next.js

O ClickStack pode ingerir traces nativos do OpenTelemetry das suas
[funções serverless do Next.js](https://nextjs.org/docs/pages/building-your-application/optimizing/open-telemetry#manual-opentelemetry-configuration)
no Next 13.2+.

Este guia integra:

* **Logs do Console**
* **Traces**

<Note>
  Se você estiver procurando por replay de sessão/monitoramento no navegador, instale a [integração Browser](/pt-BR/clickstack/ingesting-data/sdks/browser).
</Note>

<div id="installing">
  ## Instalação
</div>

<div id="enable-instrumentation-hook">
  ### Habilite o hook de instrumentação (obrigatório para a v15 e anteriores)
</div>

Para começar, você precisará habilitar o hook de instrumentação do Next.js definindo `experimental.instrumentationHook = true;` no arquivo `next.config.js`.

**Exemplo:**

```javascript theme={null}
const nextConfig = {
  experimental: {
    instrumentationHook: true,
  },
  // Ignorar avisos de pacotes otel 
  // https://github.com/open-telemetry/opentelemetry-js/issues/4173#issuecomment-1822938936
  webpack: (
    config,
    { buildId, dev, isServer, defaultLoaders, nextRuntime, webpack },
  ) => {
    if (isServer) {
      config.ignoreWarnings = [{ module: /opentelemetry/ }];
    }
    return config;
  },
};

module.exports = nextConfig;
```

<div id="install-sdk">
  ### Instale o SDK OpenTelemetry do ClickHouse
</div>

<Tabs>
  <Tab title="NPM">
    ```shell theme={null}
    npm install @hyperdx/node-opentelemetry 
    ```
  </Tab>

  <Tab title="Yarn">
    ```shell theme={null}
    yarn add @hyperdx/node-opentelemetry 
    ```
  </Tab>
</Tabs>

<div id="create-instrumentation-files">
  ### Criar arquivos de instrumentação
</div>

Crie um arquivo chamado `instrumentation.ts` (ou `.js`) na raiz do seu projeto Next.js com o seguinte conteúdo:

```javascript theme={null}
export async function register() {
  if (process.env.NEXT_RUNTIME === 'nodejs') {
    const { init } = await import('@hyperdx/node-opentelemetry');
    init({
      apiKey: '<YOUR_INGESTION_API_KEY>', // opcionalmente configure via variável de ambiente `HYPERDX_API_KEY`
      service: '<MY_SERVICE_NAME>', // opcionalmente configure via variável de ambiente `OTEL_SERVICE_NAME`
      additionalInstrumentations: [], // opcional, padrão: []
    });
  }
}
```

Isso permitirá que o Next.js importe a instrumentação do OpenTelemetry para qualquer invocação de função serverless.

<div id="configure-environment-variables">
  ### Configure as variáveis de ambiente
</div>

Se você estiver enviando traces diretamente para o ClickStack por meio do OpenTelemetry, precisará iniciar seu
servidor Next.js com as seguintes variáveis de ambiente para direcionar os spans ao OTel collector:

<Tabs>
  <Tab title="Managed ClickStack">
    ```sh copy theme={null}
    OTEL_SERVICE_NAME=<MY_SERVICE_NAME> \
    OTEL_EXPORTER_OTLP_ENDPOINT=http://your-otel-collector:4318
    npm run dev
    ```
  </Tab>

  <Tab title="ClickStack Open Source">
    ```sh copy theme={null}
    HYPERDX_API_KEY=<YOUR_INGESTION_API_KEY> \
    OTEL_SERVICE_NAME=<MY_SERVICE_NAME> \
    OTEL_EXPORTER_OTLP_ENDPOINT=http://your-otel-collector:4318
    npm run dev
    ```
  </Tab>
</Tabs>

Se você estiver implantando na Vercel, certifique-se de que todas as variáveis de ambiente acima estejam configuradas
na sua implantação.
