Skip to content

[metrics] add namespace, subsystem and refactor names #847

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 19 additions & 1 deletion pycti/connector/opencti_connector_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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(
Expand Down
59 changes: 44 additions & 15 deletions pycti/connector/opencti_metric_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@


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.

Parameters
----------
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.
"""
Expand All @@ -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,
),
}

Expand Down