ferruzzi commented on code in PR #30873:
URL: https://github.com/apache/airflow/pull/30873#discussion_r1178522012


##########
airflow/metrics/otel_logger.py:
##########
@@ -0,0 +1,234 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+import warnings
+from typing import Callable
+
+from opentelemetry import metrics
+from opentelemetry.exporter.otlp.proto.http.metric_exporter import 
OTLPMetricExporter
+from opentelemetry.metrics import Instrument
+from opentelemetry.sdk.metrics import MeterProvider
+from opentelemetry.sdk.metrics._internal.export import ConsoleMetricExporter, 
PeriodicExportingMetricReader
+from opentelemetry.sdk.resources import SERVICE_NAME, Resource
+from opentelemetry.util.types import Attributes
+
+from airflow.configuration import conf
+from airflow.metrics.protocols import DeltaType, Timer, TimerProtocol
+from airflow.metrics.validators import AllowListValidator, validate_stat
+
+# This is currently the only UDC used.  If more are added, we should add a 
better system for this.
+UP_DOWN_COUNTERS = {"airflow.dag_processing.processes"}
+OTEL_NAME_MAX_LENGTH = 63
+METRIC_NAME_PREFIX = "airflow."
+
+
+def _is_up_down_counter(name):
+    return name in UP_DOWN_COUNTERS
+
+
+class SafeOtelLogger:
+    """Otel Logger"""
+
+    def __init__(self, otel_provider, prefix: str = "airflow", 
allow_list_validator=AllowListValidator()):
+        self.otel: Callable = otel_provider
+        self.prefix: str = prefix
+        self.metrics_validator = allow_list_validator
+        self.meter = otel_provider.get_meter(__name__)
+        self.metrics_map = MetricsMap(self.meter)
+
+    @validate_stat
+    def incr(self, stat: str, count: int = 1, rate: float = 1, tags: 
Attributes = None):
+        """
+        Increment stat by count.
+
+        :param stat: The name of the stat to increment.
+        :param count: A positive integer to add to the current value of stat.
+        :param rate: TODO: define me
+        :param tags: Tags to append to the stat.
+        """
+        if (count < 0) or (rate < 0):
+            raise ValueError("count and rate must both be positive values.")
+        # TODO: I don't think this is the right use for rate???
+        value = count * rate
+
+        if self.metrics_validator.test(stat):
+            counter = self.metrics_map.get_counter(f"{self.prefix}.{stat}")
+            return counter.add(value, attributes=tags)
+
+    @validate_stat
+    def decr(self, stat: str, count: int = 1, rate: float = 1, tags: 
Attributes = None):
+        """
+        Decrement stat by count.
+
+        :param stat: The name of the stat to increment.
+        :param count: A positive integer to subtract from current value of 
stat.
+        :param rate: TODO: define me
+        :param tags: Tags to append to the stat.
+        """
+        if (count < 0) or (rate < 0):
+            raise ValueError("count and rate must both be positive values.")
+        # TODO: I don't think this is the right use for rate???
+        value = count * rate
+
+        if self.metrics_validator.test(stat):
+            counter = self.metrics_map.get_counter(f"{self.prefix}.{stat}")
+            return counter.add(-value, attributes=tags)
+
+    @validate_stat
+    def gauge(
+        self,
+        stat: str,
+        value: int | float,
+        rate: float = 1,
+        delta: bool = False,
+        *,
+        tags: Attributes = None,
+    ) -> None:
+        """Gauge stat."""
+        # To be implemented

Review Comment:
   Actually, I did.   It'll keep throwing exceptions while I work on this if I 
do that.  The way it is done here, it just carries on every time it tries to 
emit a metric type I have not yet implemented.
   
   I could do a warnings.warn() here and in the timers, if you'd like though??



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to