Skip to content

Build your personal knowledge base with Trilium Notes

feat(server): unit tests for metrics endpoints

perf3ct3479f5016d95
diff --git a/_regroup/test-etapi/api-metrics.http b/_regroup/test-etapi/api-metrics.http
new file mode 100644
index 0000000000..78aee7217f
--- /dev/null
+++ b/_regroup/test-etapi/api-metrics.http
@@ -0,0 +1,43 @@
+### Test regular API metrics endpoint (requires session authentication)
+
+### Get metrics from regular API (default Prometheus format)
+GET {{triliumHost}}/api/metrics
+
+> {%
+client.test("API metrics endpoint returns Prometheus format by default", function() {
+ client.assert(response.status === 200, "Response status is not 200");
+ client.assert(response.headers["content-type"].includes("text/plain"), "Content-Type should be text/plain");
+ client.assert(response.body.includes("trilium_info"), "Should contain trilium_info metric");
+ client.assert(response.body.includes("trilium_notes_total"), "Should contain trilium_notes_total metric");
+ client.assert(response.body.includes("# HELP"), "Should contain HELP comments");
+ client.assert(response.body.includes("# TYPE"), "Should contain TYPE comments");
+});
+%}
+
+### Get metrics in JSON format
+GET {{triliumHost}}/api/metrics?format=json
+
+> {%
+client.test("API metrics endpoint returns JSON when requested", function() {
+ client.assert(response.status === 200, "Response status is not 200");
+ client.assert(response.headers["content-type"].includes("application/json"), "Content-Type should be application/json");
+ client.assert(response.body.version, "Version info not present");
+ client.assert(response.body.database, "Database info not present");
+ client.assert(response.body.timestamp, "Timestamp not present");
+ client.assert(typeof response.body.database.totalNotes === 'number', "Total notes should be a number");
+ client.assert(typeof response.body.database.activeNotes === 'number', "Active notes should be a number");
+ client.assert(response.body.noteTypes, "Note types breakdown not present");
+ client.assert(response.body.attachmentTypes, "Attachment types breakdown not present");
+ client.assert(response.body.statistics, "Statistics not present");
+});
+%}
+
+### Test invalid format parameter
+GET {{triliumHost}}/api/metrics?format=xml
+
+> {%
+client.test("Invalid format parameter returns error", function() {
+ client.assert(response.status === 500, "Response status should be 500");
+ client.assert(response.body.message.includes("prometheus"), "Error message should mention supported formats");
+});
+%}
\ No newline at end of file
diff --git a/_regroup/test-etapi/metrics.http b/_regroup/test-etapi/metrics.http
new file mode 100644
index 0000000000..24435f954f
--- /dev/null
+++ b/_regroup/test-etapi/metrics.http
@@ -0,0 +1,82 @@
+### Test ETAPI metrics endpoint
+
+# First login to get a token
+POST {{triliumHost}}/etapi/auth/login
+Content-Type: application/json
+
+{
+ "password": "{{password}}"
+}
+
+> {%
+client.test("Login successful", function() {
+ client.assert(response.status === 201, "Response status is not 201");
+ client.assert(response.body.authToken, "Auth token not present");
+ client.global.set("authToken", response.body.authToken);
+});
+%}
+
+### Get metrics with authentication (default Prometheus format)
+GET {{triliumHost}}/etapi/metrics
+Authorization: {{authToken}}
+
+> {%
+client.test("Metrics endpoint returns Prometheus format by default", function() {
+ client.assert(response.status === 200, "Response status is not 200");
+ client.assert(response.headers["content-type"].includes("text/plain"), "Content-Type should be text/plain");
+ client.assert(response.body.includes("trilium_info"), "Should contain trilium_info metric");
+ client.assert(response.body.includes("trilium_notes_total"), "Should contain trilium_notes_total metric");
+ client.assert(response.body.includes("# HELP"), "Should contain HELP comments");
+ client.assert(response.body.includes("# TYPE"), "Should contain TYPE comments");
+});
+%}
+
+### Get metrics in JSON format
+GET {{triliumHost}}/etapi/metrics?format=json
+Authorization: {{authToken}}
+
+> {%
+client.test("Metrics endpoint returns JSON when requested", function() {
+ client.assert(response.status === 200, "Response status is not 200");
+ client.assert(response.headers["content-type"].includes("application/json"), "Content-Type should be application/json");
+ client.assert(response.body.version, "Version info not present");
+ client.assert(response.body.database, "Database info not present");
+ client.assert(response.body.timestamp, "Timestamp not present");
+ client.assert(typeof response.body.database.totalNotes === 'number', "Total notes should be a number");
+ client.assert(typeof response.body.database.activeNotes === 'number', "Active notes should be a number");
+});
+%}
+
+### Get metrics in Prometheus format explicitly
+GET {{triliumHost}}/etapi/metrics?format=prometheus
+Authorization: {{authToken}}
+
+> {%
+client.test("Metrics endpoint returns Prometheus format when requested", function() {
+ client.assert(response.status === 200, "Response status is not 200");
+ client.assert(response.headers["content-type"].includes("text/plain"), "Content-Type should be text/plain");
+ client.assert(response.body.includes("trilium_info"), "Should contain trilium_info metric");
+ client.assert(response.body.includes("trilium_notes_total"), "Should contain trilium_notes_total metric");
+});
+%}
+
+### Test invalid format parameter
+GET {{triliumHost}}/etapi/metrics?format=xml
+Authorization: {{authToken}}
+
+> {%
+client.test("Invalid format parameter returns error", function() {
+ client.assert(response.status === 400, "Response status should be 400");
+ client.assert(response.body.code === "INVALID_FORMAT", "Error code should be INVALID_FORMAT");
+ client.assert(response.body.message.includes("prometheus"), "Error message should mention supported formats");
+});
+%}
+
+### Test without authentication (should fail)
+GET {{triliumHost}}/etapi/metrics
+
+> {%
+client.test("Metrics endpoint requires authentication", function() {
+ client.assert(response.status === 401, "Response status should be 401");
+});
+%}
\ No newline at end of file