ferruzzi commented on code in PR #30873: URL: https://github.com/apache/airflow/pull/30873#discussion_r1180867664
########## tests/core/test_otel_logger.py: ########## @@ -0,0 +1,156 @@ +# 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 + +from unittest import mock + +import pytest +from opentelemetry.metrics import MeterProvider + +from airflow.metrics.otel_logger import ( + METRIC_NAME_PREFIX, + OTEL_NAME_MAX_LENGTH, + UP_DOWN_COUNTERS, + SafeOtelLogger, + _is_up_down_counter, +) + + +def full_name(name: str): + return f"{METRIC_NAME_PREFIX}{name}" + + [email protected] +def name(): + return "test_stats_run" + + +class TestOtelMetrics: + def setup_method(self): + self.meter = mock.Mock(MeterProvider) + self.stats = SafeOtelLogger(otel_provider=self.meter) + self.map = self.stats.metrics_map.map + + def test_is_up_down_counter_positive(self): + udc = next(iter(UP_DOWN_COUNTERS)) + + assert _is_up_down_counter(udc) + + def test_is_up_down_counter_negative(self): + assert not _is_up_down_counter("this_is_not_a_udc") + + def test_stat_name_must_be_a_string(self): + self.stats.incr(42) Review Comment: Interesting... it isn't caught as an exception because it just gets folded into an f-string [here](https://github.com/apache/airflow/pull/30873/files#diff-bc07cf67d2d211678aaefd4d99b1e8c6e2c6e26a6987e85c5397ccc78fed47feR89) but the incr() parameter is type-hinted as a string. I could throw an `if not isInstance(name, str): raise ..." but I wonder if a "validate_name()" helper method that asserts it is a string and truncates as needed might be a better answer? -- 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]
