Skip to content

Commit aec7bdb

Browse files
committed
server: add /v1/metrics endpoint
1 parent 254098a commit aec7bdb

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

tools/server/server-context.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2912,6 +2912,27 @@ void server_routes::init_routes() {
29122912
return res;
29132913
};
29142914

2915+
this->get_v1_metrics = [this](const server_http_req &) {
2916+
auto res = std::make_unique<server_res_generator>(ctx_server);
2917+
2918+
// Calculate uptime in seconds
2919+
int64_t uptime_sec = (llama_time_us() - t_server_start) / 1000000;
2920+
2921+
json data = {
2922+
{"status", "online"},
2923+
{"uptime_sec", uptime_sec}
2924+
};
2925+
2926+
// Include system_info if available
2927+
if (!system_info_str.empty()) {
2928+
data["system_info"] = system_info_str;
2929+
}
2930+
2931+
res->ok(data);
2932+
return res;
2933+
};
2934+
2935+
29152936
this->get_metrics = [this](const server_http_req &) {
29162937
auto res = std::make_unique<server_res_generator>(ctx_server);
29172938
if (!params.endpoint_metrics) {

tools/server/server-context.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,17 @@ struct server_context {
5151
struct server_res_generator;
5252

5353
struct server_routes {
54-
server_routes(const common_params & params, server_context & ctx_server, std::function<bool()> is_ready = []() { return true; })
55-
: params(params), ctx_server(*ctx_server.impl), is_ready(is_ready) {
54+
server_routes(const common_params & params, server_context & ctx_server, std::function<bool()> is_ready = []() { return true; }, int64_t t_start = 0)
55+
: params(params), ctx_server(*ctx_server.impl), is_ready(is_ready), t_server_start(t_start),
56+
system_info_str(common_params_get_system_info(params)) {
5657
init_routes();
5758
}
5859

5960
void init_routes();
6061
// handlers using lambda function, so that they can capture `this` without `std::bind`
6162
server_http_context::handler_t get_health;
6263
server_http_context::handler_t get_metrics;
64+
server_http_context::handler_t get_v1_metrics;
6365
server_http_context::handler_t get_slots;
6466
server_http_context::handler_t post_slots;
6567
server_http_context::handler_t get_props;
@@ -90,4 +92,6 @@ struct server_routes {
9092
const common_params & params;
9193
server_context_impl & ctx_server;
9294
std::function<bool()> is_ready;
95+
int64_t t_server_start;
96+
std::string system_info_str;
9397
};

tools/server/server.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,15 @@ int main(int argc, char ** argv, char ** envp) {
108108
return 1;
109109
}
110110

111+
// Capture server start time for metrics
112+
int64_t t_server_start = llama_time_us();
113+
111114
//
112115
// Router
113116
//
114117

115118
// register API routes
116-
server_routes routes(params, ctx_server, [&ctx_http]() { return ctx_http.is_ready.load(); });
119+
server_routes routes(params, ctx_server, [&ctx_http]() { return ctx_http.is_ready.load(); }, t_server_start);
117120

118121
bool is_router_server = params.model.path.empty();
119122
std::optional<server_models_routes> models_routes{};
@@ -153,6 +156,7 @@ int main(int argc, char ** argv, char ** envp) {
153156

154157
ctx_http.get ("/health", ex_wrapper(routes.get_health)); // public endpoint (no API key check)
155158
ctx_http.get ("/v1/health", ex_wrapper(routes.get_health)); // public endpoint (no API key check)
159+
ctx_http.get ("/v1/metrics", ex_wrapper(routes.get_v1_metrics)); // public endpoint (no API key check)
156160
ctx_http.get ("/metrics", ex_wrapper(routes.get_metrics));
157161
ctx_http.get ("/props", ex_wrapper(routes.get_props));
158162
ctx_http.post("/props", ex_wrapper(routes.post_props));

0 commit comments

Comments
 (0)