示例#1
0
    def inc(self, arg=None):
        self.gaugeValue = arg
        if self.gaugeName is not None:

            p = Pusher('pushgateway', self.pushgatewayUrl)
            registry = Registry()
            c = Gauge(self.gaugeName, 'description for gauge', {})
            registry.register(c)

            if self.gaugeValue == None:
                c.inc({'type': 'hit'})
            else:
                c.add({'type': 'hit'}, self.gaugeValue)
        else:
            return False

        p.add(registry)
        return True
示例#2
0
class TestGauge(unittest.TestCase):
    def setUp(self):
        self.data = {
            'name': "hdd_disk_used",
            'help_text': "Disk space used",
            'const_labels': {
                "server": "1.db.production.my-app"
            },
        }

        self.g = Gauge(**self.data)

    def test_set(self):
        data = ({
            'labels': {
                'max': "500G",
                'dev': "sda"
            },
            'values': range(0, 500, 50)
        }, {
            'labels': {
                'max': "1T",
                'dev': "sdb"
            },
            'values': range(0, 1000, 100)
        }, {
            'labels': {
                'max': "10T",
                'dev': "sdc"
            },
            'values': range(0, 10000, 1000)
        })

        for i in data:
            for j in i['values']:
                self.g.set(i['labels'], j)

        self.assertEqual(len(data), len(self.g.values))

    def test_get(self):
        data = ({
            'labels': {
                'max': "500G",
                'dev': "sda"
            },
            'values': range(0, 500, 50)
        }, {
            'labels': {
                'max': "1T",
                'dev': "sdb"
            },
            'values': range(0, 1000, 100)
        }, {
            'labels': {
                'max': "10T",
                'dev': "sdc"
            },
            'values': range(0, 10000, 1000)
        })

        for i in data:
            for j in i['values']:
                self.g.set(i['labels'], j)
                self.assertEqual(j, self.g.get(i['labels']))

        for i in data:
            self.assertEqual(max(i['values']), self.g.get(i['labels']))

    def test_set_get_without_labels(self):
        data = {'labels': {}, 'values': range(100)}

        for i in data['values']:
            self.g.set(data['labels'], i)

        self.assertEqual(1, len(self.g.values))

        self.assertEqual(max(data['values']), self.g.get(data['labels']))

    def test_inc(self):
        iterations = 100
        labels = {'max': "10T", 'dev': "sdc"}

        for i in range(iterations):
            self.g.inc(labels)
            self.assertEqual(i + 1, self.g.get(labels))

        self.assertEqual(iterations, self.g.get(labels))

    def test_dec(self):
        iterations = 100
        labels = {'max': "10T", 'dev': "sdc"}
        self.g.set(labels, iterations)

        for i in range(iterations):
            self.g.dec(labels)
            self.assertEqual(iterations - (i + 1), self.g.get(labels))

        self.assertEqual(0, self.g.get(labels))

    def test_add(self):
        iterations = 100
        labels = {'max': "10T", 'dev': "sdc"}

        for i in range(iterations):
            self.g.add(labels, i)

        self.assertEqual(sum(range(iterations)), self.g.get(labels))

    def test_add_negative(self):
        iterations = 100
        labels = {'max': "10T", 'dev': "sdc"}

        for i in range(iterations):
            self.g.add(labels, -i)

        self.assertEqual(sum(map(lambda x: -x, range(iterations))),
                         self.g.get(labels))

    def test_sub(self):
        iterations = 100
        labels = {'max': "10T", 'dev': "sdc"}

        for i in range(iterations):
            self.g.sub(labels, i)

        self.assertEqual(sum(map(lambda x: -x, range(iterations))),
                         self.g.get(labels))

    def test_sub_positive(self):
        iterations = 100
        labels = {'max': "10T", 'dev': "sdc"}

        for i in range(iterations):
            self.g.sub(labels, -i)

        self.assertEqual(sum(range(iterations)), self.g.get(labels))
    os.path.dirname(
        os.path.dirname(
            os.path.abspath(inspect.getfile(inspect.currentframe())))))

from prometheus.pusher import Pusher
from prometheus.registry import Registry
from prometheus.collectors import Gauge

PUSHGATEWAY_URL = "http://127.0.0.1:9091"

if __name__ == "__main__":
    job_name = "example"
    p = Pusher(job_name, PUSHGATEWAY_URL)
    registry = Registry()
    g = Gauge("up_and_down", "Up and down counter.", {})
    registry.register(g)

    user = input("Hi! enter your username: "******"Enter a positive or negative number: "))

            g.add({
                'user': user,
            }, n)
            # Push to the pushgateway
            p.add(registry)
        except ValueError:
            print("Wrong input")