jason810496 commented on code in PR #56821:
URL: https://github.com/apache/airflow/pull/56821#discussion_r2452594723


##########
airflow-core/src/airflow/utils/memray_utils.py:
##########
@@ -0,0 +1,84 @@
+#
+# 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 logging
+from collections.abc import Callable
+from enum import Enum
+from functools import wraps
+from typing import ParamSpec, TypeVar
+
+from airflow.configuration import AIRFLOW_HOME, conf
+
+# Type variables for preserving function signatures
+PS = ParamSpec("PS")
+RT = TypeVar("RT")
+
+log = logging.getLogger(__name__)
+
+
+class MemrayTraceComponents(Enum):
+    """Possible airflow components can apply memray trace."""
+
+    scheduler = "scheduler"
+    dag_processor = "dag_processor"
+    api = "api"
+
+
+def enable_memray_trace(component: MemrayTraceComponents) -> 
Callable[[Callable[PS, RT]], Callable[PS, RT]]:
+    """
+    Conditionally track memory using memray based on configuration.
+
+    Args:
+        component: Enum value of the component for configuration lookup
+    """
+
+    def decorator(func: Callable[PS, RT]) -> Callable[PS, RT]:
+        @wraps(func)
+        def wrapper(*args: PS.args, **kwargs: PS.kwargs) -> RT:
+            _enable_memray_trace = conf.getboolean("profiling", 
"memray_trace_enabled")
+            if not _enable_memray_trace:
+                return func(*args, **kwargs)
+
+            _memray_trace_components = conf.getenumlist(
+                "profiling", "memray_trace_components", MemrayTraceComponents
+            )

Review Comment:
   How about let just make it simpler by using `conf.get` to get the raw string 
and processing in the `memray_utils`?
   
   ```suggestion
               _memray_trace_components = conf.get(
                   "profiling", "memray_trace_components"
               )
               # validating the string input
               # then parse to `MemrayTraceComponents` enum
               # ...
   ```



##########
airflow-core/src/airflow/configuration.py:
##########
@@ -1248,6 +1249,48 @@ def getlist(self, section: str, key: str, delimiter=",", 
**kwargs):
                 f'Current value: "{val}".'
             )
 
+    E = TypeVar("E", bound=Enum)

Review Comment:
   Then we don't need to introduce new `getenum` or `getenumlist` interfaces 
for `airflow.configuration.conf`. Since the `conf` itself is already quite 
complicated.



##########
airflow-core/src/airflow/config_templates/config.yml:
##########
@@ -2561,3 +2561,33 @@ dag_processor:
       type: boolean
       example: ~
       default: "True"
+profiling:
+  description: |
+    Configuration for memory profiling in Airflow component.
+    Currently, we provide profiling using Memray and additional tools may be 
added in the future
+    Also, see the guide in Link (TBD)
+  options:
+    memray_trace_enabled:
+      description: |
+        Whether to enable memory allocation tracing by memray in the 
scheduler. If enabled, Airflow will
+        start tracing memory allocation and store the metrics in 
"$AIRFLOW_HOME/dag_processor_memory.bin"
+        To generate analyzed view, run this command in base directory where 
the bin file is generated
+        ```
+        # see also 
https://bloomberg.github.io/memray/run.html#aggregated-capture-files
+        memray flamegraph $AIRFLOW_HOME/<component>_memory.bin
+        ```
+        This is an expensive operation and generally should not be used except 
for debugging purposes.
+      version_added: 3.1.2
+      type: boolean
+      example: ~
+      default: "False"
+    memray_trace_components:
+      description: |
+        Comma-separated list of Airflow components to profile with memray.
+        Valid components are: scheduler, api, dag_processor
+
+        This option only takes effect when memray_trace_enabled is set to True.
+      version_added: 3.1.2
+      type: string
+      example: "scheduler,api,dag_processor"
+      default: ""

Review Comment:
   Would it be possible to consolidate the two options in one.
   Somehow like: if `memray_trace_components` is `""` which means we disable 
the `profiling`.



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