Skip to content

feat: Add stats API key scope to allow external observability tool access - #10956

Open
louishhy wants to merge 2 commits into
AstrBotDevs:masterfrom
louishhy:feat/stats-api-scope
Open

louishhy wants to merge 2 commits into
AstrBotDevs:masterfrom
louishhy:feat/stats-api-scope

Conversation

@louishhy

@louishhy louishhy commented Nov 2, 2026 •

Copy link
Copy Markdown

Close #10957

Existing runtime and token usage statistics endpoints require dashboard authentication because they use the system scope, which developer API keys cannot access. This blocks external observability systems to access and leverage the metrics.

This PR adds an opt-in, read-only stats scope so external monitoring tools (e.g. Prometheus exporters) can access statistics-related endpoints.

Modifications / 改动点

  • Add stats as a supported API key scope, excluded from default permissions.

    • Access scope:
      • GET /api/v1/stats
      • GET /api/v1/stats/provider-tokens
  • Add an stats option to API key settings.

  • Add tests for scope enforcement, administrative access denial, and unchanged default permissions.

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

image

Verify with

export ASTRBOT_STATS_KEY=<key>

curl -i -H "Authorization: Bearer $ASTRBOT_STATS_KEY" \
  http://localhost:6185/api/v1/stats

Checklist / 检查清单

This is yet a reference implementation branch that is added as an appendix material for issue discussion.

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。

  • 📚 I checked the affected WebUI instructions and screenshots in docs/zh and docs/en against the changed navigation, page structure, and labels, and updated them in this PR (or explained why no documentation update is needed). For renamed, moved, or merged entry points, I included an old entry → new entry mapping in the documentation and changelog.
    / 我已对照变化后的 WebUI 入口、页面结构和术语,核对并在本 PR 中更新 docs/zh 和 docs/en 的相关操作说明与截图(或说明无需更新文档的原因)。入口改名、移动或合并时,已在文档和 changelog 中补充 旧入口 → 新入口 对照。

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txt 和 pyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Enable external observability tools to securely access runtime and token usage statistics through explicitly scoped API keys.

New Features:

  • Add an opt-in, read-only stats API key scope for runtime and provider token statistics endpoints.
  • Expose the stats scope in API key settings and public OpenAPI documentation.

Enhancements:

  • Keep statistics access separate from administrative system permissions while preserving dashboard authentication access.

Documentation:

  • Document the stats scope, supported endpoints, and usage examples in English and Chinese developer documentation.

Tests:

  • Add coverage for stats scope authorization, unauthorized administrative actions, and exclusion from default API key permissions.

Currently, runtime and token usage statistics require dashboard
authentication because their endpoints use the system scope, which
developer API keys cannot access. External collectors therefore need
dashboard credentials.

We hence add a read-only stats scope for the two existing statistics
endpoints /api/v1/stats and /api/v1/stats/provider-tokens.

The scope needs opt-in (requires explicit selection) so existing keys
and default permissions remain unchanged.
Enable developers to select "stats" permission through the existing key
creation interface.
@Soulter Soulter moved this to In Progress in AstrBot Roadmap Nov 2, 2026
@louishhy
louishhy marked this pull request as ready for review November 2, 2026 14:21

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Needs a human reviewer. A key granted stats can read runtime and token-usage statistics, including plugin and session breakdowns. Reverting removes future access, but cannot undo information already retrieved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@mantoujun6

Copy link
Copy Markdown

Hi, If you are resolving an issue, I recommend using Close #10957 in the PR description rather than a reference, so that maintainers can automatically close the issue after merging.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Linked issues

#10957 [Feature] Add `stats` API key scope to allow external observability tool access · 4 of 4 met

# Requirement Status Notes
1 Allow stats-scoped API keys to access GET /api/v1/stats. ✅ Met The endpoint now requires ScopeDependency("stats").
2 Allow stats-scoped API keys to access GET /api/v1/stats/provider-tokens. ✅ Met The endpoint now requires ScopeDependency("stats").
3 Make the scope opt-in and do not grant administrative permissions. ✅ Met stats is accepted as an explicit scope but is not in the default scopes; it does not inherit other scopes, and the added tests check that it cannot perform administrative actions.
4 Let external tools retrieve these statistics without dashboard credentials. ✅ Met The scope dependency accepts API-key authentication, and the tests cover access with a stats-only key.

@louishhy

louishhy commented Nov 3, 2026 •

Copy link
Copy Markdown
Author

@mantoujun6 Hello there! Thank you for your message.

Updated the PR message according to your suggestion : )

@louishhy

louishhy commented Nov 4, 2026

Copy link
Copy Markdown
Author

The PR's CI flow failed in tests/unit/test_trace_store.py with RuntimeError('Event loop is closed'), which the changes should not be impacting.

Similar patterns have been discovered in some other recent PRs:

Would appreciate some help on this issue (like re-running the Action jobs) 🙏

This branch has not been deployed

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

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

[Feature] Add stats API key scope to allow external observability tool access

2 participants