@@ -11,8 +11,8 @@ namespace IntegrationTests.Helpers;
1111
1212internal sealed class OtlpResourceExpector : IDisposable
1313{
14- private readonly List < ResourceExpectation > _resourceExpectations = new ( ) ;
15- private readonly List < string > _existenceChecks = new ( ) ;
14+ private readonly List < ResourceExpectation > _resourceExpectations = [ ] ;
15+ private readonly List < string > _existenceChecks = [ ] ;
1616
1717 private readonly ManualResetEvent _resourceAttributesEvent = new ( false ) ; // synchronizes access to _resourceAttributes
1818 private RepeatedField < KeyValue > ? _resourceAttributes ; // protobuf type
@@ -47,6 +47,11 @@ public void Expect(string key, long value)
4747 _resourceExpectations . Add ( new ResourceExpectation ( key , value ) ) ;
4848 }
4949
50+ public void Matches ( string key , string regex )
51+ {
52+ _resourceExpectations . Add ( new ResourceExpectation ( key , regex , isRegex : true ) ) ;
53+ }
54+
5055 public void AssertExpectations ( TimeSpan ? timeout = null )
5156 {
5257 if ( _resourceExpectations . Count == 0 && _existenceChecks . Count == 0 )
@@ -108,12 +113,16 @@ private static void AssertResource(List<ResourceExpectation> resourceExpectation
108113 continue ;
109114 }
110115
111- if ( missingExpectations [ i ] . StringValue != null && resourceAttribute . Value . StringValue != missingExpectations [ i ] . StringValue )
116+ var expectation = missingExpectations [ i ] ;
117+
118+ if ( expectation . StringValue != null &&
119+ ( ( ! expectation . IsRegex && resourceAttribute . Value . StringValue != expectation . StringValue ) ||
120+ ( expectation . IsRegex && ! System . Text . RegularExpressions . Regex . IsMatch ( resourceAttribute . Value . StringValue , expectation . StringValue ) ) ) )
112121 {
113122 continue ;
114123 }
115124
116- if ( missingExpectations [ i ] . IntValue != null && resourceAttribute . Value . IntValue != missingExpectations [ i ] . IntValue )
125+ if ( expectation . IntValue != null && resourceAttribute . Value . IntValue != expectation . IntValue )
117126 {
118127 continue ;
119128 }
@@ -132,15 +141,17 @@ private static void AssertResource(List<ResourceExpectation> resourceExpectation
132141
133142 private static void FailResource ( List < ResourceExpectation > missingExpectations , RepeatedField < KeyValue > ? attributes )
134143 {
135- attributes ??= new ( ) ;
144+ attributes ??= [ ] ;
136145
137146 var message = new StringBuilder ( ) ;
138147 message . AppendLine ( ) ;
139148
140149 message . AppendLine ( "Missing resource expectations:" ) ;
141150 foreach ( var expectation in missingExpectations )
142151 {
143- var value = ! string . IsNullOrEmpty ( expectation . StringValue ) ? expectation . StringValue : expectation . IntValue ! . Value . ToString ( CultureInfo . InvariantCulture ) ;
152+ var value = string . IsNullOrEmpty ( expectation . StringValue )
153+ ? expectation . IntValue ! . Value . ToString ( CultureInfo . InvariantCulture )
154+ : expectation . IsRegex ? $ "/{ expectation . StringValue } /" : expectation . StringValue ;
144155 message . AppendLine ( CultureInfo . InvariantCulture , $ " - \" { expectation . Key } ={ value } \" ") ;
145156 }
146157
@@ -156,10 +167,11 @@ private static void FailResource(List<ResourceExpectation> missingExpectations,
156167
157168 private sealed class ResourceExpectation
158169 {
159- public ResourceExpectation ( string key , string stringValue )
170+ public ResourceExpectation ( string key , string stringValue , bool isRegex = false )
160171 {
161172 Key = key ;
162173 StringValue = stringValue ;
174+ IsRegex = isRegex ;
163175 }
164176
165177 public ResourceExpectation ( string key , long intValue )
@@ -173,5 +185,7 @@ public ResourceExpectation(string key, long intValue)
173185 public string ? StringValue { get ; }
174186
175187 public long ? IntValue { get ; }
188+
189+ public bool IsRegex { get ; }
176190 }
177191}
0 commit comments