@@ -54,7 +54,6 @@ internal class GrpcWorkerChannel : IRpcWorkerChannel, IDisposable
54
54
private readonly ChannelWriter < OutboundGrpcEvent > _outbound ;
55
55
private readonly ChannelReader < InboundGrpcEvent > _inbound ;
56
56
private readonly IOptions < FunctionsHostingConfigOptions > _hostingConfigOptions ;
57
-
58
57
private IDisposable _functionLoadRequestResponseEvent ;
59
58
private bool _disposed ;
60
59
private bool _disposing ;
@@ -80,6 +79,7 @@ internal class GrpcWorkerChannel : IRpcWorkerChannel, IDisposable
80
79
private TimeSpan _functionLoadTimeout = TimeSpan . FromMinutes ( 1 ) ;
81
80
private bool _isSharedMemoryDataTransferEnabled ;
82
81
private bool _cancelCapabilityEnabled ;
82
+ private bool _isWorkerApplicationInsightsLoggingEnabled ;
83
83
84
84
private System . Timers . Timer _timer ;
85
85
@@ -94,7 +94,6 @@ internal GrpcWorkerChannel(
94
94
IEnvironment environment ,
95
95
IOptionsMonitor < ScriptApplicationHostOptions > applicationHostOptions ,
96
96
ISharedMemoryManager sharedMemoryManager ,
97
- IFunctionDataCache functionDataCache ,
98
97
IOptions < WorkerConcurrencyOptions > workerConcurrencyOptions ,
99
98
IOptions < FunctionsHostingConfigOptions > hostingConfigOptions )
100
99
{
@@ -392,7 +391,9 @@ internal void WorkerInitResponse(GrpcEvent initEvent)
392
391
}
393
392
394
393
_state = _state | RpcWorkerChannelState . Initialized ;
395
- _workerCapabilities . UpdateCapabilities ( _initMessage . Capabilities ) ;
394
+
395
+ UpdateCapabilities ( _initMessage . Capabilities ) ;
396
+
396
397
_isSharedMemoryDataTransferEnabled = IsSharedMemoryDataTransferEnabled ( ) ;
397
398
_cancelCapabilityEnabled = ! string . IsNullOrEmpty ( _workerCapabilities . GetCapabilityState ( RpcWorkerConstants . HandlesInvocationCancelMessage ) ) ;
398
399
@@ -402,9 +403,22 @@ internal void WorkerInitResponse(GrpcEvent initEvent)
402
403
ScriptHost . IsFunctionDataCacheEnabled = false ;
403
404
}
404
405
406
+ if ( _environment . IsApplicationInsightsAgentEnabled ( ) ||
407
+ ( bool . TryParse ( _workerCapabilities . GetCapabilityState ( RpcWorkerConstants . WorkerApplicationInsightsLoggingEnabled ) , out bool appInsightsWorkerEnabled ) &&
408
+ appInsightsWorkerEnabled ) )
409
+ {
410
+ _isWorkerApplicationInsightsLoggingEnabled = true ;
411
+ }
412
+
405
413
_workerInitTask . TrySetResult ( true ) ;
406
414
}
407
415
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
+
408
422
public void SetupFunctionInvocationBuffers ( IEnumerable < FunctionMetadata > functions )
409
423
{
410
424
_functions = functions ;
@@ -882,7 +896,7 @@ internal async Task InvokeResponse(InvocationResponse invokeResponse)
882
896
}
883
897
884
898
/// <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).
886
900
/// </summary>
887
901
/// <param name="outputMaps">List of names of shared memory maps to close from the worker.</param>
888
902
internal void SendCloseSharedMemoryResourcesForInvocationRequest ( IList < string > outputMaps )
@@ -905,12 +919,18 @@ internal void SendCloseSharedMemoryResourcesForInvocationRequest(IList<string> o
905
919
internal void Log ( GrpcEvent msg )
906
920
{
907
921
var rpcLog = msg . Message . RpcLog ;
908
- LogLevel logLevel = ( LogLevel ) rpcLog . Level ;
909
922
if ( _executingInvocations . TryGetValue ( rpcLog . InvocationId , out ScriptInvocationContext context ) )
910
923
{
911
924
// 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 ) =>
913
926
{
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
+
914
934
if ( rpcLog . LogCategory == RpcLogCategory . CustomMetric )
915
935
{
916
936
if ( rpcLog . PropertiesMap . TryGetValue ( LogConstants . NameKey , out var metricName )
@@ -925,18 +945,27 @@ internal void Log(GrpcEvent msg)
925
945
}
926
946
else
927
947
{
928
- if ( rpcLog . Exception != null )
948
+ try
929
949
{
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
+ }
933
962
}
934
- else
963
+ finally
935
964
{
936
- context . Logger . Log ( logLevel , new EventId ( 0 , rpcLog . EventId ) , rpcLog . Message , null , ( state , exc ) => state ) ;
965
+ WorkerTraceFilterTelemetryProcessor . FilterApplicationInsightsFromWorker . Value = false ;
937
966
}
938
967
}
939
- } , null ) ;
968
+ } , ( context , rpcLog , _isWorkerApplicationInsightsLoggingEnabled ) ) ;
940
969
}
941
970
}
942
971
0 commit comments