/// <summary>
        /// OnMethodBegin callback
        /// </summary>
        /// <typeparam name="TTarget">Type of the target</typeparam>
        /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
        /// <returns>Calltarget state value</returns>
        internal static CallTargetState OnMethodBegin <TTarget>(TTarget instance)
        {
            if (XUnitIntegration.IsEnabled)
            {
                TestRunnerStruct runnerInstance = instance.DuckCast <TestRunnerStruct>();

                // Skip test support
                if (runnerInstance.SkipReason != null)
                {
                    XUnitIntegration.CreateScope(ref runnerInstance, instance.GetType());
                }
            }

            return(CallTargetState.GetDefault());
        }
        /// <summary>
        /// OnMethodBegin callback
        /// </summary>
        /// <typeparam name="TTarget">Type of the target</typeparam>
        /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
        /// <returns>Calltarget state value</returns>
        public static CallTargetState OnMethodBegin <TTarget>(TTarget instance)
        {
            if (Tracer.Instance.Settings.IsIntegrationEnabled(IntegrationId))
            {
                TestRunnerStruct runnerInstance = instance.As <TestRunnerStruct>();

                // Skip test support
                if (runnerInstance.SkipReason != null)
                {
                    XUnitIntegration.CreateScope(ref runnerInstance, instance.GetType());
                }
            }

            return(CallTargetState.GetDefault());
        }
        /// <summary>
        /// OnMethodBegin callback
        /// </summary>
        /// <typeparam name="TTarget">Type of the target</typeparam>
        /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
        /// <returns>Calltarget state value</returns>
        public static CallTargetState OnMethodBegin <TTarget>(TTarget instance)
        {
            if (Tracer.Instance.Settings.IsIntegrationEnabled(IntegrationId))
            {
                return(CallTargetState.GetDefault());
            }

            TestInvokerStruct invokerInstance = instance.As <TestInvokerStruct>();
            TestRunnerStruct  runnerInstance  = new TestRunnerStruct
            {
                Aggregator          = invokerInstance.Aggregator,
                TestCase            = invokerInstance.TestCase,
                TestClass           = invokerInstance.TestClass,
                TestMethod          = invokerInstance.TestMethod,
                TestMethodArguments = invokerInstance.TestMethodArguments
            };

            return(new CallTargetState(XUnitIntegration.CreateScope(ref runnerInstance, instance.GetType())));
        }
예제 #4
0
        /// <summary>
        /// OnMethodBegin callback
        /// </summary>
        /// <typeparam name="TTarget">Type of the target</typeparam>
        /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
        /// <returns>Calltarget state value</returns>
        internal static CallTargetState OnMethodBegin <TTarget>(TTarget instance)
        {
            if (!XUnitIntegration.IsEnabled)
            {
                return(CallTargetState.GetDefault());
            }

            TestInvokerStruct invokerInstance = instance.DuckCast <TestInvokerStruct>();
            TestRunnerStruct  runnerInstance  = new TestRunnerStruct
            {
                Aggregator          = invokerInstance.Aggregator,
                TestCase            = invokerInstance.TestCase,
                TestClass           = invokerInstance.TestClass,
                TestMethod          = invokerInstance.TestMethod,
                TestMethodArguments = invokerInstance.TestMethodArguments
            };

            return(new CallTargetState(XUnitIntegration.CreateScope(ref runnerInstance, instance.GetType())));
        }
        internal static Scope CreateScope(ref TestRunnerStruct runnerInstance, Type targetType)
        {
            string testBundle = runnerInstance.TestClass.Assembly?.GetName().Name;
            string testSuite  = runnerInstance.TestClass.ToString();
            string testName   = runnerInstance.TestMethod.Name;

            string testFramework = "xUnit";

            Scope scope = Tracer.Instance.StartActiveInternal("xunit.test");
            Span  span  = scope.Span;

            span.Type = SpanTypes.Test;
            span.SetTraceSamplingPriority(SamplingPriorityValues.AutoKeep);
            span.ResourceName = $"{testSuite}.{testName}";
            span.SetTag(Tags.Origin, TestTags.CIAppTestOriginName);
            span.SetTag(TestTags.Bundle, testBundle);
            span.SetTag(TestTags.Suite, testSuite);
            span.SetTag(TestTags.Name, testName);
            span.SetTag(TestTags.Framework, testFramework);
            span.SetTag(TestTags.FrameworkVersion, targetType.Assembly?.GetName().Version.ToString());
            span.SetTag(TestTags.Type, TestTags.TypeTest);

            var framework = FrameworkDescription.Instance;

            CIEnvironmentValues.Instance.DecorateSpan(span);

            span.SetTag(CommonTags.LibraryVersion, TracerConstants.AssemblyVersion);
            span.SetTag(CommonTags.RuntimeName, framework.Name);
            span.SetTag(CommonTags.RuntimeVersion, framework.ProductVersion);
            span.SetTag(CommonTags.RuntimeArchitecture, framework.ProcessArchitecture);
            span.SetTag(CommonTags.OSArchitecture, framework.OSArchitecture);
            span.SetTag(CommonTags.OSPlatform, framework.OSPlatform);
            span.SetTag(CommonTags.OSVersion, Environment.OSVersion.VersionString);

            // Get test parameters
            object[]        testMethodArguments = runnerInstance.TestMethodArguments;
            ParameterInfo[] methodParameters    = runnerInstance.TestMethod.GetParameters();
            if (methodParameters?.Length > 0 && testMethodArguments?.Length > 0)
            {
                TestParameters testParameters = new TestParameters();
                testParameters.Metadata  = new Dictionary <string, object>();
                testParameters.Arguments = new Dictionary <string, object>();
                testParameters.Metadata[TestTags.MetadataTestName] = runnerInstance.TestCase.DisplayName;

                for (int i = 0; i < methodParameters.Length; i++)
                {
                    if (i < testMethodArguments.Length)
                    {
                        testParameters.Arguments[methodParameters[i].Name] = Common.GetParametersValueData(testMethodArguments[i]);
                    }
                    else
                    {
                        testParameters.Arguments[methodParameters[i].Name] = "(default)";
                    }
                }

                span.SetTag(TestTags.Parameters, testParameters.ToJSON());
            }

            // Get traits
            Dictionary <string, List <string> > traits = runnerInstance.TestCase.Traits;

            if (traits.Count > 0)
            {
                span.SetTag(TestTags.Traits, Datadog.Trace.Vendors.Newtonsoft.Json.JsonConvert.SerializeObject(traits));
            }

            // Test code and code owners
            Common.DecorateSpanWithSourceAndCodeOwners(span, runnerInstance.TestMethod);

            Tracer.Instance.TracerManager.Telemetry.IntegrationGeneratedSpan(IntegrationId);
            Ci.Coverage.CoverageReporter.Handler.StartSession();

            // Skip tests
            if (runnerInstance.SkipReason != null)
            {
                span.SetTag(TestTags.Status, TestTags.StatusSkip);
                span.SetTag(TestTags.SkipReason, runnerInstance.SkipReason);

                var coverageSession = Ci.Coverage.CoverageReporter.Handler.EndSession();
                if (coverageSession is not null)
                {
                    scope.Span.SetTag("test.coverage", Datadog.Trace.Vendors.Newtonsoft.Json.JsonConvert.SerializeObject(coverageSession));
                }

                span.Finish(TimeSpan.Zero);
                scope.Dispose();
                return(null);
            }

            span.ResetStartTime();
            return(scope);
        }
        internal static Scope CreateScope(ref TestRunnerStruct runnerInstance, Type targetType)
        {
            string testSuite = runnerInstance.TestClass.ToString();
            string testName  = runnerInstance.TestMethod.Name;

            AssemblyName testInvokerAssemblyName = targetType.Assembly.GetName();

            string testFramework = "xUnit " + testInvokerAssemblyName.Version.ToString();

            Scope scope = Common.TestTracer.StartActive("xunit.test", serviceName: Common.ServiceName);
            Span  span  = scope.Span;

            span.Type = SpanTypes.Test;
            span.SetTraceSamplingPriority(SamplingPriority.AutoKeep);
            span.ResourceName = $"{testSuite}.{testName}";
            span.SetTag(TestTags.Suite, testSuite);
            span.SetTag(TestTags.Name, testName);
            span.SetTag(TestTags.Framework, testFramework);
            span.SetTag(TestTags.Type, TestTags.TypeTest);
            CIEnvironmentValues.DecorateSpan(span);

            var framework = FrameworkDescription.Instance;

            span.SetTag(CommonTags.RuntimeName, framework.Name);
            span.SetTag(CommonTags.RuntimeVersion, framework.ProductVersion);
            span.SetTag(CommonTags.RuntimeArchitecture, framework.ProcessArchitecture);
            span.SetTag(CommonTags.OSArchitecture, framework.OSArchitecture);
            span.SetTag(CommonTags.OSPlatform, framework.OSPlatform);
            span.SetTag(CommonTags.OSVersion, Environment.OSVersion.VersionString);

            // Get test parameters
            object[]        testMethodArguments = runnerInstance.TestMethodArguments;
            ParameterInfo[] methodParameters    = runnerInstance.TestMethod.GetParameters();
            if (methodParameters?.Length > 0 && testMethodArguments?.Length > 0)
            {
                TestParameters testParameters = new TestParameters();
                testParameters.Metadata  = new Dictionary <string, object>();
                testParameters.Arguments = new Dictionary <string, object>();
                testParameters.Metadata[TestTags.MetadataTestName] = runnerInstance.TestCase.DisplayName;

                for (int i = 0; i < methodParameters.Length; i++)
                {
                    if (i < testMethodArguments.Length)
                    {
                        testParameters.Arguments[methodParameters[i].Name] = testMethodArguments[i]?.ToString() ?? "(null)";
                    }
                    else
                    {
                        testParameters.Arguments[methodParameters[i].Name] = "(default)";
                    }
                }

                span.SetTag(TestTags.Parameters, testParameters.ToJSON());
            }

            // Get traits
            Dictionary <string, List <string> > traits = runnerInstance.TestCase.Traits;

            if (traits.Count > 0)
            {
                span.SetTag(TestTags.Traits, Datadog.Trace.Vendors.Newtonsoft.Json.JsonConvert.SerializeObject(traits));
            }

            // Skip tests
            if (runnerInstance.SkipReason != null)
            {
                span.SetTag(TestTags.Status, TestTags.StatusSkip);
                span.SetTag(TestTags.SkipReason, runnerInstance.SkipReason);
                span.Finish(new TimeSpan(10));
                scope.Dispose();
                return(null);
            }

            span.ResetStartTime();
            return(scope);
        }
예제 #7
0
        internal static Scope CreateScope(ref TestRunnerStruct runnerInstance, Type targetType)
        {
            string testSuite = runnerInstance.TestClass.ToString();
            string testName  = runnerInstance.TestMethod.Name;

            AssemblyName testInvokerAssemblyName = targetType.Assembly.GetName();

            Tracer tracer        = Tracer.Instance;
            string testFramework = "xUnit " + testInvokerAssemblyName.Version.ToString();

            Scope scope = tracer.StartActive("xunit.test");
            Span  span  = scope.Span;

            span.Type = SpanTypes.Test;
            span.SetTraceSamplingPriority(SamplingPriority.AutoKeep);
            span.ResourceName = $"{testSuite}.{testName}";
            span.SetTag(TestTags.Suite, testSuite);
            span.SetTag(TestTags.Name, testName);
            span.SetTag(TestTags.Framework, testFramework);
            span.SetTag(TestTags.Type, TestTags.TypeTest);
            CIEnvironmentValues.DecorateSpan(span);

            var framework = FrameworkDescription.Instance;

            span.SetTag(CommonTags.RuntimeName, framework.Name);
            span.SetTag(CommonTags.RuntimeOSArchitecture, framework.OSArchitecture);
            span.SetTag(CommonTags.RuntimeOSPlatform, framework.OSPlatform);
            span.SetTag(CommonTags.RuntimeProcessArchitecture, framework.ProcessArchitecture);
            span.SetTag(CommonTags.RuntimeVersion, framework.ProductVersion);

            // Get test parameters
            object[]        testMethodArguments = runnerInstance.TestMethodArguments;
            ParameterInfo[] methodParameters    = runnerInstance.TestMethod.GetParameters();
            if (methodParameters?.Length > 0 && testMethodArguments?.Length > 0)
            {
                for (int i = 0; i < methodParameters.Length; i++)
                {
                    if (i < testMethodArguments.Length)
                    {
                        span.SetTag($"{TestTags.Arguments}.{methodParameters[i].Name}", testMethodArguments[i]?.ToString() ?? "(null)");
                    }
                    else
                    {
                        span.SetTag($"{TestTags.Arguments}.{methodParameters[i].Name}", "(default)");
                    }
                }
            }

            // Get traits
            Dictionary <string, List <string> > traits = runnerInstance.TestCase.Traits;

            if (traits.Count > 0)
            {
                foreach (KeyValuePair <string, List <string> > traitValue in traits)
                {
                    span.SetTag($"{TestTags.Traits}.{traitValue.Key}", string.Join(", ", traitValue.Value) ?? "(null)");
                }
            }

            // Skip tests
            if (runnerInstance.SkipReason != null)
            {
                span.SetTag(TestTags.Status, TestTags.StatusSkip);
                span.SetTag(TestTags.SkipReason, runnerInstance.SkipReason);
                span.Finish(TimeSpan.Zero);
                scope.Dispose();
                return(null);
            }

            span.ResetStartTime();
            return(scope);
        }