forked from OpenCTI-Platform/client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencti_metric_handler.py
116 lines (104 loc) · 3.64 KB
/
opencti_metric_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from typing import Type, Union
from prometheus_client import Counter, Enum, start_http_server
class OpenCTIMetricHandler:
def __init__(self, connector_logger, activated: bool = False, port: int = 9095):
"""
Init of OpenCTIMetricHandler class.
Parameters
----------
activated : bool, default False
If True use metrics in client and connectors.
port : int, default 9095
Port for prometheus server.
"""
self.activated = activated
self.connector_logger = connector_logger
if self.activated:
self.connector_logger.info("Exposing metrics on port", {"port": port})
start_http_server(port)
self._metrics = {
"bundle_send": Counter(
"bundle_send",
"Number of bundle send",
),
"record_send": Counter(
"record_send",
"Number of record (objects per bundle) send",
),
"run_count": Counter(
"run_count",
"Number of run",
),
"ping_api_count": Counter(
"ping_api_count",
"Number of ping to the api",
),
"ping_api_error": Counter(
"ping_api_error",
"Number of error when pinging the api",
),
"error_count": Counter(
"error_count",
"Number of error",
),
"client_error_count": Counter(
"client_error_count",
"Number of client error",
),
"state": Enum(
"state", "State of connector", states=["idle", "running", "stopped"]
),
}
def _metric_exists(
self, name: str, expected_type: Union[Type[Counter], Type[Enum]]
) -> bool:
"""
Check if a metric exists and has the correct type.
If it does not, log an error and return False.
Parameters
----------
name : str
Name of the metric to check.
expected_type : Counter or Enum
Expected type of the metric.
Returns
-------
bool
True if the metric exists and is of the correct type else False.
"""
if name not in self._metrics:
self.connector_logger.error("Metric does not exist.", {"name": name})
return False
if not isinstance(self._metrics[name], expected_type):
self.connector_logger.error(
"Metric not of expected type",
{"name": name, "expected_type": expected_type},
)
return False
return True
def inc(self, name: str, n: int = 1):
"""
Increment the metric (counter) `name` by `n`.
Parameters
----------
name : str
Name of the metric to increment.
n : int, default 1
Increment the counter by `n`.
"""
if self.activated:
if self._metric_exists(name, Counter):
self._metrics[name].inc(n)
def state(self, state: str, name: str = "state"):
"""
Set the state `state` for metric `name`.
Parameters
----------
state : str
State to set.
name : str, default = "state"
Name of the metric to set.
"""
if self.activated:
if self._metric_exists(name, Enum):
self._metrics[name].state(state)