From 5aaa90ac0dc0a2fb14f95f52bbe18bdd6d1e105c Mon Sep 17 00:00:00 2001 From: Ali Kelkawi Date: Wed, 28 Feb 2024 11:51:30 +0300 Subject: [PATCH] add sentry sample rate config --- config.yaml | 8 ++++++++ src/charm.py | 4 ++++ src/resources/worker.py | 1 + tests/integration/conftest.py | 1 + tests/integration/helpers.py | 4 ++-- tests/unit/test_charm.py | 9 +++++++++ 6 files changed, 25 insertions(+), 2 deletions(-) diff --git a/config.yaml b/config.yaml index f76d1e1..e5e82ea 100644 --- a/config.yaml +++ b/config.yaml @@ -61,6 +61,14 @@ options: default: false type: boolean + sentry-sample-rate: + description: | + A value between 0 (0% of errors) and 1 (100% of errors) to indicate the proportion of errors + to be captured by Sentry. + + default: 1.0 + type: float + workflows-file-name: description: Name of the wheel file resource attached to the charm. default: "" diff --git a/src/charm.py b/src/charm.py index c6cde50..ec45026 100755 --- a/src/charm.py +++ b/src/charm.py @@ -283,6 +283,10 @@ def _validate(self, event): # noqa: C901 elif self.config["auth-provider"] == "google": self._check_required_config(REQUIRED_OIDC_CONFIG) + sample_rate = self.config["sentry-sample-rate"] + if self.config["sentry-dsn"] and (sample_rate < 0 or sample_rate > 1): + raise ValueError("Invalid config: sentry-sample-rate must be between 0 and 1") + def _update(self, event): """Update the Temporal worker configuration and replan its execution. diff --git a/src/resources/worker.py b/src/resources/worker.py index cc8d748..adbdd75 100644 --- a/src/resources/worker.py +++ b/src/resources/worker.py @@ -135,6 +135,7 @@ async def run_worker(unpacked_file_name, module_name): release=os.getenv("TWC_SENTRY_RELEASE").strip() or None, environment=os.getenv("TWC_SENTRY_ENVIRONMENT").strip() or None, redact_params=os.getenv("TWC_SENTRY_REDACT_PARAMS"), + sample_rate=os.getenv("TWC_SENTRY_SAMPLE_RATE"), ) worker_opt = WorkerOptions(sentry=sentry) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 8022da8..9be93e5 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -28,6 +28,7 @@ @pytest_asyncio.fixture(name="deploy", scope="module") async def deploy(ops_test: OpsTest): """Verify the app is up and running.""" + await ops_test.model.set_config({"update-status-hook-interval": "1m"}) await setup_temporal_ecosystem(ops_test) charm = await ops_test.build_charm(".") diff --git a/tests/integration/helpers.py b/tests/integration/helpers.py index eb4303b..f26e749 100644 --- a/tests/integration/helpers.py +++ b/tests/integration/helpers.py @@ -135,10 +135,10 @@ async def setup_temporal_ecosystem(ops_test: OpsTest): ops_test: PyTest object. """ await asyncio.gather( - ops_test.model.deploy(APP_NAME_SERVER, channel="edge"), + ops_test.model.deploy(APP_NAME_SERVER, channel="edge", config={"num-history-shards": 1}), ops_test.model.deploy(APP_NAME_ADMIN, channel="edge"), ops_test.model.deploy(APP_NAME_UI, channel="edge"), - ops_test.model.deploy("postgresql-k8s", channel="14", trust=True), + ops_test.model.deploy("postgresql-k8s", channel="14/stable", trust=True), ) async with ops_test.fast_forward(): diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index 9be9ed9..4e853af 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -137,6 +137,7 @@ def test_ready(self, _process_wheel_file, _setup_container): "TWC_SENTRY_DSN": "", "TWC_SENTRY_ENVIRONMENT": "", "TWC_SENTRY_RELEASE": "", + "TWC_SENTRY_SAMPLE_RATE": 1.0, "TWC_SENTRY_REDACT_PARAMS": False, "TWC_SUPPORTED_ACTIVITIES": "all", "TWC_SUPPORTED_WORKFLOWS": "all", @@ -149,6 +150,14 @@ def test_ready(self, _process_wheel_file, _setup_container): "on-check-failure": {"up": "ignore"}, } }, + "checks": { + "up": { + "override": "replace", + "level": "alive", + "period": "10s", + "exec": {"command": "python check_status.py"}, + } + }, } got_plan = harness.get_container_pebble_plan("temporal-worker").to_dict() self.assertEqual(got_plan, want_plan)