posix4e / rust-metrics

Multi reporter metrics library (carbon, graphite, postgresql, prometheus)
Other
99 stars 19 forks source link

Add histogram support to prometheus dump #58

Closed posix4e closed 7 years ago

overvenus commented 8 years ago

Hello, the following snippet creates a protobuf Histogram, copied from rust-prometheus.

    fn proto(&self) -> proto::Histogram {
        let mut h = proto::Histogram::new();
        h.set_sample_sum(self.sum);
        h.set_sample_count(self.count);

        let mut count = 0;
        let mut buckets = Vec::with_capacity(self.upper_bounds.len());
        for (i, upper_bound) in self.upper_bounds.iter().enumerate() {
            count += self.counts[i];
            let mut b = proto::Bucket::new();
            b.set_cumulative_count(count);
            b.set_upper_bound(*upper_bound);
            buckets.push(b);
        }
        h.set_bucket(RepeatedField::from_vec(buckets));

        h
    }

Hope it helps!


BTW, rust-prometheus is a prometheus client for rust, ported from client_golang. It has a TextEncoder that converts a MetricFamily proto message into text format, if you want to supprot text format, it would be a shortcut ;-)

posix4e commented 7 years ago

Focusing on tic