Skip to content

Fix metrics reporting for UDT#385

Open
dpw13 wants to merge 1 commit intoopensensor:mainfrom
dpw13:fix/udt-metrics
Open

Fix metrics reporting for UDT#385
dpw13 wants to merge 1 commit intoopensensor:mainfrom
dpw13:fix/udt-metrics

Conversation

@dpw13
Copy link
Copy Markdown
Contributor

@dpw13 dpw13 commented Apr 14, 2026

  • Fix reporting of sparklines for metrics
  • Allocate metrics slot for any metrics reporting
  • Instrument UDT with some basic metrics
  • Adds minor smoothing to fps and bitrate metrics

* Fix reporting of sparklines for metrics
* Allocate metrics slot for any metrics reporting
* Instrument UDT with some basic metrics
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes and expands stream metrics reporting (including sparklines) by ensuring metrics slots are allocated when metrics are recorded, instrumenting Unified Detection Thread (UDT) with basic metrics, and adding smoothing to reported FPS/bitrate values.

Changes:

  • Fix /api/health?sparklines=... handling to correctly detect the query parameter.
  • Instrument UDT to record per-packet/frame metrics and recording lifecycle metrics.
  • Update metrics slot allocation behavior and add basic EMA smoothing for FPS/bitrate sampling.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/web/api_handlers_health.c Fixes query-param handling for enabling sparklines in the health response.
src/video/unified_detection_thread.c Adds stream metrics recording (frames/bytes) and recording lifecycle metrics for UDT.
src/video/stream_state.c Removes explicit metrics slot pre-allocation; relies on allocation via metrics APIs.
src/telemetry/stream_metrics.c Changes slot allocation strategy, adds smoothing to sampled FPS/bitrate, and adjusts configured FPS usage.
include/telemetry/stream_metrics.h Removes the public metrics_get_slot API from the header.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +108 to +109
m->current_fps = 0.5 * m->current_fps + 0.5 * (double)df / dt;
m->current_bitrate_bps = 0.5 * m->current_bitrate_bps + 0.5 * ((double)db * 8.0) / dt;
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

The exponential smoothing update halves the first computed FPS/bitrate after startup because current_fps/current_bitrate_bps are initially 0.0; the first delta sample becomes 0.50 + 0.5instant. Consider initializing the EMA on the first computed sample (e.g., if prev_sample_time>0 but prior value is 0/uninitialized, assign the instantaneous value directly) to avoid systematically underreporting immediately after allocation/reset.

Suggested change
m->current_fps = 0.5 * m->current_fps + 0.5 * (double)df / dt;
m->current_bitrate_bps = 0.5 * m->current_bitrate_bps + 0.5 * ((double)db * 8.0) / dt;
double instant_fps = (double)df / dt;
double instant_bitrate_bps = ((double)db * 8.0) / dt;
if (m->prev_frames_total == 0 && m->prev_bytes_received == 0) {
m->current_fps = instant_fps;
m->current_bitrate_bps = instant_bitrate_bps;
} else {
m->current_fps = 0.5 * m->current_fps + 0.5 * instant_fps;
m->current_bitrate_bps = 0.5 * m->current_bitrate_bps + 0.5 * instant_bitrate_bps;
}

Copilot uses AI. Check for mistakes.
if (!g_initialized || !stream_name) return;
int idx = find_active_slot(stream_name);
int idx = metrics_get_slot(stream_name);
if (idx < 0) return;
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

metrics_set_configured_fps() now stores the provided fps without any validation/clamping. Because the sampler compares current_fps against configured_fps * STREAM_HEALTH_FPS_RATIO, a caller passing 0 or a negative value will effectively disable the degraded-FPS check (or produce a negative threshold). Consider keeping the previous guard (e.g., clamp to a sane default/minimum) or explicitly rejecting invalid values.

Suggested change
if (idx < 0) return;
if (idx < 0) return;
if (fps <= 0) {
log_warn("Ignoring invalid configured FPS %.3f for stream '%s'", fps, stream_name);
return;
}

Copilot uses AI. Check for mistakes.
Comment on lines +1322 to +1323
// Record stream metrics
metrics_record_frame(ctx->stream_name, pkt->size, is_video);
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

metrics_record_frame() updates last_frame_ts for every packet; in this call site it is invoked for both video and audio packets (is_video=false for audio), which can make stream health look "up" based on audio activity even if video is stalled. Consider only calling metrics_record_frame for video packets here, or change metrics_record_frame to update last_frame_ts only when is_video is true (and add a separate API/counter if you still want to track total bytes including audio).

Suggested change
// Record stream metrics
metrics_record_frame(ctx->stream_name, pkt->size, is_video);
// Record stream metrics for video packets only so stream health reflects
// actual video frame arrival rather than audio activity.
if (is_video) {
metrics_record_frame(ctx->stream_name, pkt->size, true);
}

Copilot uses AI. Check for mistakes.
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.

2 participants