o-nikolas commented on code in PR #30873:
URL: https://github.com/apache/airflow/pull/30873#discussion_r1178408926
##########
airflow/jobs/local_task_job_runner.py:
##########
@@ -306,3 +306,13 @@ def _log_return_code_metric(self, return_code: int):
"local_task_job.task_exit."
f"{self.job.id}.{self.task_instance.dag_id}.{self.task_instance.task_id}.{return_code}"
)
+ # Same metric with tagging
+ Stats.incr(
Review Comment:
It's unfortunate that you can't detect these cases inside `incr(...)` :cry:
##########
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"""
Review Comment:
Nit: Maybe add some more useful info or just drop this
##########
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
+ return None
+
+ @validate_stat
+ def timing(
+ self,
+ stat: str,
+ dt: DeltaType,
+ *,
+ tags: Attributes = None,
+ ) -> None:
+ """Stats timing."""
+ # To be implemented
+ return None
+
+ @validate_stat
+ def timer(
+ self,
+ stat: str | None = None,
+ *args,
+ tags: Attributes = None,
+ **kwargs,
+ ) -> TimerProtocol:
+ """Timer metric that can be cancelled."""
+ # To be implemented
+ return Timer()
+
+
+class BaseInstrument(Instrument):
+ """Instrument clss is abstract and must be implemented."""
+
+ def __init__(self, name: str, unit: str = "", description: str = "",
attributes: Attributes = None):
+ self.name: str = name
+ self.unit: str = unit
+ self.description: str = description
+ self.attributes: Attributes = attributes
+
+
+class MetricsMap:
+ """Stores Otel Counters."""
+
+ def __init__(self, meter):
+ self.meter = meter
+ self.map = {}
+
+ def clear(self) -> None:
+ self.map.clear()
+
+ def _create_counter(self, name):
+ """Creates a new counter or up_down_counter for the provided name."""
+ otel_safe_name = name[:OTEL_NAME_MAX_LENGTH]
+ if name != otel_safe_name:
+ warnings.warn(
+ f"Metric name `{name}` exceeds OpenTelemetry's name length
limit of "
+ f"{OTEL_NAME_MAX_LENGTH} characters and will be truncated to
`{otel_safe_name}`."
+ )
+
+ if _is_up_down_counter(name):
+ counter = self.meter.create_up_down_counter(name=otel_safe_name)
+ else:
+ counter = self.meter.create_counter(name=otel_safe_name)
+
+ print(f"--> created {otel_safe_name} as type:
{str(type(counter)).split('.')[-1][:-2]}")
+ return counter
+
+ def get_counter(self, name: str, attributes: Attributes = None):
+ """
+ Returns the value of the counter; creates a new one if it does not
exist.
+
+ :param name: The name of the counter to fetch or create.
+ :param attributes: Counter attributes, used to generate a unique key
to store the counter.
+ """
+ key: str = name + str(attributes)
+ if key in self.map.keys():
+ return self.map[key]
+ else:
+ new_counter = self._create_counter(name)
+ self.map[key] = new_counter
+ return new_counter
+
+ def del_counter(self, name: str, attributes: Attributes = None) -> None:
+ """
+ Deletes a counter.
+
+ :param name: The name of the counter to fetch or create.
+ :param attributes: Counter attributes which were used to generate a
unique key to store the counter.
+ """
+ key: str = name + str(attributes)
+ if key in self.map.keys():
+ del self.map[key]
+
+
+def get_otel_logger(cls) -> SafeOtelLogger:
+ """Get Otel logger"""
Review Comment:
nit: I'd add more to this or just drop.
##########
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.
Review Comment:
s/increment/decrement/g
##########
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
+ return None
+
+ @validate_stat
+ def timing(
+ self,
+ stat: str,
+ dt: DeltaType,
+ *,
+ tags: Attributes = None,
+ ) -> None:
+ """Stats timing."""
+ # To be implemented
+ return None
+
+ @validate_stat
+ def timer(
+ self,
+ stat: str | None = None,
+ *args,
+ tags: Attributes = None,
+ **kwargs,
+ ) -> TimerProtocol:
+ """Timer metric that can be cancelled."""
+ # To be implemented
+ return Timer()
+
+
+class BaseInstrument(Instrument):
+ """Instrument clss is abstract and must be implemented."""
+
+ def __init__(self, name: str, unit: str = "", description: str = "",
attributes: Attributes = None):
+ self.name: str = name
+ self.unit: str = unit
+ self.description: str = description
+ self.attributes: Attributes = attributes
+
+
+class MetricsMap:
+ """Stores Otel Counters."""
Review Comment:
Nit: more context/info?
##########
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???
Review Comment:
Agreed, see my reply to the other thread.
##########
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.
Review Comment:
Can you link to what an updown counter is and more context on why this is
the only one and why it's important to treat it differently?
##########
airflow/jobs/local_task_job_runner.py:
##########
@@ -306,3 +306,13 @@ def _log_return_code_metric(self, return_code: int):
"local_task_job.task_exit."
f"{self.job.id}.{self.task_instance.dag_id}.{self.task_instance.task_id}.{return_code}"
)
+ # Same metric with tagging
Review Comment:
Maybe add a bit about why you're doing this? Can link to an issue or
discussion as well.
##########
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:
nit: maybe raise the `NotImplementedError` here, and below, so it's clear
users should not use this.
##########
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
+ return None
+
+ @validate_stat
+ def timing(
+ self,
+ stat: str,
+ dt: DeltaType,
+ *,
+ tags: Attributes = None,
+ ) -> None:
+ """Stats timing."""
+ # To be implemented
+ return None
+
+ @validate_stat
+ def timer(
+ self,
+ stat: str | None = None,
+ *args,
+ tags: Attributes = None,
+ **kwargs,
+ ) -> TimerProtocol:
+ """Timer metric that can be cancelled."""
+ # To be implemented
+ return Timer()
+
+
+class BaseInstrument(Instrument):
+ """Instrument clss is abstract and must be implemented."""
+
+ def __init__(self, name: str, unit: str = "", description: str = "",
attributes: Attributes = None):
+ self.name: str = name
+ self.unit: str = unit
+ self.description: str = description
+ self.attributes: Attributes = attributes
+
+
+class MetricsMap:
+ """Stores Otel Counters."""
+
+ def __init__(self, meter):
+ self.meter = meter
+ self.map = {}
+
+ def clear(self) -> None:
+ self.map.clear()
+
+ def _create_counter(self, name):
+ """Creates a new counter or up_down_counter for the provided name."""
+ otel_safe_name = name[:OTEL_NAME_MAX_LENGTH]
+ if name != otel_safe_name:
+ warnings.warn(
+ f"Metric name `{name}` exceeds OpenTelemetry's name length
limit of "
+ f"{OTEL_NAME_MAX_LENGTH} characters and will be truncated to
`{otel_safe_name}`."
+ )
+
+ if _is_up_down_counter(name):
+ counter = self.meter.create_up_down_counter(name=otel_safe_name)
+ else:
+ counter = self.meter.create_counter(name=otel_safe_name)
+
+ print(f"--> created {otel_safe_name} as type:
{str(type(counter)).split('.')[-1][:-2]}")
+ return counter
+
+ def get_counter(self, name: str, attributes: Attributes = None):
+ """
+ Returns the value of the counter; creates a new one if it does not
exist.
Review Comment:
Does it return the value of the counter? Or just the counter object itself?
##########
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
+ return None
+
+ @validate_stat
+ def timing(
+ self,
+ stat: str,
+ dt: DeltaType,
+ *,
+ tags: Attributes = None,
+ ) -> None:
+ """Stats timing."""
+ # To be implemented
+ return None
+
+ @validate_stat
+ def timer(
+ self,
+ stat: str | None = None,
+ *args,
+ tags: Attributes = None,
+ **kwargs,
+ ) -> TimerProtocol:
+ """Timer metric that can be cancelled."""
+ # To be implemented
+ return Timer()
+
+
+class BaseInstrument(Instrument):
+ """Instrument clss is abstract and must be implemented."""
+
+ def __init__(self, name: str, unit: str = "", description: str = "",
attributes: Attributes = None):
+ self.name: str = name
+ self.unit: str = unit
+ self.description: str = description
+ self.attributes: Attributes = attributes
+
+
+class MetricsMap:
+ """Stores Otel Counters."""
+
+ def __init__(self, meter):
+ self.meter = meter
+ self.map = {}
+
+ def clear(self) -> None:
+ self.map.clear()
+
+ def _create_counter(self, name):
+ """Creates a new counter or up_down_counter for the provided name."""
+ otel_safe_name = name[:OTEL_NAME_MAX_LENGTH]
+ if name != otel_safe_name:
+ warnings.warn(
+ f"Metric name `{name}` exceeds OpenTelemetry's name length
limit of "
+ f"{OTEL_NAME_MAX_LENGTH} characters and will be truncated to
`{otel_safe_name}`."
+ )
+
+ if _is_up_down_counter(name):
+ counter = self.meter.create_up_down_counter(name=otel_safe_name)
+ else:
+ counter = self.meter.create_counter(name=otel_safe_name)
+
+ print(f"--> created {otel_safe_name} as type:
{str(type(counter)).split('.')[-1][:-2]}")
+ return counter
+
+ def get_counter(self, name: str, attributes: Attributes = None):
+ """
+ Returns the value of the counter; creates a new one if it does not
exist.
+
+ :param name: The name of the counter to fetch or create.
+ :param attributes: Counter attributes, used to generate a unique key
to store the counter.
+ """
+ key: str = name + str(attributes)
+ if key in self.map.keys():
+ return self.map[key]
+ else:
+ new_counter = self._create_counter(name)
+ self.map[key] = new_counter
+ return new_counter
+
+ def del_counter(self, name: str, attributes: Attributes = None) -> None:
+ """
+ Deletes a counter.
+
+ :param name: The name of the counter to fetch or create.
Review Comment:
copy/paste, you're not creating any here.
##########
airflow/metrics/otel_logger.py:
##########
Review Comment:
I'm not seeing any tests for this module or the classes in it? Do we think
there'd be any value in that?
##########
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
+ return None
+
+ @validate_stat
+ def timing(
+ self,
+ stat: str,
+ dt: DeltaType,
+ *,
+ tags: Attributes = None,
+ ) -> None:
+ """Stats timing."""
+ # To be implemented
+ return None
+
+ @validate_stat
+ def timer(
+ self,
+ stat: str | None = None,
+ *args,
+ tags: Attributes = None,
+ **kwargs,
+ ) -> TimerProtocol:
+ """Timer metric that can be cancelled."""
+ # To be implemented
+ return Timer()
+
+
+class BaseInstrument(Instrument):
+ """Instrument clss is abstract and must be implemented."""
+
+ def __init__(self, name: str, unit: str = "", description: str = "",
attributes: Attributes = None):
+ self.name: str = name
+ self.unit: str = unit
+ self.description: str = description
+ self.attributes: Attributes = attributes
+
+
+class MetricsMap:
+ """Stores Otel Counters."""
+
+ def __init__(self, meter):
+ self.meter = meter
+ self.map = {}
+
+ def clear(self) -> None:
+ self.map.clear()
+
+ def _create_counter(self, name):
+ """Creates a new counter or up_down_counter for the provided name."""
+ otel_safe_name = name[:OTEL_NAME_MAX_LENGTH]
+ if name != otel_safe_name:
+ warnings.warn(
+ f"Metric name `{name}` exceeds OpenTelemetry's name length
limit of "
+ f"{OTEL_NAME_MAX_LENGTH} characters and will be truncated to
`{otel_safe_name}`."
+ )
+
+ if _is_up_down_counter(name):
+ counter = self.meter.create_up_down_counter(name=otel_safe_name)
+ else:
+ counter = self.meter.create_counter(name=otel_safe_name)
+
+ print(f"--> created {otel_safe_name} as type:
{str(type(counter)).split('.')[-1][:-2]}")
+ return counter
+
+ def get_counter(self, name: str, attributes: Attributes = None):
+ """
+ Returns the value of the counter; creates a new one if it does not
exist.
+
+ :param name: The name of the counter to fetch or create.
+ :param attributes: Counter attributes, used to generate a unique key
to store the counter.
+ """
+ key: str = name + str(attributes)
+ if key in self.map.keys():
+ return self.map[key]
+ else:
+ new_counter = self._create_counter(name)
+ self.map[key] = new_counter
+ return new_counter
+
+ def del_counter(self, name: str, attributes: Attributes = None) -> None:
+ """
+ Deletes a counter.
+
+ :param name: The name of the counter to fetch or create.
+ :param attributes: Counter attributes which were used to generate a
unique key to store the counter.
+ """
+ key: str = name + str(attributes)
+ if key in self.map.keys():
+ del self.map[key]
+
+
+def get_otel_logger(cls) -> SafeOtelLogger:
+ """Get Otel logger"""
+ host = conf.get("metrics", "otel_host") # ex: "breeze-otel-collector"
+ port = conf.getint("metrics", "otel_port") # ex: 4318
+ prefix = conf.get("metrics", "otel_prefix") # ex: "airflow"
+ interval = conf.getint("metrics", "otel_interval_milliseconds") # ex:
30000
+
+ allow_list = conf.get("metrics", "metrics_allow_list", fallback=None)
+ allow_list_validator = AllowListValidator(allow_list)
+
+ resource = Resource(attributes={SERVICE_NAME: "Airflow"})
+ # TODO: figure out https instead of http ??
+ endpoint = f"http://{host}:{port}/v1/metrics"
+
+ print(f"[Metric Exporter] Connecting to OTLP at ---> {endpoint}")
+ readers = [
+ PeriodicExportingMetricReader(
+ OTLPMetricExporter(
+ endpoint=endpoint,
+ headers={"Content-Type": "application/json"},
+ ),
+ export_interval_millis=interval,
+ )
+ ]
+
+ # TODO: remove this console exporter before launch?? It's handy for
debugging maybe?
Review Comment:
If you are going to keep it, you should add a config option for it so that
it can be enabled without modifying source code (like changing log level). But
it's also fine to drop it, since it likely won't be used much.
##########
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
+ return None
+
+ @validate_stat
+ def timing(
+ self,
+ stat: str,
+ dt: DeltaType,
+ *,
+ tags: Attributes = None,
+ ) -> None:
+ """Stats timing."""
+ # To be implemented
+ return None
+
+ @validate_stat
+ def timer(
+ self,
+ stat: str | None = None,
+ *args,
+ tags: Attributes = None,
+ **kwargs,
+ ) -> TimerProtocol:
+ """Timer metric that can be cancelled."""
+ # To be implemented
+ return Timer()
+
+
+class BaseInstrument(Instrument):
+ """Instrument clss is abstract and must be implemented."""
+
+ def __init__(self, name: str, unit: str = "", description: str = "",
attributes: Attributes = None):
+ self.name: str = name
+ self.unit: str = unit
+ self.description: str = description
+ self.attributes: Attributes = attributes
+
+
+class MetricsMap:
+ """Stores Otel Counters."""
+
+ def __init__(self, meter):
+ self.meter = meter
+ self.map = {}
+
+ def clear(self) -> None:
+ self.map.clear()
+
+ def _create_counter(self, name):
+ """Creates a new counter or up_down_counter for the provided name."""
+ otel_safe_name = name[:OTEL_NAME_MAX_LENGTH]
+ if name != otel_safe_name:
+ warnings.warn(
+ f"Metric name `{name}` exceeds OpenTelemetry's name length
limit of "
+ f"{OTEL_NAME_MAX_LENGTH} characters and will be truncated to
`{otel_safe_name}`."
+ )
+
+ if _is_up_down_counter(name):
+ counter = self.meter.create_up_down_counter(name=otel_safe_name)
+ else:
+ counter = self.meter.create_counter(name=otel_safe_name)
+
+ print(f"--> created {otel_safe_name} as type:
{str(type(counter)).split('.')[-1][:-2]}")
Review Comment:
You should be able to instantiate a logger in this module, that'd be
preferable over using `print`
##########
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
+ return None
+
+ @validate_stat
+ def timing(
+ self,
+ stat: str,
+ dt: DeltaType,
+ *,
+ tags: Attributes = None,
+ ) -> None:
+ """Stats timing."""
+ # To be implemented
+ return None
+
+ @validate_stat
+ def timer(
+ self,
+ stat: str | None = None,
+ *args,
+ tags: Attributes = None,
+ **kwargs,
+ ) -> TimerProtocol:
+ """Timer metric that can be cancelled."""
+ # To be implemented
+ return Timer()
+
+
+class BaseInstrument(Instrument):
+ """Instrument clss is abstract and must be implemented."""
+
+ def __init__(self, name: str, unit: str = "", description: str = "",
attributes: Attributes = None):
+ self.name: str = name
+ self.unit: str = unit
+ self.description: str = description
+ self.attributes: Attributes = attributes
Review Comment:
Thanks for the clarification, I did have a comment about that (which I
deleted, I'll reply here). I'm happy either way, it's a small piece of code so
it's not a big deal one way or another.
--
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]