Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Timestamps and minor refactor #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,15 @@ uses
Prometheus.Collectors.Histogram;

begin
LHistogram := THistogram.Create('Name of histogram metric', 'Help text for histogram metric');
// If buckets argument is not supplied, the default values will be used:
// [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10, INFINITE].
LHistogram.Observe(0.01);
LHistogram.Observe(0.04);
LHistogram.Observe(1);
lHistogram := TCollectorRegistry.DefaultRegistry.GetCollector<THistogram>('Name of histogram metric');
if not Assigned(lHistogram) then begin
lHistogram := THistogram.Create('Name of histogram metric', 'Help text for histogram metric');
// if Buckets argument is not supplied, the default values will be used
//[0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10, INFINITE]
lHistogram.Register;
end;

lHistogram.Observe(lRequestTimeDec);
end.
```

Expand Down Expand Up @@ -197,4 +200,3 @@ You can find official **Prometheus Client middlewares** into these separate repo

+ [Prometheus Official Page](https://prometheus.io)
+ [Using Delphi with Prometheus and Grafana (in Italian language)](https://www.youtube.com/watch?v=-bPDl6MP6jo)

32 changes: 16 additions & 16 deletions Samples/Starter-DMVC/WebModules.App.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ interface

type

{ TAppWebModule }

TAppWebModule = class(TWebModule)
procedure WebModuleCreate(Sender: TObject);
procedure WebModuleDestroy(Sender: TObject);
Expand All @@ -28,13 +26,13 @@ implementation

uses
MVCFramework.Commons,
MVCFramework.Middleware.Metrics,
Prometheus.Collectors.Counter,
Prometheus.Collectors.Histogram,
Prometheus.Registry,
Controllers.Demo;

{ TAppWebModule }

procedure TAppWebModule.WebModuleCreate(Sender: TObject);
begin
// Creates the Delphi MVC Framework server application engine.
Expand All @@ -43,17 +41,18 @@ procedure TAppWebModule.WebModuleCreate(Sender: TObject);
// Add a sample controller.
FEngine.AddController(TDemoController);

// Configure some sample metrics...
// Add the metrics middleware! It will export all values using the
// default endpoint '/metrics' but you can change it as shown below:
FEngine.AddMiddleware(GetMetricsMiddleware('/metrics'));

// Configure some sample metrics...
// ... a simple counter
TCounter.Create('http_requests_count', 'Received HTTP request count').Register();

// ... A request time histogram with two labels for path and status
THistogram.Create(
'request_duration_seconds', 'Time taken to process request- in seconds',
[0.05, 0.1, 0.25, 0.5, 1, 2, 10], ['path', 'status'])
.Register();

// .. A request time histogram with no labels and an increasing bucket sequence
THistogram.Create('response_length', 'Number of bytes sent in response', 10, 3, 10, []).Register();

Expand All @@ -63,19 +62,19 @@ procedure TAppWebModule.WebModuleCreate(Sender: TObject);
var
LNextValue: Double;
const
StartValue = 10;
ValueCount = 5;
StepValue = 5;
Start = 10;
Count = 5;
Step = 5;
begin
SetLength(Result, ValueCount);
LNextValue := StartValue;
for var LIndex := 0 to ValueCount - 1 do
SetLength(result, Count);
LNextValue := Start;
for var I := 0 to Count - 1 do
begin
Result[LIndex] := LNextValue;
LNextValue := LNextValue + StepValue;
Result[I] := lNextValue;
lNextValue := lNextValue + Step;
end;
end,
[])
[])
.Register();

FEngine.SetExceptionHandler(
Expand All @@ -90,6 +89,7 @@ procedure TAppWebModule.WebModuleCreate(Sender: TObject);
.Labels([WebContext.Request.PathInfo, WebContext.Response.StatusCode.ToString])
.Observe(AssumedDuration);
end);

end;

procedure TAppWebModule.WebModuleDestroy(Sender: TObject);
Expand Down
31 changes: 29 additions & 2 deletions Source/Prometheus.Collectors.Counter.pas
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ interface
Prometheus.Samples,
Prometheus.SimpleCollector;

const
SECS_TO_MILLISECS = 1000;

type

{ TCounterChild }
Expand Down Expand Up @@ -51,10 +54,19 @@ TCounterChild = class
/// </remarks>
TCounter = class (TSimpleCollector<TCounterChild>)
strict private
FLock: TObject;
function GetValue: Double;
strict protected
function CreateChild: TCounterChild; override;
public
/// <summary>
/// Creates a new instance of this counter collector.
/// </summary>
constructor Create(const AName: string; const AHelp: string = ''; const ALabelNames: TLabelNames = []); override;
/// <summary>
/// Performs object cleanup releasing all the owned instances.
/// </summary>
destructor Destroy; override;
/// <summary>
/// Collects all the metrics and the samples from this collector.
/// </summary>
Expand All @@ -76,6 +88,7 @@ TCounter = class (TSimpleCollector<TCounterChild>)
implementation

uses
System.DateUtils,
System.SysUtils,
Prometheus.Resources;

Expand Down Expand Up @@ -108,9 +121,22 @@ procedure TCounterChild.Inc(const AAmount: Double);

{ TCounter }

constructor TCounter.Create(const AName: string; const AHelp: string = ''; const ALabelNames: TLabelNames = []);
begin
inherited Create(AName, AHelp, ALabelNames);
FLock := TObject.Create;
end;

destructor TCounter.Destroy;
begin
if Assigned(FLock) then
FreeAndNil(FLock);
inherited Destroy;
end;

function TCounter.Collect: TArray<TMetricSamples>;
begin
TMonitor.Enter(Lock);
TMonitor.Enter(FLock);
try
SetLength(Result, 1);
var LMetric := PMetricSamples(@Result[0]);
Expand All @@ -126,12 +152,13 @@ function TCounter.Collect: TArray<TMetricSamples>;
LSample^.MetricName := Self.Name;
LSample^.LabelNames := Self.LabelNames;
LSample^.LabelValues := ALabelValues;
LSample^.TimeStamp := DateTimeToUnix(TTimeZone.Local.ToUniversalTime(Now)) * SECS_TO_MILLISECS;
LSample^.Value := AChild.Value;
System.Inc(LIndex);
end
);
finally
TMonitor.Exit(Lock);
TMonitor.Exit(FLock);
end;
end;

Expand Down
32 changes: 30 additions & 2 deletions Source/Prometheus.Collectors.Gauge.pas
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ interface
Prometheus.Samples,
Prometheus.SimpleCollector;

const
SECS_TO_MILLISECS = 1000;

type

{ TGaugeChild }
Expand Down Expand Up @@ -68,10 +71,20 @@ TGaugeChild = class
/// </remarks>
TGauge = class (TSimpleCollector<TGaugeChild>)
strict private
FLock: TObject;
function GetValue: Double;
strict protected
function CreateChild: TGaugeChild; override;
public
/// <summary>
/// Creates a new instance of this gauge collector.
/// </summary>
constructor Create(const AName: string; const AHelp: string = '';
const ALabelNames: TLabelNames = []); override;
/// <summary>
/// Performs object cleanup releasing all the owned instances.
/// </summary>
destructor Destroy; override;
/// <summary>
/// Collects all the metrics and the samples from this collector.
/// </summary>
Expand Down Expand Up @@ -189,9 +202,23 @@ procedure TGaugeChild.SetToCurrentTime;

{ TGauge }

constructor TGauge.Create(const AName, AHelp: string;
const ALabelNames: TLabelNames);
begin
inherited Create(AName, AHelp, ALabelNames);
FLock := TObject.Create;
end;

destructor TGauge.Destroy;
begin
if Assigned(FLock) then
FreeAndNil(FLock);
inherited Destroy;
end;

function TGauge.Collect: TArray<TMetricSamples>;
begin
TMonitor.Enter(Lock);
TMonitor.Enter(FLock);
try
SetLength(Result, 1);
var LMetric := PMetricSamples(@Result[0]);
Expand All @@ -207,12 +234,13 @@ function TGauge.Collect: TArray<TMetricSamples>;
LSample^.MetricName := Self.Name;
LSample^.LabelNames := Self.LabelNames;
LSample^.LabelValues := ALabelValues;
LSample^.TimeStamp := DateTimeToUnix(TTimeZone.Local.ToUniversalTime(Now)) * SECS_TO_MILLISECS;
LSample^.Value := AChild.Value;
System.Inc(LIndex);
end
);
finally
TMonitor.Exit(Lock);
TMonitor.Exit(FLock);
end;
end;

Expand Down
Loading