@@ -27,15 +27,8 @@ public void TracerProviderSdkAddSource()
27
27
. AddSource ( source1 . Name )
28
28
. Build ( ) ;
29
29
30
- using ( var activity = source1 . StartActivity ( "test" ) )
31
- {
32
- Assert . NotNull ( activity ) ;
33
- }
34
-
35
- using ( var activity = source2 . StartActivity ( "test" ) )
36
- {
37
- Assert . Null ( activity ) ;
38
- }
30
+ Assert . True ( IsSourceEnabled ( source1 ) ) ;
31
+ Assert . False ( IsSourceEnabled ( source2 ) ) ;
39
32
}
40
33
41
34
[ Fact ]
@@ -50,50 +43,97 @@ public void TracerProviderSdkAddSourceWithWildcards()
50
43
. AddSource ( $ "{ Utils . GetCurrentMethodName ( ) } .*")
51
44
. Build ( ) )
52
45
{
53
- using ( var activity = source1 . StartActivity ( "test" ) )
54
- {
55
- Assert . NotNull ( activity ) ;
56
- }
46
+ Assert . True ( IsSourceEnabled ( source1 ) ) ;
47
+ Assert . True ( IsSourceEnabled ( source2 ) ) ;
48
+ Assert . True ( IsSourceEnabled ( source3 ) ) ;
49
+ Assert . True ( IsSourceEnabled ( source4 ) ) ;
50
+ }
57
51
58
- using ( var activity = source2 . StartActivity ( "test" ) )
59
- {
60
- Assert . NotNull ( activity ) ;
61
- }
52
+ using ( var tracerProvider = Sdk . CreateTracerProviderBuilder ( )
53
+ . AddSource ( $ "{ Utils . GetCurrentMethodName ( ) } .?")
54
+ . Build ( ) )
55
+ {
56
+ Assert . True ( IsSourceEnabled ( source1 ) ) ;
57
+ Assert . False ( IsSourceEnabled ( source2 ) ) ;
58
+ Assert . False ( IsSourceEnabled ( source3 ) ) ;
59
+ Assert . True ( IsSourceEnabled ( source4 ) ) ;
60
+ }
61
+ }
62
62
63
- using ( var activity = source3 . StartActivity ( "test" ) )
64
- {
65
- Assert . NotNull ( activity ) ;
66
- }
63
+ [ Fact ]
64
+ public void TracerProviderSdkAddSourceWithPredicate ( )
65
+ {
66
+ using var source1 = new ActivitySource ( $ "{ Utils . GetCurrentMethodName ( ) } .A", "1.0.0" ) ;
67
+ using var source2 = new ActivitySource ( $ "{ Utils . GetCurrentMethodName ( ) } .A", "2.0.0" ) ;
68
+ using var source3 = new ActivitySource ( $ "{ Utils . GetCurrentMethodName ( ) } .B") ;
69
+ using var source4 = new ActivitySource ( $ "B.{ Utils . GetCurrentMethodName ( ) } ") ;
67
70
68
- using ( var activity = source4 . StartActivity ( "test" ) )
69
- {
70
- Assert . NotNull ( activity ) ;
71
- }
71
+ using ( var tracerProvider = Sdk . CreateTracerProviderBuilder ( )
72
+ . AddSource ( source => source . Version == "2.0.0" || source . Name . EndsWith ( ".B" ) )
73
+ . Build ( ) )
74
+ {
75
+ Assert . False ( IsSourceEnabled ( source1 ) ) ;
76
+ Assert . True ( IsSourceEnabled ( source2 ) ) ;
77
+ Assert . True ( IsSourceEnabled ( source3 ) ) ;
78
+ Assert . False ( IsSourceEnabled ( source4 ) ) ;
72
79
}
80
+ }
81
+
82
+ [ Fact ]
83
+ public void TracerProviderSdkAddSourceWithMultiplePredicates ( )
84
+ {
85
+ using var source1 = new ActivitySource ( $ "{ Utils . GetCurrentMethodName ( ) } .A", "1.0.0" ) ;
86
+ using var source2 = new ActivitySource ( $ "{ Utils . GetCurrentMethodName ( ) } .A", "2.0.0" ) ;
87
+ using var source3 = new ActivitySource ( $ "{ Utils . GetCurrentMethodName ( ) } .B") ;
88
+ using var source4 = new ActivitySource ( $ "B.{ Utils . GetCurrentMethodName ( ) } ") ;
73
89
74
90
using ( var tracerProvider = Sdk . CreateTracerProviderBuilder ( )
75
- . AddSource ( $ "{ Utils . GetCurrentMethodName ( ) } .?")
91
+ . AddSource ( source => source . Version == "2.0.0" )
92
+ . AddSource ( source => source . Name . StartsWith ( "B." ) )
76
93
. Build ( ) )
77
94
{
78
- using ( var activity = source1 . StartActivity ( "test" ) )
79
- {
80
- Assert . NotNull ( activity ) ;
81
- }
95
+ Assert . False ( IsSourceEnabled ( source1 ) ) ;
96
+ Assert . True ( IsSourceEnabled ( source2 ) ) ;
97
+ Assert . False ( IsSourceEnabled ( source3 ) ) ;
98
+ Assert . True ( IsSourceEnabled ( source4 ) ) ;
99
+ }
100
+ }
82
101
83
- using ( var activity = source2 . StartActivity ( "test" ) )
84
- {
85
- Assert . Null ( activity ) ;
86
- }
102
+ [ Fact ]
103
+ public void TracerProviderSdkAddSourceWithWildCardAndPredicate ( )
104
+ {
105
+ using var source1 = new ActivitySource ( $ "{ Utils . GetCurrentMethodName ( ) } .A", "1.0.0" ) ;
106
+ using var source2 = new ActivitySource ( $ "{ Utils . GetCurrentMethodName ( ) } .A", "2.0.0" ) ;
107
+ using var source3 = new ActivitySource ( $ "{ Utils . GetCurrentMethodName ( ) } .B") ;
108
+ using var source4 = new ActivitySource ( $ "B.{ Utils . GetCurrentMethodName ( ) } ") ;
87
109
88
- using ( var activity = source3 . StartActivity ( "test" ) )
89
- {
90
- Assert . Null ( activity ) ;
91
- }
110
+ using ( var tracerProvider = Sdk . CreateTracerProviderBuilder ( )
111
+ . AddSource ( "B.*" )
112
+ . AddSource ( source => source . Version == "2.0.0" )
113
+ . Build ( ) )
114
+ {
115
+ Assert . False ( IsSourceEnabled ( source1 ) ) ;
116
+ Assert . True ( IsSourceEnabled ( source2 ) ) ;
117
+ Assert . False ( IsSourceEnabled ( source3 ) ) ;
118
+ Assert . True ( IsSourceEnabled ( source4 ) ) ;
119
+ }
120
+ }
92
121
93
- using ( var activity = source4 . StartActivity ( "test" ) )
94
- {
95
- Assert . NotNull ( activity ) ;
96
- }
122
+ [ Fact ]
123
+ public void TracerProviderSdkAddSourceWithConflictingWildCardAndPredicate ( )
124
+ {
125
+ using var source1 = new ActivitySource ( $ "{ Utils . GetCurrentMethodName ( ) } .A", "1.0.0" ) ;
126
+ using var source2 = new ActivitySource ( $ "{ Utils . GetCurrentMethodName ( ) } .B") ;
127
+ using var source3 = new ActivitySource ( $ "B.{ Utils . GetCurrentMethodName ( ) } ") ;
128
+
129
+ using ( var tracerProvider = Sdk . CreateTracerProviderBuilder ( )
130
+ . AddSource ( "B.*" )
131
+ . AddSource ( source => ! source . Name . StartsWith ( "B." ) )
132
+ . Build ( ) )
133
+ {
134
+ Assert . True ( IsSourceEnabled ( source1 ) ) ;
135
+ Assert . True ( IsSourceEnabled ( source2 ) ) ;
136
+ Assert . True ( IsSourceEnabled ( source3 ) ) ;
97
137
}
98
138
}
99
139
@@ -1242,6 +1282,14 @@ public void Dispose()
1242
1282
GC . SuppressFinalize ( this ) ;
1243
1283
}
1244
1284
1285
+ private static bool IsSourceEnabled ( ActivitySource source )
1286
+ {
1287
+ using ( var activity = source . StartActivity ( "test" ) )
1288
+ {
1289
+ return activity != null ;
1290
+ }
1291
+ }
1292
+
1245
1293
private sealed class TestTracerProviderBuilder : TracerProviderBuilderBase
1246
1294
{
1247
1295
public TracerProviderBuilder AddInstrumentation ( )
0 commit comments