-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #175 from hamed-shirbandi/feat-174-add-metrics-usi…
…ng-prometheus Add metrics using prometheus
- Loading branch information
Showing
42 changed files
with
257 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/1-BuildingBlocks/Web.MVC/Configuration/Metric/MetricConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Prometheus; | ||
|
||
namespace TaskoMask.BuildingBlocks.Web.MVC.Configuration.Metric | ||
{ | ||
public static class MetricConfiguration | ||
{ | ||
public static void AddMetrics(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
var metricOptions = configuration.GetSection("Metric").Get<MetricOptions>(); | ||
|
||
//It starts the metrics exporter as a background service using a stand alone kestrel | ||
if (metricOptions.StandAloneKestrelServerEnabled) | ||
{ | ||
services.AddMetricServer(options => | ||
{ | ||
options.Port = metricOptions.Port; | ||
options.Url = metricOptions.Url; | ||
options.Hostname = metricOptions.Hostname; | ||
}); | ||
} | ||
|
||
//Inject IMetricFactory to be used in application objects instead of coupling their implementation with Metrics | ||
services.AddSingleton<IMetricFactory>(Metrics.DefaultFactory); | ||
} | ||
|
||
public static void UseMetrics(this IApplicationBuilder app, IConfiguration configuration) | ||
{ | ||
var metricOptions = configuration.GetSection("Metric").Get<MetricOptions>(); | ||
|
||
//If kestrel server is not enabled then use current app server | ||
if (!metricOptions.StandAloneKestrelServerEnabled) | ||
app.UseMetricServer(metricOptions.Port, metricOptions.Url); | ||
|
||
if (metricOptions.HttpMetricsEnabled) | ||
{ | ||
app.UseHttpMetrics(options => | ||
{ | ||
options.AddCustomLabel("host", context => context.Request.Host.Host); | ||
}); | ||
} | ||
|
||
if (metricOptions.SuppressDefaultMetrics) | ||
Metrics.SuppressDefaultMetrics(); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/1-BuildingBlocks/Web.MVC/Configuration/Metric/MetricOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace TaskoMask.BuildingBlocks.Web.MVC.Configuration.Metric | ||
{ | ||
public class MetricOptions | ||
{ | ||
/// <summary> | ||
/// Set it to true if you want to run the metrics on a stand alone kestrel server | ||
/// </summary> | ||
public bool StandAloneKestrelServerEnabled { get; set; } | ||
public ushort Port { get; set; } | ||
public string Url { get; set; } | ||
|
||
/// <summary> | ||
/// It is used when StandAloneKestrelServerEnabled is true. | ||
/// Will listen for requests using this hostname. "+" indicates listen on all hostnames. | ||
/// By setting this to "localhost", you can easily prevent access from remote systems.- | ||
/// </summary> | ||
public string Hostname { get; set; } | ||
|
||
/// <summary> | ||
/// Set it to true if you want to use the built-in metrics for http requests. | ||
/// The metrics are: | ||
/// Number of HTTP requests in progress. | ||
/// Total number of received HTTP requests. | ||
/// Duration of HTTP requests. | ||
/// </summary> | ||
public bool HttpMetricsEnabled { get; set; } | ||
|
||
|
||
/// <summary> | ||
/// The library enables various default metrics and integrations by default. | ||
/// If these default metrics are not desirable, set SuppressDefaultMetrics to true. | ||
/// </summary> | ||
public bool SuppressDefaultMetrics { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.