1+ using System ;
12using System . Collections . Generic ;
23using System . Diagnostics ;
34using System . Linq ;
45using System . Threading . Tasks ;
6+ using Humanizer ;
57using Microsoft . VisualStudio . TestTools . UnitTesting ;
68using OpenTelemetry . Instrumentation . Digma . Helpers ;
79using OpenTelemetry . Instrumentation . Digma . Tests . Stubs ;
@@ -16,82 +18,148 @@ public class TestTracingDecorator
1618 private static readonly string ServiceInterfaceFqn =
1719 "OpenTelemetry.Instrumentation.Digma.Tests.Stubs.IDecoratedService" ;
1820
19- [ TestMethod ]
20- public void Activity_Created_For_Attribute_Marked_Method ( )
21- {
22- DecoratedService service = new DecoratedService ( ) ;
23- IDecoratedService tracingDecorator = TraceDecorator < IDecoratedService > . Create ( service ) ;
24- tracingDecorator . MethodExplicitlyMarkedForTracing ( ( ) =>
25- {
26- Assert . IsNotNull ( Activity . Current ) ;
27- AssertHasCommonTags ( Activity . Current , ServiceInterfaceFqn ,
28- "MethodExplicitlyMarkedForTracing" , "Action" ) ;
29- } ) ;
30- }
21+ private MockProcessor _mockProcessor = new ( ) ;
22+
23+ // [TestMethod]
24+ // public void Activity_Created_For_Attribute_Marked_Method()
25+ // {
26+ // DecoratedService service = new DecoratedService();
27+ // IDecoratedService tracingDecorator = TraceDecorator<IDecoratedService>.Create(service);
28+ // tracingDecorator.MethodExplicitlyMarkedForTracing(() =>
29+ // {
30+ // Assert.IsNotNull(Activity.Current);
31+ // AssertHasCommonTags(Activity.Current, ServiceInterfaceFqn,
32+ // "MethodExplicitlyMarkedForTracing", "Action");
33+ // });
34+ // }
3135
3236 [ TestInitialize ]
3337 public void SetupOtel ( )
3438 {
39+ _mockProcessor . Reset ( ) ;
3540 Sdk . CreateTracerProviderBuilder ( )
3641 . AddSource ( "*" )
3742 . SetResourceBuilder (
3843 ResourceBuilder . CreateDefault ( )
3944 . AddService ( serviceName : "test" , serviceVersion : "2.2" ) )
45+ . AddProcessor ( _mockProcessor )
4046 . Build ( ) ;
4147 }
4248
43- [ TestMethod ]
44- public async Task Activity_Created_For_Async_Attribute_Marked_Method ( )
45- {
46- DecoratedService service = new DecoratedService ( ) ;
47- IDecoratedService tracingDecorator = TraceDecorator < IDecoratedService > . Create ( service ) ;
48- await tracingDecorator . AsyncMethodExplicitlyMarkedForTracing ( ( ) =>
49- {
50- Assert . IsNotNull ( Activity . Current ) ;
51- AssertHasCommonTags ( Activity . Current , ServiceInterfaceFqn ,
52- "AsyncMethodExplicitlyMarkedForTracing" , "Action" ) ;
53- } ) ;
54- }
49+ // [TestMethod]
50+ // public async Task Activity_Created_For_Async_Attribute_Marked_Method()
51+ // {
52+ // DecoratedService service = new DecoratedService();
53+ // IDecoratedService tracingDecorator = TraceDecorator<IDecoratedService>.Create(service);
54+ // await tracingDecorator.AsyncMethodExplicitlyMarkedForTracing(() =>
55+ // {
56+ // Assert.IsNotNull(Activity.Current);
57+ // AssertHasCommonTags(Activity.Current, ServiceInterfaceFqn,
58+ // "AsyncMethodExplicitlyMarkedForTracing", "Action");
59+ // });
60+ // }
61+ //
62+ // [TestMethod]
63+ // public void Activity_Created_MethodWithStrangeParams1()
64+ // {
65+ // DecoratedService service = new DecoratedService();
66+ // IDecoratedService tracingDecorator = TraceDecorator<IDecoratedService>.Create(service);
67+ // int intVal = 5;
68+ // tracingDecorator.MethodWithStrangeParams1(() =>
69+ // {
70+ // Assert.IsNotNull(Activity.Current);
71+ // AssertHasCommonTags(Activity.Current, ServiceInterfaceFqn, "MethodWithStrangeParams1",
72+ // "Action|IList`1[]|ISet`1|IDictionary`2|Int32&");
73+ // },
74+ // new List<string>[] { }, new HashSet<int[]>(), new Dictionary<int, ICollection<string>>(), ref intVal
75+ // );
76+ // }
77+ //
78+ // [TestMethod]
79+ // public void Activity_Created_MethodJaggedAndMultiDimArraysParams()
80+ // {
81+ // DecoratedService service = new DecoratedService();
82+ // IDecoratedService tracingDecorator = TraceDecorator<IDecoratedService>.Create(service);
83+ // string strVal;
84+ // tracingDecorator.MethodJaggedAndMultiDimArraysParams(() =>
85+ // {
86+ // Assert.IsNotNull(Activity.Current);
87+ // AssertHasCommonTags(Activity.Current, ServiceInterfaceFqn, "MethodJaggedAndMultiDimArraysParams",
88+ // "Action|String&|Boolean[][][]|Int16[,,][,][,,,]|Int64[][,][][,,]");
89+ // },
90+ // out strVal, new bool[][][] { }, new short[,,,][,][,,] { }, new long[,,][][,][] { }
91+ // );
92+ // }
93+ //
94+ // [TestMethod]
95+ // public void Activity_Not_Created_For_Non_Attribute_Marked_Method_If_All_Methods_False()
96+ // {
97+ // DecoratedService service = new DecoratedService();
98+ // IDecoratedService tracingDecorator =
99+ // TraceDecorator<IDecoratedService>.Create(service, decorateAllMethods: false);
100+ // tracingDecorator.MethodNotExplicitlyMarkedForTracing(() => { Assert.IsNull(Activity.Current); });
101+ // }
55102
56103 [ TestMethod ]
57- public void Activity_Created_MethodWithStrangeParams1 ( )
104+ public async Task Activity_Async_Void ( )
58105 {
59- DecoratedService service = new DecoratedService ( ) ;
60- IDecoratedService tracingDecorator = TraceDecorator < IDecoratedService > . Create ( service ) ;
61- int intVal = 5 ;
62- tracingDecorator . MethodWithStrangeParams1 ( ( ) =>
63- {
64- Assert . IsNotNull ( Activity . Current ) ;
65- AssertHasCommonTags ( Activity . Current , ServiceInterfaceFqn , "MethodWithStrangeParams1" ,
66- "Action|IList`1[]|ISet`1|IDictionary`2|Int32&" ) ;
67- } ,
68- new List < string > [ ] { } , new HashSet < int [ ] > ( ) , new Dictionary < int , ICollection < string > > ( ) , ref intVal
69- ) ;
106+ // Arrange
107+ var service = new DecoratedService ( ) ;
108+ var decoratedService = TraceDecorator < IDecoratedService > . Create ( service , decorateAllMethods : true ) ;
109+
110+ // Act #1
111+ await decoratedService . AsyncVoid ( ) ;
112+ var activity = _mockProcessor . Activities . Single ( ) ;
113+ AssertActivity . SpanNameIs ( "AsyncVoid" , activity ) ;
114+ AssertActivity . InstrumentationScopeIs ( "OpenTelemetry.Instrumentation.Digma.Tests.Stubs.DecoratedService" , activity ) ;
115+ AssertActivity . DurationIs ( 100 . Milliseconds ( ) , 30 . Milliseconds ( ) , activity ) ;
116+ AssertActivity . HasTag ( "code.namespace" , "OpenTelemetry.Instrumentation.Digma.Tests.Stubs.DecoratedService" , activity ) ;
117+ AssertActivity . HasTag ( "code.function" , "AsyncVoid" , activity ) ;
70118 }
71-
119+
72120 [ TestMethod ]
73- public void Activity_Created_MethodJaggedAndMultiDimArraysParams ( )
121+ public async Task Activity_Async_Value ( )
74122 {
75- DecoratedService service = new DecoratedService ( ) ;
76- IDecoratedService tracingDecorator = TraceDecorator < IDecoratedService > . Create ( service ) ;
77- string strVal ;
78- tracingDecorator . MethodJaggedAndMultiDimArraysParams ( ( ) =>
79- {
80- Assert . IsNotNull ( Activity . Current ) ;
81- AssertHasCommonTags ( Activity . Current , ServiceInterfaceFqn , "MethodJaggedAndMultiDimArraysParams" ,
82- "Action|String&|Boolean[][][]|Int16[,,][,][,,,]|Int64[][,][][,,]" ) ;
83- } ,
84- out strVal , new bool [ ] [ ] [ ] { } , new short [ , , , ] [ , ] [ , , ] { } , new long [ , , ] [ ] [ , ] [ ] { }
85- ) ;
123+ // Arrange
124+ var service = new DecoratedService ( ) ;
125+ var decoratedService = TraceDecorator < IDecoratedService > . Create ( service , decorateAllMethods : true ) ;
126+
127+ // Act #1
128+ var result = await decoratedService . AsyncValue ( ) ;
129+ Assert . AreEqual ( 123 , result ) ;
130+
131+ var activity = _mockProcessor . Activities . Single ( ) ;
132+ AssertActivity . SpanNameIs ( "AsyncValue" , activity ) ;
133+ AssertActivity . InstrumentationScopeIs ( "OpenTelemetry.Instrumentation.Digma.Tests.Stubs.DecoratedService" , activity ) ;
134+ AssertActivity . DurationIs ( 100 . Milliseconds ( ) , 30 . Milliseconds ( ) , activity ) ;
135+ AssertActivity . HasTag ( "code.namespace" , "OpenTelemetry.Instrumentation.Digma.Tests.Stubs.DecoratedService" , activity ) ;
136+ AssertActivity . HasTag ( "code.function" , "AsyncValue" , activity ) ;
86137 }
87-
138+
88139 [ TestMethod ]
89- public void Activity_Not_Created_For_Non_Attribute_Marked_Method_If_All_Methods_False ( )
140+ public async Task Activity_Async_Error ( )
90141 {
91- DecoratedService service = new DecoratedService ( ) ;
92- IDecoratedService tracingDecorator =
93- TraceDecorator < IDecoratedService > . Create ( service , decorateAllMethods : false ) ;
94- tracingDecorator . MethodNotExplicitlyMarkedForTracing ( ( ) => { Assert . IsNull ( Activity . Current ) ; } ) ;
142+ // Arrange
143+ var service = new DecoratedService ( ) ;
144+ var decoratedService = TraceDecorator < IDecoratedService > . Create ( service , decorateAllMethods : true ) ;
145+
146+ // Act #1
147+ try
148+ {
149+ await decoratedService . AsyncError ( ) ;
150+ Assert . Fail ( ) ;
151+ }
152+ catch ( Exception e )
153+ {
154+ Assert . AreEqual ( e . Message , "Bla" ) ;
155+
156+ var activity = _mockProcessor . Activities . Single ( ) ;
157+ AssertActivity . SpanNameIs ( "AsyncError" , activity ) ;
158+ AssertActivity . InstrumentationScopeIs ( "OpenTelemetry.Instrumentation.Digma.Tests.Stubs.DecoratedService" , activity ) ;
159+ AssertActivity . DurationIs ( 100 . Milliseconds ( ) , 30 . Milliseconds ( ) , activity ) ;
160+ AssertActivity . HasTag ( "code.namespace" , "OpenTelemetry.Instrumentation.Digma.Tests.Stubs.DecoratedService" , activity ) ;
161+ AssertActivity . HasTag ( "code.function" , "AsyncError" , activity ) ;
162+ }
95163 }
96164
97165 private void AssertHasCommonTags ( Activity ? activity ,
0 commit comments