public boolean pushPrometheusMetrics()

in hertzbeat-push/src/main/java/org/apache/hertzbeat/push/service/impl/PushGatewayServiceImpl.java [65:132]


    public boolean pushPrometheusMetrics(InputStream inputStream, String job, String instance) {
        try {
            long curTime = Instant.now().toEpochMilli();
            Map<String, MetricFamily> metricFamilyMap = OnlineParser.parseMetrics(inputStream);
            if (metricFamilyMap == null) {
                log.error("parse prometheus metrics is null, job: {}, instance: {}", job, instance);
                return false;
            }
            long id = 0L;
            if (job != null && instance != null) {
                // auto create monitor when job and instance not null
                // job is app, instance is the name
                id = jobInstanceMap.computeIfAbsent(job + "_" + instance, key -> {
                    log.info("auto create monitor by prometheus push, job: {}, instance: {}", job, instance);
                    long monitorId = SnowFlakeIdGenerator.generateId();
                    Monitor monitor = Monitor.builder()
                            .id(monitorId)
                            .app(job)
                            .name(instance)
                            .host(instance)
                            .type((byte) 1)
                            .status(CommonConstants.MONITOR_UP_CODE)
                            .build();
                    this.pushMonitorDao.save(monitor);
                    return monitorId;
                });
            }
            for (Map.Entry<String, MetricFamily> entry : metricFamilyMap.entrySet()) {
                CollectRep.MetricsData.Builder builder = CollectRep.MetricsData.newBuilder();
                builder.setId(id);
                builder.setApp(job);
                builder.setTime(curTime);
                String metricsName = entry.getKey();
                builder.setMetrics(metricsName);
                MetricFamily metricFamily = entry.getValue();
                if (!metricFamily.getMetricList().isEmpty()) {
                    List<String> metricsFields = new LinkedList<>();
                    for (int index = 0; index < metricFamily.getMetricList().size(); index++) {
                        MetricFamily.Metric metric = metricFamily.getMetricList().get(index);
                        if (index == 0) {
                            metric.getLabels().forEach(label -> {
                                metricsFields.add(label.getName());
                                builder.addField(CollectRep.Field.newBuilder().setName(label.getName())
                                        .setType(CommonConstants.TYPE_STRING).setLabel(true).build());
                            });
                            builder.addField(CollectRep.Field.newBuilder().setName("value")
                                    .setType(CommonConstants.TYPE_NUMBER).setLabel(false).build());
                        }
                        Map<String, String> labelMap = metric.getLabels()
                                .stream()
                                .collect(Collectors.toMap(MetricFamily.Label::getName, MetricFamily.Label::getValue));
                        CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
                        for (String field : metricsFields) {
                            String fieldValue = labelMap.get(field);
                            valueRowBuilder.addColumn(fieldValue == null ? CommonConstants.NULL_VALUE : fieldValue);
                        }
                        valueRowBuilder.addColumn(String.valueOf(metric.getValue()));
                        builder.addValueRow(valueRowBuilder.build());
                    }
                    commonDataQueue.sendMetricsData(builder.build());
                }
            }
            return true;
        } catch (Exception e) {
            log.error("push prometheus metrics error", e);
            return false;
        }
    }