// 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 } }; }
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); }); }
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); }); }
public void Constructor_FailedMetricNameValidation_Tagged() { var options = new MetricSourceOptions { MetricNameValidator = _ => false }; var source = new MetricSource(options); Assert.Throws <ArgumentException>("name", () => CreateTaggedMetric(source, StringTag)); }
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")); }
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); }
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"); }
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"); }
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); } ); }
protected override SnapshotCounter <string> CreateTaggedMetric(MetricSource source, in MetricTag <string> tag) => source.AddSnapshotCounter(Snapshot, "test", "units", "desc", tag);
protected override SnapshotCounter CreateMetric(MetricSource source) => source.AddSnapshotCounter(Snapshot, "test", "units", "desc");
protected override EventGauge CreateMetric(MetricSource source) => source.AddEventGauge("test", "units", "desc");
protected override EventGauge <string> CreateTaggedMetric(MetricSource source, in MetricTag <string> tag) => source.AddEventGauge("test", "units", "desc", tag);
protected override Counter CreateMetric(MetricSource source) => source.AddCounter("test", "units", "desc");
protected override CumulativeCounter <string> CreateTaggedMetric(MetricSource source, in MetricTag <string> tag) => source.AddCumulativeCounter("test", "units", "desc", tag);
protected override SamplingGauge CreateMetric(MetricSource source) => source.AddSamplingGauge("test", "units", "desc");
protected abstract TMetric CreateMetric(MetricSource source);
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))); }
protected override AggregateGauge CreateMetric(MetricSource source) => source.AddAggregateGauge("test", "units", "desc");
protected abstract TTaggedMetric CreateTaggedMetric(MetricSource source, in MetricTag <string> tag);