Skip to content

deep.api.plugin.metric.prometheus_metrics

Add support for prometheus metrics.

PrometheusPlugin

Bases: MetricProcessor

Connect Deep to prometheus.

Source code in deep/api/plugin/metric/prometheus_metrics.py
class PrometheusPlugin(MetricProcessor):
    """Connect Deep to prometheus."""

    def __init__(self, config):
        """Create new plugin."""
        super().__init__("PrometheusPlugin", config)
        self.__cache = {}
        self.__lock = threading.Lock()

    def __check_cache(self, name, type_name, from_default):
        cache_key = f'{name}_{type_name}'
        if cache_key in self.__cache:
            return self.__cache[cache_key]
        default = from_default()
        self.__cache[cache_key] = default
        return default

    def counter(self, name: str, labels: Dict[str, str], namespace: str, help_string: str, unit: str, value: float):
        """
        Create a counter type value in the provider.

        :param name: the metric name
        :param labels: the metric labels
        :param namespace: the metric namespace
        :param help_string: the metric help string
        :param unit: the metric unit
        :param value: the metric value
        """
        try:
            with self.__lock:
                label_keys = list(labels.keys())
                counter: Counter = self.__check_cache(name, "counter",
                                                      lambda: Counter(name=name, documentation=help_string or "",
                                                                      labelnames=label_keys,
                                                                      namespace=namespace, unit=unit))
                if len(labels) > 0:
                    counter = counter.labels(**labels)
                counter.inc(value)
        except Exception:
            deep.logging.exception(f"Error registering metric counter {namespace}_{name}")

    def gauge(self, name: str, labels: Dict[str, str], namespace: str, help_string: str, unit: str, value: float):
        """
        Create a gauge type value in the provider.

        :param name: the metric name
        :param labels: the metric labels
        :param namespace: the metric namespace
        :param help_string: the metric help string
        :param unit: the metric unit
        :param value: the metric value
        """
        try:
            with self.__lock:
                label_keys = list(labels.keys())
                gauge: Gauge = self.__check_cache(name, "gauge",
                                                  lambda: Gauge(name=name, documentation=help_string or "",
                                                                labelnames=label_keys,
                                                                namespace=namespace, unit=unit))
                if len(labels) > 0:
                    gauge = gauge.labels(**labels)
                gauge.inc(value)
        except Exception:
            deep.logging.exception(f"Error registering metric gauge {namespace}_{name}")
            pass

    def histogram(self, name: str, labels: Dict[str, str], namespace: str, help_string: str, unit: str, value: float):
        """
        Create a histogram type value in the provider.

        :param name: the metric name
        :param labels: the metric labels
        :param namespace: the metric namespace
        :param help_string: the metric help string
        :param unit: the metric unit
        :param value: the metric value
        """
        try:
            with self.__lock:
                label_keys = list(labels.keys())
                histogram: Histogram = self.__check_cache(name, "histogram",
                                                          lambda: Histogram(name=name, documentation=help_string or "",
                                                                            labelnames=label_keys,
                                                                            namespace=namespace, unit=unit))
                if len(labels) > 0:
                    histogram = histogram.labels(**labels)
                histogram.observe(value)
        except Exception:
            deep.logging.exception(f"Error registering metric histogram {namespace}_{name}")
            pass

    def summary(self, name: str, labels: Dict[str, str], namespace: str, help_string: str, unit: str, value: float):
        """
        Create a summary type value in the provider.

        :param name: the metric name
        :param labels: the metric labels
        :param namespace: the metric namespace
        :param help_string: the metric help string
        :param unit: the metric unit
        :param value: the metric value
        """
        try:
            with self.__lock:
                label_keys = list(labels.keys())
                summary: Summary = self.__check_cache(name, "summary",
                                                      lambda: Summary(name=name, documentation=help_string or "",
                                                                      labelnames=label_keys,
                                                                      namespace=namespace, unit=unit))
                if len(labels) > 0:
                    summary = summary.labels(**labels)
                summary.observe(value)
        except Exception:
            deep.logging.exception(f"Error registering metric summary {namespace}_{name}")
            pass

    @property
    def _cache(self):
        return self.__cache

    def shutdown(self):
        """Clean up and shutdown the plugin."""
        self.clear()

    def clear(self):
        """Remove any registrations."""
        with self.__lock:
            for metric in self.__cache.values():
                REGISTRY.unregister(metric)
            self.__cache = {}

__init__(config)

Create new plugin.

Source code in deep/api/plugin/metric/prometheus_metrics.py
def __init__(self, config):
    """Create new plugin."""
    super().__init__("PrometheusPlugin", config)
    self.__cache = {}
    self.__lock = threading.Lock()

clear()

Remove any registrations.

Source code in deep/api/plugin/metric/prometheus_metrics.py
def clear(self):
    """Remove any registrations."""
    with self.__lock:
        for metric in self.__cache.values():
            REGISTRY.unregister(metric)
        self.__cache = {}

counter(name, labels, namespace, help_string, unit, value)

Create a counter type value in the provider.

:param name: the metric name :param labels: the metric labels :param namespace: the metric namespace :param help_string: the metric help string :param unit: the metric unit :param value: the metric value

Source code in deep/api/plugin/metric/prometheus_metrics.py
def counter(self, name: str, labels: Dict[str, str], namespace: str, help_string: str, unit: str, value: float):
    """
    Create a counter type value in the provider.

    :param name: the metric name
    :param labels: the metric labels
    :param namespace: the metric namespace
    :param help_string: the metric help string
    :param unit: the metric unit
    :param value: the metric value
    """
    try:
        with self.__lock:
            label_keys = list(labels.keys())
            counter: Counter = self.__check_cache(name, "counter",
                                                  lambda: Counter(name=name, documentation=help_string or "",
                                                                  labelnames=label_keys,
                                                                  namespace=namespace, unit=unit))
            if len(labels) > 0:
                counter = counter.labels(**labels)
            counter.inc(value)
    except Exception:
        deep.logging.exception(f"Error registering metric counter {namespace}_{name}")

gauge(name, labels, namespace, help_string, unit, value)

Create a gauge type value in the provider.

:param name: the metric name :param labels: the metric labels :param namespace: the metric namespace :param help_string: the metric help string :param unit: the metric unit :param value: the metric value

Source code in deep/api/plugin/metric/prometheus_metrics.py
def gauge(self, name: str, labels: Dict[str, str], namespace: str, help_string: str, unit: str, value: float):
    """
    Create a gauge type value in the provider.

    :param name: the metric name
    :param labels: the metric labels
    :param namespace: the metric namespace
    :param help_string: the metric help string
    :param unit: the metric unit
    :param value: the metric value
    """
    try:
        with self.__lock:
            label_keys = list(labels.keys())
            gauge: Gauge = self.__check_cache(name, "gauge",
                                              lambda: Gauge(name=name, documentation=help_string or "",
                                                            labelnames=label_keys,
                                                            namespace=namespace, unit=unit))
            if len(labels) > 0:
                gauge = gauge.labels(**labels)
            gauge.inc(value)
    except Exception:
        deep.logging.exception(f"Error registering metric gauge {namespace}_{name}")
        pass

histogram(name, labels, namespace, help_string, unit, value)

Create a histogram type value in the provider.

:param name: the metric name :param labels: the metric labels :param namespace: the metric namespace :param help_string: the metric help string :param unit: the metric unit :param value: the metric value

Source code in deep/api/plugin/metric/prometheus_metrics.py
def histogram(self, name: str, labels: Dict[str, str], namespace: str, help_string: str, unit: str, value: float):
    """
    Create a histogram type value in the provider.

    :param name: the metric name
    :param labels: the metric labels
    :param namespace: the metric namespace
    :param help_string: the metric help string
    :param unit: the metric unit
    :param value: the metric value
    """
    try:
        with self.__lock:
            label_keys = list(labels.keys())
            histogram: Histogram = self.__check_cache(name, "histogram",
                                                      lambda: Histogram(name=name, documentation=help_string or "",
                                                                        labelnames=label_keys,
                                                                        namespace=namespace, unit=unit))
            if len(labels) > 0:
                histogram = histogram.labels(**labels)
            histogram.observe(value)
    except Exception:
        deep.logging.exception(f"Error registering metric histogram {namespace}_{name}")
        pass

shutdown()

Clean up and shutdown the plugin.

Source code in deep/api/plugin/metric/prometheus_metrics.py
def shutdown(self):
    """Clean up and shutdown the plugin."""
    self.clear()

summary(name, labels, namespace, help_string, unit, value)

Create a summary type value in the provider.

:param name: the metric name :param labels: the metric labels :param namespace: the metric namespace :param help_string: the metric help string :param unit: the metric unit :param value: the metric value

Source code in deep/api/plugin/metric/prometheus_metrics.py
def summary(self, name: str, labels: Dict[str, str], namespace: str, help_string: str, unit: str, value: float):
    """
    Create a summary type value in the provider.

    :param name: the metric name
    :param labels: the metric labels
    :param namespace: the metric namespace
    :param help_string: the metric help string
    :param unit: the metric unit
    :param value: the metric value
    """
    try:
        with self.__lock:
            label_keys = list(labels.keys())
            summary: Summary = self.__check_cache(name, "summary",
                                                  lambda: Summary(name=name, documentation=help_string or "",
                                                                  labelnames=label_keys,
                                                                  namespace=namespace, unit=unit))
            if len(labels) > 0:
                summary = summary.labels(**labels)
            summary.observe(value)
    except Exception:
        deep.logging.exception(f"Error registering metric summary {namespace}_{name}")
        pass