From e451849768234b43f9f4d6f2880617905c2c6638 Mon Sep 17 00:00:00 2001 From: Daniele Teti Date: Wed, 12 Apr 2023 17:49:30 +0200 Subject: [PATCH] Improved Prometheus Middleware --- .../Controllers.Articles.pas | 6 +-- .../MVCFramework.Middleware.Prometheus.pas | 53 ++++++++++++++++--- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/samples/prometheus/articles_crud_server_prometheus/Controllers.Articles.pas b/samples/prometheus/articles_crud_server_prometheus/Controllers.Articles.pas index 12441afa..ff42dd2e 100644 --- a/samples/prometheus/articles_crud_server_prometheus/Controllers.Articles.pas +++ b/samples/prometheus/articles_crud_server_prometheus/Controllers.Articles.pas @@ -70,7 +70,7 @@ procedure TArticlesController.CreateArticle(const Article: TArticle); begin GetArticlesService.Add(Article); Render201Created('/articles/' + Article.id.ToString, 'Article Created'); - Metrics.Inc('new_article'); + Metrics.IncCounterValue('new_article'); end; procedure TArticlesController.CreateArticles(const ArticleList: TObjectList); @@ -84,7 +84,7 @@ begin GetArticlesService.Add(lArticle); end; GetArticlesService.Commit; - Metrics.Inc('new_article', ArticleList.Count); + Metrics.IncCounterValue('new_article', ArticleList.Count); except GetArticlesService.Rollback; raise; @@ -130,7 +130,7 @@ begin lDict := ObjectDict().Add('data', GetArticlesService.GetArticles(Search)); end; Render(lDict); - Metrics.Inc('searches'); + Metrics.IncCounterValue('searches'); except on E: EServiceException do begin diff --git a/samples/prometheus/articles_crud_server_prometheus/MVCFramework.Middleware.Prometheus.pas b/samples/prometheus/articles_crud_server_prometheus/MVCFramework.Middleware.Prometheus.pas index 34f64893..5412b99e 100644 --- a/samples/prometheus/articles_crud_server_prometheus/MVCFramework.Middleware.Prometheus.pas +++ b/samples/prometheus/articles_crud_server_prometheus/MVCFramework.Middleware.Prometheus.pas @@ -24,12 +24,19 @@ type constructor Create(const PrometheusRoute: String = '/metrics'; const OnBeforeMetricsGet: TProc = nil); end; + {Simplified access to the most common Prometheus metrics} Metrics = class sealed public - class procedure Inc(const MetricName: string; const Amount: Double = 1); overload; - class procedure Inc(const MetricName: string; const Amount: Double; const Labels: TArray); overload; + // "Counter" metric + class procedure IncCounterValue(const MetricName: string; const Amount: Double = 1); overload; + class procedure IncCounterValue(const MetricName: string; const Amount: Double; const Labels: TArray); overload; + // "Gauge" metric class procedure SetGaugeValue(const MetricName: string; const Amount: Double = 1); overload; class procedure SetGaugeValue(const MetricName: string; const Amount: Double; const Labels: TArray); overload; + class procedure IncGaugeValue(const MetricName: string; const Amount: Double = 1); overload; + class procedure IncGaugeValue(const MetricName: string; const Amount: Double; const Labels: TArray); overload; + class procedure DecGaugeValue(const MetricName: string; const Amount: Double = -1); overload; + class procedure DecGaugeValue(const MetricName: string; const Amount: Double; const Labels: TArray); overload; end; @@ -61,7 +68,7 @@ end; procedure TMVCPrometheusMiddleware.OnAfterRouting(AContext: TWebContext; const AHandled: Boolean); begin - Metrics.Inc('http_requests_handled', 1, + Metrics.IncCounterValue('http_requests_handled', 1, [AContext.Request.PathInfo, IntToStr(AContext.Response.StatusCode)]); end; @@ -78,7 +85,7 @@ begin if not SameText(AContext.Request.PathInfo, fPrometheusRoute) then begin // Get the metric counter and increment it. - Metrics.Inc('http_requests_count'); + Metrics.IncCounterValue('http_requests_count'); end else begin @@ -101,7 +108,7 @@ end; { Metrics } -class procedure Metrics.Inc(const MetricName: string; const Amount: Double); +class procedure Metrics.IncCounterValue(const MetricName: string; const Amount: Double); begin TCollectorRegistry .DefaultRegistry @@ -109,7 +116,24 @@ begin .Inc(Amount); end; -class procedure Metrics.Inc(const MetricName: string; const Amount: Double; +class procedure Metrics.DecGaugeValue(const MetricName: string; + const Amount: Double; const Labels: TArray); +begin + TCollectorRegistry.DefaultRegistry + .GetCollector(MetricName) + .Labels(Labels) + .Dec(Amount); +end; + +class procedure Metrics.DecGaugeValue(const MetricName: string; + const Amount: Double); +begin + TCollectorRegistry.DefaultRegistry + .GetCollector(MetricName) + .Dec(Amount); +end; + +class procedure Metrics.IncCounterValue(const MetricName: string; const Amount: Double; const Labels: TArray); begin TCollectorRegistry @@ -119,6 +143,23 @@ begin .Inc(Amount); end; +class procedure Metrics.IncGaugeValue(const MetricName: string; + const Amount: Double); +begin + TCollectorRegistry.DefaultRegistry + .GetCollector(MetricName) + .Inc(Amount); +end; + +class procedure Metrics.IncGaugeValue(const MetricName: string; + const Amount: Double; const Labels: TArray); +begin + TCollectorRegistry.DefaultRegistry + .GetCollector(MetricName) + .Labels(Labels) + .Inc(Amount); +end; + class procedure Metrics.SetGaugeValue(const MetricName: string; const Amount: Double; const Labels: TArray); begin