From 94bf38c88aa126e2ad174aeabf9ddca635713e05 Mon Sep 17 00:00:00 2001 From: Axel Fahy Date: Wed, 19 Feb 2025 11:29:33 +0100 Subject: [PATCH] [metrics] add namespace, subsystem and refactor names --- pycti/connector/opencti_connector_helper.py | 20 ++++++- pycti/connector/opencti_metric_handler.py | 59 +++++++++++++++------ 2 files changed, 63 insertions(+), 16 deletions(-) diff --git a/pycti/connector/opencti_connector_helper.py b/pycti/connector/opencti_connector_helper.py index 0441d88e..c0a11fb9 100644 --- a/pycti/connector/opencti_connector_helper.py +++ b/pycti/connector/opencti_connector_helper.py @@ -912,6 +912,20 @@ def __init__(self, config: Dict, playbook_compatible=False) -> None: False, False, ) + metrics_namespace = get_config_variable( + "CONNECTOR_METRICS_NAMESPACE", + ["connector", "metrics_namespace"], + config, + False, + "", + ) + metrics_subsystem = get_config_variable( + "CONNECTOR_METRICS_SUBSYSTEM", + ["connector", "metrics_subsystem"], + config, + False, + "", + ) metrics_port = get_config_variable( "CONNECTOR_METRICS_PORT", ["connector", "metrics_port"], config, True, 9095 ) @@ -946,7 +960,11 @@ def __init__(self, config: Dict, playbook_compatible=False) -> None: # For retro compatibility self.metric = OpenCTIMetricHandler( - self.connector_logger, expose_metrics, metrics_port + self.connector_logger, + expose_metrics, + metrics_namespace, + metrics_subsystem, + metrics_port, ) # Register the connector in OpenCTI self.connector = OpenCTIConnector( diff --git a/pycti/connector/opencti_metric_handler.py b/pycti/connector/opencti_metric_handler.py index a4971c6d..988ad7cb 100644 --- a/pycti/connector/opencti_metric_handler.py +++ b/pycti/connector/opencti_metric_handler.py @@ -4,7 +4,14 @@ class OpenCTIMetricHandler: - def __init__(self, connector_logger, activated: bool = False, port: int = 9095): + def __init__( + self, + connector_logger, + activated: bool = False, + namespace: str = "", + subsystem: str = "", + port: int = 9095, + ): """ Init of OpenCTIMetricHandler class. @@ -12,6 +19,10 @@ def __init__(self, connector_logger, activated: bool = False, port: int = 9095): ---------- activated : bool, default False If True use metrics in client and connectors. + namespace: str, default empty + Namespace for the prometheus metrics. + subsystem: str, default empty + Subsystem for the prometheus metrics. port : int, default 9095 Port for prometheus server. """ @@ -22,35 +33,53 @@ def __init__(self, connector_logger, activated: bool = False, port: int = 9095): start_http_server(port) self._metrics = { "bundle_send": Counter( - "bundle_send", - "Number of bundle send", + "bundles_sent_total", + "Number of bundles sent", + namespace=namespace, + subsystem=subsystem, ), "record_send": Counter( - "record_send", - "Number of record (objects per bundle) send", + "records_sent_total", + "Number of records (objects per bundle) sent", + namespace=namespace, + subsystem=subsystem, ), "run_count": Counter( - "run_count", + "runs_total", "Number of run", + namespace=namespace, + subsystem=subsystem, ), "ping_api_count": Counter( - "ping_api_count", - "Number of ping to the api", + "ping_api_total", + "Number of pings to the API", + namespace=namespace, + subsystem=subsystem, ), "ping_api_error": Counter( - "ping_api_error", - "Number of error when pinging the api", + "ping_api_errors_total", + "Number of errors when pinging the API", + namespace=namespace, + subsystem=subsystem, ), "error_count": Counter( - "error_count", - "Number of error", + "errors_total", + "Number of errors", + namespace=namespace, + subsystem=subsystem, ), "client_error_count": Counter( - "client_error_count", - "Number of client error", + "client_errors_total", + "Number of client errors", + namespace=namespace, + subsystem=subsystem, ), "state": Enum( - "state", "State of connector", states=["idle", "running", "stopped"] + "state", + "State of connector", + states=["idle", "running", "stopped"], + namespace=namespace, + subsystem=subsystem, ), }