|
7 | 7 |
|
8 | 8 | package org.elasticsearch.xpack.inference.telemetry;
|
9 | 9 |
|
| 10 | +import org.elasticsearch.ElasticsearchStatusException; |
| 11 | +import org.elasticsearch.core.Nullable; |
10 | 12 | import org.elasticsearch.inference.Model;
|
| 13 | +import org.elasticsearch.inference.UnparsedModel; |
| 14 | +import org.elasticsearch.telemetry.metric.LongCounter; |
| 15 | +import org.elasticsearch.telemetry.metric.LongHistogram; |
| 16 | +import org.elasticsearch.telemetry.metric.MeterRegistry; |
11 | 17 |
|
12 |
| -public interface InferenceStats { |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Objects; |
| 20 | +import java.util.stream.Collectors; |
| 21 | +import java.util.stream.Stream; |
13 | 22 |
|
14 |
| - /** |
15 |
| - * Increment the counter for a particular value in a thread safe manner. |
16 |
| - * @param model the model to increment request count for |
17 |
| - */ |
18 |
| - void incrementRequestCount(Model model); |
| 23 | +import static java.util.Map.entry; |
| 24 | +import static java.util.stream.Stream.concat; |
19 | 25 |
|
20 |
| - InferenceStats NOOP = model -> {}; |
| 26 | +public record InferenceStats(LongCounter requestCount, LongHistogram inferenceDuration) { |
| 27 | + |
| 28 | + public InferenceStats { |
| 29 | + Objects.requireNonNull(requestCount); |
| 30 | + Objects.requireNonNull(inferenceDuration); |
| 31 | + } |
| 32 | + |
| 33 | + public static InferenceStats create(MeterRegistry meterRegistry) { |
| 34 | + return new InferenceStats( |
| 35 | + meterRegistry.registerLongCounter( |
| 36 | + "es.inference.requests.count.total", |
| 37 | + "Inference API request counts for a particular service, task type, model ID", |
| 38 | + "operations" |
| 39 | + ), |
| 40 | + meterRegistry.registerLongHistogram( |
| 41 | + "es.inference.requests.time", |
| 42 | + "Inference API request counts for a particular service, task type, model ID", |
| 43 | + "ms" |
| 44 | + ) |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + public static Map<String, Object> modelAttributes(Model model) { |
| 49 | + return toMap(modelAttributeEntries(model)); |
| 50 | + } |
| 51 | + |
| 52 | + private static Stream<Map.Entry<String, Object>> modelAttributeEntries(Model model) { |
| 53 | + var stream = Stream.<Map.Entry<String, Object>>builder() |
| 54 | + .add(entry("service", model.getConfigurations().getService())) |
| 55 | + .add(entry("task_type", model.getTaskType().toString())); |
| 56 | + if (model.getServiceSettings().modelId() != null) { |
| 57 | + stream.add(entry("model_id", model.getServiceSettings().modelId())); |
| 58 | + } |
| 59 | + return stream.build(); |
| 60 | + } |
| 61 | + |
| 62 | + private static Map<String, Object> toMap(Stream<Map.Entry<String, Object>> stream) { |
| 63 | + return stream.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 64 | + } |
| 65 | + |
| 66 | + public static Map<String, Object> responseAttributes(Model model, @Nullable Throwable t) { |
| 67 | + return toMap(concat(modelAttributeEntries(model), errorAttributes(t))); |
| 68 | + } |
| 69 | + |
| 70 | + public static Map<String, Object> responseAttributes(UnparsedModel model, @Nullable Throwable t) { |
| 71 | + var unknownModelAttributes = Stream.<Map.Entry<String, Object>>builder() |
| 72 | + .add(entry("service", model.service())) |
| 73 | + .add(entry("task_type", model.taskType().toString())) |
| 74 | + .build(); |
| 75 | + |
| 76 | + return toMap(concat(unknownModelAttributes, errorAttributes(t))); |
| 77 | + } |
| 78 | + |
| 79 | + public static Map<String, Object> responseAttributes(@Nullable Throwable t) { |
| 80 | + return toMap(errorAttributes(t)); |
| 81 | + } |
| 82 | + |
| 83 | + private static Stream<Map.Entry<String, Object>> errorAttributes(@Nullable Throwable t) { |
| 84 | + if (t == null) { |
| 85 | + return Stream.of(entry("status_code", 200)); |
| 86 | + } else if (t instanceof ElasticsearchStatusException ese) { |
| 87 | + return Stream.<Map.Entry<String, Object>>builder() |
| 88 | + .add(entry("status_code", ese.status().getStatus())) |
| 89 | + .add(entry("error.type", String.valueOf(ese.status().getStatus()))) |
| 90 | + .build(); |
| 91 | + } else { |
| 92 | + return Stream.of(entry("error.type", t.getClass().getSimpleName())); |
| 93 | + } |
| 94 | + } |
21 | 95 | }
|
0 commit comments