Skip to content

Commit

Permalink
feat(metrics): support configurability of TLS verification, proxy sup…
Browse files Browse the repository at this point in the history
…port through requests

Introducing a new configuration flag `verify_tls` for the control that
enables the user to disable TLS verification.

Implement a custom handler for pushing metrics that uses `requests` to
benefit of its feature set like proxy support.

Signed-off-by: Mattias Cockburn <[email protected]>
  • Loading branch information
mattiascockburn committed Sep 25, 2023
1 parent 0de417c commit e4b2516
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 48 additions & 4 deletions chaosprometheus/metrics.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -11,6 +11,7 @@
Histogram,
push_to_gateway,
)
from requests import Session

__all__ = ["configure_control", "after_experiment_control"]

Expand All @@ -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: bool = None,
**kwargs
):
"""
Expand All @@ -47,8 +50,22 @@ 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")

collector = PrometheusCollector(
pushgateway_url, job, trace_id, experiment_ref, grouping_key, experiment
pushgateway_url,
job,
trace_id,
experiment_ref,
grouping_key,
experiment,
verify_tls,
)


Expand All @@ -74,6 +91,8 @@ def after_experiment_control(state: Journal, *args, **kwargs) -> None:
# Private functions
###############################################################################
class PrometheusCollector:
verify_tls = True

def __init__(
self,
pushgateway_url: str,
Expand All @@ -82,6 +101,7 @@ def __init__(
experiment_ref: str,
grouping_key: Dict[str, str],
experiment: Experiment,
verify_tls: bool,
) -> None:
self.pushgateway_url = pushgateway_url
self.job = job
Expand All @@ -90,6 +110,8 @@ def __init__(
self.grouping_key = grouping_key or {
"chaostoolkit_experiment_ref": self.experiment_ref
}
if verify_tls is not None:
PrometheusCollector.verify_tls = verify_tls

labels = [
"source",
Expand Down Expand Up @@ -161,3 +183,25 @@ def push(self) -> None:
registry=self.registry,
grouping_key=self.grouping_key,
)


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

0 comments on commit e4b2516

Please sign in to comment.