|
| 1 | +/** |
| 2 | + * Copyright (C) 2000-2026 Vaadin Ltd |
| 3 | + * |
| 4 | + * This program is available under Vaadin Commercial License and Service Terms. |
| 5 | + * |
| 6 | + * See <https://vaadin.com/commercial-license-and-service-terms> for the full |
| 7 | + * license. |
| 8 | + */ |
| 9 | +package com.vaadin.observability.micrometer.devtools; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.LinkedHashMap; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | + |
| 17 | +import io.micrometer.core.instrument.Counter; |
| 18 | +import io.micrometer.core.instrument.DistributionSummary; |
| 19 | +import io.micrometer.core.instrument.FunctionCounter; |
| 20 | +import io.micrometer.core.instrument.Gauge; |
| 21 | +import io.micrometer.core.instrument.Measurement; |
| 22 | +import io.micrometer.core.instrument.Meter; |
| 23 | +import io.micrometer.core.instrument.MeterRegistry; |
| 24 | +import io.micrometer.core.instrument.Tag; |
| 25 | +import io.micrometer.core.instrument.Timer; |
| 26 | +import tools.jackson.databind.JsonNode; |
| 27 | + |
| 28 | +import com.vaadin.base.devserver.DevToolsInterface; |
| 29 | +import com.vaadin.base.devserver.DevToolsMessageHandler; |
| 30 | +import com.vaadin.observability.micrometer.ObservabilityKit; |
| 31 | + |
| 32 | +/** |
| 33 | + * Dev-mode bridge between the live Micrometer {@link MeterRegistry} and the |
| 34 | + * Vaadin Copilot metrics panel. |
| 35 | + * <p> |
| 36 | + * Discovered via the Java {@link java.util.ServiceLoader} by Flow's dev-tools |
| 37 | + * server (see {@code META-INF/services}). On request from the panel it |
| 38 | + * snapshots every {@code vaadin.*} meter and sends it to the browser over the |
| 39 | + * shared dev-tools websocket. This is a developer-only convenience view; it has |
| 40 | + * no effect in production where the dev-tools connection does not exist. |
| 41 | + */ |
| 42 | +public class ObservabilityDevToolsHandler implements DevToolsMessageHandler { |
| 43 | + |
| 44 | + static final String COMMAND_REFRESH = "observability-kit-refresh"; |
| 45 | + static final String COMMAND_METRICS = "observability-kit-metrics"; |
| 46 | + |
| 47 | + /** Only meters under this prefix are exposed to the panel. */ |
| 48 | + private static final String METER_PREFIX = "vaadin."; |
| 49 | + |
| 50 | + @Override |
| 51 | + public void handleConnect(DevToolsInterface devToolsInterface) { |
| 52 | + // Push an initial snapshot; the panel also pulls on demand. |
| 53 | + sendSnapshot(devToolsInterface); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public boolean handleMessage(String command, JsonNode data, |
| 58 | + DevToolsInterface devToolsInterface) { |
| 59 | + if (COMMAND_REFRESH.equals(command)) { |
| 60 | + sendSnapshot(devToolsInterface); |
| 61 | + return true; |
| 62 | + } |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + private void sendSnapshot(DevToolsInterface devToolsInterface) { |
| 67 | + Map<String, Object> payload = new LinkedHashMap<>(); |
| 68 | + payload.put("timestamp", System.currentTimeMillis()); |
| 69 | + payload.put("meters", snapshot()); |
| 70 | + devToolsInterface.send(COMMAND_METRICS, payload); |
| 71 | + } |
| 72 | + |
| 73 | + private List<Map<String, Object>> snapshot() { |
| 74 | + List<Map<String, Object>> meters = new ArrayList<>(); |
| 75 | + MeterRegistry registry = ObservabilityKit.getActiveMeterRegistry(); |
| 76 | + if (registry == null) { |
| 77 | + return meters; |
| 78 | + } |
| 79 | + for (Meter meter : registry.getMeters()) { |
| 80 | + Meter.Id id = meter.getId(); |
| 81 | + if (!id.getName().startsWith(METER_PREFIX)) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + Map<String, Object> entry = new LinkedHashMap<>(); |
| 85 | + entry.put("name", id.getName()); |
| 86 | + entry.put("type", id.getType().name()); |
| 87 | + |
| 88 | + Map<String, String> tags = new LinkedHashMap<>(); |
| 89 | + for (Tag tag : id.getTags()) { |
| 90 | + tags.put(tag.getKey(), tag.getValue()); |
| 91 | + } |
| 92 | + entry.put("tags", tags); |
| 93 | + |
| 94 | + // Emit derived, interpretable values per meter type rather than raw |
| 95 | + // statistics. For timers the cumulative mean is the stable, useful |
| 96 | + // figure (TOTAL_TIME is an ever-growing sum and the SimpleMeter |
| 97 | + // registry's MAX decays to 0 between polls). |
| 98 | + if (meter instanceof Timer timer) { |
| 99 | + entry.put("count", timer.count()); |
| 100 | + entry.put("mean", timer.mean(TimeUnit.MILLISECONDS)); |
| 101 | + entry.put("max", timer.max(TimeUnit.MILLISECONDS)); |
| 102 | + entry.put("unit", "ms"); |
| 103 | + } else if (meter instanceof Counter counter) { |
| 104 | + entry.put("count", (long) counter.count()); |
| 105 | + } else if (meter instanceof FunctionCounter counter) { |
| 106 | + entry.put("count", (long) counter.count()); |
| 107 | + } else if (meter instanceof Gauge gauge) { |
| 108 | + entry.put("value", gauge.value()); |
| 109 | + } else if (meter instanceof DistributionSummary summary) { |
| 110 | + entry.put("count", summary.count()); |
| 111 | + entry.put("mean", summary.mean()); |
| 112 | + entry.put("max", summary.max()); |
| 113 | + if (id.getBaseUnit() != null) { |
| 114 | + entry.put("unit", id.getBaseUnit()); |
| 115 | + } |
| 116 | + } else { |
| 117 | + // Unknown meter type: fall back to raw measurements. |
| 118 | + List<Map<String, Object>> measurements = new ArrayList<>(); |
| 119 | + for (Measurement measurement : meter.measure()) { |
| 120 | + Map<String, Object> m = new LinkedHashMap<>(); |
| 121 | + m.put("statistic", measurement.getStatistic().name()); |
| 122 | + m.put("value", measurement.getValue()); |
| 123 | + measurements.add(m); |
| 124 | + } |
| 125 | + entry.put("measurements", measurements); |
| 126 | + } |
| 127 | + meters.add(entry); |
| 128 | + } |
| 129 | + return meters; |
| 130 | + } |
| 131 | +} |
0 commit comments