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

> ClickStack 的 Next.js SDK - 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"
  }}>本指南集成了：</span>
      {supported.includes("logs") && <span style={pillStyle}>日志</span>}
      {supported.includes("metrics") && <span style={pillStyle}>指标</span>}
      {supported.includes("traces") && <span style={pillStyle}>链路追踪</span>}
    </div>;
};

在 Next 13.2 及以上版本中，ClickStack 可以从您的
[Next.js 无服务器函数](https://nextjs.org/docs/pages/building-your-application/optimizing/open-telemetry#manual-opentelemetry-configuration)
摄取原生 OpenTelemetry 链路追踪。

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

<Note>
  如果您需要会话回放/浏览器端监控，请安装 [Browser integration](/zh/clickstack/ingesting-data/sdks/browser)。
</Note>

<h2 id="installing">
  安装
</h2>

<h3 id="enable-instrumentation-hook">
  启用 插桩 hook (v15 及以下版本必需)
</h3>

首先，你需要在 `next.config.js` 中将 `experimental.instrumentationHook` 设置为 `true;`，以启用 Next.js 的 插桩 hook。

**示例：**

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

<h3 id="install-sdk">
  安装 ClickHouse OpenTelemetry SDK
</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">
  创建 插桩 文件
</h3>

在 Next.js 项目根目录中创建一个名为 `instrumentation.ts` (或 `.js`) 的文件，内容如下：

```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: []
    });
  }
}
```

这将使 Next.js 能够在调用任何无服务器函数时导入 OpenTelemetry 插桩。

<h3 id="configure-environment-variables">
  配置环境变量
</h3>

如果你通过 OpenTelemetry 将链路追踪数据直接发送到 ClickStack，则需要使用以下环境变量启动 Next.js
服务器，以便将 spans 发送到 OTel collector：

<Tabs>
  <Tab title="托管 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 开源版">
    ```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>

如果你部署在 Vercel 上，请确保已为该部署
配置上述所有环境变量。
