sentry-elixir-sdk by getsentry/sentry-for-ai
npx skills add https://github.com/getsentry/sentry-for-ai --skill sentry-elixir-sdk一个开箱即用的向导,它会扫描你的 Elixir 项目并指导你完成完整的 Sentry 设置。
sentry hex 包、getsentry/sentry-elixir 或 Elixir Sentry SDK注意: 下面的 SDK 版本和 API 反映了撰写时的 Sentry 文档(sentry v12.0.2,要求 Elixir ~> 1.13)。在实施前,请务必对照 docs.sentry.io/platforms/elixir/ 进行验证。
在提出任何建议之前,运行以下命令来了解项目:
# 检查现有的 Sentry 依赖
grep -i sentry mix.exs 2>/dev/null
# 检测 Elixir 版本
cat .tool-versions 2>/dev/null | grep elixir
grep "elixir:" mix.exs 2>/dev/null
# 检测 Phoenix 或 Plug
grep -E '"phoenix"|"plug"' mix.exs 2>/dev/null
# 检测 Phoenix LiveView
grep "phoenix_live_view" mix.exs 2>/dev/null
# 检测 Oban(作业队列 / 定时任务)
grep "oban" mix.exs 2>/dev/null
# 检测 Quantum(定时任务调度器)
grep "quantum" mix.exs 2>/dev/null
# 检测 OpenTelemetry 使用情况
grep "opentelemetry" mix.exs 2>/dev/null
# 检查配套的前端
ls assets/ frontend/ web/ client/ 2>/dev/null
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
需要注意的事项:
| 信号 | 影响 |
|---|---|
mix.exs 中已有 sentry? | 跳过安装;进入阶段 2(配置功能) |
| 检测到 Phoenix? | 添加 Sentry.PlugCapture、Sentry.PlugContext,可选添加 Sentry.LiveViewHook |
| 检测到 LiveView? | 在 my_app_web.ex 的 live_view 宏中添加 Sentry.LiveViewHook |
| 检测到 Oban? | 建议通过 Oban 集成实现定时任务和错误捕获 |
| 检测到 Quantum? | 建议通过 Quantum 集成实现定时任务 |
| 已存在 OpenTelemetry? | 追踪设置仅需 Sentry.OpenTelemetry.* 配置 |
| 找到前端目录? | 触发阶段 4 的交叉链接建议 |
根据你的发现,提出具体的建议。不要问开放式问题——直接给出方案:
推荐(核心覆盖范围):
Sentry.LoggerHandler 将崩溃报告和错误日志转发到 Sentry可选(增强可观测性):
推荐逻辑:
| 功能 | 在以下情况推荐... |
|---|---|
| 错误监控 | 始终 — 不可或缺的基线 |
| 日志记录 | 始终 — LoggerHandler 捕获非显式 capture_exception 调用的崩溃 |
| 追踪 | 检测到 Phoenix、Plug、Ecto 或 OpenTelemetry 导入 |
| 定时任务 | 检测到 Oban、Quantum 或周期性的 GenServer/Task 模式 |
| Sentry 日志 | 使用 sentry v12.0.0+ 且需要结构化日志搜索 |
建议:“我建议设置错误监控 + 日志记录 [+ 如果检测到 Phoenix/Ecto 则加上追踪]。需要我同时添加定时任务或 Sentry 日志吗?”
你需要自己运行这个 — Igniter 安装器需要交互式终端输入,这是代理无法处理的。请复制粘贴到你的终端:
mix igniter.install sentry自 sentry v11.0.0 起可用。它会自动配置
config/config.exs、config/prod.exs、config/runtime.exs和lib/my_app/application.ex。一旦完成,请返回并跳转到验证部分。
如果用户跳过 Igniter 安装器,请继续下面的选项 2(手动设置)。
添加到 mix.exs 依赖项:
# mix.exs
defp deps do
[
{:sentry, "~> 12.0"},
{:finch, "~> 0.21"}
# 如果使用 Elixir < 1.18,添加 jason:
# {:jason, "~> 1.4"},
]
end
mix deps.get
# config/config.exs
config :sentry,
dsn: System.get_env("SENTRY_DSN"),
environment_name: config_env(),
enable_source_code_context: true,
root_source_code_paths: [File.cwd!()],
in_app_otp_apps: [:my_app]
对于运行时配置(推荐用于 DSN 和 release):
# config/runtime.exs
import Config
config :sentry,
dsn: System.fetch_env!("SENTRY_DSN"),
release: System.get_env("SENTRY_RELEASE", "my-app@#{Application.spec(:my_app, :vsn)}")
此配置以合理的默认值启用最多的功能:
# config/config.exs
config :sentry,
dsn: System.get_env("SENTRY_DSN"),
environment_name: config_env(),
enable_source_code_context: true,
root_source_code_paths: [File.cwd!()],
in_app_otp_apps: [:my_app],
# Logger 处理器配置 — 捕获崩溃报告
logger: [
{:handler, :sentry_handler, Sentry.LoggerHandler, %{
config: %{
metadata: [:request_id],
capture_log_messages: true,
level: :error
}
}}
]
在 Application.start/2 中添加 Logger.add_handlers/1:
# lib/my_app/application.ex
def start(_type, _args) do
Logger.add_handlers(:my_app) # 激活上面配置的 :sentry_handler
children = [
MyAppWeb.Endpoint
# ... 其他子进程
]
Supervisor.start_link(children, strategy: :one_for_one)
end
lib/my_app_web/endpoint.ex
defmodule MyAppWeb.Endpoint do
use Sentry.PlugCapture # 在 use Phoenix.Endpoint 之上添加(仅限 Cowboy 适配器)
use Phoenix.Endpoint, otp_app: :my_app
# ...
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Phoenix.json_library()
plug Sentry.PlugContext # 在 Plug.Parsers 之下添加
# ...
end
注意:
Sentry.PlugCapture仅对 Cowboy 适配器是必需的。Phoenix 1.7+ 默认使用 Bandit,此时PlugCapture无害但非必需。Sentry.PlugContext始终推荐使用 — 它用 HTTP 请求数据丰富了事件。
LiveView 错误 — lib/my_app_web.ex
def live_view do
quote do
use Phoenix.LiveView
on_mount Sentry.LiveViewHook # 捕获 mount/handle_event/handle_info 中的错误
end
end
defmodule MyApp.Router do
use Plug.Router
use Sentry.PlugCapture # 仅限 Cowboy
plug Plug.Parsers, parsers: [:urlencoded, :multipart]
plug Sentry.PlugContext
# ...
end
逐个功能进行指导。为每个功能加载参考文件,按照其步骤操作,并在继续下一个之前进行验证:
| 功能 | 参考文件 | 何时加载 |
|---|---|---|
| 错误监控 | ${SKILL_ROOT}/references/error-monitoring.md | 始终(基线) |
| 追踪 | ${SKILL_ROOT}/references/tracing.md | 检测到 Phoenix / Ecto / OpenTelemetry |
| 日志记录 | ${SKILL_ROOT}/references/logging.md | LoggerHandler 或 Sentry 日志设置 |
| 定时任务 | ${SKILL_ROOT}/references/crons.md | 检测到 Oban、Quantum 或周期性任务 |
对于每个功能:读取 ${SKILL_ROOT}/references/<功能>.md,严格按照步骤操作,验证其是否工作。
| 选项 | 类型 | 默认值 | 用途 |
|---|---|---|---|
:dsn | `string | nil` | nil |
:environment_name | `atom | string` | "production" |
:release | `string | nil` | nil |
:sample_rate | float | 1.0 | 错误事件采样率(0.0–1.0) |
:enable_source_code_context | boolean | false | 包含错误周围的源代码行 |
:root_source_code_paths | [path] | [] | 启用源代码上下文时必需 |
:in_app_otp_apps | [atom] | [] | 其模块在堆栈跟踪中属于“应用内”的 OTP 应用 |
:before_send | `(event -> event | nil) | {m, f}` |
:after_send_event | `(event, result -> any) | {m, f}` | nil |
:filter | module | Sentry.DefaultEventFilter | 实现 Sentry.EventFilter 的模块 |
:max_breadcrumbs | integer | 100 | 每个进程的最大面包屑数量 |
:dedup_events | boolean | true | 在约 30 秒内对相同事件进行去重 |
:tags | map | %{} | 随每个事件发送的全局标签 |
:traces_sample_rate | `float | nil` | nil |
:traces_sampler | `fn | {m, f} | nil` |
:enable_logs | boolean | false | 启用 Sentry 日志协议(v12.0.0+) |
:test_mode | boolean | false | 在内存中捕获事件用于测试 |
| 变量 | 映射到 | 用途 |
|---|---|---|
SENTRY_DSN | :dsn | 数据源名称 |
SENTRY_RELEASE | :release | 应用版本(例如 my-app@1.0.0) |
SENTRY_ENVIRONMENT | :environment_name | 部署环境 |
测试 Sentry 是否正在接收事件:
# 从你的项目发送一个测试事件
MIX_ENV=dev mix sentry.send_test_event
或者在控制器动作中添加一个临时调用:
# 临时测试 — 确认后删除
def index(conn, _params) do
Sentry.capture_message("Sentry Elixir SDK 测试事件")
text(conn, "已发送")
end
在几秒钟内检查 Sentry 仪表板。如果没有出现任何内容:
config :sentry, log_level: :debug 以获取详细的 SDK 输出SENTRY_DSN 是否已设置且项目存在:environment_name 未设置为你的警报规则中 Sentry 过滤掉的值完成 Elixir 设置后,检查配套的前端是否缺少 Sentry 覆盖:
ls assets/ frontend/ web/ client/ ui/ 2>/dev/null
cat assets/package.json frontend/package.json 2>/dev/null | grep -E '"react"|"svelte"|"vue"|"next"'
如果存在前端目录但未配置 Sentry,建议使用匹配的技能:
| 检测到的前端 | 建议技能 |
|---|---|
| React / Next.js | sentry-react-sdk 或 sentry-nextjs-sdk |
| Svelte / SvelteKit | sentry-svelte-sdk |
| Vue | 参见 docs.sentry.io/platforms/javascript/guides/vue/ |
| 其他 JS/TS | sentry-browser-sdk |
将 Phoenix 后端和 JavaScript 前端与链接的 Sentry 项目连接起来,可以实现分布式追踪 — 在一个追踪视图中跨越浏览器、Phoenix HTTP 服务器和下游服务的堆栈跟踪。
| 问题 | 解决方案 |
|---|---|
| 事件未出现 | 验证 SENTRY_DSN 是否已设置;运行 mix sentry.send_test_event;设置 log_level: :debug |
| 捕获的异常缺少堆栈跟踪 | 在 rescue 块中传递 stacktrace: __STACKTRACE__:Sentry.capture_exception(e, stacktrace: __STACKTRACE__) |
PlugCapture 在 Bandit 上不工作 | Sentry.PlugCapture 仅限 Cowboy;使用 Bandit 时,错误通过 LoggerHandler 呈现 |
| 生产环境中缺少源代码上下文 | 在构建 OTP 发布版本之前运行 mix sentry.package_source_code |
| 异步事件上未出现上下文 | Sentry.Context.* 是进程作用域的;显式传递值或在进程间传播 Logger 元数据 |
| Oban 集成未报告定时任务 | 需要 Oban v2.17.6+ 或 Oban Pro;定时任务必须在作业元数据中设置 "cron" => true |
| Cowboy 崩溃导致的重复事件 | 在 LoggerHandler 配置中设置 excluded_domains: [:cowboy](这是默认设置) |
finch 未启动 | 确保 {:finch, "~> 0.21"} 在依赖项中;自 v12.0.0 起,Finch 是默认的 HTTP 客户端 |
| JSON 编码错误 | 对于 Elixir < 1.18,添加 {:jason, "~> 1.4"} 并设置 json_library: Jason |
每周安装次数
87
代码仓库
GitHub 星标数
82
首次出现
14 天前
安全审计
安装于
gemini-cli86
cursor86
codex86
kimi-cli85
github-copilot85
amp85
All Skills > SDK Setup > Elixir SDK
Opinionated wizard that scans your Elixir project and guides you through complete Sentry setup.
sentry hex package, getsentry/sentry-elixir, or Elixir Sentry SDKNote: SDK versions and APIs below reflect Sentry docs at time of writing (sentry v12.0.2, requires Elixir ~> 1.13). Always verify against docs.sentry.io/platforms/elixir/ before implementing.
Run these commands to understand the project before making any recommendations:
# Check existing Sentry dependency
grep -i sentry mix.exs 2>/dev/null
# Detect Elixir version
cat .tool-versions 2>/dev/null | grep elixir
grep "elixir:" mix.exs 2>/dev/null
# Detect Phoenix or Plug
grep -E '"phoenix"|"plug"' mix.exs 2>/dev/null
# Detect Phoenix LiveView
grep "phoenix_live_view" mix.exs 2>/dev/null
# Detect Oban (job queue / crons)
grep "oban" mix.exs 2>/dev/null
# Detect Quantum (cron scheduler)
grep "quantum" mix.exs 2>/dev/null
# Detect OpenTelemetry usage
grep "opentelemetry" mix.exs 2>/dev/null
# Check for companion frontend
ls assets/ frontend/ web/ client/ 2>/dev/null
What to note:
| Signal | Impact |
|---|---|
sentry already in mix.exs? | Skip install; go to Phase 2 (configure features) |
| Phoenix detected? | Add Sentry.PlugCapture, Sentry.PlugContext, optionally Sentry.LiveViewHook |
| LiveView detected? | Add Sentry.LiveViewHook to the live_view macro in my_app_web.ex |
Based on what you found, present a concrete recommendation. Don't ask open-ended questions — lead with a proposal:
Recommended (core coverage):
Sentry.LoggerHandler forwards crash reports and error logs to SentryOptional (enhanced observability):
Recommendation logic:
| Feature | Recommend when... |
|---|---|
| Error Monitoring | Always — non-negotiable baseline |
| Logging | Always — LoggerHandler captures crashes that aren't explicit capture_exception calls |
| Tracing | Phoenix, Plug, Ecto, or OpenTelemetry imports detected |
| Crons | Oban, Quantum, or periodic GenServer/Task patterns detected |
| Sentry Logs | sentry v12.0.0+ in use and structured log search is needed |
Propose: "I recommend setting up Error Monitoring + Logging [+ Tracing if Phoenix/Ecto detected]. Want me to also add Crons or Sentry Logs?"
You need to run this yourself — the Igniter installer requires interactive terminal input that the agent can't handle. Copy-paste into your terminal:
mix igniter.install sentryAvailable since sentry v11.0.0. It auto-configures
config/config.exs,config/prod.exs,config/runtime.exs, andlib/my_app/application.ex.Once it finishes, come back and skip toVerification.
If the user skips the Igniter installer, proceed with Option 2 (Manual Setup) below.
Add to mix.exs dependencies:
# mix.exs
defp deps do
[
{:sentry, "~> 12.0"},
{:finch, "~> 0.21"}
# Add jason if using Elixir < 1.18:
# {:jason, "~> 1.4"},
]
end
mix deps.get
# config/config.exs
config :sentry,
dsn: System.get_env("SENTRY_DSN"),
environment_name: config_env(),
enable_source_code_context: true,
root_source_code_paths: [File.cwd!()],
in_app_otp_apps: [:my_app]
For runtime configuration (recommended for DSN and release):
# config/runtime.exs
import Config
config :sentry,
dsn: System.fetch_env!("SENTRY_DSN"),
release: System.get_env("SENTRY_RELEASE", "my-app@#{Application.spec(:my_app, :vsn)}")
This config enables the most features with sensible defaults:
# config/config.exs
config :sentry,
dsn: System.get_env("SENTRY_DSN"),
environment_name: config_env(),
enable_source_code_context: true,
root_source_code_paths: [File.cwd!()],
in_app_otp_apps: [:my_app],
# Logger handler config — captures crash reports
logger: [
{:handler, :sentry_handler, Sentry.LoggerHandler, %{
config: %{
metadata: [:request_id],
capture_log_messages: true,
level: :error
}
}}
]
Add Logger.add_handlers/1 in Application.start/2:
# lib/my_app/application.ex
def start(_type, _args) do
Logger.add_handlers(:my_app) # activates the :sentry_handler configured above
children = [
MyAppWeb.Endpoint
# ... other children
]
Supervisor.start_link(children, strategy: :one_for_one)
end
lib/my_app_web/endpoint.ex
defmodule MyAppWeb.Endpoint do
use Sentry.PlugCapture # Add ABOVE use Phoenix.Endpoint (Cowboy adapter only)
use Phoenix.Endpoint, otp_app: :my_app
# ...
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Phoenix.json_library()
plug Sentry.PlugContext # Add BELOW Plug.Parsers
# ...
end
Note:
Sentry.PlugCaptureis only needed for the Cowboy adapter. Phoenix 1.7+ defaults to Bandit , wherePlugCaptureis harmless but unnecessary.Sentry.PlugContextis always recommended — it enriches events with HTTP request data.
LiveView errors —lib/my_app_web.ex
def live_view do
quote do
use Phoenix.LiveView
on_mount Sentry.LiveViewHook # captures errors in mount/handle_event/handle_info
end
end
defmodule MyApp.Router do
use Plug.Router
use Sentry.PlugCapture # Cowboy only
plug Plug.Parsers, parsers: [:urlencoded, :multipart]
plug Sentry.PlugContext
# ...
end
Walk through features one at a time. Load the reference file for each, follow its steps, and verify before moving to the next:
| Feature | Reference file | Load when... |
|---|---|---|
| Error Monitoring | ${SKILL_ROOT}/references/error-monitoring.md | Always (baseline) |
| Tracing | ${SKILL_ROOT}/references/tracing.md | Phoenix / Ecto / OpenTelemetry detected |
| Logging | ${SKILL_ROOT}/references/logging.md | LoggerHandler or Sentry Logs setup |
| Crons | ${SKILL_ROOT}/references/crons.md | Oban, Quantum, or periodic jobs detected |
For each feature: Read ${SKILL_ROOT}/references/<feature>.md, follow steps exactly, verify it works.
| Option | Type | Default | Purpose |
|---|---|---|---|
:dsn | `string | nil` | nil |
:environment_name | `atom | string` | "production" |
:release | `string | nil` | nil |
| Variable | Maps to | Purpose |
|---|---|---|
SENTRY_DSN | :dsn | Data Source Name |
SENTRY_RELEASE | :release | App version (e.g., my-app@1.0.0) |
SENTRY_ENVIRONMENT | :environment_name | Deployment environment |
Test that Sentry is receiving events:
# Send a test event from your project
MIX_ENV=dev mix sentry.send_test_event
Or add a temporary call in a controller action:
# Temporary test — remove after confirming
def index(conn, _params) do
Sentry.capture_message("Sentry Elixir SDK test event")
text(conn, "sent")
end
Check the Sentry dashboard within a few seconds. If nothing appears:
config :sentry, log_level: :debug for verbose SDK outputSENTRY_DSN is set and the project exists:environment_name is not set to a value Sentry filters in your alert rulesAfter completing Elixir setup, check for a companion frontend missing Sentry coverage:
ls assets/ frontend/ web/ client/ ui/ 2>/dev/null
cat assets/package.json frontend/package.json 2>/dev/null | grep -E '"react"|"svelte"|"vue"|"next"'
If a frontend directory exists without Sentry configured, suggest the matching skill:
| Frontend detected | Suggest skill |
|---|---|
| React / Next.js | sentry-react-sdk or sentry-nextjs-sdk |
| Svelte / SvelteKit | sentry-svelte-sdk |
| Vue | See docs.sentry.io/platforms/javascript/guides/vue/ |
| Other JS/TS | sentry-browser-sdk |
Connecting Phoenix backend and JavaScript frontend with linked Sentry projects enables distributed tracing — stack traces that span the browser, Phoenix HTTP server, and downstream services in a single trace view.
| Issue | Solution |
|---|---|
| Events not appearing | Verify SENTRY_DSN is set; run mix sentry.send_test_event; set log_level: :debug |
| Missing stack traces on captured exceptions | Pass stacktrace: __STACKTRACE__ in the rescue block: Sentry.capture_exception(e, stacktrace: __STACKTRACE__) |
PlugCapture not working on Bandit | Sentry.PlugCapture is Cowboy-only; with Bandit errors surface via |
Weekly Installs
87
Repository
GitHub Stars
82
First Seen
14 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
gemini-cli86
cursor86
codex86
kimi-cli85
github-copilot85
amp85
Convex性能审计指南 - 诊断修复Convex应用性能问题与优化方案
15,600 周安装
| Oban detected? | Recommend Crons + error capture via Oban integration |
| Quantum detected? | Recommend Crons via Quantum integration |
| OpenTelemetry already present? | Tracing setup only needs Sentry.OpenTelemetry.* config |
| Frontend directory found? | Trigger Phase 4 cross-link suggestion |
:sample_rate | float | 1.0 | Error event sample rate (0.0–1.0) |
:enable_source_code_context | boolean | false | Include source lines around errors |
:root_source_code_paths | [path] | [] | Required when source context is enabled |
:in_app_otp_apps | [atom] | [] | OTP apps whose modules are "in-app" in stacktraces |
:before_send | `(event -> event | nil) | {m, f}` |
:after_send_event | `(event, result -> any) | {m, f}` | nil |
:filter | module | Sentry.DefaultEventFilter | Module implementing Sentry.EventFilter |
:max_breadcrumbs | integer | 100 | Max breadcrumbs per process |
:dedup_events | boolean | true | Deduplicate identical events within ~30 seconds |
:tags | map | %{} | Global tags sent with every event |
:traces_sample_rate | `float | nil` | nil |
:traces_sampler | `fn | {m, f} | nil` |
:enable_logs | boolean | false | Enable Sentry Logs Protocol (v12.0.0+) |
:test_mode | boolean | false | Capture events in-memory for testing |
LoggerHandler| Source code context missing in production | Run mix sentry.package_source_code before building your OTP release |
| Context not appearing on async events | Sentry.Context.* is process-scoped; pass values explicitly or propagate Logger metadata across processes |
| Oban integration not reporting crons | Requires Oban v2.17.6+ or Oban Pro; cron jobs must have "cron" => true in job meta |
| Duplicate events from Cowboy crashes | Set excluded_domains: [:cowboy] in LoggerHandler config (this is the default) |
finch not starting | Ensure {:finch, "~> 0.21"} is in deps; Finch is the default HTTP client since v12.0.0 |
| JSON encoding error | Add {:jason, "~> 1.4"} and set json_library: Jason for Elixir < 1.18 |