public void TestDefaultMetricsSourceExternalMetricsSubscription() { const string evalId = "evalId"; const string taskId = "taskId"; const string sourceContext = "test"; const string recordName = "testrecord"; const string subscribedCounterName = "subscribedcounter"; const long subscribedCounterValue = 1001; DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector( GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName)) .GetInstance <DefaultMetricsSourceImpl>(); IMetricsFactory factory = TangFactory.GetTang().NewInjector().GetInstance <IMetricsFactory>(); MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl(); var extraCounter = factory.CreateCounter(subscribedCounterName, subscribedCounterName, subscribedCounterValue); extraCounter.Subscribe(metricsSource); metricsSource.GetMetrics(collector, true); var rb = collector.CurrentRecordBuilder; rb.Validate(subscribedCounterName, subscribedCounterValue); extraCounter.Increment(); extraCounter.OnCompleted(); // This should remove the counter from the metrics source. metricsSource.GetMetrics(collector, true); rb = collector.CurrentRecordBuilder; Assert.False(rb.LongKeyPresent(subscribedCounterName), "The counter should not be present now in the source"); }
public void TestCounterMetrics() { const string name = "countertest"; const string desc = "countertestdesc"; IMetricsFactory factory = TangFactory.GetTang().NewInjector().GetInstance <IMetricsFactory>(); var longCounter = factory.CreateCounter(name, desc, 5); Assert.Equal(name, longCounter.Info.Name); Assert.Equal(desc, longCounter.Info.Description); MetricTestUtils.RecordBuilderForTests recordBuilder = new MetricTestUtils.RecordBuilderForTests(); SnapshotRequest request = new SnapshotRequest(recordBuilder, false); longCounter.OnNext(request); recordBuilder.Validate(name, 5); recordBuilder.Reset(); longCounter.Increment(); longCounter.OnNext(request); recordBuilder.Validate(name, 6); recordBuilder.Reset(); longCounter.OnNext(request); Assert.False(recordBuilder.LongKeyPresent(name), "Metric is not supposed to be recorded."); request = new SnapshotRequest(recordBuilder, true); longCounter.OnNext(request); recordBuilder.Validate(name, 6); recordBuilder.Reset(); }
private DefaultMetricsSourceImpl( [Parameter(typeof(DefaultMetricsSourceParameters.ContextOrTaskName))] string contextOrTaskName, [Parameter(typeof(DefaultMetricsSourceParameters.EvaluatorId))] string evaluatorId, [Parameter(typeof(DefaultMetricsSourceParameters.SourceContext))] string sourceContext, [Parameter(typeof(DefaultMetricsSourceParameters.RecordName))] string recordName, IMetricsFactory metricsFactory) { _contextOrTaskName = contextOrTaskName; _evaluatorId = evaluatorId; _sourceContext = sourceContext; _recordName = recordName; _metricsFactory = metricsFactory; _counters = new MutableMetricContainer<ICounter>( (name, desc) => metricsFactory.CreateCounter(name, desc), this); _longGauges = new MutableMetricContainer<ILongGauge>( (name, desc) => metricsFactory.CreateLongGauge(name, desc), this); _doubleGauges = new MutableMetricContainer<IDoubleGauge>( (name, desc) => metricsFactory.CreateDoubleGauge(name, desc), this); _rates = new MutableMetricContainer<IRate>( (name, desc) => metricsFactory.CreateRateMetric(name, desc), this); }
private DefaultMetricsSourceImpl( [Parameter(typeof(DefaultMetricsSourceParameters.ContextOrTaskName))] string contextOrTaskName, [Parameter(typeof(DefaultMetricsSourceParameters.EvaluatorId))] string evaluatorId, [Parameter(typeof(DefaultMetricsSourceParameters.SourceContext))] string sourceContext, [Parameter(typeof(DefaultMetricsSourceParameters.RecordName))] string recordName, IMetricsFactory metricsFactory) { _contextOrTaskName = contextOrTaskName; _evaluatorId = evaluatorId; _sourceContext = sourceContext; _recordName = recordName; _metricsFactory = metricsFactory; _counters = new MutableMetricContainer <ICounter>( (name, desc) => metricsFactory.CreateCounter(name, desc), this); _longGauges = new MutableMetricContainer <ILongGauge>( (name, desc) => metricsFactory.CreateLongGauge(name, desc), this); _doubleGauges = new MutableMetricContainer <IDoubleGauge>( (name, desc) => metricsFactory.CreateDoubleGauge(name, desc), this); _rates = new MutableMetricContainer <IRate>( (name, desc) => metricsFactory.CreateRateMetric(name, desc), this); }
public ServiceWithMetrics(IMetricsFactory <ServiceWithMetrics> metricFactory, IServiceInDifferentNamespace serviceInDifferentNamespace) { _serviceInDifferentNamespace = serviceInDifferentNamespace; // Resulting label in Prometheus: metricsexample_emtpy_string_total _counter = metricFactory?.CreateCounter("empty_string_total", "Counts the total number of recieved empty strings."); // Resulting label in Prometheus: metricsexample_fancy_number _summary = metricFactory?.CreateSummary("fancy_number", "Shows the distribution of fancy numbers."); }
private void CreateMetrics(IMetricsFactory factory) { foreach (PropertyInfo property in typeof(MetricsImpl).GetProperties()) { Type metricType = property.PropertyType; if (!typeof(ICounter).IsAssignableFrom(metricType) && !typeof(ITimer).IsAssignableFrom(metricType) && !typeof(IGauge).IsAssignableFrom(metricType)) { // Some frameworks dynamically add code that this reflection will pick up // I only want this classes Stats based fields to be picked up. continue; } StringBuilder metricBuilder = new StringBuilder("jaeger:"); Dictionary <string, string> tags = new Dictionary <string, string>(); var metricAttributes = property.GetCustomAttributes <MetricAttribute>(); foreach (MetricAttribute metricAttribute in metricAttributes) { metricBuilder.Append(metricAttribute.Name); foreach (var tag in metricAttribute.Tags) { tags[tag.Key] = tag.Value; } } string metricName = metricBuilder.ToString(); if (metricType == typeof(ICounter)) { property.SetValue(this, factory.CreateCounter(metricName, tags)); } else if (metricType == typeof(IGauge)) { property.SetValue(this, factory.CreateGauge(metricName, tags)); } else if (metricType == typeof(ITimer)) { property.SetValue(this, factory.CreateTimer(metricName, tags)); } else { throw new NotSupportedException($"'{metricType}' for metric '{property.Name}' is not supported."); } } }
public PrometheusDelegatingHandler(IMetricsFactory <PrometheusDelegatingHandler> metricsFactory) { _requestTotal = metricsFactory.CreateCounter("request_total", "number of external request", false, "host", "status"); _requestLatency = metricsFactory.CreateSummary("request_latency", "request duration in ms", new[] { "host" }); }
public BackgroundTaskQueue(IMetricsFactory <BackgroundTaskQueue <T> >?metricsFactory) { _elementsInQueue = metricsFactory?.CreateGauge("task_queue_enqueued_elements", "", false, "type") ?.WithLabels(typeof(T).Name); _totalMessages = metricsFactory?.CreateCounter("total_messages", "", false, "type")?.WithLabels(typeof(T).Name); }
public ServiceInDifferentNamespace(IMetricsFactory <ServiceInDifferentNamespace>?metricsFactory) { // Resulting label in Prometheus: metricsexample_differentnamespace_counter_in_different_namespace _counter = metricsFactory?.CreateCounter("counter_in_different_namespace", "This counts something else."); }
public void TestDefaultMetricsSourceHetergeneousFunctionalities() { const string evalId = "evalId"; const string taskId = "taskId"; const string sourceContext = "test"; const string recordName = "testrecord"; string[] counterNames = { "cname1", "cname2" }; string[] longGaugeNames = { "lname1", "lname2" }; string[] doubleGaugeNames = { "dname1", "dname2" }; const string rateName = "rname1"; double[] samples = { 2, 3 }; string[] tagNames = { "tname1", "tname2" }; string[] tagValues = { "tvalue1", "tValue2" }; const long counterIncr = 2; const long longGaugeIncr = 3; const double doubleGaugeDecr = 1.2; const string subscribedCounterName = "subscribedcounter"; const long subscribedCounterValue = 1001; DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector( GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName)) .GetInstance <DefaultMetricsSourceImpl>(); IMetricsFactory factory = TangFactory.GetTang().NewInjector().GetInstance <IMetricsFactory>(); MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl(); // Create Counters foreach (var name in counterNames) { metricsSource.Counters.Create(name, name); } // Create long gauge foreach (var name in longGaugeNames) { metricsSource.LongGauges.Create(name, name); } // Create double gauge foreach (var name in doubleGaugeNames) { metricsSource.DoubleGauges.Create(name, name); } // Create rate. metricsSource.Rates.Create(rateName, rateName); // Update counters. int counter = 1; foreach (var name in counterNames) { metricsSource.Counters[name].Increment(counter * counterIncr); counter++; } // Update long gauge counter = 1; foreach (var name in longGaugeNames) { metricsSource.LongGauges[name].Increment(counter * longGaugeIncr); counter++; } // Update double gauge counter = 1; foreach (var name in doubleGaugeNames) { metricsSource.DoubleGauges[name].Decrement(counter * doubleGaugeDecr); counter++; } // Update rate foreach (var sample in samples) { metricsSource.Rates[rateName].Sample(sample); } // Create tags for (int i = 0; i < tagNames.Length; i++) { metricsSource.AddTag(tagNames[i], tagNames[i], tagValues[i]); } var extraCounter = factory.CreateCounter(subscribedCounterName, subscribedCounterName, subscribedCounterValue); extraCounter.Subscribe(metricsSource); metricsSource.GetMetrics(collector, true); var rb = collector.CurrentRecordBuilder; // Validate counters counter = 1; foreach (var name in counterNames) { rb.Validate(name, counter * counterIncr); counter++; } rb.Validate(subscribedCounterName, subscribedCounterValue); // Validate long gauges counter = 1; foreach (var name in longGaugeNames) { rb.Validate(name, counter * longGaugeIncr); counter++; } // Validate double gauges counter = 1; foreach (var name in doubleGaugeNames) { rb.Validate(name, -counter * doubleGaugeDecr, 1e-10); counter++; } // Validate tags for (int i = 0; i < tagNames.Length; i++) { rb.Validate(tagNames[i], tagValues[i]); } // Validate rate rb.Validate(rateName + "-Num", samples.Length); rb.Validate(rateName + "-RunningAvg", samples.Sum() / samples.Length, 1e-10); }
public PrometheusDelegatingMessageHandler( IMetricsFactory <PrometheusDelegatingMessageHandler <TInput> > metricsFactory) { _messageProcessingTotal = metricsFactory.CreateCounter("message_processing_total", "Message processing status total", false, "status"); }