Azure 函数异常 - 将日志写入 table 存储时出错:Microsoft.Azure.Cosmos.Table.StorageException
Azure function exception - Error writing logs to table storage: Microsoft.Azure.Cosmos.Table.StorageException
我有一个 azure 函数,它与 blob 存储通信以读取上次同步日期时间,然后基于此从 Cosmos 数据库读取数据。
很少有交互工作正常,在某些情况下会随机抛出以下异常
"Error writing logs to table storage:
Microsoft.Azure.Cosmos.Table.StorageException: An operation on a
socket could not be performed because the system lacked sufficient
buffer space or because a queue was full. --->
System.Net.Http.HttpRequestException: An operation on a socket could
not be performed because the system lacked sufficient buffer space or
because a queue was full. ---> System.Net.Sockets.SocketException
(10055): An operation on a socket could not be performed because the
system lacked sufficient buffer space or because a queue was full. at
System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port,
CancellationToken cancellationToken) --- End of inner exception stack
trace --- at System.Net.Http.ConnectHelper.ConnectAsync(String host,
Int32 port, CancellationToken cancellationToken) at
System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage
request, Boolean allowHttp2, CancellationToken cancellationToken) at
System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage
request, CancellationToken cancellationToken) at
System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage
request, CancellationToken cancellationToken) at
System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage
request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage
request, CancellationToken cancellationToken) at
System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage
request, CancellationToken cancellationToken) at
System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at Microsoft.Azure.Cosmos.Table.RestExecutor.TableCommand.Executor.ExecuteAsync[T](RESTCommand
1
cmd, IRetryPolicy policy, OperationContext operationContext,
CancellationToken token) --- End of inner exception stack trace --- at
Microsoft.Azure.Cosmos.Table.RestExecutor.TableCommand.Executor.ExecuteAsync[T](RESTCommand1 cmd, IRetryPolicy policy, OperationContext operationContext, CancellationToken token) at Microsoft.Azure.WebJobs.Logging.Utility.SafeExecuteAsync(CloudTable table, TableBatchOperation batch) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Logging\Internal\Utility.cs:line 178 at Microsoft.Azure.WebJobs.Logging.Utility.WriteBatchAsync[T](ILogTableProvider logTableProvider, IEnumerable
1 e1) in
C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Logging\Internal\Utility.cs:line
268 at
Microsoft.Azure.WebJobs.Logging.LogWriter.FlushTimelineAggregateAsync(Boolean
always) in
C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Logging\Internal\LogWriter.cs:line
265 at Microsoft.Azure.WebJobs.Logging.LogWriter.FlushCoreAsync() in
C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Logging\Internal\LogWriter.cs:line
316 Request Information RequestID: RequestDate: StatusMessage:
ErrorCode:"
任何人都可以告诉我如何修复它吗?
由于您没有分享您的函数中的任何相关代码,因此我的回答有点笼统。这似乎是由于套接字耗尽问题。虽然有一些不确定的 similar issue in github 有更多细节,但有一些很好的建议可以消除任何悬而未决的成果(我强烈建议您阅读):
我特别建议您确保 Cosmos(或 Table)客户端、HttpClient 等在方法外部的静态私有实例中重用共享连接。
Static clients
To avoid holding more connections than necessary, reuse client
instances rather than creating new ones with each function invocation.
We recommend reusing client connections for any language that you
might write your function in. For example, .NET clients like the
HttpClient,
DocumentClient,
and Azure Storage clients can manage connections if you use a single,
static client.
Here are some guidelines to follow when you're using a
service-specific client in an Azure Functions application:
- Do not create a new client with every function invocation.
- Do create a single, static client that every function invocation can use.
- Consider creating a single, static client in a shared helper class if different functions use the same service.
示例:
using Microsoft.Azure.Documents.Client;
private static Lazy<DocumentClient> lazyClient = new Lazy<DocumentClient>(InitializeDocumentClient);
private static DocumentClient documentClient => lazyClient.Value;
private static DocumentClient InitializeDocumentClient()
{
// Perform any initialization here
var uri = new Uri("example");
var authKey = "authKey";
return new DocumentClient(uri, authKey);
}
// Function
public static async Task Run(string input)
{
Uri collectionUri = UriFactory.CreateDocumentCollectionUri("database", "collection");
object document = new { Data = "example" };
await documentClient.UpsertDocumentAsync(collectionUri, document);
// Rest of function
}
我刚从 v2 升级到 v3 的函数应用程序遇到了同样的问题。
正如您所注意到的,异常不是来自您的代码,而是来自 Microsoft.Azure.WebJobs.Logging
,无论您在做什么,它都在继续它自己的事情。它也只发生在本地,而不是在我部署的实例中。
基于我的 local.settings.json
文件中的 the answer here I deleted the AzureWebJobsDashboard 设置,错误已消失。
Internally Functions uses Table Storage for logging if the AzureWebJobsDashboard
app setting is set. It's recommended to use Application Insights for logging in production scenarios, so you can safely remove this and set the APPINSIGHTS_INSTRUMENTATIONKEY
app setting instead.
看来我不需要为本地开发存储日志,而且我部署的函数都只有 APPINSIGHTS_INSTRUMENTATIONKEY
集。
我有一个 azure 函数,它与 blob 存储通信以读取上次同步日期时间,然后基于此从 Cosmos 数据库读取数据。
很少有交互工作正常,在某些情况下会随机抛出以下异常
"Error writing logs to table storage: Microsoft.Azure.Cosmos.Table.StorageException: An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full. ---> System.Net.Http.HttpRequestException: An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full. ---> System.Net.Sockets.SocketException (10055): An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full. at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken) --- End of inner exception stack trace --- at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task
1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at Microsoft.Azure.Cosmos.Table.RestExecutor.TableCommand.Executor.ExecuteAsync[T](RESTCommand
1 cmd, IRetryPolicy policy, OperationContext operationContext, CancellationToken token) --- End of inner exception stack trace --- at Microsoft.Azure.Cosmos.Table.RestExecutor.TableCommand.Executor.ExecuteAsync[T](RESTCommand1 cmd, IRetryPolicy policy, OperationContext operationContext, CancellationToken token) at Microsoft.Azure.WebJobs.Logging.Utility.SafeExecuteAsync(CloudTable table, TableBatchOperation batch) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Logging\Internal\Utility.cs:line 178 at Microsoft.Azure.WebJobs.Logging.Utility.WriteBatchAsync[T](ILogTableProvider logTableProvider, IEnumerable
1 e1) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Logging\Internal\Utility.cs:line 268 at Microsoft.Azure.WebJobs.Logging.LogWriter.FlushTimelineAggregateAsync(Boolean always) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Logging\Internal\LogWriter.cs:line 265 at Microsoft.Azure.WebJobs.Logging.LogWriter.FlushCoreAsync() in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Logging\Internal\LogWriter.cs:line 316 Request Information RequestID: RequestDate: StatusMessage: ErrorCode:"
任何人都可以告诉我如何修复它吗?
由于您没有分享您的函数中的任何相关代码,因此我的回答有点笼统。这似乎是由于套接字耗尽问题。虽然有一些不确定的 similar issue in github 有更多细节,但有一些很好的建议可以消除任何悬而未决的成果(我强烈建议您阅读):
我特别建议您确保 Cosmos(或 Table)客户端、HttpClient 等在方法外部的静态私有实例中重用共享连接。
Static clients
To avoid holding more connections than necessary, reuse client instances rather than creating new ones with each function invocation. We recommend reusing client connections for any language that you might write your function in. For example, .NET clients like the HttpClient, DocumentClient, and Azure Storage clients can manage connections if you use a single, static client.
Here are some guidelines to follow when you're using a service-specific client in an Azure Functions application:
- Do not create a new client with every function invocation.
- Do create a single, static client that every function invocation can use.
- Consider creating a single, static client in a shared helper class if different functions use the same service.
示例:
using Microsoft.Azure.Documents.Client;
private static Lazy<DocumentClient> lazyClient = new Lazy<DocumentClient>(InitializeDocumentClient);
private static DocumentClient documentClient => lazyClient.Value;
private static DocumentClient InitializeDocumentClient()
{
// Perform any initialization here
var uri = new Uri("example");
var authKey = "authKey";
return new DocumentClient(uri, authKey);
}
// Function
public static async Task Run(string input)
{
Uri collectionUri = UriFactory.CreateDocumentCollectionUri("database", "collection");
object document = new { Data = "example" };
await documentClient.UpsertDocumentAsync(collectionUri, document);
// Rest of function
}
我刚从 v2 升级到 v3 的函数应用程序遇到了同样的问题。
正如您所注意到的,异常不是来自您的代码,而是来自 Microsoft.Azure.WebJobs.Logging
,无论您在做什么,它都在继续它自己的事情。它也只发生在本地,而不是在我部署的实例中。
基于我的 local.settings.json
文件中的 the answer here I deleted the AzureWebJobsDashboard 设置,错误已消失。
Internally Functions uses Table Storage for logging if the
AzureWebJobsDashboard
app setting is set. It's recommended to use Application Insights for logging in production scenarios, so you can safely remove this and set theAPPINSIGHTS_INSTRUMENTATIONKEY
app setting instead.
看来我不需要为本地开发存储日志,而且我部署的函数都只有 APPINSIGHTS_INSTRUMENTATIONKEY
集。