Skip to content

deep.api.plugin.metric.otel_metrics

Handling for OTEL metric support.

OTelMetrics

Bases: MetricProcessor

Metric processor for otel.

Separate from OTEL Plugin for spans, as you can have one without the other.

Source code in deep/api/plugin/metric/otel_metrics.py
class OTelMetrics(MetricProcessor):
    """
    Metric processor for otel.

    Separate from OTEL Plugin for spans, as you can have one without the other.
    """

    def __init__(self, config):
        """Create new plugin."""
        super().__init__("OTelMetrics", 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:
                counter: Counter
                counter = self.__check_cache(name, 'counter',
                                             lambda: get_meter('deep').create_counter(f'{namespace}_{name}', unit,
                                                                                      help_string))
                counter.add(value, labels)
        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:
                gauge: UpDownCounter
                gauge = self.__check_cache(name, 'gauge',
                                           lambda: get_meter('deep').create_up_down_counter(f'{namespace}_{name}',
                                                                                            unit, help_string))
                gauge.add(value, labels)
        except Exception:
            deep.logging.exception(f"Error registering metric histogram {namespace}_{name}")

    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:
                histogram: Histogram
                histogram = self.__check_cache(name, 'histogram',
                                               lambda: get_meter('deep').create_histogram(f'{namespace}_{name}', unit,
                                                                                          help_string))
                histogram.record(value, labels)
        except Exception:
            deep.logging.exception(f"Error registering metric histogram {namespace}_{name}")

    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:
                histogram: Histogram
                histogram = self.__check_cache(name, 'summary',
                                               lambda: get_meter('deep').create_histogram(f'{namespace}_{name}', unit,
                                                                                          help_string))
                histogram.record(value, labels)
        except Exception:
            deep.logging.exception(f"Error registering metric summary {namespace}_{name}")

__init__(config)

Create new plugin.

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

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/otel_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:
            counter: Counter
            counter = self.__check_cache(name, 'counter',
                                         lambda: get_meter('deep').create_counter(f'{namespace}_{name}', unit,
                                                                                  help_string))
            counter.add(value, labels)
    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/otel_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:
            gauge: UpDownCounter
            gauge = self.__check_cache(name, 'gauge',
                                       lambda: get_meter('deep').create_up_down_counter(f'{namespace}_{name}',
                                                                                        unit, help_string))
            gauge.add(value, labels)
    except Exception:
        deep.logging.exception(f"Error registering metric histogram {namespace}_{name}")

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/otel_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:
            histogram: Histogram
            histogram = self.__check_cache(name, 'histogram',
                                           lambda: get_meter('deep').create_histogram(f'{namespace}_{name}', unit,
                                                                                      help_string))
            histogram.record(value, labels)
    except Exception:
        deep.logging.exception(f"Error registering metric histogram {namespace}_{name}")

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/otel_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:
            histogram: Histogram
            histogram = self.__check_cache(name, 'summary',
                                           lambda: get_meter('deep').create_histogram(f'{namespace}_{name}', unit,
                                                                                      help_string))
            histogram.record(value, labels)
    except Exception:
        deep.logging.exception(f"Error registering metric summary {namespace}_{name}")