Skip to content

Commit

Permalink
Add Sentry telemetry
Browse files Browse the repository at this point in the history
Add the Sentry SDK and use it to capture traces for debugging performance problems.
  • Loading branch information
dhirving committed Nov 18, 2024
1 parent 75449b2 commit a27be9a
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 7 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ update: update-deps init

.PHONY: update-deps
update-deps:
pip install --upgrade uv
uv pip install --upgrade pre-commit
pre-commit autoupdate
uv pip compile --upgrade --generate-hashes \
# pip install --upgrade uv
# uv pip install --upgrade pre-commit
# pre-commit autoupdate
uv pip compile --generate-hashes \
--output-file requirements/main.txt requirements/main.in
uv pip compile --upgrade --generate-hashes \
uv pip compile --generate-hashes \
--output-file requirements/dev.txt requirements/dev.in
uv pip compile --upgrade --generate-hashes \
uv pip compile --generate-hashes \
--output-file requirements/tox.txt requirements/tox.in

# Useful for testing against a Git version of Safir.
Expand Down
4 changes: 4 additions & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ anyio==4.6.2.post1 \
# via
# -c requirements/main.txt
# httpx
appnope==0.1.4 \
--hash=sha256:1de3860566df9caf38f01f86f65e0e13e379af54f9e4bee1e66b48f2efffd1ee \
--hash=sha256:502575ee11cd7a28c0205f379b525beefebab9d161b7c964670864014ed7213c
# via ipykernel
asgi-lifespan==2.1.0 \
--hash=sha256:5e2effaf0bfe39829cf2d64e7ecc47c7d86d676a6599f7afba378c31f5e3a308 \
--hash=sha256:ed840706680e28428c01e14afb3875d7d76d3206f3d5b2f2294e059b5c23804f
Expand Down
1 change: 1 addition & 0 deletions requirements/main.in
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ vo-models
defusedxml
lsst-daf-butler[postgres, remote]
lsst-dax-obscore @ git+https://github.com/lsst-dm/dax_obscore@main#egg=lsst-dax-obscore
sentry-sdk
8 changes: 7 additions & 1 deletion requirements/main.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ certifi==2024.8.30 \
# httpcore
# httpx
# requests
# sentry-sdk
cffi==1.17.1 \
--hash=sha256:045d61c734659cc045141be4bae381a41d89b741f795af1dd018bfb532fd0df8 \
--hash=sha256:0984a4925a435b1da406122d4d7968dd861c1385afe3b45ba82b750f229811e2 \
Expand Down Expand Up @@ -553,7 +554,7 @@ lsst-daf-relation==27.2024.4300 \
--hash=sha256:26cf7d4583adfcd03d708a5ffc80c18c52c78e54607cad7b104f4dcf30f3a9ff \
--hash=sha256:cae56b91e3fc44626c4fbc2aeb432817baa1084b43b0ff386e9f1e44498dd12d
# via lsst-daf-butler
lsst-dax-obscore @ git+https://github.com/lsst-dm/dax_obscore@5919c78c29630b1c9ed7544a1e075d8cc7ebda22#egg=lsst-dax-obscore
lsst-dax-obscore @ git+https://github.com/lsst-dm/dax_obscore@9897f02c276698048087a91c4e0e36c808d66fdf#egg=lsst-dax-obscore
# via -r requirements/main.in
lsst-felis==27.2024.4300 \
--hash=sha256:5584317179c191c632233408f4a972d51165d0ef48420b2d374360a2f3d0fe37 \
Expand Down Expand Up @@ -1214,6 +1215,10 @@ safir-logging==6.5.1 \
--hash=sha256:b056306de26627e29bd6a6d04b1144456a1319ec0e15a67ebbc12b43362a27cd \
--hash=sha256:ff591f0247fda10842835e714a6dbf601a894432d33d6d98e20fe035a5ad952c
# via safir
sentry-sdk==2.18.0 \
--hash=sha256:0dc21febd1ab35c648391c664df96f5f79fb0d92d7d4225cd9832e53a617cafd \
--hash=sha256:ee70e27d1bbe4cd52a38e1bd28a5fadb9b17bc29d91b5f2b97ae29c0a7610442
# via -r requirements/main.in
six==1.16.0 \
--hash=sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926 \
--hash=sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254
Expand Down Expand Up @@ -1323,6 +1328,7 @@ urllib3==2.2.3 \
# botocore
# lsst-resources
# requests
# sentry-sdk
uvicorn==0.32.0 \
--hash=sha256:60b8f3a5ac027dcd31448f411ced12b5ef452c646f76f02f8cc3f25d8d26fd82 \
--hash=sha256:f78b36b143c16f54ccdb8190d0a26b5f1901fe5a3c777e1ab29f26391af8551e
Expand Down
2 changes: 2 additions & 0 deletions src/sia/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from .handlers.external import external_router
from .handlers.internal import internal_router
from .middleware.ivoa import CaseInsensitiveFormMiddleware
from .telemetry import enable_telemetry

__all__ = ["app"]

Expand Down Expand Up @@ -63,6 +64,7 @@ async def lifespan(app: FastAPI) -> AsyncIterator[None]:
name="sia",
)
configure_uvicorn_logging(config.log_level)
enable_telemetry()

app = FastAPI(
title="sia",
Expand Down
53 changes: 53 additions & 0 deletions src/sia/telemetry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# This file is part of daf_butler.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (http://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This software is dual licensed under the GNU General Public License and also
# under a 3-clause BSD license. Recipients may choose which of these licenses
# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
# respectively. If you choose the GPL option then the following text applies
# (but note that there is still no warranty even if you opt for BSD instead):
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# 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
debugging of deployed server.
"""
try:
import sentry_sdk
except ImportError:
return

# 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, 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

0 comments on commit a27be9a

Please sign in to comment.