Skip to content

How to use Custom Collectors for Histograms #683

Closed

Description

@cunningr

When using a histogram metrics we can use the 'observe' method to evaluate which bucket our latest sample will fall in:

E.g.
h1.labels('device-1').observe(value.get('latest_sample'))
->

        for i, bound in enumerate(self._upper_bounds):
            if amount <= bound:
                self._buckets[i].inc(1)
                break

However as I am proxying metrics I believe I should be using a Customer Collector and the HistogramMetricFamily which only provides add_metric which seems to just populate all the buckets.

am I supposed to calculate the buckets in my code before writing the metric?

Activity

  1. brian-brazil commented on Sep 1, 2021

    @brian-brazil
    Contributor

    That's how I'd do it, keep in mind that you possibly want to switch the type to GaugeHistogram before sending it on if you're using a custom collector like this.

  2. brian-brazil commented on Sep 1, 2021

    @brian-brazil
    Contributor

    It's a type thing, so may be used by downstream tooling for better hints etc.. If the data is based on counters then it's a Histogram. If it's a snapshot of current state it's a GaugeHistogram.

  3. brian-brazil commented on Sep 1, 2021

    @brian-brazil
    Contributor

    You should create a new Histogram for each collect, otherwise you're risking race conditions.

  4. cunningr commented on Sep 2, 2021

    @cunningr
    Author
  5. brian-brazil commented on Sep 2, 2021

    @brian-brazil
    Contributor

    In that case, manually putting together the Histogram's samples is what I'd do.

  6. locked and limited conversation to collaborators on Sep 2, 2021
  7. unlocked this conversation on Sep 2, 2021
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