diff --git a/OpenTelemetry.Instrumentation.Digma.sln b/OpenTelemetry.Instrumentation.Digma.sln index 5fe5dae..a1bb53e 100644 --- a/OpenTelemetry.Instrumentation.Digma.sln +++ b/OpenTelemetry.Instrumentation.Digma.sln @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTelemetry.Instrumentati EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Digma.MassTransit.Integration", "src\Digma.MassTransit.Integration\Digma.MassTransit.Integration.csproj", "{61EA0936-F7BE-4A78-85D7-4C312038CDE4}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTelemetry.Instrumentation.Digma.Tests", "src\OpenTelemetry.Instrumentation.Digma.Tests\OpenTelemetry.Instrumentation.Digma.Tests.csproj", "{79CE5248-0B20-488A-9BEB-2ECC5D936CAB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {61EA0936-F7BE-4A78-85D7-4C312038CDE4}.Debug|Any CPU.Build.0 = Debug|Any CPU {61EA0936-F7BE-4A78-85D7-4C312038CDE4}.Release|Any CPU.ActiveCfg = Release|Any CPU {61EA0936-F7BE-4A78-85D7-4C312038CDE4}.Release|Any CPU.Build.0 = Release|Any CPU + {79CE5248-0B20-488A-9BEB-2ECC5D936CAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {79CE5248-0B20-488A-9BEB-2ECC5D936CAB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {79CE5248-0B20-488A-9BEB-2ECC5D936CAB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {79CE5248-0B20-488A-9BEB-2ECC5D936CAB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/OpenTelemetry.Instrumentation.Digma.Tests/OpenTelemetry.Instrumentation.Digma.Tests.csproj b/src/OpenTelemetry.Instrumentation.Digma.Tests/OpenTelemetry.Instrumentation.Digma.Tests.csproj new file mode 100644 index 0000000..2e42fdd --- /dev/null +++ b/src/OpenTelemetry.Instrumentation.Digma.Tests/OpenTelemetry.Instrumentation.Digma.Tests.csproj @@ -0,0 +1,16 @@ + + + + net6.0 + enable + false + + + + + + + + + + diff --git a/src/OpenTelemetry.Instrumentation.Digma.Tests/Stubs/DecoratedService.cs b/src/OpenTelemetry.Instrumentation.Digma.Tests/Stubs/DecoratedService.cs new file mode 100644 index 0000000..b36b8b3 --- /dev/null +++ b/src/OpenTelemetry.Instrumentation.Digma.Tests/Stubs/DecoratedService.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Threading.Tasks; +using OpenTelemetry.Instrumentation.Digma.Helpers.Attributes; + +namespace OpenTelemetry.Instrumentation.Digma.Tests.Stubs; + +public interface IDecoratedService +{ + public void MethodExplicitlyMarkedForTracing(Action stateValidation); + + public Task AsyncMethodExplicitlyMarkedForTracing(Action stateValidation); + + public void MethodNotExplicitlyMarkedForTracing(Action stateValidation); + + public void MethodWithStrangeParams1(Action stateValidation, + IList[] arrayOfList, ISet setOfArray, IDictionary> dict, + ref int intVal); + + public void MethodJaggedAndMultiDimArraysParams(Action stateValidation, out string strVal, + bool[][][] jaggedArrayOfBools, short[,,,][,][,,] multiDimArrayOfShorts, + long[,,][][,][] mixMultiDimAndJaggedArraysOfLongs + ); +} + +public class DecoratedService : IDecoratedService +{ + [TraceActivity()] + public void MethodExplicitlyMarkedForTracing(Action stateValidation) + { + var v = Activity.Current; + stateValidation(); + } + + [TraceActivity()] + public async Task AsyncMethodExplicitlyMarkedForTracing(Action stateValidation) + { + stateValidation(); + } + + public void MethodNotExplicitlyMarkedForTracing(Action stateValidation) + { + stateValidation(); + } + + public void MethodWithStrangeParams1(Action stateValidation, + IList[] arrayOfList, ISet setOfArray, IDictionary> dict, ref int intVal) + { + stateValidation(); + } + + public void MethodJaggedAndMultiDimArraysParams(Action stateValidation, out string strVal, + bool[][][] jaggedArrayOfBools, short[,,,][,][,,] multiDimArrayOfShorts, + long[,,][][,][] mixMultiDimAndJaggedArraysOfLongs) + { + strVal = "hello"; + stateValidation(); + } +} \ No newline at end of file diff --git a/src/OpenTelemetry.Instrumentation.Digma.Tests/TestTracingDecorator.cs b/src/OpenTelemetry.Instrumentation.Digma.Tests/TestTracingDecorator.cs new file mode 100644 index 0000000..f433442 --- /dev/null +++ b/src/OpenTelemetry.Instrumentation.Digma.Tests/TestTracingDecorator.cs @@ -0,0 +1,109 @@ +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using OpenTelemetry.Instrumentation.Digma.Helpers; +using OpenTelemetry.Instrumentation.Digma.Tests.Stubs; +using OpenTelemetry.Resources; +using OpenTelemetry.Trace; + +namespace OpenTelemetry.Instrumentation.Digma.Tests; + +[TestClass] +public class TestTracingDecorator +{ + private static readonly string ServiceInterfaceFqn = + "OpenTelemetry.Instrumentation.Digma.Tests.Stubs.IDecoratedService"; + + [TestMethod] + public void Activity_Created_For_Attribute_Marked_Method() + { + DecoratedService service = new DecoratedService(); + IDecoratedService tracingDecorator = TraceDecorator.Create(service); + tracingDecorator.MethodExplicitlyMarkedForTracing(() => + { + Assert.IsNotNull(Activity.Current); + AssertHasCommonTags(Activity.Current, ServiceInterfaceFqn, + "MethodExplicitlyMarkedForTracing", "Action"); + }); + } + + [TestInitialize] + public void SetupOtel() + { + Sdk.CreateTracerProviderBuilder() + .AddSource("*") + .SetResourceBuilder( + ResourceBuilder.CreateDefault() + .AddService(serviceName: "test", serviceVersion: "2.2")) + .Build(); + } + + [TestMethod] + public async Task Activity_Created_For_Async_Attribute_Marked_Method() + { + DecoratedService service = new DecoratedService(); + IDecoratedService tracingDecorator = TraceDecorator.Create(service); + await tracingDecorator.AsyncMethodExplicitlyMarkedForTracing(() => + { + Assert.IsNotNull(Activity.Current); + AssertHasCommonTags(Activity.Current, ServiceInterfaceFqn, + "AsyncMethodExplicitlyMarkedForTracing", "Action"); + }); + } + + [TestMethod] + public void Activity_Created_MethodWithStrangeParams1() + { + DecoratedService service = new DecoratedService(); + IDecoratedService tracingDecorator = TraceDecorator.Create(service); + int intVal = 5; + tracingDecorator.MethodWithStrangeParams1(() => + { + Assert.IsNotNull(Activity.Current); + AssertHasCommonTags(Activity.Current, ServiceInterfaceFqn, "MethodWithStrangeParams1", + "Action|IList`1[]|ISet`1|IDictionary`2|Int32&"); + }, + new List[] { }, new HashSet(), new Dictionary>(), ref intVal + ); + } + + [TestMethod] + public void Activity_Created_MethodJaggedAndMultiDimArraysParams() + { + DecoratedService service = new DecoratedService(); + IDecoratedService tracingDecorator = TraceDecorator.Create(service); + string strVal; + tracingDecorator.MethodJaggedAndMultiDimArraysParams(() => + { + Assert.IsNotNull(Activity.Current); + AssertHasCommonTags(Activity.Current, ServiceInterfaceFqn, "MethodJaggedAndMultiDimArraysParams", + "Action|String&|Boolean[][][]|Int16[,,][,][,,,]|Int64[][,][][,,]"); + }, + out strVal, new bool[][][] { }, new short[,,,][,][,,] { }, new long[,,][][,][] { } + ); + } + + [TestMethod] + public void Activity_Not_Created_For_Non_Attribute_Marked_Method_If_All_Methods_False() + { + DecoratedService service = new DecoratedService(); + IDecoratedService tracingDecorator = + TraceDecorator.Create(service, decorateAllMethods: false); + tracingDecorator.MethodNotExplicitlyMarkedForTracing(() => { Assert.IsNull(Activity.Current); }); + } + + private void AssertHasCommonTags(Activity? activity, + string expectedClassName, string expectedMethodName, string expectedParameterTypes) + { + var kvpTags = activity.Tags.ToArray(); + CollectionAssert.Contains(kvpTags, new KeyValuePair("code.namespace", expectedClassName)); + CollectionAssert.Contains(kvpTags, new KeyValuePair("code.function", expectedMethodName)); + if (!string.IsNullOrWhiteSpace(expectedParameterTypes)) + { + CollectionAssert.Contains(kvpTags, + new KeyValuePair("code.function.parameter.types", expectedParameterTypes)); + } + } +} \ No newline at end of file diff --git a/src/OpenTelemetry.Instrumentation.Digma/Tests/Stubs/DecoratedService.cs b/src/OpenTelemetry.Instrumentation.Digma/Tests/Stubs/DecoratedService.cs deleted file mode 100644 index 53f0f7a..0000000 --- a/src/OpenTelemetry.Instrumentation.Digma/Tests/Stubs/DecoratedService.cs +++ /dev/null @@ -1,57 +0,0 @@ -// using System.Diagnostics; -// using OpenTelemetry.Instrumentation.Digma.Helpers.Attributes; -// -// namespace OpenTelemetry.Instrumentation.Digma.Tests.Stubs; -// -// public interface IDecoratedService -// { -// public void MethodExplicitlyMarkedForTracing(Action stateValidation); -// -// public Task AsyncMethodExplicitlyMarkedForTracing(Action stateValidation); -// -// public void MethodNotExplicitlyMarkedForTracing(Action stateValidation); -// -// public void MethodWithStrangeParams1(Action stateValidation, -// IList[] arrayOfList, ISet setOfArray, IDictionary> dict, -// ref int intVal); -// -// public void MethodJaggedAndMultiDimArraysParams(Action stateValidation, out string strVal, -// bool[][][] jaggedArrayOfBools, short[,,,][,][,,] multiDimArrayOfShorts, -// long[,,][][,][] mixMultiDimAndJaggedArraysOfLongs -// ); -// } -// -// public class DecoratedService : IDecoratedService -// { -// [TraceActivity()] -// public void MethodExplicitlyMarkedForTracing(Action stateValidation) -// { -// var v = Activity.Current; -// stateValidation(); -// } -// -// [TraceActivity()] -// public async Task AsyncMethodExplicitlyMarkedForTracing(Action stateValidation) -// { -// stateValidation(); -// } -// -// public void MethodNotExplicitlyMarkedForTracing(Action stateValidation) -// { -// stateValidation(); -// } -// -// public void MethodWithStrangeParams1(Action stateValidation, -// IList[] arrayOfList, ISet setOfArray, IDictionary> dict, ref int intVal) -// { -// stateValidation(); -// } -// -// public void MethodJaggedAndMultiDimArraysParams(Action stateValidation, out string strVal, -// bool[][][] jaggedArrayOfBools, short[,,,][,][,,] multiDimArrayOfShorts, -// long[,,][][,][] mixMultiDimAndJaggedArraysOfLongs) -// { -// strVal = "hello"; -// stateValidation(); -// } -// } \ No newline at end of file diff --git a/src/OpenTelemetry.Instrumentation.Digma/Tests/TestTracingDecorator.cs b/src/OpenTelemetry.Instrumentation.Digma/Tests/TestTracingDecorator.cs deleted file mode 100644 index a2dcde6..0000000 --- a/src/OpenTelemetry.Instrumentation.Digma/Tests/TestTracingDecorator.cs +++ /dev/null @@ -1,106 +0,0 @@ -// using System.Diagnostics; -// using Microsoft.VisualStudio.TestTools.UnitTesting; -// using OpenTelemetry.Instrumentation.Digma.Helpers; -// using OpenTelemetry.Instrumentation.Digma.Tests.Stubs; -// using OpenTelemetry.Resources; -// using OpenTelemetry.Trace; -// -// namespace OpenTelemetry.Instrumentation.Digma.Tests; -// -// [TestClass] -// public class TestTracingDecorator -// { -// private static readonly string ServiceInterfaceFqn = -// "OpenTelemetry.Instrumentation.Digma.Tests.Stubs.IDecoratedService"; -// -// [TestMethod] -// public void Activity_Created_For_Attribute_Marked_Method() -// { -// DecoratedService service = new DecoratedService(); -// IDecoratedService tracingDecorator = TraceDecorator.Create(service); -// tracingDecorator.MethodExplicitlyMarkedForTracing(() => -// { -// Assert.IsNotNull(Activity.Current); -// AssertHasCommonTags(Activity.Current, ServiceInterfaceFqn, -// "MethodExplicitlyMarkedForTracing", "Action"); -// }); -// } -// -// [TestInitialize] -// public void SetupOtel() -// { -// Sdk.CreateTracerProviderBuilder() -// .AddSource("*") -// .SetResourceBuilder( -// ResourceBuilder.CreateDefault() -// .AddService(serviceName: "test", serviceVersion: "2.2")) -// .Build(); -// } -// -// [TestMethod] -// public async Task Activity_Created_For_Async_Attribute_Marked_Method() -// { -// DecoratedService service = new DecoratedService(); -// IDecoratedService tracingDecorator = TraceDecorator.Create(service); -// await tracingDecorator.AsyncMethodExplicitlyMarkedForTracing(() => -// { -// Assert.IsNotNull(Activity.Current); -// AssertHasCommonTags(Activity.Current, ServiceInterfaceFqn, -// "AsyncMethodExplicitlyMarkedForTracing", "Action"); -// }); -// } -// -// [TestMethod] -// public void Activity_Created_MethodWithStrangeParams1() -// { -// DecoratedService service = new DecoratedService(); -// IDecoratedService tracingDecorator = TraceDecorator.Create(service); -// int intVal = 5; -// tracingDecorator.MethodWithStrangeParams1(() => -// { -// Assert.IsNotNull(Activity.Current); -// AssertHasCommonTags(Activity.Current, ServiceInterfaceFqn, "MethodWithStrangeParams1", -// "Action|IList`1[]|ISet`1|IDictionary`2|Int32&"); -// }, -// new List[] { }, new HashSet(), new Dictionary>(), ref intVal -// ); -// } -// -// [TestMethod] -// public void Activity_Created_MethodJaggedAndMultiDimArraysParams() -// { -// DecoratedService service = new DecoratedService(); -// IDecoratedService tracingDecorator = TraceDecorator.Create(service); -// string strVal; -// tracingDecorator.MethodJaggedAndMultiDimArraysParams(() => -// { -// Assert.IsNotNull(Activity.Current); -// AssertHasCommonTags(Activity.Current, ServiceInterfaceFqn, "MethodJaggedAndMultiDimArraysParams", -// "Action|String&|Boolean[][][]|Int16[,,][,][,,,]|Int64[][,][][,,]"); -// }, -// out strVal, new bool[][][] { }, new short[,,,][,][,,] { }, new long[,,][][,][] { } -// ); -// } -// -// [TestMethod] -// public void Activity_Not_Created_For_Non_Attribute_Marked_Method_If_All_Methods_False() -// { -// DecoratedService service = new DecoratedService(); -// IDecoratedService tracingDecorator = -// TraceDecorator.Create(service, decorateAllMethods: false); -// tracingDecorator.MethodNotExplicitlyMarkedForTracing(() => { Assert.IsNull(Activity.Current); }); -// } -// -// private void AssertHasCommonTags(Activity? activity, -// string expectedClassName, string expectedMethodName, string expectedParameterTypes) -// { -// var kvpTags = activity.Tags.ToArray(); -// CollectionAssert.Contains(kvpTags, new KeyValuePair("code.namespace", expectedClassName)); -// CollectionAssert.Contains(kvpTags, new KeyValuePair("code.function", expectedMethodName)); -// if (!string.IsNullOrWhiteSpace(expectedParameterTypes)) -// { -// CollectionAssert.Contains(kvpTags, -// new KeyValuePair("code.function.parameter.types", expectedParameterTypes)); -// } -// } -// } \ No newline at end of file