> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-parallel-read-in-order-multi-part.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> SDK Next.js pour ClickStack - La stack d’observabilité ClickHouse

# Next.js

export const ClickStackIntegrates = ({signals = []}) => {
  const list = Array.isArray(signals) ? signals : String(signals).split(",");
  const supported = list.map(s => String(s).trim().toLowerCase());
  const pillStyle = {
    display: "inline-flex",
    alignItems: "center",
    padding: "0.125rem 0.625rem",
    borderRadius: "9999px",
    fontSize: "0.8125rem",
    fontWeight: 500,
    lineHeight: 1.6,
    color: "#161517",
    backgroundColor: "#faff69",
    border: "1px solid rgba(0, 0, 0, .1)",
    marginRight: "0.5rem"
  };
  return <div style={{
    margin: "0 0 1.25rem"
  }}>
      <span style={{
    marginRight: "0.5rem"
  }}>Ce guide intègre :</span>
      {supported.includes("logs") && <span style={pillStyle}>Logs</span>}
      {supported.includes("metrics") && <span style={pillStyle}>Métriques</span>}
      {supported.includes("traces") && <span style={pillStyle}>Traces</span>}
    </div>;
};

ClickStack peut ingérer des traces OpenTelemetry natives à partir de vos
[fonctions serverless Next.js](https://nextjs.org/docs/pages/building-your-application/optimizing/open-telemetry#manual-opentelemetry-configuration)
avec Next 13.2 et versions ultérieures.

<ClickStackIntegrates signals={['logs', 'traces']} />

<Note>
  Si vous recherchez la session replay ou la supervision côté navigateur, installez plutôt l’[intégration Browser](/fr/clickstack/ingesting-data/sdks/browser).
</Note>

<h2 id="installing">
  Installation
</h2>

<h3 id="enable-instrumentation-hook">
  Activer le hook d’instrumentation (obligatoire pour la v15 et les versions antérieures)
</h3>

Pour commencer, vous devez activer le hook d’instrumentation de Next.js en définissant `experimental.instrumentationHook = true;` dans votre fichier `next.config.js`.

**Exemple :**

```javascript theme={null}
const nextConfig = {
  experimental: {
    instrumentationHook: true,
  },
  // Ignore otel pkgs warnings 
  // 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;
```

<h3 id="install-sdk">
  Installer le SDK OpenTelemetry de ClickHouse
</h3>

<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>

<h3 id="create-instrumentation-files">
  Créer les fichiers d’instrumentation
</h3>

Créez un fichier nommé `instrumentation.ts` (ou `.js`) à la racine de votre projet Next.js avec le contenu suivant :

```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>', // optionally configure via `HYPERDX_API_KEY` env var
      service: '<YOUR_SERVICE_NAME>', // optionally configure via `OTEL_SERVICE_NAME` env var
      additionalInstrumentations: [], // optional, default: []
    });
  }
}
```

Cela permettra à Next.js d’importer l’instrumentation OpenTelemetry pour chaque invocation d’une fonction serverless.

<h3 id="configure-environment-variables">
  Configurer les variables d'environnement
</h3>

Si vous envoyez des traces directement à ClickStack via OpenTelemetry, vous devrez démarrer votre serveur Next.js
avec les variables d'environnement suivantes afin d'envoyer les spans vers l’OTel collector :

<Tabs>
  <Tab title="Managed ClickStack">
    ```sh copy theme={null}
    OTEL_SERVICE_NAME=<YOUR_SERVICE_NAME> \
    OTEL_EXPORTER_OTLP_ENDPOINT=<YOUR_OTEL_COLLECTOR_HTTP_ENDPOINT>
    npm run dev
    ```
  </Tab>

  <Tab title="ClickStack Open Source">
    ```sh copy theme={null}
    HYPERDX_API_KEY=<YOUR_INGESTION_API_KEY> \
    OTEL_SERVICE_NAME=<YOUR_SERVICE_NAME> \
    OTEL_EXPORTER_OTLP_ENDPOINT=<YOUR_OTEL_COLLECTOR_HTTP_ENDPOINT>
    npm run dev
    ```
  </Tab>
</Tabs>

Si vous effectuez le déploiement sur Vercel, assurez-vous que toutes les variables d'environnement ci-dessus sont bien configurées
pour votre déploiement.
