Skip to content

Sentry-sdk with python Azure functions not working as expected #1999

@julienFerreira

Description

@julienFerreira

How do you use Sentry?

Self-hosted/on-premise

Version

1.19.1 and 0.14.4

Steps to Reproduce

  1. Create simple Sentry test azure python http trigger function

init.py

import azure.functions as func

import sentry_sdk
from sentry_sdk.integrations.serverless import serverless_function


sentry_sdk.init(
            dsn="<my_dsn>",
            debug=True
        )

@serverless_function
def main(req: func.HttpRequest) -> func.HttpResponse:
    division_by_zero = 4/0

    return func.HttpResponse(
         "Hello from Function App.",
         status_code=200
    )
  1. add sentry-sdk in requirements.txt

  2. Launch the function pool locally or deploy to azure functions environment.

  3. Call the function

PS: Why Azure function in the python section was removed from the documentation ?

I found this example, but the documentation pages about those does not exist anymore https://github.com/getsentry/examples/tree/master/azure-functions/python

Expected Result

I got the expected result if i put the sentry_sdk.init(...) in the function.

init.py

import azure.functions as func

import sentry_sdk
from sentry_sdk.integrations.serverless import serverless_function

@serverless_function
def main(req: func.HttpRequest) -> func.HttpResponse:
    sentry_sdk.init(
            dsn="<my_dsn>",
            debug=True
        )

    division_by_zero = 4/0

    return func.HttpResponse(
         "Hello from Function App.",
         status_code=200
    )

But it doesn't make sense to do it this way.

Logs results :

[2023-04-10T08:34:53.565Z] Worker process started and initialized.
[2023-04-10T08:34:55.841Z] Executing 'Functions.func_test_sentry' (Reason='This function was programmatically called via the host APIs.', Id=f169be43-629f-4c02-b123-060377036c5e)
[2023-04-10T08:34:55.849Z]  [sentry] DEBUG: Setting up integrations (with default = True)
[2023-04-10T08:34:55.851Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.django.DjangoIntegration: Django not installed
[2023-04-10T08:34:55.852Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.flask.FlaskIntegration: Flask is not installed
[2023-04-10T08:34:55.853Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.starlette.StarletteIntegration: Starlette is not installed
[2023-04-10T08:34:55.854Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.fastapi.FastApiIntegration: Starlette is not installed
[2023-04-10T08:34:55.855Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.bottle.BottleIntegration: Bottle not installed
[2023-04-10T08:34:55.855Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.falcon.FalconIntegration: Falcon not installed
[2023-04-10T08:34:55.856Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.sanic.SanicIntegration: Sanic not installed
[2023-04-10T08:34:55.857Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.celery.CeleryIntegration: Celery not installed
[2023-04-10T08:34:55.858Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.rq.RqIntegration: RQ not installed
[2023-04-10T08:34:55.858Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.tornado.TornadoIntegration: Tornado not installed
[2023-04-10T08:34:55.859Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.sqlalchemy.SqlalchemyIntegration: SQLAlchemy not installed.
[2023-04-10T08:34:55.860Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.pyramid.PyramidIntegration: Pyramid not installed
[2023-04-10T08:34:55.861Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.boto3.Boto3Integration: botocore is not installed
[2023-04-10T08:34:55.861Z]  [sentry] DEBUG: Enabling integration logging
[2023-04-10T08:34:55.861Z]  [sentry] DEBUG: Enabling integration stdlib
[2023-04-10T08:34:55.862Z]  [sentry] DEBUG: Enabling integration excepthook
[2023-04-10T08:34:55.862Z]  [sentry] DEBUG: Enabling integration dedupe
[2023-04-10T08:34:55.862Z]  [sentry] DEBUG: Enabling integration atexit
[2023-04-10T08:34:55.862Z]  [sentry] DEBUG: Enabling integration modules
[2023-04-10T08:34:55.863Z]  [sentry] DEBUG: Enabling integration argv
[2023-04-10T08:34:55.863Z]  [sentry] DEBUG: Enabling integration threading
[2023-04-10T08:34:55.863Z]  [sentry] DEBUG: Enabling integration aiohttp
[2023-04-10T08:34:55.863Z]  [sentry] DEBUG: Enabling integration redis
[2023-04-10T08:34:55.863Z]  [sentry] DEBUG: Setting SDK name to 'sentry.python.aiohttp'
[2023-04-10T08:34:55.867Z]  [sentry] DEBUG: Flushing HTTP transport
[2023-04-10T08:34:55.867Z]  [sentry] DEBUG: background worker got flush request
**[2023-04-10T08:34:55.868Z]  [sentry] DEBUG: Sending event, type:null level:error event_id:ca0ff70d62134a56834a344977ab91bf project:161 host:sentry.vnv.ch
**[2023-04-10T08:34:55.943Z]  [sentry] DEBUG: [Tracing] Adding `sentry-trace` header e86c03971a05475094e752d10b4e6ce9-84b95bd97be74f3c- to outgoing request to https://sentry.vnv.ch/api/161/store/.
**[2023-04-10T08:34:55.950Z]  [sentry] DEBUG: background worker flushed
[2023-04-10T08:34:55.953Z] Executed 'Functions.func_test_sentry' (Failed, Id=f169be43-629f-4c02-b123-060377036c5e, Duration=112ms)
[2023-04-10T08:34:55.953Z] System.Private.CoreLib: Exception while executing function: Functions.func_test_sentry. System.Private.CoreLib: Result: Failure
[2023-04-10T08:34:55.953Z] Exception: ZeroDivisionError: division by zero
[2023-04-10T08:34:55.953Z] Stack:   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 448, in _handle__invocation_request
[2023-04-10T08:34:55.953Z]     call_result = await self._loop.run_in_executor(
[2023-04-10T08:34:55.953Z]   File "/usr/lib/python3.9/concurrent/futures/thread.py", line 58, in run
[2023-04-10T08:34:55.953Z]     result = self.fn(*self.args, **self.kwargs)
[2023-04-10T08:34:55.954Z]   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 691, in _run_sync_func
[2023-04-10T08:34:55.954Z]     return ExtensionManager.get_sync_invocation_wrapper(context,
[2023-04-10T08:34:55.954Z]   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.9/LINUX/X64/azure_functions_worker/extension.py", line 215, in _raw_invocation_wrapper
[2023-04-10T08:34:55.954Z]     result = function(**args)
[2023-04-10T08:34:55.954Z]   File "/home/julien/my-project/admin-functions/.venv/lib/python3.9/site-packages/sentry_sdk/integrations/serverless.py", line 55, in inner
[2023-04-10T08:34:55.954Z]     _capture_and_reraise()
[2023-04-10T08:34:55.954Z]   File "/home/julien/my-project/admin-functions/.venv/lib/python3.9/site-packages/sentry_sdk/integrations/serverless.py", line 80, in _capture_and_reraise
[2023-04-10T08:34:55.954Z]     reraise(*exc_info)
[2023-04-10T08:34:55.954Z]   File "/home/julien/my-project/admin-functions/.venv/lib/python3.9/site-packages/sentry_sdk/_compat.py", line 60, in reraise
[2023-04-10T08:34:55.954Z]     raise value
[2023-04-10T08:34:55.954Z]   File "/home/julien/my-project/admin-functions/.venv/lib/python3.9/site-packages/sentry_sdk/integrations/serverless.py", line 53, in inner
[2023-04-10T08:34:55.954Z]     return f(*args, **kwargs)
[2023-04-10T08:34:55.954Z]   File "/home/julien/my-project/admin-functions/func_test_sentry/__init__.py", line 17, in main
[2023-04-10T08:34:55.955Z]     division_by_zero = 4/0
[2023-04-10T08:34:55.955Z] .

Actual Result

As we can see in the logs, the error is caught by the sdk, but this one is never send to the sentry interface.

[2023-04-10T08:28:09.623Z] Worker process started and initialized.
[2023-04-10T08:28:10.532Z]  [sentry] DEBUG: Setting up integrations (with default = True)
[2023-04-10T08:28:10.532Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.django.DjangoIntegration: Django not installed
[2023-04-10T08:28:10.532Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.flask.FlaskIntegration: Flask is not installed
[2023-04-10T08:28:10.533Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.starlette.StarletteIntegration: Starlette is not installed
[2023-04-10T08:28:10.534Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.fastapi.FastApiIntegration: Starlette is not installed
[2023-04-10T08:28:10.535Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.bottle.BottleIntegration: Bottle not installed
[2023-04-10T08:28:10.535Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.falcon.FalconIntegration: Falcon not installed
[2023-04-10T08:28:10.536Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.sanic.SanicIntegration: Sanic not installed
[2023-04-10T08:28:10.537Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.celery.CeleryIntegration: Celery not installed
[2023-04-10T08:28:10.538Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.rq.RqIntegration: RQ not installed
[2023-04-10T08:28:10.539Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.tornado.TornadoIntegration: Tornado not installed
[2023-04-10T08:28:10.540Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.sqlalchemy.SqlalchemyIntegration: SQLAlchemy not installed.
[2023-04-10T08:28:10.541Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.pyramid.PyramidIntegration: Pyramid not installed
[2023-04-10T08:28:10.542Z]  [sentry] DEBUG: Did not import default integration sentry_sdk.integrations.boto3.Boto3Integration: botocore is not installed
[2023-04-10T08:28:10.542Z]  [sentry] DEBUG: Enabling integration logging
[2023-04-10T08:28:10.542Z]  [sentry] DEBUG: Enabling integration stdlib
[2023-04-10T08:28:10.542Z]  [sentry] DEBUG: Enabling integration excepthook
[2023-04-10T08:28:10.542Z]  [sentry] DEBUG: Enabling integration dedupe
[2023-04-10T08:28:10.543Z]  [sentry] DEBUG: Enabling integration atexit
[2023-04-10T08:28:10.543Z]  [sentry] DEBUG: Enabling integration modules
[2023-04-10T08:28:10.543Z]  [sentry] DEBUG: Enabling integration argv
[2023-04-10T08:28:10.543Z]  [sentry] DEBUG: Enabling integration threading
[2023-04-10T08:28:10.543Z]  [sentry] DEBUG: Enabling integration aiohttp
[2023-04-10T08:28:10.544Z]  [sentry] DEBUG: Enabling integration redis
[2023-04-10T08:28:10.544Z]  [sentry] DEBUG: Setting SDK name to 'sentry.python.aiohttp'
[2023-04-10T08:28:14.336Z] Host lock lease acquired by instance ID '000000000000000000000000DB29D462'.
[2023-04-10T08:28:46.192Z] Executing 'Functions.func_test_sentry' (Reason='This function was programmatically called via the host APIs.', Id=ce43acc7-279a-4020-9ec4-c2b6e6cb2504)
[2023-04-10T08:28:46.259Z] Executed 'Functions.func_test_sentry' (Failed, Id=ce43acc7-279a-4020-9ec4-c2b6e6cb2504, Duration=79ms)
[2023-04-10T08:28:46.259Z] System.Private.CoreLib: Exception while executing function: Functions.func_test_sentry. System.Private.CoreLib: Result: Failure
[2023-04-10T08:28:46.259Z] Exception: ZeroDivisionError: division by zero
[2023-04-10T08:28:46.259Z] Stack:   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 448, in _handle__invocation_request
[2023-04-10T08:28:46.259Z]     call_result = await self._loop.run_in_executor(
[2023-04-10T08:28:46.259Z]   File "/usr/lib/python3.9/concurrent/futures/thread.py", line 58, in run
[2023-04-10T08:28:46.260Z]     result = self.fn(*self.args, **self.kwargs)
[2023-04-10T08:28:46.260Z]   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 691, in _run_sync_func
[2023-04-10T08:28:46.260Z]     return ExtensionManager.get_sync_invocation_wrapper(context,
[2023-04-10T08:28:46.260Z]   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.9/LINUX/X64/azure_functions_worker/extension.py", line 215, in _raw_invocation_wrapper
[2023-04-10T08:28:46.260Z]     result = function(**args)
[2023-04-10T08:28:46.260Z]   File "/home/julien/my-project/admin-functions/.venv/lib/python3.9/site-packages/sentry_sdk/integrations/serverless.py", line 55, in inner
[2023-04-10T08:28:46.260Z]     _capture_and_reraise()
[2023-04-10T08:28:46.260Z]   File "/home/julien/my-project/admin-functions/.venv/lib/python3.9/site-packages/sentry_sdk/integrations/serverless.py", line 80, in _capture_and_reraise
[2023-04-10T08:28:46.260Z]     reraise(*exc_info)
[2023-04-10T08:28:46.260Z]   File "/home/julien/my-project/admin-functions/.venv/lib/python3.9/site-packages/sentry_sdk/_compat.py", line 60, in reraise
[2023-04-10T08:28:46.261Z]     raise value
[2023-04-10T08:28:46.261Z]   File "/home/julien/my-project/admin-functions/.venv/lib/python3.9/site-packages/sentry_sdk/integrations/serverless.py", line 53, in inner
[2023-04-10T08:28:46.261Z]     return f(*args, **kwargs)
[2023-04-10T08:28:46.261Z]   File "/home/julien/my-project/admin-functions/func_test_sentry/__init__.py", line 17, in main
[2023-04-10T08:28:46.261Z]     division_by_zero = 4/0
[2023-04-10T08:28:46.261Z] .

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions