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

# 借助 AI 进行插桩

> 借助编程助手和各语言 SDK 指南为应用程序插桩，将其接入 ClickHouse Observability。

使用编程助手，参照应用程序所用语言或框架对应的指南为其添加插桩。编程助手可以识别入口点、安装合适的 SDK，并配置遥测数据导出。

自动埋点可以捕获受支持的库和框架的数据。视所用语言和应用程序而定，您可能仍需修改代码或添加自定义 span 来捕获业务操作。部署前，请先审查生成的更改。

## 开始之前

您需要准备好应用程序源代码、一个可访问该代码仓库的编程助手，以及一个 ClickHouse Observability 目标端。请根据您的应用程序选择相应的[语言 SDK 指南](https://clickhouse.com/docs/zh/clickstack/observability/ingesting-data/sdks.md)，其中列出了应使用的 SDK 软件包、支持的信号以及导出器设置。

## 配置目标端

> [tab] 全托管
>
> [Beta 版服务](https://clickhouse.com/docs/reference/settings/beta-and-experimental-features.md#beta-features)
>
> 打开 **团队设置 → 数据 → Managed Telemetry**，复制 OTLP HTTP 端点和摄取 API key。将它们保存在不纳入版本控制的环境文件中，或保存到您的 Secret 存储中。
>
> ![Managed Telemetry 页面，显示已遮蔽的 OTLP HTTP 端点和 API key](https://mintcdn.com/private-7c7dfe99/Zk1feNn6fCQBpold/images/clickstack/getting-started/observability-managed-telemetry.png?fit=max\&auto=format\&n=Zk1feNn6fCQBpold\&q=85\&s=e63a2ac2e6ab996d3fc360ebe0f487a5)
>
> 如果您的服务显示 **入门指南** 页面，也可以在 **借助 AI 进行插桩** 下选择 **Copy prompt**。从 **Set it up with an AI agent** 中复制提示词，并设置该对话框中显示的 `CLICKSTACK_OTLP_ENDPOINT` 和 `CLICKSTACK_API_KEY` 变量，然后完成下文其余的检查和验证步骤。

> [tab] 自管理
>
> 您需要一个正在运行且已连接到 ClickHouse Cloud 服务的 OpenTelemetry 采集器。请参阅 [使用 OpenTelemetry 摄取数据](https://clickhouse.com/docs/zh/clickstack/observability/ingesting-data/opentelemetry.md#self-managed) 配置采集器。
>
> 使用采集器的 OTLP 端点及其已配置的摄取 key。有关身份验证格式和导出器设置，请参阅相应语言 SDK 指南中的 **自管理** 选项卡。

## 向编程助手提供语言指南

在编程助手 (例如 Claude Code、Cursor 或 Codex) 中打开您的应用程序代码仓库。复制以下提示词，并将其中的占位符替换为您的语言指南 URL、部署类型和服务名称。请在提示词中填写环境变量的名称，切勿直接粘贴凭证。

```text wrap theme={null}
Instrument this application to send telemetry to ClickHouse Observability.

Language or framework guide: <LANGUAGE_SDK_GUIDE_URL>
Destination: <fully managed or self-managed>
Service name: <SERVICE_NAME>
Endpoint environment variable: <ENDPOINT_VARIABLE_NAME>
Ingestion key environment variable: <API_KEY_VARIABLE_NAME>

Read the linked guide and use the instructions for my destination.
Inspect the application's language, framework, dependencies, and startup
commands before making changes. If you cannot read the guide, ask me for
its contents rather than guessing SDK names or configuration.

Reuse existing OpenTelemetry instrumentation where present. Otherwise,
install the SDK and supported automatic instrumentation described in the
guide. Initialize instrumentation at the point required by that SDK.
Use the guide's protocol, endpoint, and authentication format, reading
connection values from the environment. Do not hardcode or print secrets.

Preserve the application's existing behavior. Check for duplicate SDK
initialization, exporters, and logging handlers, including those installed
by automatic instrumentation. If logs are supported, check both the
application's logging level and the SDK's export level so the test log
is captured once. Use the language guide's settings for these checks.

Explain which signals are supported and any operations that need manual
instrumentation. Do not assume every SDK exports logs, metrics, traces,
or session replay.

Update the startup command and document the required environment variables.
Run the application's existing checks and a local startup check. Exercise
an instrumented operation with a unique test marker. If logs are supported,
emit one test log during that operation at an enabled severity. Allow time
for batched export or flush telemetry before shutting down.

When you have destination access, find the test events there and verify
the service name, marker, and absence of duplicates. If log-to-trace
correlation is supported, check that the test log references the operation's
trace and span. Distinguish local export, endpoint acceptance, and events
stored in ClickHouse Observability; startup or an HTTP success response
alone does not prove storage.

Summarize the changes, tests performed, and results. Clearly state which
checks you could not run and give me the remaining verification steps.
```

此提示词中的变量名称用于标识连接值的存储位置，并不能替代 SDK 所需的变量：助手必须将它们映射到对应语言指南中记录的设置。对于浏览器应用程序，请参阅[浏览器 SDK 指南](https://clickhouse.com/docs/zh/clickstack/observability/ingesting-data/sdks/browser.md)，其中包括浏览器专用的端点和密钥配置。

## 检查并运行应用程序

检查依赖项、初始化和启动相关的更改。确认 AI 助手选择了正确的目标端和身份验证格式，保留了现有的插桩，并且没有将 Secret 写入已提交的文件。

先运行应用程序现有的测试，然后使用更新后的命令和环境启动应用程序。发起一个请求或执行一项操作，以调用已插桩的库。如果需要为自动埋点未捕获的操作采集遥测数据，可以让 AI 助手使用 SDK 的 API 添加自定义跨度。

## 验证遥测数据

1. 在 ClickHouse Observability 中打开[搜索](https://clickhouse.com/docs/zh/clickstack/observability/features/search.md)。
2. 选择相应的日志或链路追踪 source，并将时间范围设置为覆盖测试活动的时段。
3. 按服务名称进行过滤，查看生成的事件，确认预期的请求或操作已出现。
4. 如果 SDK 导出了指标，请使用 [Chart Explorer](https://clickhouse.com/docs/zh/clickstack/observability/features/dashboards/overview.md#navigate-chart-explorer) 查看这些指标。

应用程序成功启动并不意味着遥测数据已成功送达。如果缺少事件，请检查应用程序的导出器错误、端点、协议以及身份验证设置。对于自管理部署，还应检查采集器日志。

如需完整的应用程序示例，请参阅 [HackerNews Analyzer 演示](https://clickhouse.com/docs/zh/clickstack/observability/example-datasets/instrument-application.md)。
