o-nikolas commented on code in PR #30873: URL: https://github.com/apache/airflow/pull/30873#discussion_r1178403467
########## 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 Review Comment: > I'm not sure how we should handle rate here. [The StatsD implementation](https://github.com/jsocol/pystatsd/blob/main/statsd/client/base.py#L63) picked a random number and if it was less than rate then it did nothing, I think? It looks like this is used as a rate-limiting feature. You provide a rate which must be between 0.00 and 0.99 (basically a percent). Then a random number between 0.00 and 1.00 is generated and if it's larger than the rate value you provided it short circuits and if it's less, then it emits the metric. So if you provide 0.95 as your rate, most numbers generated by `random.random()` will be below 0.95 so you'll emit most of the time (i.e. emit 95% of the time). If you provide a rate of 0.05, most numbers generated by `random.random()` will be above that and so it'll short circuit and not emit the metric (i.e. only emit 5% of the time). If you provide a value greater than or equal to 1 the whole rate limiting branch is not run (because you're saying you want it emitted 100% of the time essentially) -- 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]
