AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(platform): tag Sentry events with the deployed commit and allow environment overrides (#15460)
Nicholas Tindle26e95e27112e
diff --git a/autogpt_platform/backend/.env.default b/autogpt_platform/backend/.env.defaultindex b260df9d9f..4eacd7b4d4 100644--- a/autogpt_platform/backend/.env.default+++ b/autogpt_platform/backend/.env.default@@ -285,6 +285,11 @@ MAILERLITE_WEBHOOK_SECRET=# Error TrackingSENTRY_DSN=+# Git sha reported as the Sentry release. Unset reports no release.+# SENTRY_RELEASE=+# Replaces the app:<env>-behave:<mode> environment Sentry events are tagged+# with, e.g. pr-123 for a preview. Unset keeps the computed one.+# SENTRY_ENVIRONMENT=# Feature FlagsLAUNCH_DARKLY_SDK_KEY=diff --git a/autogpt_platform/backend/Dockerfile b/autogpt_platform/backend/Dockerfileindex 35b7a30cf2..68b8960fdf 100644--- a/autogpt_platform/backend/Dockerfile+++ b/autogpt_platform/backend/Dockerfile@@ -199,4 +199,9 @@ RUN POETRY_VIRTUALENVS_CREATE=true POETRY_VIRTUALENVS_IN_PROJECT=true \ENV PORT=8000+# The commit Sentry reports as the release. Declared after every layer so a new+# sha only changes image metadata and leaves the cached layers above intact.+ARG SENTRY_RELEASE=""+ENV SENTRY_RELEASE=${SENTRY_RELEASE}+CMD ["rest"]diff --git a/autogpt_platform/backend/backend/util/metrics.py b/autogpt_platform/backend/backend/util/metrics.pyindex c2638621fb..2faf1c447a 100644--- a/autogpt_platform/backend/backend/util/metrics.py+++ b/autogpt_platform/backend/backend/util/metrics.py@@ -296,6 +296,17 @@ def _before_send_transaction(event, hint):return event+def _sentry_environment() -> str:+ """The environment events are tagged with. A deployment that names itself+ (a PR preview, say) takes precedence over the one computed from the app+ environment and behaviour mode. A blank name counts as none."""+ config = settings.config+ return (+ config.sentry_environment.strip()+ or f"app:{config.app_env.value}-behave:{config.behave_as.value}"+ )++def sentry_init():if _running_under_pytest():return@@ -315,7 +326,8 @@ def sentry_init():# block's request to a user-supplied URL, carry no Sentry headers.trace_propagation_targets=[],profiles_sample_rate=1.0,- environment=f"app:{settings.config.app_env.value}-behave:{settings.config.behave_as.value}",+ release=settings.config.sentry_release.strip() or None,+ environment=_sentry_environment(),before_send=_before_send,before_send_transaction=_before_send_transaction,integrations=[diff --git a/autogpt_platform/backend/backend/util/metrics_test.py b/autogpt_platform/backend/backend/util/metrics_test.pyindex 327123188e..663811f62e 100644--- a/autogpt_platform/backend/backend/util/metrics_test.py+++ b/autogpt_platform/backend/backend/util/metrics_test.py@@ -12,6 +12,7 @@ from __future__ import annotationsimport jsonimport sys+import pytestimport sentry_sdkfrom ldclient.client import LDClientfrom sentry_sdk.consts import DEFAULT_OPTIONS@@ -31,6 +32,7 @@ from backend.util.metrics import (_PIKA_RECONNECT_SIGNATURES,_before_send,)+from backend.util.settings import AppEnvironment, BehaveAs, Configdef _log_event(logger: str, message: str) -> dict:@@ -491,3 +493,87 @@ def test_sentry_sdk_setup_from_sentry_init_hooks_no_launchdarkly(monkeypatch) ->finally:client.close()assert ld_clients == []+++# ---------- release and environment tagging ----------+++def _sentry_init_options(monkeypatch) -> dict:+ """The options sentry_init() hands the SDK once past the pytest guard."""+ calls = _spy_on_sentry_init(monkeypatch)+ monkeypatch.delitem(sys.modules, "pytest")+ monkeypatch.delenv("PYTEST_CURRENT_TEST", raising=False)++ metrics.sentry_init()++ assert len(calls) == 1+ return calls[0]+++def test_sentry_init_reports_the_deployed_commit_as_release(monkeypatch) -> None:+ monkeypatch.setattr(metrics.settings.config, "sentry_release", "0123abcd")++ assert _sentry_init_options(monkeypatch)["release"] == "0123abcd"+++def test_sentry_init_trims_the_release(monkeypatch) -> None:+ monkeypatch.setattr(metrics.settings.config, "sentry_release", " 0123abcd\n")++ assert _sentry_init_options(monkeypatch)["release"] == "0123abcd"++[email protected]("sha", ["", " ", "\n"])+def test_sentry_init_reports_no_release_for_a_blank_sha(monkeypatch, sha) -> None:+ """A build made without the SENTRY_RELEASE build arg bakes in an empty+ value, which has to read as no release rather than a blank one."""+ monkeypatch.setattr(metrics.settings.config, "sentry_release", sha)++ assert _sentry_init_options(monkeypatch)["release"] is None+++def test_sentry_init_environment_override_wins(monkeypatch) -> None:+ monkeypatch.setattr(metrics.settings.config, "sentry_environment", "pr-123")+ monkeypatch.setattr(metrics.settings.config, "app_env", AppEnvironment.PRODUCTION)+ monkeypatch.setattr(metrics.settings.config, "behave_as", BehaveAs.CLOUD)++ assert _sentry_init_options(monkeypatch)["environment"] == "pr-123"+++def test_sentry_init_trims_the_environment_override(monkeypatch) -> None:+ monkeypatch.setattr(metrics.settings.config, "sentry_environment", " pr-123 ")++ assert _sentry_init_options(monkeypatch)["environment"] == "pr-123"++[email protected]("override", ["", " "])+def test_sentry_init_computes_the_environment_without_an_override(+ monkeypatch, override+) -> None:+ monkeypatch.setattr(metrics.settings.config, "sentry_environment", override)+ monkeypatch.setattr(metrics.settings.config, "app_env", AppEnvironment.PRODUCTION)+ monkeypatch.setattr(metrics.settings.config, "behave_as", BehaveAs.CLOUD)++ environment = _sentry_init_options(monkeypatch)["environment"]++ assert environment == "app:prod-behave:cloud"+++def test_sentry_settings_read_the_deployment_env_vars(monkeypatch) -> None:+ """The names infra sets: SENTRY_RELEASE, baked into the image, and+ SENTRY_ENVIRONMENT, set per preview."""+ monkeypatch.setenv("SENTRY_RELEASE", "0123abcd")+ monkeypatch.setenv("SENTRY_ENVIRONMENT", "pr-123")++ config = Config()++ assert config.sentry_release == "0123abcd"+ assert config.sentry_environment == "pr-123"+++def test_sentry_settings_are_empty_for_blank_deployment_env_vars(monkeypatch) -> None:+ monkeypatch.setenv("SENTRY_RELEASE", "")+ monkeypatch.setenv("SENTRY_ENVIRONMENT", "")++ config = Config()++ assert (config.sentry_release, config.sentry_environment) == ("", "")diff --git a/autogpt_platform/backend/backend/util/settings.py b/autogpt_platform/backend/backend/util/settings.pyindex e5b3d67a30..c8a7ee8ab2 100644--- a/autogpt_platform/backend/backend/util/settings.py+++ b/autogpt_platform/backend/backend/util/settings.py@@ -988,6 +988,19 @@ class Config(UpdateTrackingModel["Config"], BaseSettings):description="What environment to behave as: local or cloud",)+ sentry_release: str = Field(+ default="",+ description="Git sha of the deployed build, reported to Sentry as the "+ "release. Blank reports no release.",+ )++ sentry_environment: str = Field(+ default="",+ description="Environment name Sentry events are tagged with, replacing "+ "the one computed from app_env and behave_as (a PR preview names itself "+ "pr-<n>). Blank keeps the computed one.",+ )+autopilot_bot_teams_allow_unverified: bool = Field(default=False,description="Local dev only: accept Teams activities that carry no Bot "diff --git a/autogpt_platform/frontend/.env.default b/autogpt_platform/frontend/.env.defaultindex c181433307..8b7007d73e 100644--- a/autogpt_platform/frontend/.env.default+++ b/autogpt_platform/frontend/.env.default@@ -57,6 +57,11 @@ NEXT_PUBLIC_BEHAVE_AS=LOCAL# Error reporting is off unless you set your own Sentry DSN here. A self-hosted# build never reports to AutoGPT's Sentry. Inlined at build time.# NEXT_PUBLIC_SENTRY_DSN=+# Replaces the app:<env>-behave:<mode> environment Sentry events are tagged+# with, e.g. pr-123 for a preview. Inlined at build time.+# NEXT_PUBLIC_SENTRY_ENVIRONMENT=+# Sentry project that build-time source maps upload to. Defaults to builder.+# SENTRY_PROJECT=# Feature flagsNEXT_PUBLIC_LAUNCHDARKLY_ENABLED=falsediff --git a/autogpt_platform/frontend/instrumentation-client.ts b/autogpt_platform/frontend/instrumentation-client.tsindex d97d81a288..74ab8aabae 100644--- a/autogpt_platform/frontend/instrumentation-client.ts+++ b/autogpt_platform/frontend/instrumentation-client.ts@@ -19,7 +19,7 @@ const dsn = environment.getSentryDSN();Sentry.init({dsn,- environment: environment.getEnvironmentStr(),+ environment: environment.getSentryEnvironment(),enabled: Boolean(dsn),diff --git a/autogpt_platform/frontend/next.config.mjs b/autogpt_platform/frontend/next.config.mjsindex acea0ad19a..0adf197943 100644--- a/autogpt_platform/frontend/next.config.mjs+++ b/autogpt_platform/frontend/next.config.mjs@@ -186,7 +186,7 @@ export default skipSentryPlugin// https://github.com/getsentry/sentry-webpack-plugin#optionsorg: "significant-gravitas",- project: "builder",+ project: process.env.SENTRY_PROJECT || "builder",// Only print logs for uploading source maps in CIsilent: !process.env.CI,diff --git a/autogpt_platform/frontend/sentry.edge.config.ts b/autogpt_platform/frontend/sentry.edge.config.tsindex 1afcefa7d5..de0fd0b830 100644--- a/autogpt_platform/frontend/sentry.edge.config.ts+++ b/autogpt_platform/frontend/sentry.edge.config.ts@@ -13,7 +13,7 @@ const dsn = environment.getSentryDSN();Sentry.init({dsn,- environment: environment.getEnvironmentStr(),+ environment: environment.getSentryEnvironment(),enabled: Boolean(dsn),diff --git a/autogpt_platform/frontend/sentry.server.config.ts b/autogpt_platform/frontend/sentry.server.config.tsindex 61da5b1272..0a6c031970 100644--- a/autogpt_platform/frontend/sentry.server.config.ts+++ b/autogpt_platform/frontend/sentry.server.config.ts@@ -13,7 +13,7 @@ const dsn = environment.getSentryDSN();Sentry.init({dsn,- environment: environment.getEnvironmentStr(),+ environment: environment.getSentryEnvironment(),enabled: Boolean(dsn),diff --git a/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.ts b/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.tsindex 7558b53f70..e3a54285ed 100644--- a/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.ts+++ b/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.ts@@ -1,4 +1,4 @@-import { describe, expect, it, vi } from "vitest";+import { afterEach, describe, expect, it, vi } from "vitest";const { initMock, reportingObserverIntegrationMock } = vi.hoisted(() => ({initMock: vi.fn(),@@ -27,6 +27,10 @@ vi.mock("@sentry/nextjs", () => {};});+afterEach(() => {+ vi.unstubAllEnvs();+});+describe("Sentry client instrumentation", () => {it("does not forward browser deprecation reports to Sentry", async () => {await import("../../instrumentation-client");@@ -135,4 +139,28 @@ describe("Sentry client instrumentation", () => {expect(beforeSend(blocked, {})).toBe(blocked);expect(beforeSend(ourWarning, {})).toBe(ourWarning);});++ it("tags events with the environment the deployment names for itself", async () => {+ vi.resetModules();+ initMock.mockClear();+ vi.stubEnv("NEXT_PUBLIC_SENTRY_ENVIRONMENT", "pr-123");+ await import("../../instrumentation-client");++ expect(initMock.mock.calls[0][0].environment).toBe("pr-123");+ });++ it("tags events with the computed environment when none is named", async () => {+ vi.resetModules();+ initMock.mockClear();+ vi.stubEnv("NEXT_PUBLIC_SENTRY_ENVIRONMENT", undefined);+ vi.stubEnv("NEXT_PUBLIC_APP_ENV", "local");+ vi.stubEnv("NEXT_PUBLIC_BEHAVE_AS", "LOCAL");+ vi.stubEnv("NEXT_PUBLIC_VERCEL_ENV", "");+ vi.stubEnv("VERCEL_ENV", "");+ await import("../../instrumentation-client");++ expect(initMock.mock.calls[0][0].environment).toBe(+ "app:local-behave:local",+ );+ });});diff --git a/autogpt_platform/frontend/src/__tests__/sentry-runtime-configs.test.ts b/autogpt_platform/frontend/src/__tests__/sentry-runtime-configs.test.tsindex 4efa5c1328..84b8b4f1c4 100644--- a/autogpt_platform/frontend/src/__tests__/sentry-runtime-configs.test.ts+++ b/autogpt_platform/frontend/src/__tests__/sentry-runtime-configs.test.ts@@ -1,4 +1,4 @@-import { beforeEach, describe, expect, it, vi } from "vitest";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";const { initMock } = vi.hoisted(() => ({ initMock: vi.fn() }));@@ -24,6 +24,10 @@ beforeEach(() => {vi.resetModules();});+afterEach(() => {+ vi.unstubAllEnvs();+});+describe.each([["edge", () => import("../../sentry.edge.config")],["server", () => import("../../sentry.server.config")],@@ -136,4 +140,26 @@ describe.each([expect(beforeSend(withCredentials(), {}).request).toEqual(stripped);expect(beforeSendTransaction(withCredentials()).request).toEqual(stripped);});++ it("tags events with the environment the deployment names for itself", async () => {+ vi.stubEnv("NEXT_PUBLIC_SENTRY_ENVIRONMENT", "pr-123");++ await load();++ expect(initMock.mock.calls[0][0].environment).toBe("pr-123");+ });++ it("tags events with the computed environment when none is named", async () => {+ vi.stubEnv("NEXT_PUBLIC_SENTRY_ENVIRONMENT", undefined);+ vi.stubEnv("NEXT_PUBLIC_APP_ENV", "local");+ vi.stubEnv("NEXT_PUBLIC_BEHAVE_AS", "LOCAL");+ vi.stubEnv("NEXT_PUBLIC_VERCEL_ENV", "");+ vi.stubEnv("VERCEL_ENV", "");++ await load();++ expect(initMock.mock.calls[0][0].environment).toBe(+ "app:local-behave:local",+ );+ });});diff --git a/autogpt_platform/frontend/src/services/environment/__tests__/index.test.ts b/autogpt_platform/frontend/src/services/environment/__tests__/index.test.tsindex 897a0a2303..ecd2c3a9f2 100644--- a/autogpt_platform/frontend/src/services/environment/__tests__/index.test.ts+++ b/autogpt_platform/frontend/src/services/environment/__tests__/index.test.ts@@ -123,6 +123,39 @@ describe("Sentry environment tagging", () => {expect(environment.isVercelPreview()).toBe(true);});++ it("tags events with the environment a deployment names for itself", () => {+ stubDeployment("prod", "preview");+ vi.stubEnv("NEXT_PUBLIC_SENTRY_ENVIRONMENT", "pr-123");++ expect(environment.getSentryEnvironment()).toBe("pr-123");+ });++ it("ignores whitespace around the environment a deployment names", () => {+ stubDeployment("prod", "preview");+ vi.stubEnv("NEXT_PUBLIC_SENTRY_ENVIRONMENT", " pr-123 ");++ expect(environment.getSentryEnvironment()).toBe("pr-123");+ });++ it.each([+ ["unset", undefined],+ ["empty", ""],+ ["blank", " "],+ ])(+ "keeps the computed environment when the override is %s",+ (_, override) => {+ stubDeployment("prod", "preview");+ vi.stubEnv("NEXT_PUBLIC_SENTRY_ENVIRONMENT", override);++ expect(environment.getSentryEnvironment()).toBe(+ "app:preview-behave:cloud",+ );+ expect(environment.getSentryEnvironment()).toBe(+ environment.getEnvironmentStr(),+ );+ },+ );});describe("Sentry enablement", () => {diff --git a/autogpt_platform/frontend/src/services/environment/index.ts b/autogpt_platform/frontend/src/services/environment/index.tsindex 8d323799e5..e6aebf46b1 100644--- a/autogpt_platform/frontend/src/services/environment/index.ts+++ b/autogpt_platform/frontend/src/services/environment/index.ts@@ -67,6 +67,11 @@ function getEnvironmentStr() {return `app:${appEnv}-behave:${getBehaveAs().toLowerCase()}`;}+function getSentryEnvironment() {+ const override = process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT?.trim();+ return override || getEnvironmentStr();+}+function getPreviewStealingDev() {const branch = process.env.NEXT_PUBLIC_PREVIEW_STEALING_DEV || "";const appEnv = getAppEnv();@@ -202,6 +207,7 @@ export const environment = {getCookiebotCBID,getCookiebotGeoRegions,getSentryDSN,+ getSentryEnvironment,// AssertionsisServerSide,isClientSide,