@@ -54,7 +54,6 @@ internal class GrpcWorkerChannel : IRpcWorkerChannel, IDisposable
5454 private readonly ChannelWriter < OutboundGrpcEvent > _outbound ;
5555 private readonly ChannelReader < InboundGrpcEvent > _inbound ;
5656 private readonly IOptions < FunctionsHostingConfigOptions > _hostingConfigOptions ;
57-
5857 private IDisposable _functionLoadRequestResponseEvent ;
5958 private bool _disposed ;
6059 private bool _disposing ;
@@ -80,6 +79,7 @@ internal class GrpcWorkerChannel : IRpcWorkerChannel, IDisposable
8079 private TimeSpan _functionLoadTimeout = TimeSpan . FromMinutes ( 1 ) ;
8180 private bool _isSharedMemoryDataTransferEnabled ;
8281 private bool _cancelCapabilityEnabled ;
82+ private bool _isWorkerApplicationInsightsLoggingEnabled ;
8383
8484 private System . Timers . Timer _timer ;
8585
@@ -94,7 +94,6 @@ internal GrpcWorkerChannel(
9494 IEnvironment environment ,
9595 IOptionsMonitor < ScriptApplicationHostOptions > applicationHostOptions ,
9696 ISharedMemoryManager sharedMemoryManager ,
97- IFunctionDataCache functionDataCache ,
9897 IOptions < WorkerConcurrencyOptions > workerConcurrencyOptions ,
9998 IOptions < FunctionsHostingConfigOptions > hostingConfigOptions )
10099 {
@@ -392,7 +391,9 @@ internal void WorkerInitResponse(GrpcEvent initEvent)
392391 }
393392
394393 _state = _state | RpcWorkerChannelState . Initialized ;
395- _workerCapabilities . UpdateCapabilities ( _initMessage . Capabilities ) ;
394+
395+ UpdateCapabilities ( _initMessage . Capabilities ) ;
396+
396397 _isSharedMemoryDataTransferEnabled = IsSharedMemoryDataTransferEnabled ( ) ;
397398 _cancelCapabilityEnabled = ! string . IsNullOrEmpty ( _workerCapabilities . GetCapabilityState ( RpcWorkerConstants . HandlesInvocationCancelMessage ) ) ;
398399
@@ -402,9 +403,22 @@ internal void WorkerInitResponse(GrpcEvent initEvent)
402403 ScriptHost . IsFunctionDataCacheEnabled = false ;
403404 }
404405
406+ if ( _environment . IsApplicationInsightsAgentEnabled ( ) ||
407+ ( bool . TryParse ( _workerCapabilities . GetCapabilityState ( RpcWorkerConstants . WorkerApplicationInsightsLoggingEnabled ) , out bool appInsightsWorkerEnabled ) &&
408+ appInsightsWorkerEnabled ) )
409+ {
410+ _isWorkerApplicationInsightsLoggingEnabled = true ;
411+ }
412+
405413 _workerInitTask . TrySetResult ( true ) ;
406414 }
407415
416+ // Allow tests to add capabilities, even if not directly supported by the worker.
417+ internal virtual void UpdateCapabilities ( IDictionary < string , string > fields )
418+ {
419+ _workerCapabilities . UpdateCapabilities ( fields ) ;
420+ }
421+
408422 public void SetupFunctionInvocationBuffers ( IEnumerable < FunctionMetadata > functions )
409423 {
410424 _functions = functions ;
@@ -882,7 +896,7 @@ internal async Task InvokeResponse(InvocationResponse invokeResponse)
882896 }
883897
884898 /// <summary>
885- /// Request to free memory allocated by the worker (for output bindings)
899+ /// Request to free memory allocated by the worker (for output bindings).
886900 /// </summary>
887901 /// <param name="outputMaps">List of names of shared memory maps to close from the worker.</param>
888902 internal void SendCloseSharedMemoryResourcesForInvocationRequest ( IList < string > outputMaps )
@@ -905,12 +919,18 @@ internal void SendCloseSharedMemoryResourcesForInvocationRequest(IList<string> o
905919 internal void Log ( GrpcEvent msg )
906920 {
907921 var rpcLog = msg . Message . RpcLog ;
908- LogLevel logLevel = ( LogLevel ) rpcLog . Level ;
909922 if ( _executingInvocations . TryGetValue ( rpcLog . InvocationId , out ScriptInvocationContext context ) )
910923 {
911924 // Restore the execution context from the original invocation. This allows AsyncLocal state to flow to loggers.
912- System . Threading . ExecutionContext . Run ( context . AsyncExecutionContext , ( s ) =>
925+ System . Threading . ExecutionContext . Run ( context . AsyncExecutionContext , static ( state ) =>
913926 {
927+ var stateTuple = ( ( ScriptInvocationContext Context , RpcLog RpcLog , bool AppInsightsEnabledOnWorker ) ) state ;
928+
929+ var rpcLog = stateTuple . RpcLog ;
930+ LogLevel logLevel = ( LogLevel ) rpcLog . Level ;
931+
932+ var context = stateTuple . Context ;
933+
914934 if ( rpcLog . LogCategory == RpcLogCategory . CustomMetric )
915935 {
916936 if ( rpcLog . PropertiesMap . TryGetValue ( LogConstants . NameKey , out var metricName )
@@ -925,18 +945,27 @@ internal void Log(GrpcEvent msg)
925945 }
926946 else
927947 {
928- if ( rpcLog . Exception != null )
948+ try
929949 {
930- // TODO fix RpcException catch all https://github.com/Azure/azure-functions-dotnet-worker/issues/370
931- var exception = new Workers . Rpc . RpcException ( rpcLog . Message , rpcLog . Exception . Message , rpcLog . Exception . StackTrace ) ;
932- context . Logger . Log ( logLevel , new EventId ( 0 , rpcLog . EventId ) , rpcLog . Message , exception , ( state , exc ) => state ) ;
950+ WorkerTraceFilterTelemetryProcessor . FilterApplicationInsightsFromWorker . Value = stateTuple . AppInsightsEnabledOnWorker ;
951+
952+ if ( rpcLog . Exception != null )
953+ {
954+ // TODO fix RpcException catch all https://github.com/Azure/azure-functions-dotnet-worker/issues/370
955+ var exception = new Workers . Rpc . RpcException ( rpcLog . Message , rpcLog . Exception . Message , rpcLog . Exception . StackTrace ) ;
956+ context . Logger . Log ( logLevel , new EventId ( 0 , rpcLog . EventId ) , rpcLog . Message , exception , ( state , exc ) => state ) ;
957+ }
958+ else
959+ {
960+ context . Logger . Log ( logLevel , new EventId ( 0 , rpcLog . EventId ) , rpcLog . Message , null , ( state , exc ) => state ) ;
961+ }
933962 }
934- else
963+ finally
935964 {
936- context . Logger . Log ( logLevel , new EventId ( 0 , rpcLog . EventId ) , rpcLog . Message , null , ( state , exc ) => state ) ;
965+ WorkerTraceFilterTelemetryProcessor . FilterApplicationInsightsFromWorker . Value = false ;
937966 }
938967 }
939- } , null ) ;
968+ } , ( context , rpcLog , _isWorkerApplicationInsightsLoggingEnabled ) ) ;
940969 }
941970 }
942971
0 commit comments