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