Skip to content

Commit

Permalink
Apply review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ephraimbuddy committed Mar 22, 2024
1 parent 74338b8 commit c7c2a24
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
4 changes: 2 additions & 2 deletions airflow/config_templates/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -492,14 +492,14 @@ core:
type: string
example: ~
default: "Disabled"
max_templated_field_size:
max_templated_field_length:
description: |
The maximum length of the rendered template field. If the value to be stored in the
rendered template field exceeds this size, it's redacted.
version_added: 2.9.0
type: integer
example: ~
default: "1024"
default: "4096"
database:
description: ~
options:
Expand Down
21 changes: 8 additions & 13 deletions airflow/serialization/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,14 @@ def is_jsonable(x):
else:
return True

max_size = conf.getint("core", "max_templated_field_size")
max_length = conf.getint("core", "max_templated_field_length")

if template_field and len(str(template_field)) > max_length:
return (
f"{str(template_field)[:max_length-79]}... truncated. "
"You can change this behaviour in [core]max_templated_field_length"
)
if not is_jsonable(template_field):
serialized = str(template_field)
if len(serialized) > max_size:
return (
"Value removed due to size. "
"You can change this behaviour in [core]max_templated_field_size"
)
return serialized
return str(template_field)
else:
if template_field and len(str(template_field)) > max_size:
return (
"Value removed due to size. "
"You can change this behaviour in [core]max_templated_field_size"
)
return template_field
12 changes: 8 additions & 4 deletions tests/models/test_renderedtifields.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import pytest

from airflow import settings
from airflow.configuration import conf
from airflow.models import Variable
from airflow.models.renderedtifields import RenderedTaskInstanceFields as RTIF
from airflow.operators.bash import BashOperator
Expand Down Expand Up @@ -64,12 +65,15 @@ def __ne__(self, other):

class LargeStrObject:
def __init__(self):
self.a = "a" * 2560
self.a = "a" * 5000

def __str__(self):
return self.a


max_length = conf.getint("core", "max_templated_field_length")


class TestRenderedTaskInstanceFields:
"""Unit tests for RenderedTaskInstanceFields."""

Expand Down Expand Up @@ -120,12 +124,12 @@ def teardown_method(self):
"'template_fields': ['nested1']})",
),
(
"a" * 2560,
"Value removed due to size. You can change this behaviour in [core]max_templated_field_size",
"a" * 5000,
f"{('a'*5000)[:max_length-79]}... truncated. You can change this behaviour in [core]max_templated_field_length",
),
(
LargeStrObject(),
"Value removed due to size. You can change this behaviour in [core]max_templated_field_size",
f"{str(LargeStrObject())[:max_length-79]}... truncated. You can change this behaviour in [core]max_templated_field_length",
),
],
)
Expand Down

0 comments on commit c7c2a24

Please sign in to comment.