Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 4, 2025

Metric aggregations returned NaN when any data point contained NaN, making the aggregation functions unusable for real-world metrics with missing data.

Changes

  • Core aggregation logic: Replaced standard numpy functions with NaN-safe variants:

    • sumnansum (treats NaN as 0)
    • max/minnanmax/nanmin (ignores NaN)
    • averagenanmean (ignores NaN)
    • percentilenanpercentile (ignores NaN)
    • std/varnanstd/nanvar (ignores NaN)
  • Consistency fix: Changed percentile calculation to use np_array instead of query_values list for alignment with other operations

Example

# Before: returns all NaN values
pc = PrometheusConnect(url="http://prometheus:9090")
result = pc.get_metric_aggregation(
    query="my_metric",  # Contains [1.0, NaN, 2.0, 3.0]
    operations=["sum", "max", "average"]
)
# {'sum': nan, 'max': nan, 'average': nan}

# After: returns meaningful statistics
# {'sum': 6.0, 'max': 3.0, 'average': 2.0}
Original prompt

This section details on the original issue you should resolve

<issue_title>Handling of NaN values in metric aggregations</issue_title>
<issue_description>Describe the bug
Not really sure if a bug or a design decision but I could not find this case covered in the tests.

When using numpy to calculate the different operations in get_metric_aggregation the functions chosen will return NaN if there is a value in the metric that is NaN.

For example for:

To Reproduce
Steps to reproduce the behavior:

  1. Pass a metric with a NaN value and several numerical values to get_metric_aggregation
  2. Check the result and you should see that it is NaN

Expected behavior
Handle NaN values to return a statistically meaningful number

Additional context
I could submit a PR if you think this is something that can be changed.
It could even be possible to pass a boolean flag handle_nans as an argument to get_metric_aggregation to choose the behavior.
</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Dec 4, 2025
Copilot AI and others added 3 commits December 4, 2025 06:56
- Replace numpy.sum with numpy.nansum
- Replace numpy.max with numpy.nanmax
- Replace numpy.min with numpy.nanmin
- Replace numpy.average with numpy.nanmean
- Replace numpy.percentile with numpy.nanpercentile
- Replace numpy.std with numpy.nanstd
- Replace numpy.var with numpy.nanvar
- Add comprehensive test for NaN handling

Co-authored-by: 4n4nd <22333506+4n4nd@users.noreply.github.com>
Co-authored-by: 4n4nd <22333506+4n4nd@users.noreply.github.com>
- Move math import to top of test file
- Use np_array instead of query_values for percentile calculation for consistency

Co-authored-by: 4n4nd <22333506+4n4nd@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix handling of NaN values in metric aggregations Use NaN-safe numpy functions in metric aggregations Dec 4, 2025
Copilot AI requested a review from 4n4nd December 4, 2025 07:02
Copilot finished work on behalf of 4n4nd December 4, 2025 07:02
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.

Handling of NaN values in metric aggregations

2 participants