Пример #1
0
        // Constructor
        public MetricBuilder(MetricSource source, string measurement = null)
        {
            // TODO: [TESTS] (MetricBuilder.MetricBuilder) Add tests
            if (string.IsNullOrWhiteSpace(measurement))
            {
                measurement = $"resolve:{source:G}";
            }

            Measurement  = measurement;
            UtcTimestamp = null;

            Tags = new Dictionary <string, string>
            {
                { CoreMetricTag.Source, source.ToString("G").LowerTrim() },
                { CoreMetricTag.Success, "true" },
                { CoreMetricTag.HasException, "false" },
                { CoreMetricTag.ExceptionName, string.Empty }
            };

            Fields = new Dictionary <string, object>
            {
                { CoreMetricField.Value, (long)0 },
                { CoreMetricField.CallCount, 1 },
                { CoreMetricField.UserId, 0 }
            };
        }
Пример #2
0
        public void GetReadings_HasDefaultTags()
        {
            var options = new MetricSourceOptions
            {
                DefaultTags =
                {
                    ["host"] = Environment.MachineName
                }
            };

            var source = new MetricSource(options);
            var metric = CreateMetric(source);

            UpdateMetric(metric);
            var readings = metric.GetReadings(DateTime.UtcNow).ToList();

            Assert.NotEmpty(readings);
            var reading = readings[0];

            Assert.Collection(
                reading.Tags,
                tag =>
            {
                var transformedKey   = options.TagNameTransformer("host");
                var transformedValue = options.TagValueTransformer(transformedKey, Environment.MachineName);

                Assert.Equal(transformedKey, tag.Key);
                Assert.Equal(transformedValue, tag.Value);
            });
        }
Пример #3
0
        public void Constructor_CanOverrideDefaultTags()
        {
            var options = new MetricSourceOptions();

            options.DefaultTags.Clear();
            options.DefaultTags[StringTag.Name] = "default_value";

            // create a metric and make sure the tags on its readings
            // do not have the default tag value
            var source = new MetricSource(options);
            var metric = CreateTaggedMetric(source, StringTag);

            UpdateMetric(metric, "value");
            var readings = metric.GetReadings(DateTime.UtcNow).ToList();

            Assert.NotEmpty(readings);
            var reading = readings[0];

            Assert.Collection(
                reading.Tags,
                tag =>
            {
                var transformedKey   = options.TagNameTransformer(StringTag.Name);
                var transformedValue = options.TagValueTransformer(transformedKey, "value");

                Assert.Equal(transformedKey, tag.Key);
                Assert.Equal(transformedValue, tag.Value);
            });
        }
Пример #4
0
        public void Constructor_FailedMetricNameValidation_Tagged()
        {
            var options = new MetricSourceOptions
            {
                MetricNameValidator = _ => false
            };

            var source = new MetricSource(options);

            Assert.Throws <ArgumentException>("name", () => CreateTaggedMetric(source, StringTag));
        }
Пример #5
0
        public void Constructor_FailedTagValueValidation()
        {
            var options = new MetricSourceOptions
            {
                TagValueValidator = _ => false
            };

            var source = new MetricSource(options);
            var metric = CreateTaggedMetric(source, StringTag);

            Assert.Throws <ArgumentException>("value", () => UpdateMetric(metric, "test"));
        }
Пример #6
0
        public virtual void GetReadings_Resets()
        {
            var options = new MetricSourceOptions();
            var source  = new MetricSource(options);
            var c       = CreateMetric(source);

            UpdateMetric(c);
            var readings = c.GetReadings(DateTime.UtcNow);

            Assert.NotEmpty(readings);
            readings = c.GetReadings(DateTime.UtcNow);
            Assert.Empty(readings);
        }
Пример #7
0
        private static void SetMeasurement(MetricsConfig config, MetricSource source, string builderType)
        {
            // TODO: [TESTS] (MetricService.SetMeasurement) Add tests
            var key = source.ToString("G");

            if (config.Measurements.ContainsKey(key))
            {
                return;
            }

            config.Measurements[key] = config.MeasurementTemplate
                                       .Replace("{type}", builderType);
        }
        public IQueryableSource Create(MetricSource source)
        {
            switch (source)
            {
                case MetricSource.PerformanceCounter:
                    return new PerformanceCounterSource(PerformanceCounterRegistry.Instance, new PerformanceCounterCategoryRepository());

                case MetricSource.WindowsManagementInstrumentation:
                    return new WindowsManagementInstrumentationSource();
            }

            throw new NotSupportedException("Unsupported metric source requested");
        }
Пример #9
0
        public IQueryableSource Create(MetricSource source)
        {
            switch (source)
            {
            case MetricSource.PerformanceCounter:
                return(new PerformanceCounterSource(PerformanceCounterRegistry.Instance, new PerformanceCounterCategoryRepository()));

            case MetricSource.WindowsManagementInstrumentation:
                return(new WindowsManagementInstrumentationSource());
            }

            throw new NotSupportedException("Unsupported metric source requested");
        }
Пример #10
0
        public virtual void GetMetadata_ReturnsData()
        {
            var options  = new MetricSourceOptions();
            var source   = new MetricSource(options);
            var metric   = CreateMetric(source);
            var metadata = metric.GetMetadata();

            Assert.Collection(
                metadata,
                metadata =>
            {
                Assert.Equal(metric.Name, metadata.Metric);
                Assert.Equal(MetadataNames.Rate, metadata.Name);
                switch (metric.MetricType)
                {
                case MetricType.Counter:
                case MetricType.CumulativeCounter:
                    Assert.Equal("counter", metadata.Value);
                    break;

                case MetricType.Gauge:
                    Assert.Equal("gauge", metadata.Value);
                    break;
                }
            },
                metadata =>
            {
                Assert.Equal(metric.Name, metadata.Metric);
                Assert.Equal(MetadataNames.Description, metadata.Name);
                Assert.Equal(metric.Description, metadata.Value);
            },
                metadata =>
            {
                Assert.Equal(metric.Name, metadata.Metric);
                Assert.Equal(MetadataNames.Unit, metadata.Name);
                Assert.Equal(metric.Unit, metadata.Value);
            }
                );
        }
Пример #11
0
 protected override SnapshotCounter <string> CreateTaggedMetric(MetricSource source, in MetricTag <string> tag) => source.AddSnapshotCounter(Snapshot, "test", "units", "desc", tag);
Пример #12
0
 protected override SnapshotCounter CreateMetric(MetricSource source) => source.AddSnapshotCounter(Snapshot, "test", "units", "desc");
Пример #13
0
 protected override EventGauge CreateMetric(MetricSource source) => source.AddEventGauge("test", "units", "desc");
Пример #14
0
 protected override EventGauge <string> CreateTaggedMetric(MetricSource source, in MetricTag <string> tag) => source.AddEventGauge("test", "units", "desc", tag);
Пример #15
0
 protected override Counter CreateMetric(MetricSource source) => source.AddCounter("test", "units", "desc");
Пример #16
0
 protected override CumulativeCounter <string> CreateTaggedMetric(MetricSource source, in MetricTag <string> tag) => source.AddCumulativeCounter("test", "units", "desc", tag);
Пример #17
0
 protected override SamplingGauge CreateMetric(MetricSource source) => source.AddSamplingGauge("test", "units", "desc");
Пример #18
0
 protected abstract TMetric CreateMetric(MetricSource source);
Пример #19
0
 private bool WrtLog(MetricSource src, string ruleName, string source, int metricValue, int maxValue)
 {
     ++logCounter;
     return(WriteDetail(string.Format("({4}) - {0} is exceed by [{1}]. Value/Limit is {2}/{3}", ruleName, source, metricValue, maxValue, src)));
 }
Пример #20
0
 protected override AggregateGauge CreateMetric(MetricSource source) => source.AddAggregateGauge("test", "units", "desc");
Пример #21
0
 protected abstract TTaggedMetric CreateTaggedMetric(MetricSource source, in MetricTag <string> tag);