Skip to content

Commit 0ac316d

Browse files
feat(metrics): support configurability of TLS verification, proxy support 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.
1 parent 0de417c commit 0ac316d

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

chaosprometheus/metrics.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def configure_control(
2525
grouping_key: Dict[str, str] = None,
2626
trace_id: str = None,
2727
experiment_ref: str = None,
28+
verify_tls: bool = None,
2829
**kwargs
2930
):
3031
"""
@@ -47,8 +48,14 @@ def configure_control(
4748
provided, it'll be set to a random string as a label.
4849
"""
4950
global collector
51+
52+
pushgateway_url = pushgateway_url or configuration.get('pushgateway_url', 'http://localhost:9091')
53+
experiment_ref = experiment_ref or configuration.get('experiment_ref')
54+
trace_id = trace_id or configuration.get('trace_id')
55+
verify_tls = verify_tls or configuration.get('verify_tls')
56+
5057
collector = PrometheusCollector(
51-
pushgateway_url, job, trace_id, experiment_ref, grouping_key, experiment
58+
pushgateway_url, job, trace_id, experiment_ref, grouping_key, experiment, verify_tls
5259
)
5360

5461

@@ -74,6 +81,7 @@ def after_experiment_control(state: Journal, *args, **kwargs) -> None:
7481
# Private functions
7582
###############################################################################
7683
class PrometheusCollector:
84+
verify_tls = True
7785
def __init__(
7886
self,
7987
pushgateway_url: str,
@@ -82,6 +90,7 @@ def __init__(
8290
experiment_ref: str,
8391
grouping_key: Dict[str, str],
8492
experiment: Experiment,
93+
verify_tls: bool
8594
) -> None:
8695
self.pushgateway_url = pushgateway_url
8796
self.job = job
@@ -90,6 +99,8 @@ def __init__(
9099
self.grouping_key = grouping_key or {
91100
"chaostoolkit_experiment_ref": self.experiment_ref
92101
}
102+
if verify_tls != None:
103+
PrometheusCollector.verify_tls = verify_tls
93104

94105
labels = [
95106
"source",
@@ -161,3 +172,23 @@ def push(self) -> None:
161172
registry=self.registry,
162173
grouping_key=self.grouping_key,
163174
)
175+
176+
177+
def _custom_handler(
178+
url: str,
179+
method: str,
180+
timeout: int,
181+
headers: list,
182+
data: Any,
183+
) -> Callable:
184+
"""
185+
Bare bones custom handler for pushing metrics to enable more complex
186+
scenarios. This function is fed into push_to_gateway()
187+
We also use requests to benefit from its feature set like e.g. proxy support
188+
"""
189+
def handler() -> None:
190+
s = Session()
191+
s.verify = PrometheusCollector.verify_tls
192+
h = {k:v for k,v in headers}
193+
s.request(url=url, method=method, headers=h, data=data, timeout=timeout)
194+
return handler

0 commit comments

Comments
 (0)