![]() |
VOOZH | about |
dotnet add package JF91.AppMetrics.InfluxDb2WithPrometheus --version 1.0.5
NuGet\Install-Package JF91.AppMetrics.InfluxDb2WithPrometheus -Version 1.0.5
<PackageReference Include="JF91.AppMetrics.InfluxDb2WithPrometheus" Version="1.0.5" />
<PackageVersion Include="JF91.AppMetrics.InfluxDb2WithPrometheus" Version="1.0.5" />Directory.Packages.props
<PackageReference Include="JF91.AppMetrics.InfluxDb2WithPrometheus" />Project file
paket add JF91.AppMetrics.InfluxDb2WithPrometheus --version 1.0.5
#r "nuget: JF91.AppMetrics.InfluxDb2WithPrometheus, 1.0.5"
#:package JF91.AppMetrics.InfluxDb2WithPrometheus@1.0.5
#addin nuget:?package=JF91.AppMetrics.InfluxDb2WithPrometheus&version=1.0.5Install as a Cake Addin
#tool nuget:?package=JF91.AppMetrics.InfluxDb2WithPrometheus&version=1.0.5Install as a Cake Tool
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 with Prometheus Text Formatter (/metrics-text).
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.
dotnet add package JF91.AppMetrics.InfluxDb2WithPrometheus
<br>
appsettings.json and modify it to your needs:"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"
}
<br>
launchSettings.json:"APPLICATION_NAME": "MyKickassApi"
<br>
builder.Build():builder.Services.AddMetricsServices();
builder.WebHost
.AddInfluxDb2AppMetrics(builder.Configuration)
.AddPrometheusAppMetrics();
builder.Services.AddMetricsServices();
builder.WebHost
.AddInfluxDb2AppMetrics(builder.Configuration, options =>
{
options.BaseUri = new Uri(@"http://www.test.com");
options.Organization = "Test";
options.Bucket = "Test";
options.Token = "Test";
})
.AddPrometheusAppMetrics();
<br>
builder.Build():app.UseRequestsCounterMiddleware();
app.UseRequestsDurationMiddleware();
app.UseResponsesSizeMiddleware();
app.UseResponsesApdexMiddleware();
app.UseMetricsAllMiddleware();
app.UseMetricsAllEndpoints();
<br>
app.Run():app.UseHttpStatusCodesCounterMiddleware();
1 - Create an interceptor middleware;
2 - Inject 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)
{
}
}
}
<br>
3 - Add middleware to app pipeline:
builder.UseMiddleware<MyNewMetricMiddleware>();
1 - Inject IMetrics into controller:
public WeatherForecastController(ILogger<WeatherForecastController> logger, IMetrics metrics)
{
_logger = logger;
_metrics = metrics;
}
<br>
2 - Use IMetrics:
[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();
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net7.0 net7.0 is compatible. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
Version 1.0.5:
# Added HTTP Status Code to HTTP Counter Metrics
Version 1.0.4:
# Added Context Trace Identifier to 'request_id' Metrics Tag
Version 1.0.3:
# Added delegate to AddInfluxDb2Appmetrics providing ability to customize InfluxDB options