Exemplo n.º 1
0
        public async Task TestBaselineAsync()
        {
            var epoch        = new DateTimeOffset(1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);
            var stringWriter = new StringWriter();

            var formatter = new LineProtocolFormatter <DateTimeOffset>(t => (t - epoch).Ticks * 100);
            var processor = new TextWriterProcessor <string>(stringWriter);

            var metricProvider = new MetricProvider <DateTimeOffset, string>(
                () => DateTimeOffset.Parse("2016-06-13T17:43:50.1004002Z"),
                formatter.Format,
                formatter.Format,
                processor.Process);

            var temperatureMetric = metricProvider.CreateInt64Metric("weather", "temperature", new[] { "location" });

            temperatureMetric.Update(82, new[] { "us-midwest" });

            while (metricProvider.metricQueue.Count != 0)
            {
                await Task.Delay(50);
            }

            Assert.Equal(
                "weather,location=us-midwest temperature=82 1465839830100400200",
                stringWriter.ToString().Trim());
        }
Exemplo n.º 2
0
 void LoadMetrics()
 {
     // Get code metrics (all files from the cache!)
     if (_metrics == null)
     {
         var metricProvider = new MetricProvider(Project.ProjectBase, Project.Cache, Project.GetNormalizedFileExtensions());
         _metrics = metricProvider.QueryCodeMetrics();
     }
 }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Parser.Default.ParseArguments <Options>(args)
            .WithParsed <Options>(o => {
                MetricServer server = null;
                // Start Metric server which serves metric endpoint at specified port
                try {
                    // We need this for Docker, else the metricserver is not reachable on the exposed port.
                    // I assume this way it serves on 0.0.0.0:PORT and thus listens on all interfaces.
                    server = new MetricServer(o.metricServerPort);
                    server.Start();
                } catch (HttpListenerException e) {
                    server = new MetricServer("localhost", o.metricServerPort);
                    server.Start();
                }

                // Create Metric Providers from the given metric_definition and store them.
                CreateMetricProviders(o.metricDefinitionFile);
                while (true)
                {
                    // Iterate over Metric Structs and gather data.
                    List <MetricStruct <Gauge> > gaugeStructs = mPrometheusMetricManager.GetGauges();
                    foreach (MetricStruct <Gauge> metricStruct in gaugeStructs)
                    {
                        MetricProvider metricProvider            = metricStruct.getMetricProvider();
                        Task <object> endpoint_task              = metricProvider.GetValue();
                        SortedDictionary <string, string> labels = metricProvider.GetLabels();
                        Gauge gauge = metricStruct.getPrometheusMetric();

                        endpoint_task.Wait();
                        Console.WriteLine("{0} result: {1}", metricProvider.GetMetricName(), endpoint_task.Result.ToString());
                        object result = endpoint_task.Result;

                        if (labels != null)
                        {
                            gauge.WithLabels(labels.Values.ToArray()).Set((double)endpoint_task.Result);
                        }
                        else
                        {
                            gauge.Set((double)endpoint_task.Result);
                        }
                    }
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            });
        }
Exemplo n.º 4
0
        public void UpdateCache(Progress progress, bool includeContributions)
        {
            progress.Message("Updating source control history.");

            // Note: You should have the latest code locally such that history and metrics match!
            // Update svn history
            var svnProvider = Project.CreateProvider();

            svnProvider.UpdateCache(progress, includeContributions);

            progress.Message("Updating code metrics.");

            // Update code metrics
            var metricProvider = new MetricProvider(Project.ProjectBase, Project.Cache, Project.GetNormalizedFileExtensions());

            metricProvider.UpdateCache();
        }
Exemplo n.º 5
0
        static void CreateMetricProviders(string metricDefinitionFile)
        {
            // Read metric definition and create array of MetricDefinition objects.
            string definition = File.ReadAllText(metricDefinitionFile);

            MetricDefinition[] metricDefinitions = MetricDefinition.FromJson(definition);

            foreach (MetricDefinition def in metricDefinitions)
            {
                string service_name = def.ServiceName;
                string url          = def.Url;
                foreach (Metric metric in def.Metrics)
                {
                    string                            api_endpoint            = metric.ApiEndpoint != null ? url + metric.ApiEndpoint : null;
                    string                            metric_name             = def.ServiceName + "_" + metric.MetricName;
                    string                            reponse_body_identifier = metric.DesiredResponseField;
                    AuthCredentials                   auth_credentials        = def.AuthCredentials;
                    Dictionary <string, double>       string_value_mapping    = metric.StringValueMapping;
                    SortedDictionary <string, string> labels        = metric.Labels;
                    ResponseType                      responseType  = metric.ResponseType;
                    ExecutionType                     executionType = metric.ExecutionType;

                    // Create MetricEndpoint which executes API calls or executes a program/script
                    MetricProvider metricEndpoint = null;
                    if (executionType == ExecutionType.SCRIPT && metric.Program != null && metric.Argument != null)
                    {
                        metricEndpoint = new MetricProvider(metric_name, metric.Program, metric.Argument, labels, executionType);
                    }
                    else if (executionType == ExecutionType.API_CALL && api_endpoint != null)
                    {
                        metricEndpoint = new MetricProvider(api_endpoint, metric_name, reponse_body_identifier, auth_credentials, string_value_mapping, labels, responseType, executionType);
                    }

                    // Create Prometheus Gauges
                    GaugeConfiguration config = new GaugeConfiguration();
                    if (labels != null)
                    {
                        config.LabelNames = labels.Keys.ToArray();
                    }
                    Gauge metricGauge = Metrics.CreateGauge(name: metric_name, help: metric_name, config);
                    mPrometheusMetricManager.AddMetricStruct(metricEndpoint, metricGauge);
                }
            }
        }
Exemplo n.º 6
0
        public Metric GetMetric(string name)
        {
            Metric metric;

            if (metrics.TryGetValue(name, out metric))
            {
                return(metric);
            }

            metric = MetricProvider.FetchFirstMatching("Name = ?", name);
            if (metric == null)
            {
                metric = new Metric()
                {
                    Name = name
                };
                MetricProvider.Save(metric);
            }

            metrics[name] = metric;
            return(metric);
        }
Exemplo n.º 7
0
 public void AddMetricStruct(MetricProvider metricProvider, object prometheusMetric)
 {
     if (prometheusMetric is Gauge)
     {
         mGauges.Add(new MetricStruct <Gauge>(metricProvider, (Gauge)prometheusMetric));
     }
     else if (prometheusMetric is Summary)
     {
         mSummaries.Add(new MetricStruct <Summary>(metricProvider, (Summary)prometheusMetric));
     }
     else if (prometheusMetric is Histogram)
     {
         mHistograms.Add(new MetricStruct <Histogram>(metricProvider, (Histogram)prometheusMetric));
     }
     else if (prometheusMetric is Counter)
     {
         mCounters.Add(new MetricStruct <Counter>(metricProvider, (Counter)prometheusMetric));
     }
     else
     {
         throw new InvalidOperationException("Type " + prometheusMetric.GetType().ToString() + " is not supported");
     }
 }
Exemplo n.º 8
0
 public MetricStruct(MetricProvider metricProvider, T prometheusMetric)
 {
     mMetricProvider   = metricProvider;
     mPrometheusMetric = prometheusMetric;
 }