9

I want to Test my azure function using the Azure Apps feature to Run/Test mode but it is throwing the '500 internal server error'. I am able to debug the same code in my local environment but when to trigger the same code on the azure portal then it is getting failed without any proper error logs. enter image description here This Azure function will read the json format data from the event hub and write the same to the blob storage. I am using python for the azure function development. Here is the code: init.py

from typing import List
import logging
import os
import azure.functions as func
from azure.storage.blob import BlobClient
import datetime
import json

storage_connection_string = os.getenv('storage_connection_string_FromKeyVault')

container_name = os.getenv('storage_container_name_FromKeyVault')

today = datetime.datetime.today()


def main(events: List[func.EventHubEvent]):
    for event in events:
        a = event.get_body().decode('utf-8')
        json.loads(a)
        logging.info('Python EventHub trigger processed an event: %s', a)
        logging.info(f'  SequenceNumber = {event.sequence_number}')
        logging.info(f'  Offset = {event.offset}')

        blob_client =  BlobClient.from_connection_string(storage_connection_string, container_name, str(today.year) +"/" + str(today.month) + "/" + str(today.day) + "/" + str(event.sequence_number) + ".json")

        blob_client.upload_blob(event.get_body().decode(),blob_type="AppendBlob")

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "<Endpoint1>",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "storage_connection_string_FromKeyVault": "<connectionString",
    "storage_container_name_FromKeyVault": "<container_name>",
    "EventHubReceiverPolicy_FromKeyVault": "<Endpoint2>"
  }
}

function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "events",
      "direction": "in",
      "eventHubName": "pwo-events",
      "connection": "EventHubReceiverPolicy_FromKeyVault",
      "cardinality": "many",
      "consumerGroup": "$Default",
      "dataType": "binary"
    }
  ]
}

Please note that this error is throwing when I am clicking on Run/Test on the portal. but the same code is also running fine after deployment.

Stark
  • 604
  • 3
  • 11
  • 30
  • Based on the exception info ,it is due to code issue. But it is hard to find the root issue, pls share your code or trace log. – Stanley Gong Jun 17 '21 at 01:45
  • @StanleyGong I have added my code to the question. Thanks – Stark Jun 17 '21 at 04:52
  • @StanleyGong this code is throwing the error when I am trying to test the application by using the test/run option on the portal.. but otherwise application is running on the azure portal. – Stark Jun 17 '21 at 05:42
  • I have enable the application insight so if required I can share the logs. I am using the consumption plan. – Stark Jun 17 '21 at 05:43
  • @StanleyGong good morning, can you please help me for the following question- https://stackoverflow.com/questions/68109998/azure-function-how-do-i-write-my-logs-to-azure-application-insights-locally-wi – Stark Jun 24 '21 at 05:18

2 Answers2

11

The 500 error is not helpful to solve this problem, you need to check the specific error of the azure function. You can use application insights to get the details error. The function must configure the corresponding application insights before you can view the log on the portal.

So you need to configure an application insights to your function app like this:

enter image description here

Then your function app will restart.

Of course, you can also go to kudu to view:

First, go to advanced tools, then click 'GO',

enter image description here

Then After you go to kudu, click Debug Console -> CMD -> LogFiles -> Application -> Functions -> yourtriggername. You will find log file there.

If you are based on linux OS, after go to kudu, just click 'log stream'(this is not supportted to consumption plan for linux.).

Cindy Pau
  • 13,085
  • 1
  • 15
  • 27
  • I am using the consumption plan.. I have added my code to the question. Pls have a look. Thanks – Stark Jun 17 '21 at 06:54
  • @Stark Did you check the logs? The error or exception should be in it. – Cindy Pau Jun 17 '21 at 06:56
  • not yet.. will update you very soon for this.. thanks – Stark Jun 29 '21 at 04:25
  • can you please help me out from the following questions on azure functions https://stackoverflow.com/questions/69480321/azure-function-using-powershell-to-consume-the-data-from-an-event-hub-and-write?noredirect=1#comment122808093_69480321 – Stark Oct 07 '21 at 12:18
  • I have no logs about failed requests anywhere in App Insights. it's simply "500 Internal Server Error" and I have no further information I can gather from anywhere in Azure.. – Thomas Glaser Jul 14 '23 at 12:13
0

I had this problem and I found that problem was with dependencies. Removing unexisting libraries (or using Microsoft's bring dependency document) will solve the issue.

Adding third-party dependencies in the Azure portal is currently not supported for Linux Consumption Function Apps. Click here to setup local environment. Learn more

If you need dependencies, to solve this problem, you can refer to this Microsoft Document

sglbl
  • 141
  • 1
  • 3