Skip to content

Commit

Permalink
feat(metrics): add summary metric type
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Aug 18, 2019
1 parent 3498c2a commit 2b5dba6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
16 changes: 8 additions & 8 deletions README.md
Expand Up @@ -21,6 +21,7 @@ into existing Prometheus/Grafana monitoring infrastructure.
- [Metric Types](#metric-types)
- [Counter](#counter)
- [Gauge](#gauge)
- [Summary](#summary)
- [Registries](#registries)
- [Planned Features](#planned-features)
- [Known Issues](#known-issues)
Expand All @@ -38,26 +39,25 @@ MicroPython 4.1.0 or better, with an [Adafruit Ethernet FeatherWing](https://www
This module implements a very rudimentary HTTP server that likely violates some part of the spec. However, it works
with Chrome, curl, and Prometheus itself.

Call `start_http_server(port)` to bind a socket and begin listening.

Call `await_http_request(server, registry)` to await an incoming HTTP request and respond with the metrics in
`registry`.

### Labels

Labels are not yet implemented.

### Metric Types

Currently, `Counter` and `Gauge` are the only metric types implemented.
`Counter`, `Gauge`, and `Summary` are implemented. Labels are not (yet).

#### Counter

Both `inc` and `dec` are implemented.
Incremental values. Implements `inc(value)` and `dec(value)`.

#### Gauge

Extends [counter](#counter) with `set`.
Absolute values. Extends [counter](#counter) with `set(value)`.

#### Summary

Prints count and total of `observe(value)`.

### Registries

Expand Down
47 changes: 35 additions & 12 deletions prometheus_express/metric.py
@@ -1,4 +1,19 @@
def print_help(name, desc, type):
return [
'# HELP {} {}'.format(name, desc),
'# TYPE {} {}'.format(name, type),
]


def print_name(namespace, name):
if namespace != '':
return '{}_{}'.format(namespace, name)
else:
return name

# base class for metric types


class Metric():
name = ''
desc = ''
Expand All @@ -21,17 +36,7 @@ def labels(self, *labelValues):
return self

def print(self, namespace):
name = self.printName(namespace)
return [
'# HELP {} {}'.format(name, self.desc),
'# TYPE {} {}'.format(name, self.metricType),
]

def printName(self, namespace):
if namespace != '':
return '{}_{}'.format(namespace, self.name)
else:
return self.name
return print_help(print_name(namespace, self.name), self.desc, self.metricType)


class Counter(Metric):
Expand All @@ -46,7 +51,7 @@ def dec(self, value):

def print(self, namespace):
return super().print(namespace) + [
'{} {}'.format(self.printName(namespace), self.value)
'{} {}'.format(print_name(namespace, self.name), self.value)
]


Expand All @@ -55,3 +60,21 @@ class Gauge(Counter):

def set(self, value):
self.value = value


class Summary(Metric):
metricType = 'summary'
valueCount = 0
valueTotal = 0

def observe(self, value):
self.valueCount += 1
self.valueTotal += value

def print(self, namespace):
nn = print_name(namespace, self.name)
return print_help(nn + '_count', self.desc, self.metricType) + [
'{}_count {}'.format(nn, self.valueCount),
] + print_help(nn + '_total', self.desc, self.metricType) + [
'{}_total {}'.format(nn, self.valueTotal),
]

0 comments on commit 2b5dba6

Please sign in to comment.