summaryrefslogtreecommitdiffstats
path: root/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/RemoteMetricsFetcher.java
blob: c29357611790ecadb588c902c3add3d02615b8bd (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
// 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.Metric;
import ai.vespa.metricsproxy.metric.Metrics;
import ai.vespa.metricsproxy.metric.model.DimensionId;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;

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 {

    private static final ObjectMapper jsonMapper = new ObjectMapper();

    final static String METRICS_PATH = STATE_PATH + "metrics";

    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
     */
    Metrics createMetrics(String data, int fetchCount) {
        Metrics remoteMetrics = new Metrics();
        try {
            remoteMetrics = parse(data);
        } catch (Exception e) {
            handleException(e, data, fetchCount);
        }

        return remoteMetrics;
    }

    private Metrics parse(String data) throws IOException {
        JsonNode o = jsonMapper.readTree(data);
        if (!(o.has("metrics"))) {
            return new Metrics(); //empty
        }

        JsonNode metrics = o.get("metrics");
        ArrayNode values;
        long timestamp;

        try {
            JsonNode snapshot = metrics.get("snapshot");
            timestamp = (long) snapshot.get("to").doubleValue();
            values = (ArrayNode) metrics.get("values");
        } catch (Exception 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.size(); i++) {
            JsonNode metric = values.get(i);
            String name = metric.get("name").textValue();
            String description = "";

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

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

            JsonNode aggregates = metric.get("values");
            for (Iterator<?> it = aggregates.fieldNames(); it.hasNext(); ) {
                String aggregator = (String) it.next();
                Number value = aggregates.get(aggregator).numberValue();
                if (value == null) throw new IllegalArgumentException("Value for aggregator '" + aggregator + "' is missing");
                StringBuilder metricName = (new StringBuilder()).append(name).append(".").append(aggregator);
                m.add(new Metric(metricName.toString(), value, timestamp, dim, description));
            }
        }

        return m;
    }
}