@@ -7,11 +7,14 @@ import { logger } from "@rabbit-company/web-middleware/logger";
77import { cors } from "@rabbit-company/web-middleware/cors" ;
88import pkg from "../package.json" ;
99import { openapi } from "./openapi" ;
10+ import { httpRequests , registry } from "./metrics" ;
11+ import { bearerAuth } from "@rabbit-company/web-middleware/bearer-auth" ;
1012
1113const host = process . env . SERVER_HOST || "0.0.0.0" ;
1214const port = parseInt ( process . env . SERVER_PORT || "3000" ) || 3000 ;
1315const proxy = Object . keys ( IP_EXTRACTION_PRESETS ) . includes ( process . env . PROXY || "direct" ) ? ( process . env . PROXY as CloudProvider ) : "direct" ;
1416const updateInterval = parseInt ( process . env . UPDATE_INTERVAL || "30" ) || 30 ;
17+ const openMetricsEnabled = process . env . OPEN_METRICS_ENABLED === "true" ;
1518
1619const cacheControl = [
1720 "public" ,
@@ -50,6 +53,8 @@ app.use(
5053) ;
5154
5255app . get ( "/" , ( c ) => {
56+ httpRequests . labels ( { endpoint : "/" } ) . inc ( ) ;
57+
5358 return c . json (
5459 {
5560 program : "RabbitForexAPI" ,
@@ -73,11 +78,37 @@ app.get("/", (c) => {
7378 ) ;
7479} ) ;
7580
81+ if ( openMetricsEnabled ) {
82+ app . get (
83+ "/metrics" ,
84+ bearerAuth ( {
85+ validate ( token ) {
86+ return token === process . env . OPEN_METRICS_AUTH_TOKEN ;
87+ } ,
88+ skip ( ) {
89+ return process . env . OPEN_METRICS_AUTH_TOKEN === "none" ;
90+ } ,
91+ } ) ,
92+ ( c ) => {
93+ httpRequests . labels ( { endpoint : "/metrics" } ) . inc ( ) ;
94+
95+ return c . text ( registry . metricsText ( ) , 200 , {
96+ "Content-Type" : registry . contentType ,
97+ "Cache-Control" : "no-store" ,
98+ } ) ;
99+ }
100+ ) ;
101+ }
102+
76103app . get ( "/openapi.json" , ( c ) => {
104+ httpRequests . labels ( { endpoint : "/openapi.json" } ) . inc ( ) ;
105+
77106 return c . json ( openapi , 200 , { "Cache-Control" : "public, max-age=3600 s-maxage=3600 stale-while-revalidate=36000 stale-if-error=31536000" } ) ;
78107} ) ;
79108
80109app . get ( "/v1/assets" , ( c ) => {
110+ httpRequests . labels ( { endpoint : "/v1/assets" } ) . inc ( ) ;
111+
81112 return c . json (
82113 {
83114 currencies : exchange . getSupportedCurrencies ( ) ,
@@ -97,6 +128,8 @@ app.get("/v1/assets", (c) => {
97128} ) ;
98129
99130app . get ( "/v1/rates" , ( c ) => {
131+ httpRequests . labels ( { endpoint : "/v1/rates" } ) . inc ( ) ;
132+
100133 return c . json (
101134 {
102135 base : "USD" ,
@@ -112,6 +145,9 @@ app.get("/v1/rates", (c) => {
112145
113146app . get ( "/v1/rates/:base" , ( c ) => {
114147 const base = c . params [ "base" ] ! . toUpperCase ( ) ;
148+
149+ httpRequests . labels ( { endpoint : "/v1/rates/:base" } ) . inc ( ) ;
150+
115151 return c . json (
116152 {
117153 base : base ,
@@ -126,6 +162,8 @@ app.get("/v1/rates/:base", (c) => {
126162} ) ;
127163
128164app . get ( "/v1/metals/rates" , ( c ) => {
165+ httpRequests . labels ( { endpoint : "/v1/metals/rates" } ) . inc ( ) ;
166+
129167 return c . json (
130168 {
131169 base : "USD" ,
@@ -143,6 +181,8 @@ app.get("/v1/metals/rates", (c) => {
143181app . get ( "/v1/metals/rates/:base" , ( c ) => {
144182 const base = c . params [ "base" ] ! . toUpperCase ( ) ;
145183
184+ httpRequests . labels ( { endpoint : "/v1/metals/rates/:base" } ) . inc ( ) ;
185+
146186 return c . json (
147187 {
148188 base : base ,
@@ -158,6 +198,8 @@ app.get("/v1/metals/rates/:base", (c) => {
158198} ) ;
159199
160200app . get ( "/v1/crypto/rates" , ( c ) => {
201+ httpRequests . labels ( { endpoint : "/v1/crypto/rates" } ) . inc ( ) ;
202+
161203 return c . json (
162204 {
163205 base : "USD" ,
@@ -175,6 +217,8 @@ app.get("/v1/crypto/rates", (c) => {
175217app . get ( "/v1/crypto/rates/:base" , ( c ) => {
176218 const base = c . params [ "base" ] ! . toUpperCase ( ) ;
177219
220+ httpRequests . labels ( { endpoint : "/v1/crypto/rates/:base" } ) . inc ( ) ;
221+
178222 return c . json (
179223 {
180224 base : base ,
@@ -190,6 +234,8 @@ app.get("/v1/crypto/rates/:base", (c) => {
190234} ) ;
191235
192236app . get ( "/v1/stocks/rates" , ( c ) => {
237+ httpRequests . labels ( { endpoint : "/v1/stocks/rates" } ) . inc ( ) ;
238+
193239 return c . json (
194240 {
195241 base : "USD" ,
@@ -207,6 +253,8 @@ app.get("/v1/stocks/rates", (c) => {
207253app . get ( "/v1/stocks/rates/:base" , ( c ) => {
208254 const base = c . params [ "base" ] ! . toUpperCase ( ) ;
209255
256+ httpRequests . labels ( { endpoint : "/v1/stocks/rates/:base" } ) . inc ( ) ;
257+
210258 return c . json (
211259 {
212260 base : base ,
@@ -231,6 +279,7 @@ Logger.info(`Server running on http://${host}:${port}`);
231279Logger . info ( `Exchange rates updates every ${ updateInterval } s` ) ;
232280Logger . info ( "Available endpoints:" ) ;
233281Logger . info ( " GET / - Health check and stats" ) ;
282+ Logger . info ( " GET /metrics - OpenMetrics format" ) ;
234283Logger . info ( " GET /openapi.json - OpenAPI specification" ) ;
235284Logger . info ( " GET /v1/assets - List all supported currencies, metals, stocks and cryptocurrencies" ) ;
236285Logger . info ( " GET /v1/rates - Exchange rates for USD (default)" ) ;
0 commit comments