|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from collections import Counter |
| 18 | +from collections.abc import Callable |
| 19 | +from typing import Literal |
| 20 | + |
| 21 | +from opentelemetry.metrics import CallbackOptions, MeterProvider, Observation |
| 22 | +from opentelemetry.semconv._incubating.attributes.otel_attributes import ( |
| 23 | + OTEL_COMPONENT_NAME, |
| 24 | + OTEL_COMPONENT_TYPE, |
| 25 | + OtelComponentTypeValues, |
| 26 | +) |
| 27 | +from opentelemetry.semconv._incubating.metrics.otel_metrics import ( |
| 28 | + OTEL_SDK_PROCESSOR_LOG_QUEUE_SIZE, |
| 29 | + OTEL_SDK_PROCESSOR_SPAN_QUEUE_SIZE, |
| 30 | + create_otel_sdk_processor_log_processed, |
| 31 | + create_otel_sdk_processor_log_queue_capacity, |
| 32 | + create_otel_sdk_processor_span_processed, |
| 33 | + create_otel_sdk_processor_span_queue_capacity, |
| 34 | +) |
| 35 | +from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE |
| 36 | + |
| 37 | +_component_counter = Counter() |
| 38 | + |
| 39 | + |
| 40 | +class ProcessorMetrics: |
| 41 | + def __init__( |
| 42 | + self, |
| 43 | + signal: Literal["traces", "logs"], |
| 44 | + component_type: OtelComponentTypeValues, |
| 45 | + meter_provider: MeterProvider, |
| 46 | + *, |
| 47 | + capacity: int | None = None, |
| 48 | + ) -> None: |
| 49 | + self._signal = signal |
| 50 | + meter = meter_provider.get_meter("opentelemetry-sdk") |
| 51 | + self._meter = meter |
| 52 | + |
| 53 | + count = _component_counter[component_type.value] |
| 54 | + _component_counter[component_type.value] = count + 1 |
| 55 | + |
| 56 | + self._standard_attrs = { |
| 57 | + OTEL_COMPONENT_TYPE: component_type.value, |
| 58 | + OTEL_COMPONENT_NAME: f"{component_type.value}/{count}", |
| 59 | + } |
| 60 | + |
| 61 | + self._dropped_attrs = { |
| 62 | + **self._standard_attrs, |
| 63 | + ERROR_TYPE: "queue_full", |
| 64 | + } |
| 65 | + |
| 66 | + if signal == "traces": |
| 67 | + create_processed = create_otel_sdk_processor_span_processed |
| 68 | + create_queue_capacity = ( |
| 69 | + create_otel_sdk_processor_span_queue_capacity |
| 70 | + ) |
| 71 | + else: |
| 72 | + create_processed = create_otel_sdk_processor_log_processed |
| 73 | + create_queue_capacity = ( |
| 74 | + create_otel_sdk_processor_log_queue_capacity |
| 75 | + ) |
| 76 | + |
| 77 | + self._processed = create_processed(meter) |
| 78 | + |
| 79 | + if capacity is not None: |
| 80 | + self._queue_capacity = create_queue_capacity(meter) |
| 81 | + self._queue_capacity.add(capacity, self._standard_attrs) |
| 82 | + |
| 83 | + def register_queue_size(self, get_queue_size: Callable[[], int]) -> None: |
| 84 | + def record_queue_size( |
| 85 | + _options: CallbackOptions, |
| 86 | + ) -> tuple[Observation]: |
| 87 | + return (Observation(get_queue_size(), self._standard_attrs),) |
| 88 | + |
| 89 | + if self._signal == "traces": |
| 90 | + queue_size_name = OTEL_SDK_PROCESSOR_SPAN_QUEUE_SIZE |
| 91 | + queue_size_description = "The number of spans in the queue of a given instance of an SDK span processor." |
| 92 | + queue_size_unit = "{span}" |
| 93 | + else: |
| 94 | + queue_size_name = OTEL_SDK_PROCESSOR_LOG_QUEUE_SIZE |
| 95 | + queue_size_description = "The number of logs in the queue of a given instance of an SDK log processor." |
| 96 | + queue_size_unit = "{log}" |
| 97 | + |
| 98 | + self._meter.create_observable_up_down_counter( |
| 99 | + queue_size_name, |
| 100 | + callbacks=(record_queue_size,), |
| 101 | + description=queue_size_description, |
| 102 | + unit=queue_size_unit, |
| 103 | + ) |
| 104 | + |
| 105 | + def drop_items(self, count: int) -> None: |
| 106 | + self._processed.add(count, self._dropped_attrs) |
| 107 | + |
| 108 | + def finish_items(self, count: int, error: Exception | None) -> None: |
| 109 | + if not error: |
| 110 | + self._processed.add(count, self._standard_attrs) |
| 111 | + return |
| 112 | + attrs = { |
| 113 | + **self._standard_attrs, |
| 114 | + ERROR_TYPE: type(error).__name__, |
| 115 | + } |
| 116 | + self._processed.add(count, attrs) |
0 commit comments