Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
Report the commit switch-core was built from with its metrics and logs (#710)
Abel Dantas9a913ad1e8f3
diff --git a/.github/workflows/switch-release.yml b/.github/workflows/switch-release.ymlindex d4be7f7b0..f07c25cb8 100644--- a/.github/workflows/switch-release.yml+++ b/.github/workflows/switch-release.yml@@ -180,6 +180,10 @@ jobs:file: ${{ matrix.dockerfile }}platforms: linux/amd64,linux/arm64push: ${{ needs.prep.outputs.push == 'true' }}+ # Read by Dockerfile.switch only; the other images ignore them.+ build-args: |+ SWITCH_GIT_COMMIT=${{ github.sha }}+ SWITCH_GIT_REPOSITORY_URL=${{ github.server_url }}/${{ github.repository }}tags: |${{ env.REGISTRY }}/${{ env.IMAGE_NAMESPACE }}/${{ matrix.image }}:${{ needs.prep.outputs.version }}${{ needs.prep.outputs.latest == 'true' && format('{0}/{1}/{2}:latest', env.REGISTRY, env.IMAGE_NAMESPACE, matrix.image) || '' }}diff --git a/core/switch_core/observability/bootstrap.py b/core/switch_core/observability/bootstrap.pyindex 31439dcd9..8507ed39f 100644--- a/core/switch_core/observability/bootstrap.py+++ b/core/switch_core/observability/bootstrap.py@@ -60,6 +60,7 @@ from switch_core.observability.runtime import (log_unreadable_sources,)from switch_core.transport.room_cache import RoomCacheStats+from switch_core.version import switch_core_commit, switch_core_repository_urllogger = logging.getLogger(__name__)@@ -224,6 +225,8 @@ def start_observability(service_version=version,environment=config.environment,deployment_id=str(config.deployment_id),+ commit_sha=switch_core_commit(),+ repository_url=switch_core_repository_url(),)if config.otlp_metrics_enabled:diff --git a/core/switch_core/observability/otlp.py b/core/switch_core/observability/otlp.pyindex 14a609a20..4158487e8 100644--- a/core/switch_core/observability/otlp.py+++ b/core/switch_core/observability/otlp.py@@ -122,13 +122,15 @@ class OtlpResource:``service_version`` is ``None`` when switch-core cannot read its ownversion, and is then omitted rather than placeheld — a dashboard filteredby version should show the record missing, not attribute it to a release- nobody built.+ nobody built. ``commit_sha`` and ``repository_url`` follow the same rule."""service_name: strservice_version: str | Noneenvironment: str | Nonedeployment_id: str+ commit_sha: str | None+ repository_url: str | Nonedef attributes(self) -> dict[str, AttributeValue]:values: dict[str, AttributeValue] = {@@ -142,6 +144,12 @@ class OtlpResource:if self.environment:# Datadog maps this onto `env`.values["deployment.environment"] = self.environment+ # The names Datadog reads for deployment tracking and for linking a+ # stack trace to its source.+ if self.commit_sha:+ values["git.commit.sha"] = self.commit_sha+ if self.repository_url:+ values["git.repository_url"] = self.repository_urlreturn valuesdiff --git a/core/switch_core/telemetry/sink.py b/core/switch_core/telemetry/sink.pyindex 85daaf10e..932a6518f 100644--- a/core/switch_core/telemetry/sink.py+++ b/core/switch_core/telemetry/sink.py@@ -130,6 +130,8 @@ def _resource_from(record: TelemetryRecord) -> OtlpResource:service_version=record.resource.get("service.version"),environment=record.resource.get("deployment.environment"),deployment_id=record.resource["flint.client_id"],+ commit_sha=None,+ repository_url=None,)diff --git a/core/switch_core/version.py b/core/switch_core/version.pyindex 54d6692bb..f12b8da8f 100644--- a/core/switch_core/version.py+++ b/core/switch_core/version.py@@ -17,6 +17,8 @@ the whole point of this work is that unknown must never render as fine."""import logging+import os+import refrom functools import lru_cachefrom importlib.metadata import PackageNotFoundErrorfrom importlib.metadata import version as distribution_version@@ -55,6 +57,44 @@ def switch_core_version() -> str | None:return None+# Stamped into the image at build time (deploy/shared_resources/images/+# Dockerfile.switch). The version names a release; the commit tells two builds+# of the same version apart, which is every branch deployed to a test server.+COMMIT_ENV = "SWITCH_GIT_COMMIT"+REPOSITORY_URL_ENV = "SWITCH_GIT_REPOSITORY_URL"++_COMMIT = re.compile(r"[0-9a-f]{7,40}")+++@lru_cache(maxsize=1)+def switch_core_commit() -> str | None:+ """Return the commit this build came from, or None when unknown.++ Unknown is ordinary: a checkout run with `uv run`, or an image built+ without the build argument. A value that is not a commit hash is refused+ with a warning rather than reported, since it would group the build with+ nothing real.+ """+ value = os.environ.get(COMMIT_ENV, "").strip().lower()+ if not value:+ return None+ if not _COMMIT.fullmatch(value):+ logger.warning(+ "%s is %r, which is not a commit hash, so switch-core reports its "+ "commit as unknown.",+ COMMIT_ENV,+ value,+ )+ return None+ return value+++@lru_cache(maxsize=1)+def switch_core_repository_url() -> str | None:+ """Return the repository the build came from, or None when unknown."""+ return os.environ.get(REPOSITORY_URL_ENV, "").strip() or None++def server_declaration(*contracts: str) -> dict[str, Any]:"""What this server says about itself, for the named contracts only.diff --git a/core/tests/switch_core/observability/test_exporter.py b/core/tests/switch_core/observability/test_exporter.pyindex 80936e2b6..ce535b782 100644--- a/core/tests/switch_core/observability/test_exporter.py+++ b/core/tests/switch_core/observability/test_exporter.py@@ -18,6 +18,8 @@ RESOURCE = OtlpResource(service_version="1.0.0",environment="pilot",deployment_id="0e5d1b3a-6c1f-4c22-9a4c-3a9f5a2b7d10",+ commit_sha=None,+ repository_url=None,)ATTRS = {"route": "/health", "method": "GET", "status_class": "2xx"}diff --git a/core/tests/switch_core/observability/test_logs.py b/core/tests/switch_core/observability/test_logs.pyindex e9b84b636..3ade5f1f0 100644--- a/core/tests/switch_core/observability/test_logs.py+++ b/core/tests/switch_core/observability/test_logs.py@@ -17,6 +17,8 @@ RESOURCE = OtlpResource(service_version="1.0.0",environment="pilot",deployment_id="0e5d1b3a-6c1f-4c22-9a4c-3a9f5a2b7d10",+ commit_sha=None,+ repository_url=None,)diff --git a/core/tests/switch_core/observability/test_otlp.py b/core/tests/switch_core/observability/test_otlp.pyindex d67892a9f..86671a6da 100644--- a/core/tests/switch_core/observability/test_otlp.py+++ b/core/tests/switch_core/observability/test_otlp.py@@ -30,6 +30,8 @@ RESOURCE = OtlpResource(service_version="1.2.3",environment="pilot",deployment_id="0e5d1b3a-6c1f-4c22-9a4c-3a9f5a2b7d10",+ commit_sha=None,+ repository_url=None,)START_NANOS = 1_700_000_000_000_000_000@@ -76,11 +78,30 @@ def test_unknown_version_is_omitted_rather_than_placeheld():service_version=None,environment=None,deployment_id=RESOURCE.deployment_id,+ commit_sha=None,+ repository_url=None,)attributes = resource.attributes()assert "service.version" not in attributesassert "deployment.environment" not in attributes+ assert "git.commit.sha" not in attributes+ assert "git.repository_url" not in attributes+++def test_resource_names_the_commit_it_was_built_from():+ resource = OtlpResource(+ service_name="switch-core",+ service_version="1.2.3",+ environment="pilot",+ deployment_id=RESOURCE.deployment_id,+ commit_sha="0123456789abcdef0123456789abcdef01234567",+ repository_url="https://github.com/example/switch",+ )+ attributes = resource.attributes()++ assert attributes["git.commit.sha"] == "0123456789abcdef0123456789abcdef01234567"+ assert attributes["git.repository_url"] == "https://github.com/example/switch"def test_sum_points_are_delta_and_monotonic():diff --git a/core/tests/switch_core/test_version.py b/core/tests/switch_core/test_version.pyindex 6e350b873..87b754aba 100644--- a/core/tests/switch_core/test_version.py+++ b/core/tests/switch_core/test_version.py@@ -11,7 +11,14 @@ from pathlib import Pathimport pytestfrom switch_core import version as version_module-from switch_core.version import server_declaration, switch_core_version+from switch_core.version import (+ COMMIT_ENV,+ REPOSITORY_URL_ENV,+ server_declaration,+ switch_core_commit,+ switch_core_repository_url,+ switch_core_version,+)PYPROJECT = Path(__file__).resolve().parents[2] / "pyproject.toml"@@ -19,6 +26,8 @@ PYPROJECT = Path(__file__).resolve().parents[2] / "pyproject.toml"@pytest.fixture(autouse=True)def _clear_cache() -> None:switch_core_version.cache_clear()+ switch_core_commit.cache_clear()+ switch_core_repository_url.cache_clear()def _raise_not_found(name: str) -> str:@@ -96,3 +105,33 @@ def test_db_schema_cannot_ride_alongside_a_public_contract() -> None:"""The likelier mistake than asking for it alone."""with pytest.raises(ValueError, match="internal to switch-core"):server_declaration("gateway-api", "db-schema")+++def test_the_commit_comes_from_the_build(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setenv(COMMIT_ENV, "0123456789ABCDEF0123456789abcdef01234567\n")+ assert switch_core_commit() == "0123456789abcdef0123456789abcdef01234567"+++def test_an_unstamped_build_has_no_commit(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.delenv(COMMIT_ENV, raising=False)+ assert switch_core_commit() is None+ switch_core_commit.cache_clear()+ monkeypatch.setenv(COMMIT_ENV, "")+ assert switch_core_commit() is None+++def test_a_commit_that_is_not_a_hash_is_refused_with_a_warning(+ monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture+) -> None:+ monkeypatch.setenv(COMMIT_ENV, "main")+ with caplog.at_level(logging.WARNING, logger="switch_core.version"):+ assert switch_core_commit() is None+ assert COMMIT_ENV in caplog.text+++def test_the_repository_comes_from_the_build(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setenv(REPOSITORY_URL_ENV, "https://github.com/example/switch")+ assert switch_core_repository_url() == "https://github.com/example/switch"+ switch_core_repository_url.cache_clear()+ monkeypatch.setenv(REPOSITORY_URL_ENV, " ")+ assert switch_core_repository_url() is Nonediff --git a/deploy/shared_resources/images/Dockerfile.switch b/deploy/shared_resources/images/Dockerfile.switchindex 3ad7863c7..ba30be209 100644--- a/deploy/shared_resources/images/Dockerfile.switch+++ b/deploy/shared_resources/images/Dockerfile.switch@@ -34,4 +34,13 @@ COPY core/README.md ./RUN uv sync --frozen --no-dev+# The commit and repository this image was built from, reported with every+# metric and log so two builds of the same version can be told apart. Declared+# last so a new commit does not invalidate the layers above. Left empty, the+# server reports its commit as unknown.+ARG SWITCH_GIT_COMMIT=""+ARG SWITCH_GIT_REPOSITORY_URL=""+ENV SWITCH_GIT_COMMIT=${SWITCH_GIT_COMMIT} \+ SWITCH_GIT_REPOSITORY_URL=${SWITCH_GIT_REPOSITORY_URL}+CMD ["python", "-m", "switch_core.main"]