Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions airflow-core/src/airflow/serialization/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,36 @@
from airflow.timetables.base import Timetable as CoreTimetable


def _truncate_rendered_value(rendered: str, max_length: int) -> str:
if max_length <= 0:
return ""

prefix = (
"Truncated. You can change this behaviour in "
"[core]max_templated_field_length. "
)
suffix = "..."
value = str(rendered)

trunc_only = f"{prefix}{suffix}"

if max_length < len(trunc_only):
return trunc_only

overhead = len(prefix) + 2 + len(suffix)
available = max_length - overhead

if available <= 0:
return trunc_only

return f"{prefix}'{value[:available]}'{suffix}"



def _safe_truncate_rendered_value(rendered: Any, max_length: int) -> str:
return _truncate_rendered_value(str(rendered), max_length)
Comment on lines +32 to +59
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still doesn't prioritise message first. Take these test cases for example:

[(1, 'test', 'Minimum value'),
 (3, 'test', 'At ellipsis length'),
 (5, 'test', 'Very small'),
 (10, 'password123', 'Small'),
 (20, 'secret_value', 'Small with content'),
 (50, 'This is a test string', 'Medium'),
 (83, 'Hello World', 'At prefix+suffix boundary v1'),
 (84, 'Hello World', 'Just above boundary v1'),
 (86, 'Hello World', 'At overhead boundary v2'),
 (90, 'short', 'Normal case - short string'),
 (100, 'This is a longer string', 'Normal case'),
 (150,
  'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'Large max_length, long string'),
 (100, 'None', "String 'None'"),
 (100, 'True', "String 'True'"),
 (100, "{'key': 'value'}", 'Dict-like string'),
 (100, "test's", 'String with apostrophe'),
 (90, '"quoted"', 'String with quotes')]

This is what your function returns:

for max_length, rendered, description in test_cases:
    v1_result = truncate_v1(rendered, max_length)
    print(v1_result)
    
.
...
Trunc
Truncated.
Truncated. You can c
Truncated. You can change this behaviour in [core]
Truncated. You can change this behaviour in [core]max_templated_field_length. He...
Truncated. You can change this behaviour in [core]max_templated_field_length. Hel...
Truncated. You can change this behaviour in [core]max_templated_field_length. Hello...
Truncated. You can change this behaviour in [core]max_templated_field_length. short...
Truncated. You can change this behaviour in [core]max_templated_field_length. This is a longer st...
Truncated. You can change this behaviour in [core]max_templated_field_length. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
Truncated. You can change this behaviour in [core]max_templated_field_length. None...
Truncated. You can change this behaviour in [core]max_templated_field_length. True...
Truncated. You can change this behaviour in [core]max_templated_field_length. {'key': 'value'}...
Truncated. You can change this behaviour in [core]max_templated_field_length. test's...
Truncated. You can change this behaviour in [core]max_templated_field_length. "quoted"...

This is what I would expect:

Truncated. You can change this behaviour in [core]max_templated_field_length. ... 
Truncated. You can change this behaviour in [core]max_templated_field_length. ... 
Truncated. You can change this behaviour in [core]max_templated_field_length. ... 
Truncated. You can change this behaviour in [core]max_templated_field_length. ... 
Truncated. You can change this behaviour in [core]max_templated_field_length. ... 
Truncated. You can change this behaviour in [core]max_templated_field_length. ... 
Truncated. You can change this behaviour in [core]max_templated_field_length. ... 
Truncated. You can change this behaviour in [core]max_templated_field_length. ... 
Truncated. You can change this behaviour in [core]max_templated_field_length. 'He'... 
Truncated. You can change this behaviour in [core]max_templated_field_length. 'short'... 
Truncated. You can change this behaviour in [core]max_templated_field_length. 'This is a longer'... 
Truncated. You can change this behaviour in [core]max_templated_field_length. 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'... 
Truncated. You can change this behaviour in [core]max_templated_field_length. 'None'... 
Truncated. You can change this behaviour in [core]max_templated_field_length. 'True'... 
Truncated. You can change this behaviour in [core]max_templated_field_length. "{'key': 'value'}"... 
Truncated. You can change this behaviour in [core]max_templated_field_length. "test's"... 
Truncated. You can change this behaviour in [core]max_templated_field_length. '"quote'... 

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the examples. I agree and have updated the logic to always prioritise the truncation message and avoid partial prefix or value output. The results now match the expected outputs you shared.



def serialize_template_field(template_field: Any, name: str) -> str | dict | list | int | float:
"""
Return a serializable representation of the templated field.
Expand Down Expand Up @@ -74,10 +104,7 @@ def sort_dict_recursively(obj: Any) -> Any:
serialized = str(template_field)
if len(serialized) > max_length:
rendered = redact(serialized, name)
return (
"Truncated. You can change this behaviour in [core]max_templated_field_length. "
f"{rendered[: max_length - 79]!r}... "
)
return _safe_truncate_rendered_value(rendered, max_length)
return serialized
if not template_field and not isinstance(template_field, tuple):
# Avoid unnecessary serialization steps for empty fields unless they are tuples
Expand All @@ -91,10 +118,7 @@ def sort_dict_recursively(obj: Any) -> Any:
serialized = str(template_field)
if len(serialized) > max_length:
rendered = redact(serialized, name)
return (
"Truncated. You can change this behaviour in [core]max_templated_field_length. "
f"{rendered[: max_length - 79]!r}... "
)
return _safe_truncate_rendered_value(rendered, max_length)
return template_field


Expand Down
28 changes: 28 additions & 0 deletions airflow-core/tests/unit/serialization/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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


def test_serialize_template_field_with_very_small_max_length(monkeypatch):
monkeypatch.setenv("AIRFLOW__CORE__MAX_TEMPLATED_FIELD_LENGTH", "1")

from airflow.serialization.helpers import serialize_template_field

result = serialize_template_field("This is a long string", "test")

assert result
assert len(result) <= 1
36 changes: 28 additions & 8 deletions task-sdk/src/airflow/sdk/execution_time/task_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,32 @@ def startup() -> tuple[RuntimeTaskInstance, Context, Logger]:

return ti, ti.get_template_context(), log

def _truncate_rendered_value(rendered: str, max_length: int) -> str:
if max_length <= 0:
return ""

prefix = (
"Truncated. You can change this behaviour in "
"[core]max_templated_field_length. "
)
suffix = "..."
value = str(rendered)

trunc_only = f"{prefix}{suffix}"

if max_length < len(trunc_only):
return trunc_only

overhead = len(prefix) + 2 + len(suffix)
available = max_length - overhead

if available <= 0:
return trunc_only

return f"{prefix}'{value[:available]}'{suffix}"
def _safe_truncate_rendered_value(rendered: Any, max_length: int) -> str:
return _truncate_rendered_value(str(rendered), max_length)


def _serialize_template_field(template_field: Any, name: str) -> str | dict | list | int | float:
"""
Expand Down Expand Up @@ -877,10 +903,7 @@ def sort_dict_recursively(obj: Any) -> Any:
serialized = str(template_field)
if len(serialized) > max_length:
rendered = redact(serialized, name)
return (
"Truncated. You can change this behaviour in [core]max_templated_field_length. "
f"{rendered[: max_length - 79]!r}... "
)
return _safe_truncate_rendered_value(rendered, max_length)
return serialized
if not template_field and not isinstance(template_field, tuple):
# Avoid unnecessary serialization steps for empty fields unless they are tuples
Expand All @@ -894,10 +917,7 @@ def sort_dict_recursively(obj: Any) -> Any:
serialized = str(template_field)
if len(serialized) > max_length:
rendered = redact(serialized, name)
return (
"Truncated. You can change this behaviour in [core]max_templated_field_length. "
f"{rendered[: max_length - 79]!r}... "
)
return _safe_truncate_rendered_value(rendered, max_length)
return template_field


Expand Down
Loading