summaryrefslogtreecommitdiffstats
path: root/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/ServiceHealthGatherer.java
blob: b7e441d21f789347b275e40fba86af810640f666 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.metricsproxy.node;

import ai.vespa.metricsproxy.metric.model.MetricsPacket;
import ai.vespa.metricsproxy.metric.model.StatusCode;
import ai.vespa.metricsproxy.metric.model.json.YamasJsonUtil;
import ai.vespa.metricsproxy.service.VespaServices;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.stream.Collectors;

import static ai.vespa.metricsproxy.node.NodeMetricGatherer.ROUTING_JSON;

/**
 * @author olaa
 */
public class ServiceHealthGatherer {


    protected static List<MetricsPacket.Builder> gatherServiceHealthMetrics(VespaServices vespaServices)  {
        return vespaServices.getVespaServices()
                .stream()
                .map(service -> {
                    try {
                        StatusCode healthStatus = service.getHealth().getStatus();
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("status_code", healthStatus.code);
                        jsonObject.put("status_message", healthStatus.status);
                        jsonObject.put("application", service.getMonitoringName());
                        JSONObject dimensions = new JSONObject();
                        dimensions.put("instance", service.getInstanceName());
                        dimensions.put("metrictype", "health");
                        jsonObject.put("dimensions", dimensions);
                        jsonObject.put("routing", ROUTING_JSON);
                        return YamasJsonUtil.toMetricsPackets(jsonObject.toString()).get(0);
                    } catch (JSONException e) {
                        throw new RuntimeException(e.getMessage());
                    }
                })
                .collect(Collectors.toList());
    }
}