From 41ad594229bdcc149740e0307bac9e0a15ed57f6 Mon Sep 17 00:00:00 2001 From: Etienne Donneger Date: Mon, 26 Aug 2024 14:06:10 +0000 Subject: [PATCH] Change Prometheus `/metrics` response from JSON to text (#68) --- index.ts | 4 ++-- src/types/zod.gen.ts | 9 +++++++-- src/typespec/openapi3.tsp | 2 +- static/@openapi-to-graphql/graphql/schema.graphql | 2 +- static/@typespec/openapi3/openapi.json | 13 +++++++++++-- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/index.ts b/index.ts index 1824214..91979d8 100644 --- a/index.ts +++ b/index.ts @@ -72,7 +72,7 @@ async function AntelopeTokenAPI() { app.get( "/metrics", - async (ctx: Context) => ctx.json, 200>(await prometheus.registry.getMetricsAsJSON()) + async (ctx: Context) => new Response(await prometheus.registry.metrics()) ); // -------------------------- @@ -133,7 +133,7 @@ async function AntelopeTokenAPI() { return response.success ? "OK" : `[500] bad_database_response: ${response.error.message}`; }, openapi: () => openapi, - metrics: async () => await prometheus.registry.getMetricsAsJSON(), + metrics: async () => await prometheus.registry.metrics(), version: () => APP_VERSION } ); diff --git a/src/types/zod.gen.ts b/src/types/zod.gen.ts index 8a52176..985f833 100644 --- a/src/types/zod.gen.ts +++ b/src/types/zod.gen.ts @@ -78,12 +78,17 @@ export type MonitoringHealthQueryResponseSchema = z.infer; +/** + * @description An unexpected error response. + */ +export const monitoringMetricsErrorSchema = z.lazy(() => apiErrorSchema); +export type MonitoringMetricsErrorSchema = z.infer; /** * @description Metrics as text. */ -export const monitoringMetricsQueryResponseSchema = z.object({}); +export const monitoringMetricsQueryResponseSchema = z.coerce.string(); export type MonitoringMetricsQueryResponseSchema = z.infer; /** diff --git a/src/typespec/openapi3.tsp b/src/typespec/openapi3.tsp index 9133b53..2c7a88c 100644 --- a/src/typespec/openapi3.tsp +++ b/src/typespec/openapi3.tsp @@ -242,5 +242,5 @@ interface Monitoring { @summary("Prometheus metrics") @route("/metrics") @get - metrics(): Record; + metrics(): APIResponse; } diff --git a/static/@openapi-to-graphql/graphql/schema.graphql b/static/@openapi-to-graphql/graphql/schema.graphql index d544624..3add36c 100644 --- a/static/@openapi-to-graphql/graphql/schema.graphql +++ b/static/@openapi-to-graphql/graphql/schema.graphql @@ -32,7 +32,7 @@ type Query { Equivalent to GET /metrics """ - metrics: JSON + metrics: String """ Reflection endpoint to return OpenAPI JSON spec. Also used by Swagger to generate the frontpage. diff --git a/static/@typespec/openapi3/openapi.json b/static/@typespec/openapi3/openapi.json index 93d72ab..a931736 100644 --- a/static/@typespec/openapi3/openapi.json +++ b/static/@typespec/openapi3/openapi.json @@ -151,8 +151,17 @@ "content": { "application/json": { "schema": { - "type": "object", - "additionalProperties": {} + "type": "string" + } + } + } + }, + "default": { + "description": "An unexpected error response.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/APIError" } } }