summaryrefslogtreecommitdiffstats
path: root/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/RemoteHealthMetricFetcher.java
blob: 4bc9055c6a5fc7d7cab6e509e3f6c2f285e170e7 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2020 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.metricsproxy.service;

import ai.vespa.metricsproxy.metric.HealthMetric;
import java.util.logging.Level;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.logging.Logger;

/**
 * Fetch health status for a given vespa service
 *
 * @author Jo Kristian Bergum
 */
public class RemoteHealthMetricFetcher extends HttpMetricFetcher {
    private final static Logger log = Logger.getLogger(RemoteHealthMetricFetcher.class.getPackage().getName());

    private final static String HEALTH_PATH = STATE_PATH + "health";

    public RemoteHealthMetricFetcher(VespaService service, int port) {
        super(service, port, HEALTH_PATH);
    }

    /**
     * Connect to remote service over http and fetch metrics
     */
    public HealthMetric getHealth(int fetchCount) {
        String data = "{}";
        try {
            data = getJson();
        } catch (IOException e) {
            logMessageNoResponse(errMsgNoResponse(e), fetchCount);
        }
        return createHealthMetrics(data, fetchCount);
    }

    /**
     * Connect to remote service over http and fetch metrics
     */
    private HealthMetric createHealthMetrics(String data, int fetchCount) {
        HealthMetric healthMetric = HealthMetric.getDown("Failed fetching status page for service");
        try {
            healthMetric = parse(data);
        } catch (Exception e) {
            handleException(e, data, fetchCount);
        }
        return healthMetric;
    }

    private HealthMetric parse(String data) {
        if (data == null || data.isEmpty()) {
            return HealthMetric.getUnknown("Empty response from status page");
        }
        try {
            JSONObject o = new JSONObject(data);
            JSONObject status = o.getJSONObject("status");
            String code = status.getString("code");
            String message = "";
            if (status.has("message")) {
                message = status.getString("message");
            }
            return HealthMetric.get(code, message);

        } catch (JSONException e) {
            log.log(LogLevel.DEBUG, "Failed to parse json response from metrics page:" + e + ":" + data);
            return HealthMetric.getUnknown("Not able to parse json from status page");
        }
    }
}