31 lines
1.0 KiB
C#
31 lines
1.0 KiB
C#
using System.Diagnostics.Metrics;
|
|
|
|
namespace YarpGateway.Metrics;
|
|
|
|
public class GatewayMetrics
|
|
{
|
|
private readonly Counter<int> _requestsTotal;
|
|
private readonly Histogram<double> _requestDuration;
|
|
|
|
public GatewayMetrics(IMeterFactory meterFactory)
|
|
{
|
|
var meter = meterFactory.Create("fengling.gateway");
|
|
_requestsTotal = meter.CreateCounter<int>(
|
|
"gateway_requests_total",
|
|
"Total number of requests");
|
|
_requestDuration = meter.CreateHistogram<double>(
|
|
"gateway_request_duration_seconds",
|
|
"Request duration in seconds");
|
|
}
|
|
|
|
public void RecordRequest(string tenant, string service, int statusCode, double duration)
|
|
{
|
|
var tag = new KeyValuePair<string, object?>("tenant", tenant);
|
|
var tag2 = new KeyValuePair<string, object?>("service", service);
|
|
var tag3 = new KeyValuePair<string, object?>("status", statusCode.ToString());
|
|
|
|
_requestsTotal.Add(1, tag, tag2, tag3);
|
|
_requestDuration.Record(duration, tag, tag2);
|
|
}
|
|
}
|