Elasticsearch

The App.Metrics.Reporting.Elasticsearch nuget package reports metrics to ElasticSearch using the App.Metrics.Formatters.Elasticsearch nuget package to format metrics as Elasticsearch documents.

Getting started

To use the Elasticsearch reporter, first install the nuget package:

nuget install App.Metrics.Reporting.Elasticsearch

Then enable the reporter using Report.ToElasticsearch(...):

var metrics = new MetricsBuilder()
    .Report.ToElasticsearch("http://127.0.0.1:9200", "metricsindex")
    .Build();

App Metrics at the moment leaves report scheduling up the the user unless using App.Metrics.AspNetCore.Reporting. To run all configured reports use the ReportRunner on IMetricsRoot:

await metrics.ReportRunner.RunAllAsync();

Report Scheduling will be added when Microsoft.Extensions.Hosting.Background is released, otherwise there are various other scheduling solutions available for C#. For ASP.NET Core reporting see details on the App.Metrics.AspNetCore.Reporting support package.

Configuration

Configuration options are provided as a setup action used with ToElasticsearch(...).

To configure the Elasticsearch reporters options:

var filter = new MetricsFilter().WhereType(MetricType.Timer);
var metrics = new MetricsBuilder()
    .Report.OverHttp(
        options => {
            options.Elasticsearch.Index = "metricsindex";
            options.Elasticsearch.BaseUri = new Uri("http://127.0.0.1:9200");
            options.Elasticsearch.AuthorizationSchema = ElasticSearchAuthorizationSchemes.Basic;
            options.Elasticsearch.BearerToken = "token";
            options.Elasticsearch.UserName = "admin";
            options.Elasticsearch.Password = "password";
            options.HttpPolicy.BackoffPeriod = TimeSpan.FromSeconds(30);
            options.HttpPolicy.FailuresBeforeBackoff = 5;
            options.HttpPolicy.Timeout = TimeSpan.FromSeconds(10);
            options.MetricsOutputFormatter = new MetricsJsonOutputFormatter();
            options.Filter = filter;
            options.FlushInterval = TimeSpan.FromSeconds(20);
        })
    .Build();

Configuration options provided are:

Option Description
MetricsOutputFormatter The formatter used when reporting metrics to Elasticsearch.
Filter The filter used to filter metrics just for this reporter.
FlushInterval The delay between reporting metrics to Elasticsearch.
Elasticsearch.Index The Elasticsearch index where metrics are reported.
Elasticsearch.BaseUri The URI of the Elasticsearch server.
Elasticsearch.AuthorizationSchema The authentication scheme used to authenticate with Elasticsearch, Anonymous, Basic or BearerToken.
Elasticsearch.BearerToken The password when using OAuth to auth with Elasticsearch.
Elasticsearch.UserName The username when using basic auth to auth with Elasticsearch.
Elasticsearch.Password The password when using basic auth to auth with Elasticsearch.
HttpPolicy.BackoffPeriod The TimeSpan to back-off when metrics are failing to report to the metrics ingress endpoint.
HttpPolicy.FailuresBeforeBackoff The number of failures before backing-off when metrics are failing to report to the metrics ingress endpoint.
HttpPolicy.Timeout The HTTP timeout duration when attempting to report metrics to the metrics ingress endpoint.

Web Monitoring

Grafana

The App.Metrics.AspNetCore.Tracking nuget package automatically records typical web metrics when added to an ASP.NET Core application. App Metrics includes a couple of pre-built Grafana dashboards to visualize these metrics:

  1. Add an index and the document mappings to Elasticsearch using the default mappings provided in the github repository
  2. Install the App Metrics Grafana dashboard. There is also an OAuth2 dashboard for APIs using OAuth2.