From 632e7cd758368403031e405921d99a79ee71ef3d Mon Sep 17 00:00:00 2001
From: "David H. Irving" <david.irving@noirlab.edu>
Date: Fri, 15 Nov 2024 15:36:06 -0700
Subject: [PATCH] Do not trace health check endpoint

Avoid cluttering trace logs by not tracking the health check endpoint.
---
 .../daf/butler/remote_butler/server/_telemetry.py  | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/python/lsst/daf/butler/remote_butler/server/_telemetry.py b/python/lsst/daf/butler/remote_butler/server/_telemetry.py
index 031b66f2bd..22a067e56a 100644
--- a/python/lsst/daf/butler/remote_butler/server/_telemetry.py
+++ b/python/lsst/daf/butler/remote_butler/server/_telemetry.py
@@ -25,6 +25,8 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from typing import Any
+
 
 def enable_telemetry() -> None:
     """Turn on upload of trace telemetry to Sentry, to allow performance
@@ -38,4 +40,14 @@ def enable_telemetry() -> None:
     # Configuration will be pulled from SENTRY_* environment variables
     # (see https://docs.sentry.io/platforms/python/configuration/options/).
     # If SENTRY_DSN is not present, telemetry is disabled.
-    sentry_sdk.init(enable_tracing=True)
+    sentry_sdk.init(enable_tracing=True, traces_sampler=_decide_whether_to_sample_trace)
+
+
+def _decide_whether_to_sample_trace(context: dict[str, Any]) -> float:
+    asgi_scope = context.get("asgi_scope")
+    if asgi_scope is not None:
+        # Do not log health check endpoint.
+        if asgi_scope.get("path") == "/":
+            return 0
+
+    return 1