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


##########
airflow/metrics/otel_logger.py:
##########
@@ -55,7 +60,13 @@
 # 
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#updowncounter
 UP_DOWN_COUNTERS = {"airflow.dag_processing.processes"}
 
-METRIC_NAME_PREFIX = "airflow."
+DEFAULT_METRIC_NAME_PREFIX = "airflow"
+# Delimiter is placed between the universal metric prefix and the unique 
metric name.
+DEFAULT_METRIC_NAME_DELIMITER = "."
+
+
+def full_name(name: str, *, prefix: str = DEFAULT_METRIC_NAME_PREFIX) -> str:

Review Comment:
   This used to live in the test module.  I moved it here and use it in both 
places now.



##########
airflow/metrics/otel_logger.py:
##########
@@ -55,7 +60,13 @@
 # 
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#updowncounter
 UP_DOWN_COUNTERS = {"airflow.dag_processing.processes"}
 
-METRIC_NAME_PREFIX = "airflow."
+DEFAULT_METRIC_NAME_PREFIX = "airflow"
+# Delimiter is placed between the universal metric prefix and the unique 
metric name.
+DEFAULT_METRIC_NAME_DELIMITER = "."

Review Comment:
   We can maybe eventually add support for custom delimiters to the config, but 
that sounds like an idea for another day.  This should make it relatively easy 
to add that later.



##########
airflow/metrics/otel_logger.py:
##########
@@ -83,10 +94,45 @@ def name_is_otel_safe(prefix: str, name: str) -> bool:
     return bool(stat_name_otel_handler(prefix, name, 
max_length=OTEL_NAME_MAX_LENGTH))
 
 
+def _type_as_str(obj: Instrument) -> str:
+    """
+    Given an OpenTelemetry Instrument, returns the type of instrument as a 
string.
+
+    type() will return something like: <class 
'opentelemetry.sdk.metrics._internal.instrument._Counter'>
+    This extracts that down to just "Counter" (or "Gauge" or whatever) for 
cleaner logging purposes.
+
+    :param obj: An OTel Instrument or subclass
+    :returns: The type() of the Instrument without all the nested class info
+    """
+    return str(type(obj)).split(".")[-1][1:-2]
+
+
+def _get_otel_safe_name(name: str) -> str:
+    """
+    OpenTelemetry has a maximum length for metric names.  This method returns 
the
+    name, truncated if it is too long, and logs a warning so the user will 
know.
+
+    :param name: The original metric name
+    :returns: The name, truncated to an OTel-acceptable length if required
+    """
+    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}`."
+        )
+    return otel_safe_name

Review Comment:
   Moved out of _create_counter into a helper so _create_gauge can reuse it.



-- 
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