|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import logging |
| 21 | +from collections.abc import Callable |
| 22 | +from enum import Enum |
| 23 | +from functools import wraps |
| 24 | +from typing import ParamSpec, TypeVar |
| 25 | + |
| 26 | +from airflow.configuration import AIRFLOW_HOME, conf |
| 27 | + |
| 28 | +# Type variables for preserving function signatures |
| 29 | +PS = ParamSpec("PS") |
| 30 | +RT = TypeVar("RT") |
| 31 | + |
| 32 | +log = logging.getLogger(__name__) |
| 33 | + |
| 34 | + |
| 35 | +class MemrayTraceComponents(Enum): |
| 36 | + """Possible airflow components can apply memray trace.""" |
| 37 | + |
| 38 | + scheduler = "scheduler" |
| 39 | + dag_processor = "dag_processor" |
| 40 | + api = "api" |
| 41 | + |
| 42 | + |
| 43 | +def enable_memray_trace(component: MemrayTraceComponents) -> Callable[[Callable[PS, RT]], Callable[PS, RT]]: |
| 44 | + """ |
| 45 | + Conditionally track memory using memray based on configuration. |
| 46 | +
|
| 47 | + Args: |
| 48 | + component: Enum value of the component for configuration lookup |
| 49 | + """ |
| 50 | + |
| 51 | + def decorator(func: Callable[PS, RT]) -> Callable[PS, RT]: |
| 52 | + @wraps(func) |
| 53 | + def wrapper(*args: PS.args, **kwargs: PS.kwargs) -> RT: |
| 54 | + _enable_memray_trace = conf.getboolean("profiling", "memray_trace_enabled") |
| 55 | + if not _enable_memray_trace: |
| 56 | + return func(*args, **kwargs) |
| 57 | + |
| 58 | + _memray_trace_components = conf.getenumlist( |
| 59 | + "profiling", "memray_trace_components", MemrayTraceComponents |
| 60 | + ) |
| 61 | + if component not in _memray_trace_components: |
| 62 | + return func(*args, **kwargs) |
| 63 | + |
| 64 | + try: |
| 65 | + import memray |
| 66 | + |
| 67 | + airflow_component_name = component.value |
| 68 | + profile_path = f"{AIRFLOW_HOME}/{airflow_component_name}_memory.bin" |
| 69 | + log.info("enable_memray_trace is on. so memory state is tracked by memray") |
| 70 | + with memray.Tracker( |
| 71 | + profile_path, |
| 72 | + ): |
| 73 | + log.info( |
| 74 | + "Memray tracing enabled for %s. Output: %s", airflow_component_name, profile_path |
| 75 | + ) |
| 76 | + return func(*args, **kwargs) |
| 77 | + except ImportError as error: |
| 78 | + # Silently fall back to running without tracking |
| 79 | + log.warning("ImportError memray.Tracker: %s", error.msg) |
| 80 | + return func(*args, **kwargs) |
| 81 | + |
| 82 | + return wrapper |
| 83 | + |
| 84 | + return decorator |
0 commit comments