|
1 | | -"""Opt-in stamping of the agent's source commit onto its spans. |
| 1 | +"""Stamping of the agent's source commit onto its spans. |
2 | 2 |
|
3 | | -Nothing is stamped until the agent calls :func:`enable`, mirroring the |
4 | | -``lineage`` registry next door: a process-wide switch the agent sets once at |
5 | | -import, rather than automatic behaviour every agent inherits. When enabled the |
6 | | -resolved commit lands in span data under ``__commit_sha__`` and is searchable in |
7 | | -the SGP Traces UI as ``__commit_sha__:<sha>``. |
| 3 | +Stamping turns on when the process starts with ``AGENT_COMMIT_SHA`` set, which |
| 4 | +the SGP cloud deploy does from the build record's attested commit, or when the |
| 5 | +agent calls :func:`enable` itself. Nothing is stamped otherwise: upgrading the |
| 6 | +SDK alone never starts emitting the field. When on, the resolved commit lands in |
| 7 | +span data under ``__commit_sha__`` and is searchable in the SGP Traces UI as |
| 8 | +``__commit_sha__:<sha>``. |
8 | 9 |
|
9 | 10 | This is deliberately separate from ``__agent_version__``, which is automatic and |
10 | 11 | carries the deployed image tag verbatim ("image tag or git sha"). That tag is a |
|
21 | 22 |
|
22 | 23 | from agentex.lib.utils.logging import make_logger |
23 | 24 |
|
24 | | -__all__ = ("COMMIT_SHA_KEY", "enable", "disable", "is_enabled", "commit_sha") |
| 25 | +__all__ = ("COMMIT_SHA_KEY", "enable", "disable", "is_enabled", "commit_sha", "is_git_object_name") |
25 | 26 |
|
26 | 27 | logger = make_logger(__name__) |
27 | 28 |
|
|
31 | 32 | # git's own 7-character minimum. |
32 | 33 | _GIT_SHA_RE = re.compile(r"[0-9a-fA-F]{7,64}") |
33 | 34 |
|
| 35 | + |
| 36 | +def is_git_object_name(value: str) -> bool: |
| 37 | + """Whether ``value`` is a full or abbreviated git SHA-1/SHA-256 object name.""" |
| 38 | + return _GIT_SHA_RE.fullmatch(value.strip()) is not None |
| 39 | + |
| 40 | + |
34 | 41 | _COMMIT_SHA_ENV = "AGENT_COMMIT_SHA" |
35 | 42 | # Fallback only: automatic, and only usable when it happens to be SHA-shaped. |
36 | 43 | _AGENT_VERSION_ENV = "AGENT_VERSION" |
|
42 | 49 |
|
43 | 50 |
|
44 | 51 | def enable(commit_sha: str | None = None) -> None: |
45 | | - """Opt this process in to stamping ``__commit_sha__`` onto every span. |
| 52 | + """Turn on stamping ``__commit_sha__`` onto every span from this process. |
46 | 53 |
|
47 | 54 | Value precedence: the explicit ``commit_sha`` argument, else |
48 | 55 | ``AGENT_COMMIT_SHA``, else ``AGENT_VERSION`` when the deployment happened to |
49 | 56 | set it to a bare commit SHA. A value that is not a git object name is |
50 | 57 | refused with a warning and leaves stamping off -- better an absent field |
51 | 58 | than one named for a commit that holds an image tag. |
| 59 | +
|
| 60 | + Called once at import when ``AGENT_COMMIT_SHA`` is set, so a deployment that |
| 61 | + supplies the commit needs no code change in the agent. |
52 | 62 | """ |
53 | 63 | global _commit_sha |
54 | 64 |
|
@@ -103,3 +113,12 @@ def is_enabled() -> bool: |
103 | 113 | def commit_sha() -> str | None: |
104 | 114 | """The resolved commit SHA, or ``None`` when stamping is not enabled.""" |
105 | 115 | return _commit_sha |
| 116 | + |
| 117 | + |
| 118 | +def _enable_from_environment() -> None: |
| 119 | + """Auto-enable on ``AGENT_COMMIT_SHA`` only; ``AGENT_VERSION`` stays an explicit fallback.""" |
| 120 | + if os.environ.get(_COMMIT_SHA_ENV, "").strip(): |
| 121 | + enable() |
| 122 | + |
| 123 | + |
| 124 | +_enable_from_environment() |
0 commit comments