Skip to content

Commit ea047cf

Browse files
authored
Allow fully qualified collector modules (#45)
* Allow fully qualified collector modules To make development of collectors more indipendent from p3exporter developments we now allow fully qualified (dotted notation) module names. Naming convention of collector class stay in place. * Update CHANGELOG.md add new feature to changelog
1 parent a380a9e commit ea047cf

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a Ch
66
## Unreleased
77

88
### New
9+
* Allow to load external collectors with fully qualified dotted notation
910

1011
### Changes
1112
* Switch sphinx from recommonmark to myst_parser

p3.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
exporter_name: "Python prammable Prometheus exporter"
22
collectors:
3-
- example
3+
- p3exporter.collector.example
44
- loadavg
55
- netdev
66
collector_opts:

p3exporter/collector/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ def collector_name_from_class(self):
6262
class Collector(object):
6363
"""Base class to load collectors.
6464
65-
All collectors have to be placed inside the directory `collector`. You have to follow the naming convention:
65+
Collectors needs to be placed either in the directory `collector` within this module (local) or in a separate module.
66+
Collectors in separate modules needs to be addressed in dotted notation.
67+
68+
Collectors have to follow the following naming convention:
6669
6770
1. Place the collector code in a <name>.py file (e.g. `my.py`)
6871
2. Within the file <name>.py` a class <Name>Collector (e.g. `MyController`) needs to be defined.
@@ -71,10 +74,11 @@ class Collector(object):
7174

7275
def __init__(self, config: CollectorConfig):
7376
"""Instantiate an CollectorBase object."""
74-
for c in config.collectors:
77+
_collectors = [c if "." in c else "p3exporter.collector.{}".format(c) for c in config.collectors]
78+
for c in _collectors:
7579
try:
76-
collector_module = import_module("p3exporter.collector.{}".format(c), package=None)
77-
collector_class = getattr(collector_module, "{0}Collector".format(inflection.camelize(c)))
80+
collector_module = import_module(c, package=None)
81+
collector_class = getattr(collector_module, "{0}Collector".format(inflection.camelize(c.split('.')[-1])))
7882
collector = collector_class(config)
7983
REGISTRY.register(collector)
8084
logging.info("Collector '{0}' was loaded and registred successfully".format(c))

0 commit comments

Comments
 (0)