0

My Isolated Azure functions cosmosDB trigger is invoked saying there are new changes on the container. But no new changes are received and the execution fails. This is creating false alerts as the request fails. Below are the two messages in the logs. How to fix this ?

Executing 'Functions.TestCosmosDBTrigger' (Reason='New changes on container TestContainer at 2023-02-16T00:09:39.3365522Z', Id=xxxxx-xxxx-xxx)
Executed 'Functions.TestCosmosDBTrigger' (Failed, Id=xxxxx-xxxx-xxx, Duration=86ms)

Edit:

[Function("TestCosmosDBTrigger")]
public async Task RunAsync([CosmosDBTrigger(
    databaseName: "testdata",
    containerName: "TestContainer",
    Connection = "connect",
    LeaseContainerName = "leases")] IReadOnlyList<MyDocument> input)
{   
    //first line
    logger.LogInformation("Received count {}", input.Count);
}

Update :

After enabling logs. The only relevant logs available are as below:

"Category":"Host.Triggers.CosmosDB","LogLevel":"Error","prop__{OriginalFormat}":"Lease {LeaseToken} experienced an error during processing

enter image description here

enter image description here

DxG
  • 147
  • 4
  • 17
  • Your Function has failures processing, you need to enable the logs to see which problem it is: https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/how-to-configure-cosmos-db-trigger#enabling-trigger-specific-logs – Matias Quaranta Feb 17 '23 at 16:30

1 Answers1

0

Most commonly you are experiencing processing/unhandled exceptions in your Function code.

The CosmosDBTrigger has detailed logs that can tell you what the problem is and help in debugging multiple scenarios.

Edit the host.json file in your project and add the logging configuration for the Trigger as per https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/how-to-configure-cosmos-db-trigger#enabling-trigger-specific-logs. You can select the log level you desire (if you are debugging I would recommend Debug temporarily until you figure out the problem, then you can leave it as Error).

Example host.json:

{
  "version": "2.0",
  "logging": {
    "fileLoggingMode": "always",
    "logLevel": {
      "Host.Triggers.CosmosDB": "Debug"
    }
  }
}

Your file might already have other content, make sure you add it to the logging section.

EDIT AFTER NEW CODE: The most common cause is a deserialization error, it will show up in the Trigger logs. The SDK is trying to take the raw JSON and deserialize it to MyDocument and might be failing due to something on that class definition. Enable the logs for more information.

EDIT AFTER LOGS: The logs show that the changes are delivered to your Function code, and there is a processing error. This error can be either an unhandled exception during execution or a serialization error. Check for the "Exception" column on App Insight logs. Based on comments, if you want to use System.Text.Json, you need to customize the serialization engine: https://stackoverflow.com/a/71243311/5641598

Matias Quaranta
  • 13,907
  • 1
  • 22
  • 47
  • I doubt if it is due to unhandled exception. I added the sample code now. The first line in the RunAsync() prints the count, which didn't execute during the failure. It is like the execution never entered the RunAsync() method. Debug logging might not help here as we are not sure when this error may occur. – DxG Feb 17 '23 at 17:35
  • It could be a deserialization error too. I would recommend adding the logs and then taking a look at what it's shown there. – Matias Quaranta Feb 17 '23 at 18:20
  • Any deserialization error trying to use your type `MyDocument` will not trigger the Function code and show up in the logs. – Matias Quaranta Feb 17 '23 at 18:27
  • I have enabled logs and updated the info above. Additionally, I'm using "IReadOnlyList input" instead of "IReadOnlyList" , As JsonObject from System.Text.Json should accept any valid json and cosmosDb can have only valid json, I doubt if this is a deserialization error – DxG Feb 24 '23 at 00:44
  • Remember that the Trigger does not use System.Text.Json, it uses Newtonsoft.Json. If you want to use System.Text.Json, you need to customize the serialization engine:https://stackoverflow.com/a/71243311/5641598 – Matias Quaranta Feb 24 '23 at 14:46
  • unfortunately, there is no clear log explaining the exception. I added a new image now. If you see, the unhandled exception is also logged as warning. Not sure if it is actually the root cause. Regarding the System.Text.Json, I'm using isolated azure function with Microsoft.Azure.Functions.Worker.Extensions.CosmosDB --version 4.0.1, the serialization works fine. The issue in question occurs very randomly, probably once in day. My doubt is if the instance loses ownership of the lease while processing the change, does the failure occurs ? – DxG Feb 24 '23 at 19:18