Skip to content

fix(sentry): flush global telemetry buffers periodically - #1087

Open
huangdijia wants to merge 1 commit into
mainfrom
sentry-pr/r5
Open

fix(sentry): flush global telemetry buffers periodically#1087
huangdijia wants to merge 1 commit into
mainfrom
sentry-pr/r5

Conversation

@huangdijia

Copy link
Copy Markdown
Contributor

问题

Sentry SDK 的 Logs/Metrics 聚合器归属"当前 runtime context"(见 vendor/sentry/sentry/src/Logs/Logs.phpMetrics/TraceMetrics.php)。没有 active context 的协程(引擎协程、主进程等)写入 global context 的聚合器:

  • 默认配置下聚合器是有界环形缓冲(1000 条,LogsAggregator::LOGS_BUFFER_SIZE),不会 OOM 但会静默丢弃日志。
  • 用户设置 log_flush_threshold/metric_flush_threshold 后,存储变为无界数组(Util/TelemetryStorage::unbounded()),仅在达到阈值时 flush —— 阈值过大时存在内存溢出风险。

修改

  • 新增 Metrics\Listener\TelemetryFlushListener:监听 MetricFactoryReady,按 metrics_interval 周期 tick,对 global context 的日志/指标聚合器执行 flush 兜底(用 SentrySdk::endContext() 结束 tick 协程自身 context 后再 flush,确保落在 global context;每个 flush 独立 try/catch,幂等无副作用)。
  • ConfigProvider 注册新 listener(默认优先级)。
  • Feature 新增 isLogsEnabled(bool $default = true) 读取 sentry.enable_logs,与 isMetricsEnabled 保持一致;二者任一开启即启动周期 flush。
  • publish/sentry.phplog_flush_threshold 补充注释:达到阈值自动 flush、telemetry flush listener 周期兜底、内存占用与阈值成正比建议 <= 5000(默认值不变)。
  • 新增测试 tests/Sentry/Metrics/Listener/TelemetryFlushListenerTest.php(FakeTimer 不 spawn 协程):重复 process 只 tick 一次;tick 闭包可安全调用(空聚合器 flush 返回 null);logs/metrics 均关闭时零 tick。

测试

  • vendor/bin/pest --group=sentry:46 passed(基线 43 + 新增 3)。
  • vendor/bin/php-cs-fixer fix --dry-run --diff:改动文件 0 处可修复。
  • vendor/bin/phpstan analyse src/sentry:No errors。

验证

  • git diff --check 通过;commit f848978b;分支 sentry-pr/r5 已推送。
  • 未改动其它 listener/transport、默认配置数值及 docs。

@coderabbitai

coderabbitai Bot commented Aug 20, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@huangdijia, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 58 minutes

Limit details: You’ve used all 2 included reviews currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

Wait for the limit to reset, then comment @coderabbitai review or push new commits to the PR.

An organization admin can change what happens after included review limits in Billing.

How do review limits work?

CodeRabbit enforces per-developer PR review limits within each organization.

For paid Pro and Pro+ reviews, CodeRabbit uses a developer's included PR review attempts over the past 7 days to set the current hourly allowance. At typical activity levels, the full plan allowance applies. Higher sustained activity can lower the allowance until earlier attempts leave the 7-day window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: c7f9654a-c381-459a-b296-fa865787afc3

📥 Commits

Reviewing files that changed from the base of the PR and between c814785 and f848978.

📒 Files selected for processing (5)
  • src/sentry/publish/sentry.php
  • src/sentry/src/ConfigProvider.php
  • src/sentry/src/Feature.php
  • src/sentry/src/Metrics/Listener/TelemetryFlushListener.php
  • tests/Sentry/Metrics/Listener/TelemetryFlushListenerTest.php

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f848978bbc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +40 to +42
return [
MetricFactoryReady::class,
];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Start the flush timer independently of metrics readiness

With the published defaults (enable_logs=true, enable_metrics=false), this listener is never invoked: every production dispatch of MetricFactoryReady is gated by isMetricsEnabled() or isCommandMetricsEnabled(). Moreover, OnWorkerStart dispatches it only for worker 0, so the other worker processes receive no timer even when metrics are enabled. Consequently, the new fallback does not flush global log buffers in the default configuration or across all workers, leaving the reported log-loss/unbounded-memory scenario unresolved; start it from a lifecycle event that runs in every relevant process or arrange an unconditional per-worker dispatch.

Useful? React with 👍 / 👎.

Comment on lines +47 to +49
// Reaching the threshold triggers an automatic flush, and the telemetry
// flush listener also periodically flushes as a fallback. Memory usage
// grows linearly with the threshold, so keep it <= 5000.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Synchronize the telemetry guidance across Sentry docs

This adds user-facing behavior and threshold guidance to the published configuration, but both component READMEs and all four localized Sentry pages still omit log_flush_threshold, the periodic fallback, and its memory recommendation. Update those six sources so users who follow the package documentation receive the same telemetry behavior and configuration guidance.

AGENTS.md reference: AGENTS.md:L129-L130

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant