Azure / Azure-Functions

1.11k stars 195 forks source link

HTTP triggered function with Cosmos DB output Binding in-process mode #2408

Open ikhtesam opened 1 year ago

ikhtesam commented 1 year ago

Function App name: afreen-funcapp-001 Tools used: Azure Portal New V4 app or existing V3 app migrated to V4: V4

While trying to integrate cosmos DB output binding in HTTP triggered function using Integration tab, v2.x+ binding parameters are getting generated for v4 function. Hence its throwing 500 error (Cosmos DB connection configuration 'CosmosDB' does not exist. Make sure that it is a defined App Setting) initially. After correcting ConnectionStringSetting to connection, later its throwing Value cannot be null. (Parameter 'databaseId') error for incorrect attribute collectionName and getting expected result post replacing collectionName to containerName for v4.

image

Auto generated binding with error-

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "name": "outputDocument",
      "direction": "out",
      "type": "cosmosDB",
      "connectionStringSetting": "afreen-cosmosdb_DOCUMENTDB",
      "databaseName": "outDatabase",
      "collectionName": "MyCollection",
      "createIfNotExists": true,
      "partitionKey": "/id"
    }
  ]
}

After Correction-

Below Bindings are working as expected.

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "name": "outputDocument",
      "direction": "out",
      "type": "cosmosDB",
      "connection": "afreen-cosmosdb_DOCUMENTDB",
      "databaseName": "outDatabase",
      "containerName": "MyCollection",
      "createIfNotExists": true,
      "partitionKey": "/id"
    }
  ]
}
image
mburakeker commented 1 year ago

I can confirm this issue. Thanks for providing the correct bindings.

SimonSkotheimsvik commented 11 months ago

I am getting the exact same problem, and I see the same name of the strings as you do. However, I can't change from "connectionStringSetting" to "connection", or from "collectionName" to "containerName". It seems to revert back to it's original settings again leaving me with the error message saying "Cosmos DB connection configuration 'CosmosDB' does not exist. Make sure that it is a defined App Setting." Any information available for how this can be solved?

Andy-Cuong commented 11 months ago

I am having the same issue as well. If you switch to Filesystem Logs before running the function, it generates the following logs before reverting to original settings:

2023-10-11T21:10:41.819 [Information] Executing 'Functions.HttpTrigger-Cosmos' (Reason='This function was programmatically called via the host APIs.', Id=73db5037-d596-4580-9428-53a35f8c27bd) 2023-10-11T21:10:41.822 [Information] C# HTTP trigger function processed a request. 2023-10-11T21:10:42.258 [Error] Executed 'Functions.HttpTrigger-Cosmos' (Failed, Id=73db5037-d596-4580-9428-53a35f8c27bd, Duration=839ms)Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: 06b81c49-f7b1-48fd-bff9-012044ebf84e; Reason: (Message: {"Errors":["One of the specified inputs is invalid"]}ActivityId: 06b81c49-f7b1-48fd-bff9-012044ebf84e, Request URI: /apps/57535da0-0cf9-4ab6-8701-79da54b264cd/services/7a6e7bcc-2e35-4085-913e-decb50664268/partitions/f4b2fe6e-09c9-46d9-a28b-888d80f0972f/replicas/133415253021034469p/, RequestStats:RequestStartTime: 2023-10-11T21:10:42.1546204Z, RequestEndTime: 2023-10-11T21:10:42.1641123Z, Number of regions attempted:1{"systemHistory":[{"dateUtc":"2023-10-11T21:09:47.6749900Z","cpu":0.919,"memory":426825120.000,"threadInfo":{"isThreadStarving":"False","threadWaitIntervalInMs":0.0485,"availableThreads":32764,"minThreads":64,"maxThreads":32767},"numberOfOpenTcpConnection":483},{"dateUtc":"2023-10-11T21:09:57.6845997Z","cpu":0.229,"memory":426835964.000,"threadInfo":{"isThreadStarving":"False","threadWaitIntervalInMs":0.0578,"availableThreads":32764,"minThreads":64,"maxThreads":32767},"numberOfOpenTcpConnection":483},{"dateUtc":"2023-10-11T21:10:07.6949372Z","cpu":0.548,"memory":426809804.000,"threadInfo":{"isThreadStarving":"False","threadWaitIntervalInMs":0.037,"availableThreads":32763,"minThreads":64,"maxThreads":32767},"numberOfOpenTcpConnection":483},{"dateUtc":"2023-10-11T21:10:17.7050665Z","cpu":1.023,"memory":426813416.000,"threadInfo":{"isThreadStarving":"False","threadWaitIntervalInMs":0.0418,"availableThreads":32764,"minThreads":64,"maxThreads":32767},"numberOfOpenTcpConnection":484},{"dateUtc":"2023-10-11T21:10:27.7147882Z","cpu":0.463,"memory":426836676.000,"threadInfo":{"isThreadStarving":"False","threadWaitIntervalInMs":0.037,"availableThreads":32764,"minThreads":64,"maxThreads":32767},"numberOfOpenTcpConnection":484},{"dateUtc":"2023-10-11T21:10:37.7243787Z","cpu":0.158,"memory":426836076.000,"threadInfo":{"isThreadStarving":"False","threadWaitIntervalInMs":0.0587,"availableThreads":32764,"minThreads":64,"maxThreads":32767},"numberOfOpenTcpConnection":481}]}RequestStart: 2023-10-11T21:10:42.1547722Z; ResponseTime: 2023-10-11T21:10:42.1641123Z; StoreResult: StorePhysicalAddress: rntbd://cdb-ms-prod-northeurope1-be39.documents.azure.com:14318/apps/57535da0-0cf9-4ab6-8701-79da54b264cd/services/7a6e7bcc-2e35-4085-913e-decb50664268/partitions/f4b2fe6e-09c9-46d9-a28b-888d80f0972f/replicas/133415253021034469p/, LSN: 14, GlobalCommittedLsn: 14, PartitionKeyRangeId: 0, IsValid: True, StatusCode: 400, SubStatusCode: 0, RequestCharge: 0, ItemLSN: -1, SessionToken: 2#14#14=-1, UsingLocalLSN: False, TransportException: null, BELatencyMs: 0.344, ActivityId: 06b81c49-f7b1-48fd-bff9-012044ebf84e, RetryAfterInMs: , ReplicaHealthStatuses: [(port: 14318 | status: Connected | lkt: 10/11/2023 8:12:46 PM)], TransportRequestTimeline: {"requestTimeline":[{"event": "Created", "startTimeUtc": "2023-10-11T21:10:42.1547731Z", "durationInMs": 0.011},{"event": "ChannelAcquisitionStarted", "startTimeUtc": "2023-10-11T21:10:42.1547841Z", "durationInMs": 7.8585},{"event": "Pipelined", "startTimeUtc": "2023-10-11T21:10:42.1626426Z", "durationInMs": 0.0565},{"event": "Transit Time", "startTimeUtc": "2023-10-11T21:10:42.1626991Z", "durationInMs": 0.7964},{"event": "Received", "startTimeUtc": "2023-10-11T21:10:42.1634955Z", "durationInMs": 0.081},{"event": "Completed", "startTimeUtc": "2023-10-11T21:10:42.1635765Z", "durationInMs": 0}],"serviceEndpointStats":{"inflightRequests":1,"openConnections":1},"connectionStats":{"waitforConnectionInit":"True","callsPendingReceive":0,"lastSendAttempt":"2023-10-11T21:10:42.1621072Z","lastSend":"2023-10-11T21:10:42.1621256Z","lastReceive":"2023-10-11T21:10:42.1623393Z"},"requestSizeInBytes":610,"requestBodySizeInBytes":20,"responseMetadataSizeInBytes":223,"responseBodySizeInBytes":53};ResourceType: Document, OperationType: Upsert, SDK: Microsoft.Azure.Documents.Common/2.14.0, Microsoft.Azure.Cosmos.Tracing.TraceData.ClientSideRequestStatisticsTraceDatum, Windows/10.0.14393 cosmos-netstandard-sdk/3.30.8);

After this the binding is reverted as @SimonSkotheimsvik commented

jme107 commented 10 months ago

Is there any info when the problem with CosmosDB Output Binding will be solved? The problem is very annoying because i have to manually fix it every time. I wonder how this got through testing ....

@SimonSkotheimsvik I also noticed the problem, if you modify values in ContainerName etc. in the function.json and then immediately do a test/run, azure function overwrites the values again with the old incorrect values. What works for me is to change the function.json / the output binding Cosmos DB and then stop the whole function app (restart is not enough) and then start it again and then the customized values are saved correctly. But I also had the case that I had to create a new function because nothing worked anymore....

The definition of insanity or Azure Function is doing the same thing over and over again and expecting different results. :)

AmiKlein-Twig commented 10 months ago

Any update on the resolution of this issue?

marcosgm commented 9 months ago

It's not just the connection, it's the rest of attributes that must be renamed. https://learn.microsoft.com/en-us/azure/azure-functions/migrate-cosmos-db-version-3-version-4?tabs=in-process&pivots=programming-language-csharp#rename-the-binding-attributes.

So the Azure Portal created this functions.json (v3 attributes)

        {
            "name": "outputDocument",
            "direction": "out",
            "type": "cosmosDB",
            "connectionStringSetting": "wth-serverless_DOCUMENTDB",
            "databaseName": "LicensePlates",
            "collectionName": "Processed"
        }

which has to be translated to v4 (connection and containerName)

        {
            "name": "outputDocument",
            "direction": "out",
            "type": "cosmosDB",
            "connection": "wth-serverless_DOCUMENTDB",
            "databaseName": "LicensePlates",
            "containerName": "Processed"
        }

Basically, the portal still creates functions as the v3 extension where the code runs the v4 now