Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

Commit

Permalink
Changing metric names to represent values better in NewRelic
Browse files Browse the repository at this point in the history
  • Loading branch information
amir-jakoby committed Oct 2, 2016
1 parent 8520eb2 commit 6c86a2a
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 50 deletions.
44 changes: 26 additions & 18 deletions pyformance/reporters/newrelic_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import print_function
import json
import logging
from math import ceil

import os
import socket
Expand Down Expand Up @@ -87,6 +88,16 @@ def format_unit(metric):
return '[{}]'.format(metric.unit)


def create_metric(m):
return {
"total": m,
"count": ceil(m),
"min": 0 if m == 0 else m % 1 or 1,
"max": 0 if m == 0 else m if m < 0 else 1,
"sum_of_squares": int(m) + (m % 1)**2
}


class NewRelicReporter(Reporter):
"""
Reporter for new relic
Expand Down Expand Up @@ -152,16 +163,15 @@ def create_metrics(self, registry, sink_type='regular_sink', key_name_prefix='Co
# noinspection PyProtectedMember
gauges = registry._gauges.items()
for key, gauge in gauges:
key = '{}/gauge{}'.format(self._get_key_name(key, key_name_prefix), format_unit(gauge))
key = '{}/gauge'.format(self._get_key_name(key, key_name_prefix))
results[key] = gauge.get_value()

# noinspection PyProtectedMember
for key, histogram in registry._histograms.items():
key = self._get_key_name(key, key_name_prefix)
snapshot = histogram.get_snapshot()
key = '{}/{{}}{}'.format(key, format_unit(histogram))
key = '{}/{{}}'.format(key)

results[key.format('avg')] = histogram.get_mean()
results[key.format('mean_rate')] = create_metric(histogram.get_mean())
results[key.format('std_dev')] = histogram.get_stddev()
snapshot = histogram.get_snapshot()
results[key.format('75_percentile')] = snapshot.get_75th_percentile()
Expand All @@ -171,24 +181,22 @@ def create_metrics(self, registry, sink_type='regular_sink', key_name_prefix='Co

# noinspection PyProtectedMember
for key, meter in registry._meters.items():
key = '{}/{{}}[{}/minute]'.format(self._get_key_name(key, key_name_prefix), meter.unit if meter.unit else 'event')

results[key.format('15m_rate')] = meter.get_fifteen_minute_rate()
results[key.format('5m_rate')] = meter.get_five_minute_rate()
results[key.format('1m_rate')] = meter.get_one_minute_rate()
results[key.format('mean_rate')] = meter.get_mean_rate()
key = '{}/{{}}'.format(self._get_key_name(key, key_name_prefix))
results[key.format('15m_rate')] = create_metric(meter.get_fifteen_minute_rate())
results[key.format('5m_rate')] = create_metric(meter.get_five_minute_rate())
results[key.format('1m_rate')] = create_metric(meter.get_one_minute_rate())
results[key.format('mean_rate')] = create_metric(meter.get_mean_rate())

# noinspection PyProtectedMember
for key, timer in registry._timers.items():
key = '{}/{{}}{}'.format(self._get_key_name(key, key_name_prefix), format_unit(timer))
key = '{}/{{}}'.format(self._get_key_name(key, key_name_prefix))
snapshot = timer.get_snapshot()
results.update({key.format("avg"): timer.get_mean(),
key.format("count"): timer.get_count(),
results.update({key.format("count"): timer.get_count(),
key.format("std_dev"): timer.get_stddev(),
key.format("15m_rate"): timer.get_fifteen_minute_rate(),
key.format("5m_rate"): timer.get_five_minute_rate(),
key.format("1m_rate"): timer.get_one_minute_rate(),
key.format("mean_rate"): timer.get_mean_rate(),
key.format("15m_rate"): create_metric(timer.get_fifteen_minute_rate()),
key.format("5m_rate"): create_metric(timer.get_five_minute_rate()),
key.format("1m_rate"): create_metric(timer.get_one_minute_rate()),
key.format("mean_rate"): create_metric(timer.get_mean_rate()),
key.format("50_percentile"): snapshot.get_median(),
key.format("75_percentile"): snapshot.get_75th_percentile(),
key.format("95_percentile"): snapshot.get_95th_percentile(),
Expand All @@ -204,7 +212,7 @@ def create_metrics(self, registry, sink_type='regular_sink', key_name_prefix='Co

if not sink.count:
continue
key = "{}/{}{}".format(self._get_key_name(key, key_name_prefix), "raw", format_unit(value))
key = "{}/{}".format(self._get_key_name(key, key_name_prefix), "raw")
results[key] = {
"total": sink.total,
"count": sink.count,
Expand Down
117 changes: 85 additions & 32 deletions tests/test__newrelic_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,67 +53,120 @@ def test_report_now(self):
"duration": 1,
"guid": "com.github.pyformance",
"metrics": {
"Component/hist/raw[a/b]": {
"Component/hist/raw": {
"max": 512,
"total": 1023,
"min": 1,
"count": 10,
"sum_of_squares": 349525
},
"Component/t1/95_percentile[event/minute]": 1.,
"Component/hist/999_percentile[a/b]": 512,
"Component/counter-2/raw[c]": {
"Component/t1/95_percentile": 1.,
"Component/hist/999_percentile": 512,
"Component/counter-2/raw": {
"total": -3,
"max": -1,
"count": 2,
"sum_of_squares": 5,
"min": -2
},
"Component/t1/mean_rate[event/minute]": 1.,
"Component/t1/999_percentile[event/minute]": 1.,
"Component/m1/1m_rate[u1/minute]": 0,
"Component/t1/15m_rate[event/minute]": 0,
"Component/hist/99_percentile[a/b]": 512,
"Component/t1/raw[event/minute]": {
"Component/t1/mean_rate": {
"max": 1,
"count": 1,
"total": 1.,
"min": 1,
"sum_of_squares": 1.
},
"Component/t1/999_percentile": 1.,
"Component/m1/1m_rate": {
"min": 0,
"total": 0,
"max": 0,
"sum_of_squares": 0,
"count": 0
},
"Component/t1/15m_rate": {
"total": 0,
"min": 0,
"max": 0,
"sum_of_squares": 0,
"count": 0
},
"Component/hist/99_percentile": 512,
"Component/t1/raw": {
"min": 1.,
"count": 1,
"sum_of_squares": 1,
"max": 1,
"total": 1
"sum_of_squares": 1.,
"max": 1.,
"total": 1.
},
"Component/m1/mean_rate[u1/minute]": 3.,
"Component/hist/std_dev[a/b]": 164.94851048466947,
"Component/counter-1/raw[c]": {
"Component/m1/mean_rate": {
"sum_of_squares": 3.,
"count": 3,
"total": 3.,
"min": 1,
"max": 1
},
"Component/hist/std_dev": 164.94851048466947,
"Component/counter-1/raw": {
"count": 1,
"sum_of_squares": 1,
"min": 1,
"max": 1,
"total": 1
},
"Component/t1/50_percentile[event/minute]": 1.,
"Component/t1/99_percentile[event/minute]": 1.,
"Component/hist/95_percentile[a/b]": 512,
"Component/m1/15m_rate[u1/minute]": 0,
"Component/hist/75_percentile[a/b]": 160.,
"Component/t1/5m_rate[event/minute]": 0,
"Component/hist/avg[a/b]": 102.3,
"Component/t1/count[event/minute]": 1.,
"Component/g1/gauge[g]": 10,
"Component/t1/1m_rate[event/minute]": 0,
"Component/t1/75_percentile[event/minute]": 1.,
"Component/t1/std_dev[event/minute]": 0,
"Component/m1/raw[u1]": {
"Component/t1/50_percentile": 1.,
"Component/t1/99_percentile": 1.,
"Component/hist/95_percentile": 512,
"Component/m1/15m_rate": {
"count": 0,
"sum_of_squares": 0,
"max": 0,
"min": 0,
"total": 0
},
"Component/hist/75_percentile": 160.,
"Component/t1/5m_rate": {
"count": 0,
"min": 0,
"total": 0,
"sum_of_squares": 0,
"max": 0
},
"Component/hist/mean_rate": {
"count": 103,
"max": 1,
"sum_of_squares": 102.09,
"min": 0.29999999999999716,
"total": 102.3
},
"Component/t1/count": 1.,
"Component/g1/gauge": 10,
"Component/t1/1m_rate": {
"count": 0,
"total": 0,
"min": 0,
"max": 0,
"sum_of_squares": 0
},
"Component/t1/75_percentile": 1.,
"Component/t1/std_dev": 0.,
"Component/m1/raw": {
"min": 1,
"sum_of_squares": 3,
"count": 3,
"total": 3,
"max": 1
},
"Component/t1/avg[event/minute]": 1.,
"Component/m1/5m_rate[u1/minute]": 0
"Component/m1/5m_rate": {
"count": 0,
"min": 0,
"total": 0,
"sum_of_squares": 0,
"max": 0
}
},
"name": "foo"}
]
}

self.assertEqual(expected, output)
self.assertEqual(json.loads(json.dumps(expected)), json.loads(json.dumps(output)))

0 comments on commit 6c86a2a

Please sign in to comment.