JF91.AppMetrics.InfluxDB2.Net7
1.0.0
Saya telah membuat paket ini sehingga Anda dapat dengan mudah mengintegrasikan App.Metrics ke dalam ASP WEB API Anda untuk mengirim metrik yang dikumpulkan ke Server InfluxDB2.
Penyiapannya sangat mudah, cukup instal Paket Nuget, tambahkan bagian konfigurasi ke appsettings.json Anda dan masukkan layanan ke program.cs Anda.
Harap ikuti instruksi dengan cermat.
Instal Nuget:
dotnet add package JF91.AppMetricsInfluxDB2 --version 1.2.0
appsettings.json
Anda dan ubah sesuai kebutuhan Anda: "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"
}
launchSettings.json
Anda : "APPLICATION_NAME": "MyKickassApi"
builder.Build()
: builder.Services.AddMetricsServices();
builder.WebHost.AddInfluxDb2AppMetrics(builder.Configuration);
builder.Build()
: app.UseRequestsCounterMiddleware();
app.UseRequestsDurationMiddleware();
app.UseResponsesSizeMiddleware();
app.UseResponsesApdexMiddleware();
app.UseMetricsAllMiddleware();
app.UseMetricsAllEndpoints();
app.Run()
: app.UseHttpStatusCodesCounterMiddleware();
1 - Buat middleware pencegat;
2 - Suntikkan IMetrics
;
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 - Tambahkan middleware ke saluran aplikasi:
builder.UseMiddleware();
1 - Suntikkan IMetrics
ke pengontrol:
public WeatherForecastController(ILogger logger, IMetrics metrics)
{
_logger = logger;
_metrics = metrics;
}
2 - Gunakan IMetrics
:
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable 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();
}