Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
MetricsRecorder.java
Go to the documentation of this file.
1/*
2 * Ollama4j - Java library for interacting with Ollama server.
3 * Copyright (c) 2025 Amith Koujalgi and contributors.
4 *
5 * Licensed under the MIT License (the "License");
6 * you may not use this file except in compliance with the License.
7 *
8*/
9package io.github.ollama4j.metrics;
10
11import com.google.common.base.Throwables;
12import io.github.ollama4j.models.request.ThinkMode;
13import io.prometheus.client.Counter;
14import io.prometheus.client.Histogram;
15import java.util.Map;
16
17public class MetricsRecorder {
18
19 // Corrected: Removed duplicate "format" label and ensured label count matches usage
20 private static final Counter requests =
21 Counter.build()
22 .name("ollama_api_requests_total")
23 .help("Total requests to Ollama API")
24 .labelNames(
25 "endpoint",
26 "model",
27 "raw",
28 "streaming",
29 "thinking",
30 "http_status",
31 "options",
32 "format")
33 .register();
34
35 private static final Histogram requestLatency =
36 Histogram.build()
37 .name("ollama_api_request_duration_seconds")
38 .help("Request latency in seconds")
39 .labelNames(
40 "endpoint",
41 "model",
42 "raw",
43 "streaming",
44 "thinking",
45 "http_status",
46 "options",
47 "format")
48 .register();
49
50 private static final Histogram responseSize =
51 Histogram.build()
52 .name("ollama_api_response_size_bytes")
53 .help("Response size in bytes")
54 .labelNames("endpoint", "model", "options")
55 .register();
56
57 public static void record(
58 String endpoint,
59 String model,
60 boolean raw,
61 ThinkMode thinkMode,
62 boolean streaming,
63 Map<String, Object> options,
64 Object format,
65 long startTime,
66 int responseHttpStatus,
67 Object response) {
68 long endTime = System.currentTimeMillis();
69
70 String httpStatus = String.valueOf(responseHttpStatus);
71
72 String formatString = "";
73 if (format instanceof String) {
74 formatString = (String) format;
75 } else if (format instanceof Map) {
76 formatString = mapToString((Map<String, Object>) format);
77 } else if (format != null) {
78 formatString = format.toString();
79 }
80
81 // Ensure the number of labels matches the labelNames above (8 labels)
82 requests.labels(
83 endpoint,
84 safe(model),
85 String.valueOf(raw),
86 String.valueOf(streaming),
87 String.valueOf(thinkMode),
88 httpStatus,
89 safe(mapToString(options)),
90 safe(formatString))
91 .inc();
92 double durationSeconds = (endTime - startTime) / 1000.0;
93
94 // Ensure the number of labels matches the labelNames above (8 labels)
95 requestLatency
96 .labels(
97 endpoint,
98 safe(model),
99 String.valueOf(raw),
100 String.valueOf(streaming),
101 String.valueOf(thinkMode),
102 httpStatus,
103 safe(mapToString(options)),
104 safe(formatString))
105 .observe(durationSeconds);
106
107 // Record response size (only if response is a string or json-like object)
108 if (response != null) {
109 if (response instanceof Exception) {
110 response = Throwables.getStackTraceAsString((Throwable) response);
111 }
112 int size = response.toString().length();
113 responseSize.labels(endpoint, safe(model), safe(mapToString(options))).observe(size);
114 }
115 }
116
117 // Utility method to convert options Map to string (you can adjust this for more detailed
118 // representation)
119 private static String mapToString(Map<String, Object> map) {
120 if (map == null || map.isEmpty()) {
121 return "none";
122 }
123 // Convert the map to a string (can be customized to fit the use case)
124 return map.toString();
125 }
126
127 private static String safe(String value) {
128 return (value == null || value.isEmpty()) ? "none" : value;
129 }
130}
static void record(String endpoint, String model, boolean raw, ThinkMode thinkMode, boolean streaming, Map< String, Object > options, Object format, long startTime, int responseHttpStatus, Object response)