uranusjr commented on code in PR #55068:
URL: https://github.com/apache/airflow/pull/55068#discussion_r2986536583
##########
task-sdk/src/airflow/sdk/definitions/_internal/templater.py:
##########
@@ -125,15 +167,47 @@ def _do_render_template_fields(
jinja_env: jinja2.Environment,
seen_oids: set[int],
) -> None:
- for attr_name in template_fields:
- value = getattr(parent, attr_name)
- rendered_content = self.render_template(
- value,
- context,
- jinja_env,
- seen_oids,
- )
- if rendered_content:
+ """
+ Render template fields on *parent* in-place.
+
+ For each non-empty field yielded by :meth:`_iter_templated_fields`,
the value is
+ rendered (or called, when it is callable) and the result is written
back via
+ ``setattr``. Rendering errors are logged with masked values before
being re-raised.
+
+ :param parent: The object whose attributes will be templated.
+ :param template_fields: Names of the attributes to render.
+ :param context: Context dict with values to apply on content.
+ :param jinja_env: Jinja2 environment to use for rendering.
+ :param seen_oids: Set of already-rendered object ids used to prevent
infinite
+ recursion on circular references.
+ """
+ for attr_name, value in self._iter_templated_fields(parent,
template_fields):
+ try:
+ if callable(value):
+ rendered_content = value(context=context,
jinja_env=jinja_env)
+ else:
+ rendered_content = self.render_template(value, context,
jinja_env, seen_oids)
+ except Exception:
+ # Mask sensitive values in the template before logging
+ from airflow.sdk._shared.secrets_masker import redact
+
+ masked_value = redact(value)
+ if hasattr(self, "task_id"):
+ log.exception(
+ "Exception rendering Jinja template for task '%s',
field '%s'. Template: %r",
+ self.task_id,
+ attr_name,
+ masked_value,
+ )
+ else:
+ log.exception(
+ "Exception rendering Jinja template for %s, field
'%s'. Template: %r",
+ type(parent).__name__,
+ attr_name,
+ masked_value,
+ )
Review Comment:
Maybe a good time to change `log` to structlog. This would simplify this
quite a bit. Does not need to be a part of this PR.
--
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]