Example #1
0
        public void Transform(ImmutableCpuSample sample)
        {
            try
            {
                var cpuUserTime        = GetCpuUserTime(sample.CurrentUserProcessorTime, sample.LastUserProcessorTime);
                var cpuUserUtilization = GetCpuUserUtilization(cpuUserTime, sample.CurrentSampleTime, sample.LastSampleTime, sample.ProcessorCount);

                var unscopedCpuUserTimeMetric = MetricBuilder.TryBuildCpuUserTimeMetric(cpuUserTime);
                RecordMetric(unscopedCpuUserTimeMetric);

                var unscopedCpuUserUtilizationMetric = MetricBuilder.TryBuildCpuUserUtilizationMetric(cpuUserUtilization);
                RecordMetric(unscopedCpuUserUtilizationMetric);
            }
            catch (Exception ex)
            {
                Log.Debug("No CPU metrics will be reported: " + ex);
            }
        }
        public void TransformSample_CreatesUnscopedMetrics()
        {
            var expectedCpuTimeMetric        = _metricBuilder.TryBuildCpuUserTimeMetric(TimeSpan.FromSeconds(1));
            var expectedCpuUtilizationMetric = _metricBuilder.TryBuildCpuUserUtilizationMetric(0.5f);

            //var generatedMetrics = new List<MetricWireModel>();
            Mock.Arrange(() => _metricBuilder.TryBuildCpuUserTimeMetric(Arg.IsAny <TimeSpan>())).Returns(expectedCpuTimeMetric);
            Mock.Arrange(() => _metricBuilder.TryBuildCpuUserUtilizationMetric(Arg.IsAny <float>())).Returns(expectedCpuUtilizationMetric);
            //Mock.Arrange(() => _metricAggregator.Collect(Arg.IsAny<MetricWireModel>())).DoInstead<MetricWireModel>(metric => generatedMetrics.Add(metric));

            var sample = new ImmutableCpuSample(1, DateTime.UtcNow, TimeSpan.FromSeconds(1), DateTime.UtcNow, TimeSpan.FromSeconds(1));

            _cpuSampleTransformer.Transform(sample);

            Mock.Assert(() => _metricBuilder.TryBuildCpuUserTimeMetric(TimeSpan.FromSeconds(1)));
            Mock.Assert(() => _metricBuilder.TryBuildCpuUserUtilizationMetric(0.5f));
            //Assert.IsTrue(generatedMetrics.Contains(expectedCpuTimeMetric));
            //Assert.IsTrue(generatedMetrics.Contains(expectedCpuUtilizationMetric));
        }