Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APM-Instrumented Azure Function crashes when querying to Azure Table #2118

Open
cpiment opened this issue Sep 5, 2024 · 3 comments · May be fixed by #2119
Open

APM-Instrumented Azure Function crashes when querying to Azure Table #2118

cpiment opened this issue Sep 5, 2024 · 3 comments · May be fixed by #2119
Labels
agent-python community Issues opened by the community triage Issues awaiting triage

Comments

@cpiment
Copy link

cpiment commented Sep 5, 2024

Describe the bug:

When using Azure Table Client (package azure-data-tables) inside an Azure Function instrumented with elasticapm.contrib.serverless.azure.ElasticAPMExtension and querying a data table using PartitionKey and RowKey, the underlying HTTP request to the table service sends the request parameters in the query string and the request has no body. When Azure Tables instrumentation from Elastic APM reaches this line:

body = request.body
try:
body = json.loads(body)
except json.decoder.JSONDecodeError: # str not bytes
body = {}
# /tablename(PartitionKey='<partition-key>',RowKey='<row-key>')

It crashes because the body is None

To Reproduce

  1. Create an Storage Account in Azure and create a Table named 'test' and insert an entity with PartitionKey 'KEY' and RowKey 'ROW'
  2. Get the Storage Account SAS_TOKEN to connect to the table service and query the test table
  3. Create an Azure Function with this code
import azure.functions as func

from elasticapm.contrib.serverless.azure import ElasticAPMExtension
from azure.data.tables import TableServiceClient
from azure.core.credentials import AzureSasCredential

ElasticAPMExtension.configure()


def main(req: func.HttpRequest) -> func.HttpResponse:
    _table_service_client = TableServiceClient(endpoint=TABLE_ENDPOINT,credential=AzureSasCredential(TABLE_SAS_TOKEN))
    _table_client = _table_service_client.get_table_client('test')
    entity = _table_client.get_entity(
            partition_key='KEY',
            row_key='ROW'
        )
  1. Run func host start to start the local development environment
  2. Connect to the URL of the function and see the exception in the func logs

Environment (please complete the following information)

  • OS: Windows
  • Python version: 3.10.11
  • Framework and version [e.g. Django 2.1]: Azure Functions
  • APM Server version: 7.17
  • Agent version: 6.23.0

Additional context

Add any other context about the problem here.

  • requirements.txt:

    Click to expand
    elasticapm
    azure-functions
    azure-data-tables
    
@github-actions github-actions bot added agent-python community Issues opened by the community triage Issues awaiting triage labels Sep 5, 2024
@xrmx
Copy link
Member

xrmx commented Sep 5, 2024

Thanks for reporting, I think if you are keen to contributing a possible patch would be to just skip the json decoding if body is falsy and set it to {}.

@cpiment
Copy link
Author

cpiment commented Sep 5, 2024

Thanks @xrmx , will do that. What would be the best way to check "falsyness"? Something in the line of this would be enough?

if not body:
  body = {}
else:
  try: 
    body = json.loads(body) 
  except json.decoder.JSONDecodeError:  # str not bytes 
    body = {} 

@xrmx
Copy link
Member

xrmx commented Sep 5, 2024

I would keep the happy path before (if body:) but yeah, something like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent-python community Issues opened by the community triage Issues awaiting triage
Projects
None yet
2 participants