diff --git a/CHANGELOG.md b/CHANGELOG.md index 957d3a5..11714b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,17 @@ [Unreleased]: https://github.com/chaostoolkit/chaostoolkit-prometheus/compare/0.6.0...HEAD +### Added + +- Add new `verify_tls` flag for the control to disable/enable the verification of TLS certificates + +### Changed + +- Implemented a custom handler for the prometheus `push_to_gateway` function that uses `requests`. + This enables features like the use of HTTP proxies. +- Allow setting of `experiment_ref`, `trace_id`, `pushgateway_url` and `verify_tls` through a + `configuration` block. Direct configuration through arguments is still possible. + ## [0.6.0][] - 2023-09-19 [0.6.0]: https://github.com/chaostoolkit/chaostoolkit-prometheus/compare/0.5.0...0.6.0 diff --git a/chaosprometheus/metrics.py b/chaosprometheus/metrics.py index f099bad..6de7f0d 100644 --- a/chaosprometheus/metrics.py +++ b/chaosprometheus/metrics.py @@ -1,8 +1,8 @@ from secrets import token_hex -from typing import Dict, List +from typing import Any, Callable, Dict, List from chaoslib import __version__, experiment_hash -from chaoslib.types import Experiment, Journal +from chaoslib.types import Configuration, Experiment, Journal from prometheus_client import ( CollectorRegistry, Counter, @@ -11,6 +11,7 @@ Histogram, push_to_gateway, ) +from requests import Session __all__ = ["configure_control", "after_experiment_control"] @@ -19,12 +20,14 @@ def configure_control( - experiment: Experiment, + configuration: Configuration = None, + experiment: Experiment = None, pushgateway_url: str = "http://localhost:9091", job: str = "chaostoolkit", grouping_key: Dict[str, str] = None, trace_id: str = None, experiment_ref: str = None, + verify_tls: str = None, **kwargs ): """ @@ -47,8 +50,24 @@ def configure_control( provided, it'll be set to a random string as a label. """ global collector + + pushgateway_url = pushgateway_url or configuration.get( + "pushgateway_url", "http://localhost:9091" + ) + experiment_ref = experiment_ref or configuration.get("experiment_ref") + trace_id = trace_id or configuration.get("trace_id") + verify_tls = verify_tls or configuration.get("verify_tls") + if not verify_tls: + verify_tls = "true" + collector = PrometheusCollector( - pushgateway_url, job, trace_id, experiment_ref, grouping_key, experiment + pushgateway_url, + job, + trace_id, + experiment_ref, + grouping_key, + experiment, + verify_tls, ) @@ -74,6 +93,8 @@ def after_experiment_control(state: Journal, *args, **kwargs) -> None: # Private functions ############################################################################### class PrometheusCollector: + verify_tls = True + def __init__( self, pushgateway_url: str, @@ -82,6 +103,7 @@ def __init__( experiment_ref: str, grouping_key: Dict[str, str], experiment: Experiment, + verify_tls: str, ) -> None: self.pushgateway_url = pushgateway_url self.job = job @@ -90,6 +112,7 @@ def __init__( self.grouping_key = grouping_key or { "chaostoolkit_experiment_ref": self.experiment_ref } + PrometheusCollector.verify_tls = verify_tls.upper() == "TRUE" labels = [ "source", @@ -160,4 +183,27 @@ def push(self) -> None: job=self.job, registry=self.registry, grouping_key=self.grouping_key, + handler=_custom_handler, ) + + +def _custom_handler( + url: str, + method: str, + timeout: int, + headers: list, + data: Any, +) -> Callable: + """ + Bare bones custom handler for pushing metrics to enable more complex + scenarios. This function is fed into push_to_gateway() + We also use requests to benefit from its feature set like e.g. proxy support + """ + + def handler() -> None: + s = Session() + s.verify = PrometheusCollector.verify_tls + h = {k: v for k, v in headers} + s.request(url=url, method=method, headers=h, data=data, timeout=timeout) + + return handler