Skip to content

Histogram does not support negative buckets #782

Closed

Description

@baryluk

Histogram allows to pass buckets with negative bucket boundaries, but they are never updated, and read as 0 when exporting. Also, when negative entries are present a -Inf should also be added, and the summary part (_count and _sum should be probably decomposed into positive and negative parts, otherwise the _sum will be pretty useless)

No idea about the support on Prometheus side in histogram_percentile.

I do often have need for double sided histogram, and right now usually implement it as two histograms (positive and negative), but that is far from optimal.

Activity

  1. csmarchbanks commented on Mar 11, 2022

    @csmarchbanks
    Member

    Hmm, could I get a bit more detail on how they are not supported/an example or failing test? The very brief tests I am running everything seems to work ok. We certainly aim to support negative buckets!

    Note that I don't think -Inf should be be added as it doesn't make any sense for a value to be less than or equal to -Inf. Also, according to the Open Metrics spec, in the presence of negative histogram buckets _sum MUST be omitted, and that is how it is handled by this library.

  2. baryluk commented on Mar 15, 2022

    @baryluk
    Author

    Yes, -Inf doesn't make much sens probably.

    But here is an example of the failure:

    #!/usr/bin/env python3
    
    from prometheus_client import Histogram
    
    buckets = [-100, -10, -5, 0, 5, 10, 100]
    
    h = Histogram('h', '', buckets=buckets)
    h.observe(6)
    print(h.collect())
    [Metric(h, , histogram, , [
    Sample(name='h_bucket', labels={'le': '-100.0'}, value=0.0, timestamp=None, exemplar=None),
    Sample(name='h_bucket', labels={'le': '-10.0'}, value=0.0, timestamp=None, exemplar=None),
    Sample(name='h_bucket', labels={'le': '-5.0'}, value=0.0, timestamp=None, exemplar=None),
    Sample(name='h_bucket', labels={'le': '0.0'}, value=0.0, timestamp=None, exemplar=None),
    Sample(name='h_bucket', labels={'le': '5.0'}, value=0.0, timestamp=None, exemplar=None),
    Sample(name='h_bucket', labels={'le': '10.0'}, value=1.0, timestamp=None, exemplar=None),
    Sample(name='h_bucket', labels={'le': '100.0'}, value=1.0, timestamp=None, exemplar=None),
    Sample(name='h_bucket', labels={'le': '+Inf'}, value=1.0, timestamp=None, exemplar=None),
    Sample(name='h_count', labels={}, value=1.0, timestamp=None, exemplar=None),
    Sample(name='h_created', labels={}, value=1647348798.9148076, timestamp=None, exemplar=None)])]
    

    Notice value=0.0 for non-positive buckets.

  3. baryluk commented on Mar 15, 2022

    @baryluk
    Author

    My bad. It actually works:

    h.observe(-60)
    [Metric(h, , histogram, , [
    Sample(name='h_bucket', labels={'le': '-100.0'}, value=0.0, timestamp=None, exemplar=None),
    Sample(name='h_bucket', labels={'le': '-10.0'}, value=1.0, timestamp=None, exemplar=None),
    Sample(name='h_bucket', labels={'le': '-5.0'}, value=1.0, timestamp=None, exemplar=None),
    Sample(name='h_bucket', labels={'le': '0.0'}, value=1.0, timestamp=None, exemplar=None),
    Sample(name='h_bucket', labels={'le': '5.0'}, value=1.0, timestamp=None, exemplar=None),
    Sample(name='h_bucket', labels={'le': '10.0'}, value=1.0, timestamp=None, exemplar=None),
    Sample(name='h_bucket', labels={'le': '100.0'}, value=1.0, timestamp=None, exemplar=None),
    Sample(name='h_bucket', labels={'le': '+Inf'}, value=1.0, timestamp=None, exemplar=None),
    Sample(name='h_count', labels={}, value=1.0, timestamp=None, exemplar=None),
    Sample(name='h_created', labels={}, value=1647348909.1770196, timestamp=None, exemplar=None)])]
    

    My bad.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions