Skip to content

Commit

Permalink
Allow fully qualified collector modules (#45)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
cmeissner committed Jul 8, 2022
1 parent a380a9e commit ea047cf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a Ch
## Unreleased

### New
* Allow to load external collectors with fully qualified dotted notation

### Changes
* Switch sphinx from recommonmark to myst_parser
Expand Down
2 changes: 1 addition & 1 deletion p3.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
exporter_name: "Python prammable Prometheus exporter"
collectors:
- example
- p3exporter.collector.example
- loadavg
- netdev
collector_opts:
Expand Down
12 changes: 8 additions & 4 deletions p3exporter/collector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def collector_name_from_class(self):
class Collector(object):
"""Base class to load collectors.
All collectors have to be placed inside the directory `collector`. You have to follow the naming convention:
Collectors needs to be placed either in the directory `collector` within this module (local) or in a separate module.
Collectors in separate modules needs to be addressed in dotted notation.
Collectors have to follow the following naming convention:
1. Place the collector code in a <name>.py file (e.g. `my.py`)
2. Within the file <name>.py` a class <Name>Collector (e.g. `MyController`) needs to be defined.
Expand All @@ -71,10 +74,11 @@ class Collector(object):

def __init__(self, config: CollectorConfig):
"""Instantiate an CollectorBase object."""
for c in config.collectors:
_collectors = [c if "." in c else "p3exporter.collector.{}".format(c) for c in config.collectors]
for c in _collectors:
try:
collector_module = import_module("p3exporter.collector.{}".format(c), package=None)
collector_class = getattr(collector_module, "{0}Collector".format(inflection.camelize(c)))
collector_module = import_module(c, package=None)
collector_class = getattr(collector_module, "{0}Collector".format(inflection.camelize(c.split('.')[-1])))
collector = collector_class(config)
REGISTRY.register(collector)
logging.info("Collector '{0}' was loaded and registred successfully".format(c))
Expand Down

0 comments on commit ea047cf

Please sign in to comment.