Loading...
Loading...
Compare original and translation side by side
configure()instrument_*()configure()instrument_*()pyproject.tomlrequirements.txtpackage.jsonCargo.tomlpyproject.tomlrequirements.txtpackage.jsonCargo.tomllogfireinstrument_*()uv add 'logfire[fastapi,httpx,asyncpg]'fastapistarlettedjangoflaskhttpxrequestsasyncpgpsycopgpsycopg2sqlalchemyredispymongomysqlsqlite3celeryaiohttpaws-lambdasystem-metricslitellmdspygoogle-genailogfireinstrument_*()uv add 'logfire[fastapi,httpx,asyncpg]'fastapistarlettedjangoflaskhttpxrequestsasyncpgpsycopgpsycopg2sqlalchemyredispymongomysqlsqlite3celeryaiohttpaws-lambdasystem-metricslitellmdspygoogle-genailogfire.configure()instrument_*()instrument_*()configure()import logfirelogfire.configure()instrument_*()configure()instrument_*()import logfire
Placement rules:
- `logfire.configure()` goes in the application entry point (`main.py`, or the module that creates the app)
- Call it **once per process** - not inside request handlers, not in library code
- `instrument_*()` calls go right after `configure()`
- Web framework instrumentors (`instrument_fastapi`, `instrument_flask`, `instrument_django`) need the app instance as an argument. HTTP client and database instrumentors (`instrument_httpx`, `instrument_asyncpg`) are global and take no arguments.
- In **Gunicorn** deployments, call `logfire.configure()` inside the `post_fork` hook, not at module level - each worker is a separate process
放置规则:
- `logfire.configure()`放在应用入口文件(`main.py`,或创建应用的模块)
- **每个进程仅调用一次** - 不要在请求处理函数内调用,也不要在库代码中调用
- `instrument_*()`调用紧跟在`configure()`之后
- Web框架埋点方法(`instrument_fastapi`、`instrument_flask`、`instrument_django`)需要传入应用实例作为参数。HTTP客户端和数据库埋点方法(`instrument_httpx`、`instrument_asyncpg`)是全局的,不需要参数。
- 在**Gunicorn**部署场景下,在`post_fork`钩子内调用`logfire.configure()`,不要在模块顶层调用——每个工作进程都是独立的进程。print()logging.*(){key}undefinedprint()logging.*(){key}undefined
For grouping related operations and measuring duration, use spans:
```python
with logfire.span("Processing order {order_id}", order_id=order_id):
items = await fetch_items(order_id)
total = calculate_total(items)
logfire.info("Calculated total {total}", total=total)logfire.exception()try:
await process_order(order_id)
except Exception:
logfire.exception("Failed to process order {order_id}", order_id=order_id)
raise
如果需要对相关操作分组并统计耗时,可以使用跨度(span):
```python
with logfire.span("Processing order {order_id}", order_id=order_id):
items = await fetch_items(order_id)
total = calculate_total(items)
logfire.info("Calculated total {total}", total=total)logfire.exception()try:
await process_order(order_id)
except Exception:
logfire.exception("Failed to process order {order_id}", order_id=order_id)
raiseuv add 'logfire[pydantic-ai]'uv add 'logfire[pydantic-ai]'
Available AI extras: `pydantic-ai`, `openai`, `anthropic`, `litellm`, `dspy`, `google-genai`.
```python
logfire.configure()
logfire.instrument_pydantic_ai() # captures agent runs, tool calls, LLM request/response
可用的AI扩展:`pydantic-ai`、`openai`、`anthropic`、`litellm`、`dspy`、`google-genai`。
```python
logfire.configure()
logfire.instrument_pydantic_ai() # 捕获Agent运行、工具调用、LLM请求/响应
For PydanticAI, each agent run becomes a parent span containing child spans for every tool call and LLM request.
---
对于PydanticAI,每个Agent运行都会成为父跨度,包含所有工具调用和LLM请求的子跨度。
---undefinedundefinedundefinedundefinedinstrumentation.tsimport * as logfire from '@pydantic/logfire-node'
logfire.configure()node --require ./instrumentation.js app.jsLOGFIRE_TOKENtokenconfigure()instrument()import { instrument } from '@pydantic/logfire-cf-workers'
export default instrument(handler, {
service: { name: 'my-worker', version: '1.0.0' }
})OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://logfire-api.pydantic.dev/v1/traces
OTEL_EXPORTER_OTLP_HEADERS=Authorization=<your-write-token>instrumentation.tsimport * as logfire from '@pydantic/logfire-node'
logfire.configure()node --require ./instrumentation.js app.jsLOGFIRE_TOKENconfigure()tokeninstrument()import { instrument } from '@pydantic/logfire-cf-workers'
export default instrument(handler, {
service: { name: 'my-worker', version: '1.0.0' }
})OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://logfire-api.pydantic.dev/v1/traces
OTEL_EXPORTER_OTLP_HEADERS=Authorization=<your-write-token>// Structured attributes as second argument
logfire.info('Created user', { user_id: uid })
logfire.error('Payment failed', { amount: 100, currency: 'USD' })
// Spans
logfire.span('Processing order', { order_id }, {}, async () => {
logfire.info('Processing step completed')
})
// Error reporting
logfire.reportError('order processing', error)tracedebuginfonoticewarnerrorfatal// 结构化属性作为第二个参数
logfire.info('Created user', { user_id: uid })
logfire.error('Payment failed', { amount: 100, currency: 'USD' })
// 跨度
logfire.span('Processing order', { order_id }, {}, async () => {
logfire.info('Processing step completed')
})
// 错误上报
logfire.reportError('order processing', error)tracedebuginfonoticewarnerrorfatal[dependencies]
logfire = "0.6"[dependencies]
logfire = "0.6"let shutdown_handler = logfire::configure()
.install_panic_handler()
.finish()?;LOGFIRE_TOKENlet shutdown_handler = logfire::configure()
.install_panic_handler()
.finish()?;LOGFIRE_TOKENtracingopentelemetrytracing// Spans
logfire::span!("processing order", order_id = order_id).in_scope(|| {
// traced code
});
// Events
logfire::info!("Created user {user_id}", user_id = uid);shutdown_handler.shutdown()tracingopentelemetrytracing// 跨度
logfire::span!("processing order", order_id = order_id).in_scope(|| {
// 被埋点的代码
});
// 事件
logfire::info!("Created user {user_id}", user_id = uid);shutdown_handler.shutdown()logfire authLOGFIRE_TOKENconfigure()instrument_*()LOGFIRE_TOKENlogfire authLOGFIRE_TOKENconfigure()instrument_*()LOGFIRE_TOKEN${CLAUDE_PLUGIN_ROOT}/skills/instrumentation/references/python/logging-patterns.md${CLAUDE_PLUGIN_ROOT}/skills/instrumentation/references/python/integrations.md${CLAUDE_PLUGIN_ROOT}/skills/instrumentation/references/javascript/patterns.md${CLAUDE_PLUGIN_ROOT}/skills/instrumentation/references/javascript/frameworks.md${CLAUDE_PLUGIN_ROOT}/skills/instrumentation/references/rust/patterns.md${CLAUDE_PLUGIN_ROOT}/skills/instrumentation/references/python/logging-patterns.md${CLAUDE_PLUGIN_ROOT}/skills/instrumentation/references/python/integrations.md${CLAUDE_PLUGIN_ROOT}/skills/instrumentation/references/javascript/patterns.md${CLAUDE_PLUGIN_ROOT}/skills/instrumentation/references/javascript/frameworks.md${CLAUDE_PLUGIN_ROOT}/skills/instrumentation/references/rust/patterns.md