Hi all,

I've submitted a PR that adds an OTLP(OpenTelemetry Protocol)/HTTP receiver
to IoTDB's REST service, allowing developers to store telemetry data from
AI coding assistants (Claude Code, Codex, Gemini CLI, etc.) directly into a
local IoTDB instance for personal analytics.

PR: https://github.com/apache/iotdb/pull/17516

## Why This Matters

Modern AI coding tools like Claude Code emit rich telemetry via
OpenTelemetry — token usage, API costs, tool execution stats, and session
events. By routing this data into IoTDB, you can:

- Track token consumption across models (input / output / cacheRead /
cacheCreation)
- Monitor daily/weekly API costs
- Analyze tool success rates and latencies
- Correlate events within a single prompt via prompt.id

## Quick Setup (Claude Code Example)

1. Enable REST service in `iotdb-system.properties`:

```properties
enable_rest_service=true
rest_service_port=18080
```

2. Add these environment variables to `~/.zshrc` or `~/.bashrc`:

```bash
export CLAUDE_CODE_ENABLE_TELEMETRY=1
export OTEL_METRICS_EXPORTER=otlp
export OTEL_LOGS_EXPORTER=otlp
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:18080/rest/v1/otlp
```

3. Start IoTDB, open a new terminal, run `claude`. That's it — data flows
automatically.

4. Query examples:

```sql
-- Token usage by model
USE claude_code;
SELECT model, type, sum(value) FROM metrics
WHERE metric_name = 'claude_code.token.usage'
GROUP BY model, type;

-- Daily cost
SELECT date_bin(1d, time) AS day, sum(value) AS cost
FROM metrics WHERE metric_name = 'claude_code.cost.usage'
GROUP BY 1 ORDER BY 1;

-- Tool execution stats
SELECT tool_name, count_if(success = 'true') AS ok, count(*) AS total
FROM logs WHERE event_name = 'tool_result'
GROUP BY tool_name;
```

## Design Highlights

- **Zero extra infrastructure**: reuses the existing REST service (port
18080), no gRPC or Collector needed
- **Dynamic database routing**: database name is derived from `service.name`
attribute — `claude-code` → `claude_code`, `codex` → `codex`, etc.
- **Structured schema**: OTLP attributes are flattened into typed
TAG/ATTRIBUTE/FIELD columns, not stored as JSON blobs
- **Auto schema creation**: database and tables are created on first ingest

Feedback welcome — especially on the table schema design and whether this
should support additional OTLP features.


---

大家好,

我提交了一个 PR,为 IoTDB 的 REST 服务添加了 OTLP(OpenTelemetry Protocol)/HTTP
接收器,使开发者可以将 AI 编程助手(Claude Code、Codex、Gemini CLI 等)的遥测数据直接存入本地 IoTDB
实例,用于个人使用分析。

PR: https://github.com/apache/iotdb/pull/17516

## 为什么有用

Claude Code 等 AI 编程工具通过 OpenTelemetry 协议发送丰富的遥测数据——Token 用量、API
成本、工具执行统计和会话事件。将这些数据路由到 IoTDB 后,你可以:

- 追踪各模型的 Token 消耗(input / output / cacheRead / cacheCreation)
- 监控每日/每周 API 花费
- 分析工具调用成功率和延迟
- 通过 prompt.id 关联单次提示下的所有事件

## 快速配置(以 Claude Code 为例)

1. 在 `iotdb-system.properties` 中启用 REST 服务:

```properties
enable_rest_service=true
rest_service_port=18080
```

2. 在 `~/.zshrc` 或 `~/.bashrc` 中添加以下环境变量:

```bash
export CLAUDE_CODE_ENABLE_TELEMETRY=1
export OTEL_METRICS_EXPORTER=otlp
export OTEL_LOGS_EXPORTER=otlp
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:18080/rest/v1/otlp
```

3. 启动 IoTDB,打开新终端,运行 `claude`。数据会自动流入。

4. 查询示例:

```sql
-- 按模型统计 Token 用量
USE claude_code;
SELECT model, type, sum(value) FROM metrics
WHERE metric_name = 'claude_code.token.usage'
GROUP BY model, type;

-- 每日花费
SELECT date_bin(1d, time) AS day, sum(value) AS cost
FROM metrics WHERE metric_name = 'claude_code.cost.usage'
GROUP BY 1 ORDER BY 1;

-- 工具执行统计
SELECT tool_name, count_if(success = 'true') AS ok, count(*) AS total
FROM logs WHERE event_name = 'tool_result'
GROUP BY tool_name;
```

## 设计亮点

- **零额外基础设施**:复用已有 REST 服务(18080 端口),无需 gRPC 或 Collector
- **动态数据库路由**:根据 `service.name` 属性自动推导数据库名——`claude-code` →
`claude_code`、`codex` → `codex`
- **结构化 Schema**:OTLP 属性被展平为带类型的 TAG/ATTRIBUTE/FIELD 列,而非存为 JSON 字符串
- **自动建表**:首次写入时自动创建数据库和表

欢迎反馈——特别是关于表结构设计以及是否需要支持更多 OTLP 特性。

Best regards,
----------------
Yuan Tian

Reply via email to