1
0
Fork 0

feat: add package structure

This commit is contained in:
ssube 2019-12-23 16:23:11 -06:00
parent a1dcc99903
commit 4eb9e928fc
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
9 changed files with 78 additions and 13 deletions

4
.gitignore vendored
View File

@ -1,3 +1,7 @@
dist/
*.swp
*.pyc
*.egg-info/
**/__pycache__/

2
Makefile Normal file
View File

@ -0,0 +1,2 @@
test:
python -m unittest discover -s tests/

View File

@ -13,7 +13,7 @@ into existing Prometheus/Grafana monitoring infrastructure.
For those unfamiliar with Prometheus, the examples expose an HTTP server on port `:8080` that reports metrics in a
plaintext format:
```
```none
# HELP prom_express_gas gas from the bme680 sensor
# TYPE prom_express_gas gauge
prom_express_gas 1060948
@ -67,7 +67,7 @@ Labels are not yet implemented.
### Metric Types
`Counter`, `Gauge`, and `Summary` are implemented. Labels are not (yet).
`Counter`, `Gauge`, and `Summary` are implemented with labels.
#### Counter
@ -85,10 +85,10 @@ Prints count and total of `observe(value)`.
Registries may be created with a namespace: `CollectorRegistry(namespace='foo')`
Call `registry.print()` to format metrics in Prometheus'
Call `registry.render()` to format metrics in Prometheus'
[plain text exposition format](https://prometheus.io/docs/instrumenting/exposition_formats/#text-based-format):
```
```none
# HELP prom_express_test_counter a test counter
# TYPE prom_express_test_counter counter
prom_express_test_counter 1588
@ -101,8 +101,8 @@ Metrics may be registered with multiple registries.
## Planned Features
- `push_to_gateway`
- remaining metric types (Histogram, Summary)
- push support: `push_to_gateway`
- remaining metric types: `Histogram`
## Known Issues

View File

@ -0,0 +1,5 @@
from prometheus_express.metric import render_help, render_labels, render_name, Metric, Counter, Gauge, Summary
from prometheus_express.registry import CollectorRegistry
from prometheus_express.router import Router
from prometheus_express.server import start_http_server, Server
from prometheus_express.utils import check_network, scan_i2c_bus

View File

@ -27,7 +27,7 @@ def render_name(namespace, name):
# base class for metric types
class Metric():
class Metric(object):
name = ''
desc = ''
labelKeys = []
@ -77,7 +77,7 @@ class Counter(Metric):
self.labelValues = self.emptyLabels
def render(self, namespace):
lines = super().render(namespace)
lines = super(Counter, self).render(namespace)
for l, v in self.values.items():
lines.append('{}{} {}'.format(render_name(
namespace, self.name), render_labels(self.labelKeys, l), v))
@ -113,7 +113,7 @@ class Summary(Metric):
def render(self, namespace):
nn = render_name(namespace, self.name)
lines = super().render(namespace)
lines = super(Summary, self).render(namespace)
for l, v in self.values.items():
ll = render_labels(self.labelKeys, l)
lines.extend(render_help(nn + '_count', self.desc, self.metricType))

View File

@ -1,6 +1,3 @@
from prometheus_express.server import start_http_server
def check_network(eth):
online = eth.connected
network = eth.ifconfig()

2
setup.cfg Normal file
View File

@ -0,0 +1,2 @@
[metadata]
description-file = README.md

25
setup.py Normal file
View File

@ -0,0 +1,25 @@
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="prometheus_express",
version="0.0.1",
author="Sean Sube",
author_email="seansube@gmail.com",
description="Prometheus client/server for CircuitPython Express ARM devices",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/ssube/prometheus_express",
keywords=[
'prometheus',
],
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.0',
)

View File

@ -24,4 +24,34 @@ class RenderNameTest(unittest.TestCase):
self.assertEqual(
'foo_bar',
pm.render_name('foo', 'bar')
)
)
class MetricRenderTest(unittest.TestCase):
def test(self):
m = pm.Metric('bin', 'bin values', [
'key-1', 'key-2',
])
m.labels('value-1', 'value-2')
r = m.render('foo')
self.assertEqual([
'# HELP foo_bin bin values',
'# TYPE foo_bin untyped',
# no label/value line
], r)
class CounterRenderTest(unittest.TestCase):
def test(self):
m = pm.Counter('bin', 'bin values', [
'key-1', 'key-2',
])
m.labels('value-1', 'value-2')
m.inc(90)
r = m.render('foo')
self.assertEqual([
'# HELP foo_bin bin values',
'# TYPE foo_bin counter',
'foo_bin{key-1="value-1",key-2="value-2"} 90',
'foo_bin{key-1="None",key-2="None"} 0',
], r)