question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Python DateTime Error For Blob Events In Event Grid Azure Function

See original GitHub issue

Issue Description

The Azure Event Grid Trigger function in python throws an error on receiving a blob event. The error is related to DateTime format. The date returned by the blob event to the python function cannot be parsed by python as the millisecond value has seven digits. E.g. the date ‘2018-12-09T23:47:03.285137Z’ cannot be parsed by python as the millisecond value has seven digits which is not recognized in python.

Result: Failure
Exception: ValueError: time data '2018-12-12T03:16:34.2191989Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'
Stack:   File "/home/site/wwwroot/.python_packages/lib/python3.6/site-packages/azure/functions_worker/dispatcher.py", line 250, in _handle__invocation_request
    pytype=pb_type_info.pytype)
  File "/home/site/wwwroot/.python_packages/lib/python3.6/site-packages/azure/functions_worker/bindings/meta.py", line 285, in from_incoming_proto
    trigger_metadata=trigger_metadata)
  File "/home/site/wwwroot/.python_packages/lib/python3.6/site-packages/azure/functions_worker/bindings/eventgrid.py", line 94, in from_proto
    event_time=cls._parse_datetime(body.get('eventTime')),
  File "/home/site/wwwroot/.python_packages/lib/python3.6/site-packages/azure/functions_worker/bindings/meta.py", line 181, in _parse_datetime
    raise last_error
  File "/home/site/wwwroot/.python_packages/lib/python3.6/site-packages/azure/functions_worker/bindings/meta.py", line 176, in _parse_datetime
    dt = datetime.datetime.strptime(datetime_str, fmt)
  File "/root/.pyenv/versions/3.6.6/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/root/.pyenv/versions/3.6.6/lib/python3.6/_strptime.py", line 362, in _strptime
    (data_string, format))

Steps to reproduce issue

#### STEP 1: CREATE AZURE FUNCTION

groupName="<resource_group_name>"
location="westeurope"
functionStorageName="<function_storage_account_name>"
appinsightsKey="<application_insights_instrumentation_key>"
appName="<function_app_name>"

#### CREATE RESOURCE GROUP

az group create --name $groupName --location $location

#### REGISTER EVENT GRID PROVIDER NAMESPACE

az provider register --namespace Microsoft.EventGrid

#### CREATE STORAGE ACCOUNT FOR FUNCTION APP
az storage account create --name $functionStorageName --location $location --resource-group $groupName --sku Standard_LRS --kind StorageV2

#### CREATE FUNCTION APP
az functionapp create --resource-group $groupName --os-type Linux --consumption-plan-location $location --runtime python --name $appName --storage-account $functionStorageName

#### CONFIGURE APP INSIGHTS FOR FUNCTION
az functionapp config appsettings set --name $appName --resource-group $groupName --settings "APPINSIGHTS_INSTRUMENTATIONKEY=$appinsightsKey"

#### STEP 2: WRITE FUNCTION CODE AND PUBLISH
func init testfunction --worker-runtime python
cd testfunction
func new --language python --template "Azure Event Grid trigger" --name EventGridTrigger
func azure functionapp publish $appName

#### STEP 3: CREATE A STORAGE ACCOUNT WHOSE EVENTS NEED TO BE RECORDED
generalStorageName="<storage_account_name>"
az storage account create --name $generalStorageName --location $location --resource-group $groupName --sku Standard_LRS --kind StorageV2
storageId=$(az storage account show --name $generalStorageName --resource-group $groupName --query id --output tsv)
accountKey=$(az storage account keys list --account-name $generalStorageName | grep "value" | awk -F ": " '{print $NF}' | sed 's/"//g' | head -1)

#### STEP 4: GET FUNCTION MASTER KEY FROM AZURE PORTAL (browse to 'Manage' section under the EventGridTrigger function)
functionMasterKey="<function_master_key>"
endpoint="https://$appName.azurewebsites.net/runtime/webhooks/eventgrid?functionName=EventGridTrigger&code=$functionMasterKey"

#### STEP 5: REGISTER EVENT GRID SUBSCRIPTION
az eventgrid event-subscription create --resource-id $storageId --name "myevtgridtrig" --endpoint $endpoint

#### STEP 6: UPLOAD A FILE TO STORAGE BLOB TO TEST EVENT
az storage container create --name test1 --account-name $generalStorageName --account-key $accountKey
echo "hello" > hello.txt
az storage blob upload --container-name test1 --account-name $generalStorageName --account-key $accountKey --file hello.txt --name "hello.txt"

STEP 7: REVIEW MONITOR OR APP INSIGHTS FOR LOGS/ERRORS

Resources

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
elpranscommented, Jan 30, 2019

I’ll post a fix shortly

0reactions
kavithamadhavarajcommented, Feb 11, 2019

@nicklooo Hi, my zip folder has /worker_venv/lib/python3.6/site-packages/azure/functions_worker/bindings/ this path only. I’m using --build-with-native-deps flag. This makes the folder structure to the above mentioned one. If not, I can see {zip folder}\.python_packages\lib\python3.6\site-packages\azure\functions_worker in my zip file. Nevertheless, when i made changes to the packages & my function app code, the new changes from the later was reflecting the portal but the fix in the package file didn’t work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Event Grid-triggered Function Keeps Triggering After ...
using the BlobClient.from_blob_url method worked fine when testing blob uploads using Azure Storage Explorer. But when using Azure Data Factory, ...
Read more >
Azure Function - Blob Trigger Event Grid
Hi,. I'm trying to do the following : run an Azure Function (python 3.9) each time a blob is uploaded to a container...
Read more >
Azure Functions error handling and retry guidance
Learn to handle errors and retry events in Azure Functions with links to specific binding errors, including information on retry policies.
Read more >
Azure Event Grid client library for Python
A system topic in Event Grid represents one or more events published by Azure services such as Azure Storage or Azure Event Hubs....
Read more >
Azure Event Grid trigger for Azure Functions
Learn to run code when Event Grid events in Azure Functions are dispatched.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found