summaryrefslogtreecommitdiffstats
path: root/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/RemoteMetricsFetcher.java
blob: a606ec7d8cd9a51b6e4dea9f7d00085c80102213 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright 2019 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.Metric;
import ai.vespa.metricsproxy.metric.Metrics;
import ai.vespa.metricsproxy.metric.model.DimensionId;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import static ai.vespa.metricsproxy.metric.model.DimensionId.toDimensionId;

/**
 * Fetch metrics for a given vespa service
 *
 * @author Jo Kristian Bergum
 */
public class RemoteMetricsFetcher extends HttpMetricFetcher {
    /**
     * @param service The service to fetch metrics from
     * @param port    The port to use
     */
    RemoteMetricsFetcher(VespaService service, int port) {
        super(service, port, METRICS_PATH);
    }

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

        return createMetrics(data, fetchCount);
    }

    /**
     * Connect to remote service over http and fetch metrics
     */
    public Metrics createMetrics(String data, int fetchCount) {
        Metrics remoteMetrics = new Metrics();
        try {
            remoteMetrics = parse(data);
        } catch (Exception e) {
            handleException(e, data, fetchCount);
        }

        return remoteMetrics;
    }

    Metrics parse(String data) throws JSONException {
        JSONObject o = new JSONObject(data);
        if (!(o.has("metrics"))) {
            return new Metrics(); //empty
        }

        JSONObject metrics = o.getJSONObject("metrics");
        JSONArray values;
        long timestamp;

        try {
            JSONObject snapshot = metrics.getJSONObject("snapshot");
            timestamp = (long) snapshot.getDouble("to");
            values = metrics.getJSONArray("values");
        } catch (JSONException e) {
            // snapshot might not have been produced. Do not throw exception into log
            return new Metrics();
        }
        long now = System.currentTimeMillis() / 1000;
        timestamp = Metric.adjustTime(timestamp, now);
        Metrics m = new Metrics(timestamp);

        Map<DimensionId, String> noDims = Collections.emptyMap();
        Map<String, Map<DimensionId, String>> uniqueDimensions = new HashMap<>();
        for (int i = 0; i < values.length(); i++) {
            JSONObject metric = values.getJSONObject(i);
            String name = metric.getString("name");
            String description = "";

            if (metric.has("description")) {
                description = metric.getString("description");
            }

            Map<DimensionId, String> dim = noDims;
            if (metric.has("dimensions")) {
                JSONObject dimensions = metric.getJSONObject("dimensions");
                StringBuilder sb = new StringBuilder();
                for (Iterator<?> it = dimensions.keys(); it.hasNext(); ) {
                    String k = (String) it.next();
                    String v = dimensions.getString(k);
                    sb.append(toDimensionId(k)).append(v);
                }
                if ( ! uniqueDimensions.containsKey(sb.toString())) {
                    dim = new HashMap<>();
                    for (Iterator<?> it = dimensions.keys(); it.hasNext(); ) {
                        String k = (String) it.next();
                        String v = dimensions.getString(k);
                        dim.put(toDimensionId(k), v);
                    }
                    uniqueDimensions.put(sb.toString(), Collections.unmodifiableMap(dim));
                }
                dim = uniqueDimensions.get(sb.toString());
            }

            JSONObject aggregates = metric.getJSONObject("values");
            for (Iterator<?> it = aggregates.keys(); it.hasNext(); ) {
                String aggregator = (String) it.next();
                Number value = (Number) aggregates.get(aggregator);
                StringBuilder metricName = (new StringBuilder()).append(name).append(".").append(aggregator);
                m.add(new Metric(metricName.toString(), value, timestamp, dim, description));
            }
        }

        return m;
    }
}