pycti.connector.opencti_metric_handler

OpenCTI Metric Handler module.

This module provides Prometheus metrics support for OpenCTI connectors, allowing monitoring of connector performance and health.

Classes

OpenCTIMetricHandler

Handler for Prometheus metrics in OpenCTI connectors.

Module Contents

class pycti.connector.opencti_metric_handler.OpenCTIMetricHandler(connector_logger, activated: bool = False, namespace: str = '', subsystem: str = '', port: int = 9095)[source]

Handler for Prometheus metrics in OpenCTI connectors.

This class manages Prometheus metrics for monitoring connector behavior, including bundle sends, records processed, run counts, API pings, and errors.

When activated, it starts an HTTP server to expose metrics for scraping by Prometheus or compatible monitoring systems.

Parameters:
  • connector_logger (logging.Logger) – Logger instance for the connector

  • activated (bool) – Whether to enable metrics collection and exposure

  • namespace (str) – Prometheus metrics namespace prefix

  • subsystem (str) – Prometheus metrics subsystem prefix

  • port (int) – Port number for the Prometheus HTTP server

Example

>>> handler = OpenCTIMetricHandler(
...     connector_logger=logger,
...     activated=True,
...     namespace="opencti",
...     subsystem="connector",
...     port=9095
... )
>>> handler.inc("bundle_send")
>>> handler.state("running")

Initialize the OpenCTIMetricHandler instance.

Parameters:
  • connector_logger (logging.Logger) – Logger instance for the connector

  • activated (bool) – Whether to enable metrics (default: False)

  • namespace (str) – Prometheus metrics namespace prefix (default: “”)

  • subsystem (str) – Prometheus metrics subsystem prefix (default: “”)

  • port (int) – Port for Prometheus HTTP server (default: 9095)

activated = False[source]
connector_logger[source]
inc(name: str, n: int = 1) None[source]

Increment a counter metric by a specified amount.

Increments the named counter metric. If metrics are not activated or the metric does not exist, this method does nothing.

Available counter metrics:
  • bundle_send: Number of bundles sent

  • record_send: Number of records sent

  • run_count: Number of connector runs

  • ping_api_count: Number of API pings

  • ping_api_error: Number of API ping errors

  • error_count: Total number of errors

  • client_error_count: Number of client errors

Parameters:
  • name (str) – Name of the counter metric to increment

  • n (int) – Amount to increment the counter by (default: 1)

Example

>>> handler.inc("bundle_send")
>>> handler.inc("record_send", 10)
state(state: str, name: str = 'state') None[source]

Set the state of an Enum metric.

Updates the named Enum metric to the specified state value. If metrics are not activated or the metric does not exist, this method does nothing.

Available states for the default “state” metric:
  • idle: Connector is idle

  • running: Connector is running

  • stopped: Connector is stopped

Parameters:
  • state (str) – State value to set (must be a valid state for the metric)

  • name (str) – Name of the Enum metric to update (default: “state”)

Example

>>> handler.state("running")
>>> handler.state("idle", "state")