logfire-query
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseQuery Logfire Data
查询Logfire数据
When to Use This Skill
何时使用此技能
Invoke this skill when:
- User wants to query traces, logs, spans, or metrics from Logfire
- User wants to search for specific events, errors, or patterns in telemetry data
- User wants to analyze OpenTelemetry data stored in Logfire
- User wants to add programmatic query capabilities to their code
- User asks to "query logfire", "search traces", "find logs", "get metrics"
- User wants to build reports or dashboards from Logfire data
在以下场景调用此技能:
- 用户想要从Logfire查询traces、logs、spans或metrics
- 用户想要在遥测数据中搜索特定事件、错误或模式
- 用户想要分析存储在Logfire中的OpenTelemetry数据
- 用户想要为其代码添加程序化查询功能
- 用户要求“查询logfire”、“搜索traces”、“查找logs”、“获取metrics”
- 用户想要基于Logfire数据构建报表或仪表板
Two Approaches
两种查询方式
| Aspect | MCP | REST API |
|---|---|---|
| Best for | Interactive exploration in Claude | Adding query code to a project |
| Auth | OAuth via MCP session | Bearer read token |
| Setup | Already configured via plugin | Need a read token |
| Formats | JSON rows | JSON, CSV, Apache Arrow |
| Default window | Last 30 min | Last 24 hours |
| Max range | 14 days | 14 days |
| Row limit | Must be in SQL | Default 500, max 10,000 |
| 维度 | MCP | REST API |
|---|---|---|
| 最佳适用场景 | 在Claude中进行交互式探索 | 为项目添加查询代码 |
| 认证方式 | 通过MCP会话进行OAuth认证 | Bearer只读令牌 |
| 设置要求 | 已通过插件配置完成 | 需要只读令牌 |
| 输出格式 | JSON行 | JSON、CSV、Apache Arrow |
| 默认时间窗口 | 最近30分钟 | 最近24小时 |
| 最大时间范围 | 14天 | 14天 |
| 行数限制 | 必须在SQL中指定 | 默认500条,最大10,000条 |
Quick Schema Reference
快速架构参考
records
table (spans and logs)
recordsrecords
表(spans和logs)
recordsKey columns for querying:
| Column | Type | Description |
|---|---|---|
| timestamp (UTC) | When span/log was created |
| timestamp (UTC) | When span/log completed |
| double (seconds) | Time between start and end; NULL for logs |
| string (32 hex) | Unique trace identifier |
| string (16 hex) | Unique span identifier |
| string (16 hex) | Parent span; NULL for root spans |
| string | Low-cardinality label for similar records |
| string | Human-readable description with arguments filled in |
| integer | Severity (supports |
| string | |
| string | Service identifier |
| boolean | Whether an exception was recorded |
| string | Exception class name |
| string | Exception message |
| string | Full traceback |
| JSON | Structured data; query with |
| string[] | Grouping labels; query with |
| integer | HTTP status code |
| string | HTTP method |
| string | HTTP route pattern |
| string | Span status |
查询常用列:
| 列名 | 类型 | 描述 |
|---|---|---|
| timestamp (UTC) | span/log的创建时间 |
| timestamp (UTC) | span/log的完成时间 |
| double (seconds) | 起止时间间隔;logs为NULL |
| string (32 hex) | 唯一追踪标识符 |
| string (16 hex) | 唯一跨度标识符 |
| string (16 hex) | 父级跨度;根跨度为NULL |
| string | 同类记录的低基数标签 |
| string | 填充参数后的可读描述信息 |
| integer | 严重程度(支持 |
| string | |
| string | 服务标识符 |
| boolean | 是否记录了异常 |
| string | 异常类名称 |
| string | 异常消息 |
| string | 完整回溯信息 |
| JSON | 结构化数据;使用 |
| string[] | 分组标签;使用 |
| integer | HTTP状态码 |
| string | HTTP方法 |
| string | HTTP路由模式 |
| string | 跨度状态 |
metrics
table
metricsmetrics
表
metrics| Column | Type | Description |
|---|---|---|
| timestamp (UTC) | When metric was recorded |
| string | Metric name |
| string | Type (gauge, counter, histogram) |
| string | Unit of measurement |
| double | Metric value |
| string | Service identifier |
| JSON | Metric dimensions |
Full schema:
references/schema.md| 列名 | 类型 | 描述 |
|---|---|---|
| timestamp (UTC) | 指标记录时间 |
| string | 指标名称 |
| string | 类型(gauge、counter、histogram) |
| string | 度量单位 |
| double | 指标数值 |
| string | 服务标识符 |
| JSON | 指标维度 |
完整架构:
references/schema.mdSQL Syntax
SQL语法
Logfire uses Apache DataFusion (Postgres-like). Key patterns:
sql
-- Time filtering
WHERE start_timestamp > now() - interval '1 hour'
-- JSON attribute access
WHERE attributes->>'user_id' = '123'
SELECT attributes->>'http.url' as url FROM records
-- Nested JSON
attributes->'request'->>'method'
-- Array filtering
WHERE array_has(tags, 'production')
-- Level filtering (string comparison works)
WHERE level = 'error'
-- Case-insensitive matching
WHERE message ILIKE '%timeout%'
-- Time bucketing for aggregation
SELECT time_bucket(interval '5 minutes', start_timestamp) as bucket,
count(*) FROM records GROUP BY bucket ORDER BY bucketLogfire使用Apache DataFusion(类Postgres)。核心用法示例:
sql
-- 时间过滤
WHERE start_timestamp > now() - interval '1 hour'
-- JSON属性访问
WHERE attributes->>'user_id' = '123'
SELECT attributes->>'http.url' as url FROM records
-- 嵌套JSON
attributes->'request'->>'method'
-- 数组过滤
WHERE array_has(tags, 'production')
-- 级别过滤(支持字符串比较)
WHERE level = 'error'
-- 不区分大小写匹配
WHERE message ILIKE '%timeout%'
-- 时间分桶聚合
SELECT time_bucket(interval '5 minutes', start_timestamp) as bucket,
count(*) FROM records GROUP BY bucket ORDER BY bucketMCP Approach (Interactive)
MCP方式(交互式)
Call the MCP tool:
query_run- (required): SQL query string
query - (optional): target project (default: user's current project)
project - /
min_timestamp(optional): ISO timestamps for time windowmax_timestamp
Default window is last 30 min. Max range is 14 days. Always include in SQL.
LIMIT调用 MCP工具:
query_run- (必填):SQL查询字符串
query - (可选):目标项目(默认:用户当前项目)
project - /
min_timestamp(可选):时间窗口的ISO时间戳max_timestamp
默认时间窗口为最近30分钟,最大范围为14天。SQL中必须包含。
LIMITCommon queries
常用查询示例
sql
-- Recent errors
SELECT start_timestamp, message, exception_type, exception_message
FROM records WHERE is_exception LIMIT 20
-- Slow spans
SELECT span_name, duration, start_timestamp
FROM records WHERE duration > 1.0 ORDER BY duration DESC LIMIT 20
-- Endpoint errors
SELECT start_timestamp, message, http_response_status_code
FROM records WHERE http_route = '/api/users' AND level = 'error' LIMIT 20
-- Full trace
SELECT span_name, message, duration, parent_span_id
FROM records WHERE trace_id = '<id>' ORDER BY start_timestamp
-- Error breakdown by service
SELECT service_name, count(*) as errors
FROM records WHERE is_exception GROUP BY service_name ORDER BY errors DESCsql
-- 近期错误
SELECT start_timestamp, message, exception_type, exception_message
FROM records WHERE is_exception LIMIT 20
-- 慢跨度
SELECT span_name, duration, start_timestamp
FROM records WHERE duration > 1.0 ORDER BY duration DESC LIMIT 20
-- 端点错误
SELECT start_timestamp, message, http_response_status_code
FROM records WHERE http_route = '/api/users' AND level = 'error' LIMIT 20
-- 完整追踪
SELECT span_name, message, duration, parent_span_id
FROM records WHERE trace_id = '<id>' ORDER BY start_timestamp
-- 按服务统计错误
SELECT service_name, count(*) as errors
FROM records WHERE is_exception GROUP BY service_name ORDER BY errors DESCREST API Approach (Programmatic)
REST API方式(程序化)
Endpoint:
GET https://logfire-api.pydantic.dev/v1/queryRegion variants:
- US:
https://logfire-us.pydantic.dev/v1/query - EU:
https://logfire-eu.pydantic.dev/v1/query
Auth:
Authorization: Bearer <read_token>Parameters:
- (required): SQL query
sql - /
min_timestamp(optional): ISO timestampsmax_timestamp - (optional): row limit (default 500, max 10,000)
limit
Response formats (via header):
Accept- — column-oriented JSON (default)
application/json - with
application/jsonparam — row-oriented JSONrow_oriented=true - — CSV
text/csv - — Apache Arrow
application/vnd.apache.arrow.stream
Python clients: (sync), (async), (PEP 249 / pandas).
LogfireQueryClientAsyncLogfireQueryClientlogfire.db_apiDetailed examples:
references/client-usage.md端点:
GET https://logfire-api.pydantic.dev/v1/query区域变体:
- 美国:
https://logfire-us.pydantic.dev/v1/query - 欧盟:
https://logfire-eu.pydantic.dev/v1/query
认证:
Authorization: Bearer <read_token>参数:
- (必填):SQL查询语句
sql - /
min_timestamp(可选):ISO时间戳max_timestamp - (可选):行数限制(默认500条,最大10,000条)
limit
响应格式(通过头指定):
Accept- — 列导向JSON(默认)
application/json - +
application/json参数 — 行导向JSONrow_oriented=true - — CSV格式
text/csv - — Apache Arrow格式
application/vnd.apache.arrow.stream
Python客户端:(同步)、(异步)、(符合PEP 249 / pandas兼容)。
LogfireQueryClientAsyncLogfireQueryClientlogfire.db_api详细示例:
references/client-usage.mdQuery Best Practices
查询最佳实践
- Always LIMIT — start with 20, increase as needed
- Use /
min_timestampparams for simple time windows instead of SQLmax_timestampWHERE - Filter efficiently — ,
service_name,span_name,trace_idare fast filtersis_exception - Use for JSON attribute access (returns text); use
->>'key'for nested JSON objects-> - Avoid — select only the columns you need
SELECT * - Max 14-day range — queries cannot span more than 14 days
- 始终使用LIMIT — 从20条开始,按需增加
- 使用/
min_timestamp参数处理简单时间窗口,而非SQL的max_timestamp子句WHERE - 高效过滤 — 、
service_name、span_name、trace_id为快速过滤字段is_exception - **使用**访问JSON属性(返回文本);使用
->>'key'访问嵌套JSON对象-> - 避免— 仅选择所需列
SELECT * - 最大14天范围 — 查询时间跨度不能超过14天