Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

App.Metrics with InfluxDB2 for ASP.NET 7 Web API Plug-And-Play.

NotificationsYou must be signed in to change notification settings

JoaoFranco91/JF91.AppMetrics.InfluxDB2.Net7

Repository files navigation

I've created this package so you can easily integrate App.Metrics into you ASP WEB API to send collected metrics to an InfluxDB2 Server.

The setup is very straightforward, just install the Nuget Package, add the config section to your appsettings.json and inject the services into your program.cs.

Please follow the instructions carefully.

Install Nuget:

dotnet add package JF91.AppMetricsInfluxDB2 --version 1.2.0
"MetricsOptions": {    "DefaultContextLabel": "MyCustomContext",    "Enabled": true},    "MetricsWebTrackingOptions": {    "ApdexTrackingEnabled": true,    "ApdexTSeconds": 0.1,    "IgnoredHttpStatusCodes": [ 404 ],    "IgnoredRoutesRegexPatterns": [],    "OAuth2TrackingEnabled": true},    "MetricEndpointsOptions": {    "MetricsEndpointEnabled": true,    "MetricsTextEndpointEnabled": true,    "PingEndpointEnabled": true,    "EnvironmentInfoEndpointEnabled": true},    "MetricsReportingInfluxDb2Options": {    "InfluxDb2": {    "BaseUri": "http://127.0.0.1:8086",    "Organization": "metrics",    "Bucket": "metrics",    "Token": "changeme"},    "HttpPolicy": {    "BackoffPeriod": "0:0:30",    "FailuresBeforeBackoff": 5,    "Timeout": "0:0:40"},    "ReportInterval": "0:0:1"}

2 - Add this Environment Variable to yourlaunchSettings.json:

"APPLICATION_NAME": "MyKickassApi"

3 - Add this beforebuilder.Build():

builder.Services.AddMetricsServices();builder.WebHost.AddInfluxDb2AppMetrics(builder.Configuration);

4 - Add this afterbuilder.Build():

app.UseRequestsCounterMiddleware();app.UseRequestsDurationMiddleware();app.UseResponsesSizeMiddleware();app.UseResponsesApdexMiddleware();app.UseMetricsAllMiddleware();app.UseMetricsAllEndpoints();

5 - Add this beforeapp.Run():

app.UseHttpStatusCodesCounterMiddleware();

How to add a new metrics middleware:

1 - Create an interceptor middleware;

2 - InjectIMetrics;

public class MyNewMetricMiddleware{    private readonly RequestDelegate _next;    private readonly IMetrics _metrics;    public MyNewMetricMiddleware(RequestDelegate next, IMetrics metrics)    {        _next = next;        _metrics = metrics;    }    public async Task InvokeAsync(HttpContext context)    {        try        {            await _next(context);            var tags = new MetricTags            (                new[] { "Tag1-Key", "Tag2-Key" },                new[] { "Tag1-Value, "Tag2-Value }            );            _metrics.Measure.Counter.Increment            (                new CounterOptions                {                    Name = "my-new-metric",                    Context = "my-new-context",                    MeasurementUnit = Unit.Calls,                    Tags = tags                }            );        }        catch (Exception ex)        {        }    }}

3 - Add middleware to app pipeline:

builder.UseMiddleware<MyNewMetricMiddleware>();

How to add a new metrics in controller:

1 - InjectIMetrics into controller:

public WeatherForecastController(ILogger<WeatherForecastController> logger, IMetrics metrics){    _logger = logger;    _metrics = metrics;}

2 - UseIMetrics:

[HttpGet(Name = "GetWeatherForecast")]public IEnumerable<WeatherForecast> Get(){    var tags = new MetricTags    (        new[] { "Tag1-Key", "Tag2-Key" },        new[] { "Tag1-Value, "Tag2-Value }    );        _metrics.Measure.Counter.Increment    (        new CounterOptions        {            Name = "my-new-metric",            Context = "my-new-context",            MeasurementUnit = Unit.Calls,            Tags = tags        }    );    return Enumerable.Range(1, 5).Select        (            index => new WeatherForecast            {                Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),                TemperatureC = Random.Shared.Next(-20, 55),                Summary = Summaries[Random.Shared.Next(Summaries.Length)]            }        )        .ToArray();}

About

App.Metrics with InfluxDB2 for ASP.NET 7 Web API Plug-And-Play.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp