3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
+ using System . Diagnostics ;
6
7
using System . IO ;
7
8
using System . Linq ;
8
9
using System . Runtime . InteropServices ;
9
10
using System . Threading ;
10
11
using System . Threading . Tasks ;
12
+ using Microsoft . Azure . WebJobs . Logging ;
11
13
using Microsoft . Azure . WebJobs . Script . Description ;
12
14
using Microsoft . Azure . WebJobs . Script . Diagnostics ;
13
15
using Microsoft . Azure . WebJobs . Script . Eventing ;
@@ -36,6 +38,7 @@ public class GrpcWorkerChannelTests : IDisposable
36
38
private readonly string _workerId = "testWorkerId" ;
37
39
private readonly string _scriptRootPath = "c:\t estdir" ;
38
40
private readonly IScriptEventManager _eventManager = new ScriptEventManager ( ) ;
41
+ private readonly Mock < IScriptEventManager > _eventManagerMock = new Mock < IScriptEventManager > ( ) ;
39
42
private readonly TestMetricsLogger _metricsLogger = new TestMetricsLogger ( ) ;
40
43
private readonly Mock < IWorkerConsoleLogSource > _mockConsoleLogger = new Mock < IWorkerConsoleLogSource > ( ) ;
41
44
private readonly Mock < FunctionRpc . FunctionRpcBase > _mockFunctionRpcService = new Mock < FunctionRpc . FunctionRpcBase > ( ) ;
@@ -52,6 +55,7 @@ public class GrpcWorkerChannelTests : IDisposable
52
55
private readonly IFunctionDataCache _functionDataCache ;
53
56
private readonly IOptions < WorkerConcurrencyOptions > _workerConcurrencyOptions ;
54
57
private GrpcWorkerChannel _workerChannel ;
58
+ private GrpcWorkerChannel _workerChannelwithMockEventManager ;
55
59
56
60
public GrpcWorkerChannelTests ( )
57
61
{
@@ -104,6 +108,23 @@ public GrpcWorkerChannelTests()
104
108
_sharedMemoryManager ,
105
109
_functionDataCache ,
106
110
_workerConcurrencyOptions ) ;
111
+
112
+ _eventManagerMock . Setup ( proxy => proxy . Publish ( It . IsAny < OutboundGrpcEvent > ( ) ) ) . Verifiable ( ) ;
113
+ _workerChannelwithMockEventManager = new GrpcWorkerChannel (
114
+ _workerId ,
115
+ _eventManagerMock . Object ,
116
+ _testWorkerConfig ,
117
+ _mockrpcWorkerProcess . Object ,
118
+ _logger ,
119
+ _metricsLogger ,
120
+ 0 ,
121
+ _testEnvironment ,
122
+ _hostOptionsMonitor ,
123
+ _sharedMemoryManager ,
124
+ _functionDataCache ,
125
+ _workerConcurrencyOptions ) ;
126
+
127
+ _testEnvironment . SetEnvironmentVariable ( "APPLICATIONINSIGHTS_ENABLE_AGENT" , "true" ) ;
107
128
}
108
129
109
130
public void Dispose ( )
@@ -730,6 +751,57 @@ public async Task GetLatencies_DoesNot_StartTimer_WhenDynamicConcurrencyDisabled
730
751
Assert . True ( latencyHistory . Count ( ) == 0 ) ;
731
752
}
732
753
754
+ [ Fact ]
755
+ public async Task SendInvocationRequest_ValidateTraceContext ( )
756
+ {
757
+ ScriptInvocationContext scriptInvocationContext = GetTestScriptInvocationContext ( Guid . NewGuid ( ) , null ) ;
758
+ await _workerChannelwithMockEventManager . SendInvocationRequest ( scriptInvocationContext ) ;
759
+ if ( _testEnvironment . IsApplicationInsightsAgentEnabled ( ) )
760
+ {
761
+ _eventManagerMock . Verify ( proxy => proxy . Publish ( It . Is < OutboundGrpcEvent > (
762
+ grpcEvent => grpcEvent . Message . InvocationRequest . TraceContext . Attributes . ContainsKey ( ScriptConstants . LogPropertyProcessIdKey )
763
+ && grpcEvent . Message . InvocationRequest . TraceContext . Attributes . ContainsKey ( ScriptConstants . LogPropertyHostInstanceIdKey )
764
+ && grpcEvent . Message . InvocationRequest . TraceContext . Attributes . ContainsKey ( LogConstants . CategoryNameKey )
765
+ && grpcEvent . Message . InvocationRequest . TraceContext . Attributes [ LogConstants . CategoryNameKey ] . Equals ( "testcat1" )
766
+ && grpcEvent . Message . InvocationRequest . TraceContext . Attributes . Count == 3 ) ) ) ;
767
+ }
768
+ else
769
+ {
770
+ _eventManagerMock . Verify ( proxy => proxy . Publish ( It . Is < OutboundGrpcEvent > (
771
+ grpcEvent => ! grpcEvent . Message . InvocationRequest . TraceContext . Attributes . ContainsKey ( ScriptConstants . LogPropertyProcessIdKey )
772
+ && ! grpcEvent . Message . InvocationRequest . TraceContext . Attributes . ContainsKey ( ScriptConstants . LogPropertyHostInstanceIdKey )
773
+ && ! grpcEvent . Message . InvocationRequest . TraceContext . Attributes . ContainsKey ( LogConstants . CategoryNameKey ) ) ) ) ;
774
+ }
775
+ }
776
+
777
+ [ Fact ]
778
+ public async Task SendInvocationRequest_ValidateTraceContext_SessionId ( )
779
+ {
780
+ string sessionId = "sessionId1234" ;
781
+ Activity activity = new Activity ( "testActivity" ) ;
782
+ activity . AddBaggage ( ScriptConstants . LiveLogsSessionAIKey , sessionId ) ;
783
+ activity . Start ( ) ;
784
+ ScriptInvocationContext scriptInvocationContext = GetTestScriptInvocationContext ( Guid . NewGuid ( ) , null ) ;
785
+ await _workerChannelwithMockEventManager . SendInvocationRequest ( scriptInvocationContext ) ;
786
+ activity . Stop ( ) ;
787
+ _eventManagerMock . Verify ( p => p . Publish ( It . Is < OutboundGrpcEvent > ( grpcEvent => ValidateInvocationRequest ( grpcEvent , sessionId ) ) ) ) ;
788
+ }
789
+
790
+ private bool ValidateInvocationRequest ( OutboundGrpcEvent grpcEvent , string sessionId )
791
+ {
792
+ if ( _testEnvironment . IsApplicationInsightsAgentEnabled ( ) )
793
+ {
794
+ return grpcEvent . Message . InvocationRequest . TraceContext . Attributes [ ScriptConstants . LiveLogsSessionAIKey ] . Equals ( sessionId )
795
+ && grpcEvent . Message . InvocationRequest . TraceContext . Attributes . ContainsKey ( LogConstants . CategoryNameKey )
796
+ && grpcEvent . Message . InvocationRequest . TraceContext . Attributes [ LogConstants . CategoryNameKey ] . Equals ( "testcat1" )
797
+ && grpcEvent . Message . InvocationRequest . TraceContext . Attributes . Count == 4 ;
798
+ }
799
+ else
800
+ {
801
+ return ! grpcEvent . Message . InvocationRequest . TraceContext . Attributes . ContainsKey ( LogConstants . CategoryNameKey ) ;
802
+ }
803
+ }
804
+
733
805
private IEnumerable < FunctionMetadata > GetTestFunctionsList ( string runtime )
734
806
{
735
807
var metadata1 = new FunctionMetadata ( )
@@ -739,15 +811,17 @@ private IEnumerable<FunctionMetadata> GetTestFunctionsList(string runtime)
739
811
} ;
740
812
741
813
metadata1 . SetFunctionId ( "TestFunctionId1" ) ;
742
-
814
+ metadata1 . Properties . Add ( LogConstants . CategoryNameKey , "testcat1" ) ;
815
+ metadata1 . Properties . Add ( ScriptConstants . LogPropertyHostInstanceIdKey , "testhostId1" ) ;
743
816
var metadata2 = new FunctionMetadata ( )
744
817
{
745
818
Language = runtime ,
746
819
Name = "js2" ,
747
820
} ;
748
821
749
822
metadata2 . SetFunctionId ( "TestFunctionId2" ) ;
750
-
823
+ metadata2 . Properties . Add ( LogConstants . CategoryNameKey , "testcat2" ) ;
824
+ metadata2 . Properties . Add ( ScriptConstants . LogPropertyHostInstanceIdKey , "testhostId2" ) ;
751
825
return new List < FunctionMetadata > ( )
752
826
{
753
827
metadata1 ,
0 commit comments