[PR #24046] [CLOSED] fix(telemetry): run OTEL metric callbacks synchronously #66347

Closed
opened 2026-05-06 12:39:57 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/24046
Author: @franciseliang99-dot
Created: 4/23/2026
Status: Closed

Base: devHead: fix/otel-metrics-sync-callbacks


📝 Commits (1)

  • fe8fcf3 fix(telemetry): run OTEL metric callbacks synchronously

📊 Changes

2 files changed (+35 additions, -8 deletions)

View changed files

📝 backend/open_webui/models/users.py (+23 -1)
📝 backend/open_webui/utils/telemetry/metrics.py (+12 -7)

📄 Description

OTEL observable-gauge callbacks run on the PeriodicExportingMetricReader thread, which has no event loop. The three async callbacks in utils/telemetry/metrics.py (observe_active_users, observe_total_registered_users, observe_users_active_today) returned coroutines instead of iterables of Observation, causing TypeError: 'coroutine' object is not iterable on every export cycle.

This PR:

  • Adds sync COUNT(*) variants on UsersTable (get_num_users_sync, get_active_user_count_sync, get_num_users_active_today_sync) using the existing sync SessionLocal from open_webui/internal/db.py.
  • Converts the three OTEL callbacks back to plain def and points them at the new sync methods.

Sync SessionLocal is documented in db.py as "used ONLY for startup config loading"; OTEL metric callbacks are the one legitimate runtime exception because OTel's PeriodicExportingMetricReader runs them on a worker thread outside any event loop. Option 2 (asyncio.run_coroutine_threadsafe against the captured main loop) was considered but would require a lifespan hook plus cross-thread scheduling per export, with no runtime advantage for metrics polled every ~30s.

Closes #23936
Closes #23938

Contributor License Agreement

Note

Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/open-webui/open-webui/pull/24046 **Author:** [@franciseliang99-dot](https://github.com/franciseliang99-dot) **Created:** 4/23/2026 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `fix/otel-metrics-sync-callbacks` --- ### 📝 Commits (1) - [`fe8fcf3`](https://github.com/open-webui/open-webui/commit/fe8fcf313569ffc43fd8d1274f09a5d285a328e2) fix(telemetry): run OTEL metric callbacks synchronously ### 📊 Changes **2 files changed** (+35 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/models/users.py` (+23 -1) 📝 `backend/open_webui/utils/telemetry/metrics.py` (+12 -7) </details> ### 📄 Description OTEL observable-gauge callbacks run on the `PeriodicExportingMetricReader` thread, which has no event loop. The three async callbacks in `utils/telemetry/metrics.py` (`observe_active_users`, `observe_total_registered_users`, `observe_users_active_today`) returned coroutines instead of iterables of `Observation`, causing `TypeError: 'coroutine' object is not iterable` on every export cycle. This PR: - Adds sync `COUNT(*)` variants on `UsersTable` (`get_num_users_sync`, `get_active_user_count_sync`, `get_num_users_active_today_sync`) using the existing sync `SessionLocal` from `open_webui/internal/db.py`. - Converts the three OTEL callbacks back to plain `def` and points them at the new sync methods. Sync `SessionLocal` is documented in `db.py` as "used ONLY for startup config loading"; OTEL metric callbacks are the one legitimate runtime exception because OTel's `PeriodicExportingMetricReader` runs them on a worker thread outside any event loop. Option 2 (`asyncio.run_coroutine_threadsafe` against the captured main loop) was considered but would require a lifespan hook plus cross-thread scheduling per export, with no runtime advantage for metrics polled every ~30s. Closes #23936 Closes #23938 ### Contributor License Agreement <!-- 🚨 DO NOT DELETE THE TEXT BELOW 🚨 Keep the "Contributor License Agreement" confirmation text intact. Deleting it will trigger the CLA-Bot to INVALIDATE your PR. Your PR will NOT be reviewed or merged until you check the box below confirming that you have read and agree to the terms of the CLA. --> - [X] By submitting this pull request, I confirm that I have read and fully agree to the [Contributor License Agreement (CLA)](https://github.com/open-webui/open-webui/blob/main/CONTRIBUTOR_LICENSE_AGREEMENT), and I am providing my contributions under its terms. > [!NOTE] > Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-05-06 12:39:57 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#66347