public Integer doProcessWatchCall()

in dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListMetric.java [70:226]


    public Integer doProcessWatchCall() throws Exception {
        List<Row> rows = new ArrayList<>();

        List<Long> pids = findPids(name);
        ProcessHandle.allProcesses()
                .filter(ph -> pids.contains(ph.pid()))
                .forEach(ph -> {
                    JsonObject root = loadStatus(ph.pid());
                    // there must be a status file for the running Camel integration
                    if (root != null) {
                        Row row = new Row();
                        JsonObject context = (JsonObject) root.get("context");
                        if (context == null) {
                            return;
                        }
                        row.name = context.getString("name");
                        if ("CamelJBang".equals(row.name)) {
                            row.name = ProcessHelper.extractName(root, ph);
                        }
                        row.pid = Long.toString(ph.pid());
                        row.uptime = extractSince(ph);
                        row.age = TimeUtils.printSince(row.uptime);
                        Row baseRow = row.copy();

                        JsonObject mo = (JsonObject) root.get("micrometer");
                        if (mo != null) {
                            JsonArray arr = (JsonArray) mo.get("counters");
                            if (arr != null) {
                                for (int i = 0; i < arr.size(); i++) {
                                    row = baseRow.copy();
                                    JsonObject jo = (JsonObject) arr.get(i);
                                    row.type = "counter";
                                    row.metricName = jo.getString("name");
                                    row.metricDescription = jo.getString("description");
                                    row.metricId = extractId(jo);
                                    row.tags = extractTags(jo);
                                    row.count = jo.getDouble("count");

                                    if (custom && row.metricName.startsWith("Camel")) {
                                        continue; // skip internal camel metrics
                                    }
                                    if (!all && getNumber(row.count).isEmpty()) {
                                        continue;
                                    }
                                    if (filter == null || row.type.equals(filter) || row.metricName.contains(filter)) {
                                        rows.add(row);
                                    }
                                }
                            }
                            arr = (JsonArray) mo.get("timers");
                            if (arr != null) {
                                for (int i = 0; i < arr.size(); i++) {
                                    row = baseRow.copy();
                                    JsonObject jo = (JsonObject) arr.get(i);
                                    row.type = "timer";
                                    row.metricName = jo.getString("name");
                                    row.metricDescription = jo.getString("description");
                                    row.metricId = extractId(jo);
                                    row.tags = extractTags(jo);
                                    row.count = jo.getDouble("count");
                                    row.mean = jo.getDouble("mean");
                                    row.max = jo.getDouble("max");
                                    row.total = jo.getDouble("total");

                                    if (custom && row.metricName.startsWith("Camel")) {
                                        continue; // skip internal camel metrics
                                    }
                                    if (!all && getNumber(row.count).isEmpty()) {
                                        continue;
                                    }
                                    if (filter == null || row.type.equals(filter) || row.metricName.contains(filter)
                                            || row.tags.contains(filter)) {
                                        rows.add(row);
                                    }
                                }
                            }
                            arr = (JsonArray) mo.get("gauges");
                            if (arr != null) {
                                for (int i = 0; i < arr.size(); i++) {
                                    row = baseRow.copy();
                                    JsonObject jo = (JsonObject) arr.get(i);
                                    row.type = "gauge";
                                    row.metricName = jo.getString("name");
                                    row.metricDescription = jo.getString("description");
                                    row.metricId = extractId(jo);
                                    row.tags = extractTags(jo);
                                    row.count = jo.getDouble("value");

                                    if (custom && row.metricName.startsWith("Camel")) {
                                        continue; // skip internal camel metrics
                                    }
                                    if (!all && getNumber(row.count).isEmpty()) {
                                        continue;
                                    }
                                    if (filter == null || row.type.equals(filter) || row.metricName.contains(filter)) {
                                        rows.add(row);
                                    }
                                }
                            }
                            arr = (JsonArray) mo.get("distribution");
                            if (arr != null) {
                                for (int i = 0; i < arr.size(); i++) {
                                    row = baseRow.copy();
                                    JsonObject jo = (JsonObject) arr.get(i);
                                    row.type = "distribution";
                                    row.metricName = jo.getString("name");
                                    row.metricDescription = jo.getString("description");
                                    row.metricId = extractId(jo);
                                    row.tags = extractTags(jo);
                                    row.count = jo.getDouble("value");
                                    row.mean = jo.getDouble("mean");
                                    row.max = jo.getDouble("max");
                                    row.total = jo.getDouble("totalAmount");

                                    if (custom && row.metricName.startsWith("Camel")) {
                                        continue; // skip internal camel metrics
                                    }
                                    if (!all && getNumber(row.count).isEmpty()) {
                                        continue;
                                    }
                                    if (filter == null || row.type.equals(filter) || row.metricName.contains(filter)) {
                                        rows.add(row);
                                    }
                                }
                            }
                        }
                    }
                });

        // sort rows
        rows.sort(this::sortRow);

        if (!rows.isEmpty()) {
            printer().println(AsciiTable.getTable(AsciiTable.NO_BORDERS, rows, Arrays.asList(
                    new Column().header("PID").headerAlign(HorizontalAlign.CENTER).with(r -> r.pid),
                    new Column().header("NAME").dataAlign(HorizontalAlign.LEFT).maxWidth(30, OverflowBehaviour.ELLIPSIS_RIGHT)
                            .with(r -> r.name),
                    new Column().header("TYPE").dataAlign(HorizontalAlign.LEFT).with(r -> r.type),
                    new Column().header("METRIC").dataAlign(HorizontalAlign.LEFT).maxWidth(40, OverflowBehaviour.ELLIPSIS_RIGHT)
                            .with(r -> r.metricName),
                    new Column().header("ID").dataAlign(HorizontalAlign.LEFT).maxWidth(40, OverflowBehaviour.ELLIPSIS_RIGHT)
                            .with(r -> r.metricId),
                    new Column().header("VALUE").headerAlign(HorizontalAlign.RIGHT).dataAlign(HorizontalAlign.RIGHT)
                            .with(r -> getNumber(r.count)),
                    new Column().header("MEAN").headerAlign(HorizontalAlign.RIGHT).dataAlign(HorizontalAlign.RIGHT)
                            .with(r -> getNumber(r.mean)),
                    new Column().header("MAX").headerAlign(HorizontalAlign.RIGHT).dataAlign(HorizontalAlign.RIGHT)
                            .with(r -> getNumber(r.max)),
                    new Column().header("TOTAL").headerAlign(HorizontalAlign.RIGHT).dataAlign(HorizontalAlign.RIGHT)
                            .with(r -> getNumber(r.total)),
                    new Column().header("TAGS").visible(tags).dataAlign(HorizontalAlign.LEFT)
                            .maxWidth(60, OverflowBehaviour.NEWLINE)
                            .with(r -> r.tags))));
        }

        return 0;
    }