Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(metrics): support configurability of TLS verification, proxy support through requests #17

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
54 changes: 50 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: str = None,
**kwargs
):
"""
Expand All @@ -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,
)


Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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