static XUnitIntegration()
        {
            // Preload environment variables.
            CIEnvironmentValues.DecorateSpan(null);

            _runtimeDescription = FrameworkDescription.Create();
        }
        public void CheckEnvironmentVariables(string name, Dictionary <string, string>[][] data)
        {
            SpanContext    context = new SpanContext(null, null, null);
            DateTimeOffset time    = DateTimeOffset.UtcNow;

            foreach (Dictionary <string, string>[] testItem in data)
            {
                Dictionary <string, string> envData  = testItem[0];
                Dictionary <string, string> spanData = testItem[1];

                Span span = new Span(context, time);

                SetEnvironmentFromDictionary(envData);
                CIEnvironmentValues.ReloadEnvironmentData();
                CIEnvironmentValues.DecorateSpan(span);
                ResetEnvironmentFromDictionary(envData);

                foreach (KeyValuePair <string, string> spanDataItem in spanData)
                {
                    string value = span.Tags.GetTag(spanDataItem.Key);
                    Assert.Equal(spanDataItem.Value, value);
                }

                string providerName = span.Tags.GetTag(CommonTags.CIProvider);
                Assert.Equal(name, providerName);
            }
        }
Example #3
0
        private void EventSource_BuildStarted(object sender, BuildStartedEventArgs e)
        {
            try
            {
                Log.Debug("Build Started");

                _buildSpan = _tracer.StartSpan(BuildTags.BuildOperationName);
                _buildSpan.SetMetric(Tags.Analytics, 1.0d);
                _buildSpan.SetTraceSamplingPriority(SamplingPriority.AutoKeep);

                _buildSpan.Type = SpanTypes.Build;
                _buildSpan.SetTag(BuildTags.BuildName, e.SenderName);
                foreach (KeyValuePair <string, string> envValue in e.BuildEnvironment)
                {
                    _buildSpan.SetTag($"{BuildTags.BuildEnvironment}.{envValue.Key}", envValue.Value);
                }

                _buildSpan.SetTag(BuildTags.BuildCommand, Environment.CommandLine);
                _buildSpan.SetTag(BuildTags.BuildWorkingFolder, Environment.CurrentDirectory);
                _buildSpan.SetTag(BuildTags.BuildStartMessage, e.Message);

                _buildSpan.SetTag(CommonTags.RuntimeOSArchitecture, Environment.Is64BitOperatingSystem ? "x64" : "x86");
                _buildSpan.SetTag(CommonTags.RuntimeProcessArchitecture, Environment.Is64BitProcess ? "x64" : "x86");

                CIEnvironmentValues.DecorateSpan(_buildSpan);
            }
            catch (Exception ex)
            {
                Log.SafeLogError(ex, "Error in BuildStarted event");
            }
        }
        static Common()
        {
            TestTracer  = Tracer.Instance;
            ServiceName = TestTracer.DefaultServiceName;

            // Preload environment variables.
            CIEnvironmentValues.DecorateSpan(null);
        }
Example #5
0
        static Common()
        {
            var settings = TracerSettings.FromDefaultSources();

            settings.TraceBufferSize = 1024 * 1024 * 45; // slightly lower than the 50mb payload agent limit.

            TestTracer  = new Tracer(settings);
            ServiceName = TestTracer.DefaultServiceName;

            // Preload environment variables.
            CIEnvironmentValues.DecorateSpan(null);
        }
Example #6
0
 static DatadogLogger()
 {
     try
     {
         // Preload environment variables.
         CIEnvironmentValues.DecorateSpan(null);
     }
     catch (Exception ex)
     {
         Log.SafeLogError(ex, "Error initializing DatadogLogger type.");
     }
 }
Example #7
0
        private static void CheckCIEnvironmentValuesDecoration(MockTracerAgent.Span targetSpan)
        {
            var context = new SpanContext(null, null, null, null);
            var span    = new Span(context, DateTimeOffset.UtcNow);

            CIEnvironmentValues.DecorateSpan(span);

            AssertEqual(CommonTags.CIProvider);
            AssertEqual(CommonTags.CIPipelineId);
            AssertEqual(CommonTags.CIPipelineName);
            AssertEqual(CommonTags.CIPipelineNumber);
            AssertEqual(CommonTags.CIPipelineUrl);
            AssertEqual(CommonTags.CIJobUrl);
            AssertEqual(CommonTags.CIJobName);
            AssertEqual(CommonTags.StageName);
            AssertEqual(CommonTags.CIWorkspacePath);
            AssertEqual(CommonTags.GitRepository);
            AssertEqual(CommonTags.GitCommit);
            AssertEqual(CommonTags.GitBranch);
            AssertEqual(CommonTags.GitTag);
            AssertEqual(CommonTags.GitCommitAuthorName);
            AssertEqual(CommonTags.GitCommitAuthorEmail);
            AssertEqual(CommonTags.GitCommitAuthorDate);
            AssertEqual(CommonTags.GitCommitCommitterName);
            AssertEqual(CommonTags.GitCommitCommitterEmail);
            AssertEqual(CommonTags.GitCommitCommitterDate);
            AssertEqual(CommonTags.GitCommitMessage);
            AssertEqual(CommonTags.BuildSourceRoot);

            void AssertEqual(string key)
            {
                if (span.GetTag(key) is not null)
                {
                    Assert.Equal(span.GetTag(key), targetSpan.Tags[key]);
                    targetSpan.Tags.Remove(key);
                }
            }
        }
Example #8
0
        public static int Process(string jsonFilePath)
        {
            Console.WriteLine("Importing Crank json result file...");
            try
            {
                string jsonContent = File.ReadAllText(jsonFilePath);
                var    result      = JsonConvert.DeserializeObject <Models.ExecutionResult>(jsonContent);

                if (result?.JobResults?.Jobs?.Count > 0)
                {
                    var fileName = Path.GetFileName(jsonFilePath);

                    var tracerSettings = TracerSettings.FromDefaultSources();
                    if (string.IsNullOrEmpty(tracerSettings.ServiceName))
                    {
                        tracerSettings.ServiceName = "crank";
                    }

                    Tracer tracer = new Tracer(tracerSettings);

                    foreach (var jobItem in result.JobResults.Jobs)
                    {
                        var jobResult = jobItem.Value;
                        if (jobResult is null)
                        {
                            continue;
                        }

                        DateTimeOffset minTimeStamp = DateTimeOffset.UtcNow;
                        DateTimeOffset maxTimeStamp = minTimeStamp;
                        var            measurements = jobResult.Measurements?.SelectMany(i => i).ToList() ?? new List <Models.Measurement>();
                        if (measurements.Count > 0)
                        {
                            maxTimeStamp = measurements.Max(i => i.Timestamp).ToUniversalTime();
                            minTimeStamp = measurements.Min(i => i.Timestamp).ToUniversalTime();
                        }

                        var duration = (maxTimeStamp - minTimeStamp);

                        Span span = tracer.StartSpan("crank.test", startTime: minTimeStamp);

                        span.SetTraceSamplingPriority(SamplingPriority.AutoKeep);
                        span.Type         = SpanTypes.Test;
                        span.ResourceName = $"{fileName}/{jobItem.Key}";
                        CIEnvironmentValues.DecorateSpan(span);

                        span.SetTag(TestTags.Name, jobItem.Key);
                        span.SetTag(TestTags.Type, TestTags.TypeBenchmark);
                        span.SetTag(TestTags.Suite, fileName);
                        span.SetTag(TestTags.Framework, $"Crank");
                        span.SetTag(TestTags.Status, result.ReturnCode == 0 ? TestTags.StatusPass : TestTags.StatusFail);

                        if (result.JobResults.Properties?.Count > 0)
                        {
                            string scenario = string.Empty;
                            string profile  = string.Empty;
                            string arch     = string.Empty;
                            string testName = jobItem.Key;
                            foreach (var propItem in result.JobResults.Properties)
                            {
                                span.SetTag("test.properties." + propItem.Key, propItem.Value);

                                if (propItem.Key == "name")
                                {
                                    testName = propItem.Value + "." + jobItem.Key;
                                }
                                else if (propItem.Key == "scenario")
                                {
                                    scenario = propItem.Value;
                                }
                                else if (propItem.Key == "profile")
                                {
                                    profile = propItem.Value;
                                }
                                else if (propItem.Key == "arch")
                                {
                                    arch = propItem.Value;
                                }
                            }

                            string suite = fileName;
                            if (!string.IsNullOrEmpty(scenario))
                            {
                                suite = scenario;

                                if (!string.IsNullOrEmpty(profile))
                                {
                                    suite += "." + profile;
                                }

                                if (!string.IsNullOrEmpty(arch))
                                {
                                    suite += "." + arch;
                                }
                            }

                            span.SetTag(TestTags.Suite, suite);
                            span.SetTag(TestTags.Name, testName);
                            span.ResourceName = $"{suite}/{testName}";
                        }

                        try
                        {
                            if (jobResult.Results?.Count > 0)
                            {
                                foreach (var resultItem in jobResult.Results)
                                {
                                    if (string.IsNullOrEmpty(resultItem.Key))
                                    {
                                        continue;
                                    }

                                    if (resultItem.Value is string valueString)
                                    {
                                        span.SetTag("test.results." + resultItem.Key.Replace("/", ".").Replace("-", "_"), valueString);
                                    }
                                    else
                                    {
                                        NumberResultConverter numberConverter = default;

                                        bool converted = false;
                                        foreach (var converter in Converters)
                                        {
                                            if (converter.CanConvert(resultItem.Key))
                                            {
                                                converter.SetToSpan(span, "test.results." + resultItem.Key.Replace("/", ".").Replace("-", "_"), resultItem.Value);
                                                converted = true;
                                                break;
                                            }
                                        }

                                        if (!converted)
                                        {
                                            numberConverter.SetToSpan(span, "test.results." + resultItem.Key.Replace("/", ".").Replace("-", "_"), resultItem.Value);
                                        }
                                    }
                                }
                            }

                            if (jobResult.Environment?.Count > 0)
                            {
                                foreach (var envItem in jobResult.Environment)
                                {
                                    span.SetTag("environment." + envItem.Key, envItem.Value?.ToString() ?? "(null)");
                                }
                            }
                        }
                        finally
                        {
                            if (duration == TimeSpan.Zero)
                            {
                                span.Finish();
                            }
                            else
                            {
                                span.Finish(span.StartTime.Add(duration));
                            }
                        }
                    }

                    // Ensure all the spans gets flushed before we report the success.
                    // In some cases the process finishes without sending the traces in the buffer.
                    SynchronizationContext context = SynchronizationContext.Current;
                    try
                    {
                        SynchronizationContext.SetSynchronizationContext(null);
                        tracer.FlushAsync().GetAwaiter().GetResult();
                    }
                    finally
                    {
                        SynchronizationContext.SetSynchronizationContext(context);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(1);
            }

            Console.WriteLine("The result file was imported successfully.");
            return(0);
        }
 static XUnitIntegration()
 {
     // Preload environment variables.
     CIEnvironmentValues.DecorateSpan(null);
 }
Example #10
0
        /// <inheritdoc />
        public IEnumerable <string> ExportToFiles(Summary summary, ILogger consoleLogger)
        {
            DateTimeOffset startTime = DateTimeOffset.UtcNow;
            Exception      exception = null;

            try
            {
                Tracer tracer = new Tracer();

                foreach (var report in summary.Reports)
                {
                    Span   span = tracer.StartSpan("benchmarkdotnet.test", startTime: startTime);
                    double durationNanoseconds = 0;

                    span.SetTraceSamplingPriority(SamplingPriority.AutoKeep);
                    span.Type         = SpanTypes.Test;
                    span.ResourceName = $"{report.BenchmarkCase.Descriptor.Type.FullName}.{report.BenchmarkCase.Descriptor.WorkloadMethod.Name}";
                    CIEnvironmentValues.DecorateSpan(span);

                    span.SetTag(TestTags.Name, report.BenchmarkCase.Descriptor.WorkloadMethodDisplayInfo);
                    span.SetTag(TestTags.Type, TestTags.TypeBenchmark);
                    span.SetTag(TestTags.Suite, report.BenchmarkCase.Descriptor.Type.FullName);
                    span.SetTag(TestTags.Framework, $"BenchmarkDotNet {summary.HostEnvironmentInfo.BenchmarkDotNetVersion}");
                    span.SetTag(TestTags.Status, report.Success ? TestTags.StatusPass : TestTags.StatusFail);

                    if (summary.HostEnvironmentInfo != null)
                    {
                        span.SetTag("benchmark.host.processor.name", ProcessorBrandStringHelper.Prettify(summary.HostEnvironmentInfo.CpuInfo.Value));
                        span.SetMetric("benchmark.host.processor.physical_processor_count", summary.HostEnvironmentInfo.CpuInfo.Value.PhysicalProcessorCount);
                        span.SetMetric("benchmark.host.processor.physical_core_count", summary.HostEnvironmentInfo.CpuInfo.Value.PhysicalCoreCount);
                        span.SetMetric("benchmark.host.processor.logical_core_count", summary.HostEnvironmentInfo.CpuInfo.Value.LogicalCoreCount);
                        span.SetMetric("benchmark.host.processor.max_frequency_hertz", summary.HostEnvironmentInfo.CpuInfo.Value.MaxFrequency?.Hertz);
                        span.SetTag("benchmark.host.os_version", summary.HostEnvironmentInfo.OsVersion.Value);
                        span.SetTag("benchmark.host.runtime_version", summary.HostEnvironmentInfo.RuntimeVersion);
                        span.SetMetric("benchmark.host.chronometer.frequency_hertz", summary.HostEnvironmentInfo.ChronometerFrequency.Hertz);
                        span.SetMetric("benchmark.host.chronometer.resolution", summary.HostEnvironmentInfo.ChronometerResolution.Nanoseconds);
                    }

                    if (report.BenchmarkCase.Job != null)
                    {
                        var job = report.BenchmarkCase.Job;
                        span.SetTag("benchmark.job.description", job.DisplayInfo);

                        if (job.Environment != null)
                        {
                            var jobEnv = job.Environment;
                            span.SetTag("benchmark.job.environment.platform", jobEnv.Platform.ToString());

                            if (jobEnv.Runtime != null)
                            {
                                span.SetTag("benchmark.job.runtime.name", jobEnv.Runtime.Name);
                                span.SetTag("benchmark.job.runtime.moniker", jobEnv.Runtime.MsBuildMoniker);
                            }
                        }
                    }

                    if (report.ResultStatistics != null)
                    {
                        var stats = report.ResultStatistics;
                        span.SetMetric("benchmark.runs", stats.N);
                        span.SetMetric("benchmark.duration.mean", stats.Mean);

                        span.SetMetric("benchmark.statistics.n", stats.N);
                        span.SetMetric("benchmark.statistics.max", stats.Max);
                        span.SetMetric("benchmark.statistics.min", stats.Min);
                        span.SetMetric("benchmark.statistics.mean", stats.Mean);
                        span.SetMetric("benchmark.statistics.median", stats.Median);
                        span.SetMetric("benchmark.statistics.std_dev", stats.StandardDeviation);
                        span.SetMetric("benchmark.statistics.std_err", stats.StandardError);
                        span.SetMetric("benchmark.statistics.kurtosis", stats.Kurtosis);
                        span.SetMetric("benchmark.statistics.skewness", stats.Skewness);

                        if (stats.Percentiles != null)
                        {
                            span.SetMetric("benchmark.statistics.p90", stats.Percentiles.P90);
                            span.SetMetric("benchmark.statistics.p95", stats.Percentiles.P95);
                            span.SetMetric("benchmark.statistics.p99", stats.Percentiles.Percentile(99));
                        }

                        durationNanoseconds = stats.Mean;
                    }

                    if (report.Metrics != null)
                    {
                        foreach (var keyValue in report.Metrics)
                        {
                            if (keyValue.Value is null || keyValue.Value.Descriptor is null)
                            {
                                continue;
                            }

                            span.SetTag($"benchmark.metrics.{keyValue.Key}.displayName", keyValue.Value.Descriptor.DisplayName);
                            span.SetTag($"benchmark.metrics.{keyValue.Key}.legend", keyValue.Value.Descriptor.Legend);
                            span.SetTag($"benchmark.metrics.{keyValue.Key}.unit", keyValue.Value.Descriptor.Unit);
                            span.SetMetric($"benchmark.metrics.{keyValue.Key}.value", keyValue.Value.Value);
                        }
                    }

                    if (report.BenchmarkCase.Config?.HasMemoryDiagnoser() == true)
                    {
                        span.SetMetric("benchmark.memory.gen0Collections", report.GcStats.Gen0Collections);
                        span.SetMetric("benchmark.memory.gen1Collections", report.GcStats.Gen1Collections);
                        span.SetMetric("benchmark.memory.gen2Collections", report.GcStats.Gen2Collections);
                        span.SetMetric("benchmark.memory.total_operations", report.GcStats.TotalOperations);
                        span.SetMetric("benchmark.memory.mean_bytes_allocations", report.GcStats.BytesAllocatedPerOperation);
                        span.SetMetric("benchmark.memory.total_bytes_allocations", report.GcStats.GetTotalAllocatedBytes(false));
                    }

                    var duration = TimeSpan.FromTicks((long)(durationNanoseconds / TimeConstants.NanoSecondsPerTick));
                    span.Finish(startTime.Add(duration));
                }

                // Ensure all the spans gets flushed before we report the success.
                // In some cases the process finishes without sending the traces in the buffer.
                SynchronizationContext context = SynchronizationContext.Current;
                try
                {
                    SynchronizationContext.SetSynchronizationContext(null);
                    tracer.FlushAsync().GetAwaiter().GetResult();
                }
                finally
                {
                    SynchronizationContext.SetSynchronizationContext(context);
                }
            }
            catch (Exception ex)
            {
                exception = ex;
                consoleLogger.WriteLine(LogKind.Error, ex.ToString());
            }

            if (exception is null)
            {
                return(new string[] { "Datadog Exporter ran successfully." });
            }
            else
            {
                return(new string[] { "Datadog Exporter error: " + exception.ToString() });
            }
        }
Example #11
0
 static DatadogExporter()
 {
     // Preload environment variables.
     CIEnvironmentValues.DecorateSpan(null);
 }
Example #12
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);
        }
        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);
        }
        internal static Scope CreateScope(ITest currentTest, Type targetType)
        {
            MethodInfo testMethod = currentTest.Method.MethodInfo;

            object[]     testMethodArguments  = currentTest.Arguments;
            IPropertyBag testMethodProperties = currentTest.Properties;

            if (testMethod == null)
            {
                return(null);
            }

            string testFramework = "NUnit " + targetType?.Assembly?.GetName().Version;
            string testSuite     = testMethod.DeclaringType?.FullName;
            string testName      = testMethod.Name;
            string skipReason    = null;

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

            span.Type = SpanTypes.Test;
            span.SetTraceSamplingPriority(SamplingPriority.AutoKeep);
            span.ResourceName = $"{testSuite}.{testName}";
            span.SetTag(Tags.Origin, TestTags.CIAppTestOriginName);
            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
            ParameterInfo[] methodParameters = testMethod.GetParameters();
            if (methodParameters?.Length > 0)
            {
                TestParameters testParameters = new TestParameters();
                testParameters.Metadata  = new Dictionary <string, object>();
                testParameters.Arguments = new Dictionary <string, object>();
                testParameters.Metadata[TestTags.MetadataTestName] = currentTest.Name;

                for (int i = 0; i < methodParameters.Length; i++)
                {
                    if (testMethodArguments != null && 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
            if (testMethodProperties != null)
            {
                Dictionary <string, List <string> > traits = new Dictionary <string, List <string> >();
                skipReason = (string)testMethodProperties.Get("_SKIPREASON");
                foreach (var key in testMethodProperties.Keys)
                {
                    if (key == "_SKIPREASON" || key == "_JOINTYPE")
                    {
                        continue;
                    }

                    IList value = testMethodProperties[key];
                    if (value != null)
                    {
                        List <string> lstValues = new List <string>();
                        foreach (object valObj in value)
                        {
                            if (valObj is null)
                            {
                                continue;
                            }

                            lstValues.Add(valObj.ToString());
                        }

                        traits[key] = lstValues;
                    }
                    else
                    {
                        traits[key] = null;
                    }
                }

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

            if (skipReason != null)
            {
                FinishSkippedScope(scope, skipReason);
                scope = null;
            }

            span.ResetStartTime();
            return(scope);
        }
Example #15
0
 static Common()
 {
     // Preload environment variables.
     CIEnvironmentValues.DecorateSpan(null);
 }
Example #16
0
        internal static Scope CreateScope <TContext>(TContext executionContext, Type targetType)
            where TContext : ITestExecutionContext
        {
            ITest      currentTest = executionContext.CurrentTest;
            MethodInfo testMethod  = currentTest.Method.MethodInfo;

            object[]     testMethodArguments  = currentTest.Arguments;
            IPropertyBag testMethodProperties = currentTest.Properties;

            if (testMethod == null)
            {
                return(null);
            }

            string testFramework = "NUnit " + targetType?.Assembly?.GetName().Version;
            string testSuite     = testMethod.DeclaringType?.FullName;
            string testName      = testMethod.Name;
            string skipReason    = null;

            Tracer tracer = Tracer.Instance;
            Scope  scope  = tracer.StartActive("nunit.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
            ParameterInfo[] methodParameters = testMethod.GetParameters();
            if (methodParameters?.Length > 0)
            {
                for (int i = 0; i < methodParameters.Length; i++)
                {
                    if (testMethodArguments != null && 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
            if (testMethodProperties != null)
            {
                skipReason = (string)testMethodProperties.Get("_SKIPREASON");
                foreach (var key in testMethodProperties.Keys)
                {
                    if (key == "_SKIPREASON")
                    {
                        continue;
                    }

                    IList value = testMethodProperties[key];
                    if (value != null)
                    {
                        List <string> lstValues = new List <string>();
                        foreach (object valObj in value)
                        {
                            if (valObj is null)
                            {
                                continue;
                            }

                            lstValues.Add(valObj.ToString());
                        }

                        span.SetTag($"{TestTags.Traits}.{key}", string.Join(", ", lstValues));
                    }
                    else
                    {
                        span.SetTag($"{TestTags.Traits}.{key}", "(null)");
                    }
                }
            }

            if (skipReason != null)
            {
                span.SetTag(TestTags.Status, TestTags.StatusSkip);
                span.SetTag(TestTags.SkipReason, skipReason);
                span.Finish(TimeSpan.Zero);
                scope.Dispose();
                scope = null;
            }

            span.ResetStartTime();
            return(scope);
        }
        /// <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)
            where TTarget : ITestMethodRunner, IDuckType
        {
            if (!Tracer.Instance.Settings.IsIntegrationEnabled(IntegrationId))
            {
                return(CallTargetState.GetDefault());
            }

            ITestMethod testMethodInfo = instance.TestMethodInfo;
            MethodInfo  testMethod     = testMethodInfo.MethodInfo;

            object[] testMethodArguments = testMethodInfo.Arguments;

            string testFramework = "MSTestV2 " + instance.Type.Assembly.GetName().Version;
            string testSuite     = testMethodInfo.TestClassName;
            string testName      = testMethodInfo.TestMethodName;

            Tracer tracer = Tracer.Instance;
            Scope  scope  = tracer.StartActive("mstest.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
            ParameterInfo[] methodParameters = testMethod.GetParameters();
            if (methodParameters?.Length > 0)
            {
                for (int i = 0; i < methodParameters.Length; i++)
                {
                    if (testMethodArguments != null && 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> > testTraits = GetTraits(testMethod);

            if (testTraits != null)
            {
                foreach (KeyValuePair <string, List <string> > keyValuePair in testTraits)
                {
                    span.SetTag($"{TestTags.Traits}.{keyValuePair.Key}", string.Join(", ", keyValuePair.Value) ?? "(null)");
                }
            }

            span.ResetStartTime();
            return(new CallTargetState(scope));
        }
 static TestMethodRunnerExecuteIntegration()
 {
     // Preload environment variables.
     CIEnvironmentValues.DecorateSpan(null);
 }
        /// <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)
            where TTarget : ITestMethodRunner, IDuckType
        {
            if (!Common.TestTracer.Settings.IsIntegrationEnabled(IntegrationId))
            {
                return(CallTargetState.GetDefault());
            }

            ITestMethod testMethodInfo = instance.TestMethodInfo;
            MethodInfo  testMethod     = testMethodInfo.MethodInfo;

            object[] testMethodArguments = testMethodInfo.Arguments;

            string testFramework = "MSTestV2 " + instance.Type.Assembly.GetName().Version;
            string testSuite     = testMethodInfo.TestClassName;
            string testName      = testMethodInfo.TestMethodName;

            Scope scope = Common.TestTracer.StartActive("mstest.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
            ParameterInfo[] methodParameters = testMethod.GetParameters();
            if (methodParameters?.Length > 0)
            {
                TestParameters testParameters = new TestParameters();
                testParameters.Metadata  = new Dictionary <string, object>();
                testParameters.Arguments = new Dictionary <string, object>();

                for (int i = 0; i < methodParameters.Length; i++)
                {
                    if (testMethodArguments != null && 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> > testTraits = GetTraits(testMethod);

            if (testTraits != null && testTraits.Count > 0)
            {
                span.SetTag(TestTags.Traits, Datadog.Trace.Vendors.Newtonsoft.Json.JsonConvert.SerializeObject(testTraits));
            }

            span.ResetStartTime();
            return(new CallTargetState(scope));
        }
Example #20
0
        private static Scope CreateScope(object testExecutionContext, Type testMethodCommandType)
        {
            Scope scope = null;

            try
            {
                if (testExecutionContext.TryGetPropertyValue <object>("CurrentTest", out object currentTest))
                {
                    MethodInfo testMethod          = null;
                    object[]   testMethodArguments = null;
                    object     properties          = null;

                    if (currentTest != null)
                    {
                        if (currentTest.TryGetPropertyValue <object>("Method", out object method))
                        {
                            method?.TryGetPropertyValue <MethodInfo>("MethodInfo", out testMethod);
                        }

                        currentTest.TryGetPropertyValue <object[]>("Arguments", out testMethodArguments);
                        currentTest.TryGetPropertyValue <object>("Properties", out properties);
                    }

                    if (testMethod != null)
                    {
                        string testFramework = "NUnit " + testMethodCommandType.Assembly.GetName().Version;
                        string testSuite     = testMethod.DeclaringType?.FullName;
                        string testName      = testMethod.Name;
                        string skipReason    = null;
                        List <KeyValuePair <string, string> > testArguments = null;
                        List <KeyValuePair <string, string> > testTraits    = null;

                        // Get test parameters
                        ParameterInfo[] methodParameters = testMethod.GetParameters();
                        if (methodParameters?.Length > 0)
                        {
                            testArguments = new List <KeyValuePair <string, string> >();

                            for (int i = 0; i < methodParameters.Length; i++)
                            {
                                if (testMethodArguments != null && i < testMethodArguments.Length)
                                {
                                    testArguments.Add(new KeyValuePair <string, string>($"{TestTags.Arguments}.{methodParameters[i].Name}", testMethodArguments[i]?.ToString() ?? "(null)"));
                                }
                                else
                                {
                                    testArguments.Add(new KeyValuePair <string, string>($"{TestTags.Arguments}.{methodParameters[i].Name}", "(default)"));
                                }
                            }
                        }

                        // Get traits
                        if (properties != null)
                        {
                            properties.TryCallMethod <string, string>("Get", "_SKIPREASON", out skipReason);

                            if (properties.TryGetFieldValue <Dictionary <string, IList> >("inner", out Dictionary <string, IList> traits) && traits.Count > 0)
                            {
                                testTraits = new List <KeyValuePair <string, string> >();

                                foreach (KeyValuePair <string, IList> traitValue in traits)
                                {
                                    if (traitValue.Key == "_SKIPREASON")
                                    {
                                        continue;
                                    }

                                    IEnumerable <string> values = Enumerable.Empty <string>();
                                    if (traitValue.Value != null)
                                    {
                                        List <string> lstValues = new List <string>();
                                        foreach (object valObj in traitValue.Value)
                                        {
                                            if (valObj is null)
                                            {
                                                continue;
                                            }

                                            lstValues.Add(valObj.ToString());
                                        }

                                        values = lstValues;
                                    }

                                    testTraits.Add(new KeyValuePair <string, string>($"{TestTags.Traits}.{traitValue.Key}", string.Join(", ", values) ?? "(null)"));
                                }
                            }
                        }

                        Tracer tracer = Tracer.Instance;
                        scope = tracer.StartActive("nunit.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);

                        if (testArguments != null)
                        {
                            foreach (KeyValuePair <string, string> argument in testArguments)
                            {
                                span.SetTag(argument.Key, argument.Value);
                            }
                        }

                        if (testTraits != null)
                        {
                            foreach (KeyValuePair <string, string> trait in testTraits)
                            {
                                span.SetTag(trait.Key, trait.Value);
                            }
                        }

                        if (skipReason != null)
                        {
                            span.SetTag(TestTags.Status, TestTags.StatusSkip);
                            span.SetTag(TestTags.SkipReason, skipReason);
                            span.Finish(TimeSpan.Zero);
                            scope.Dispose();
                            scope = null;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Error creating or populating scope.");
            }

            return(scope);
        }