summaryrefslogtreecommitdiffstats
path: root/metrics-proxy/src/main/java/ai/vespa/metricsproxy/http/application/NodeMetricsClient.java
blob: 28130380c13323a9ebfdb3d23fa721aab0681b27 (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
/*
 * Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
 */

package ai.vespa.metricsproxy.http.application;

import ai.vespa.metricsproxy.http.MetricsHandler;
import ai.vespa.metricsproxy.metric.model.MetricsPacket;
import ai.vespa.metricsproxy.metric.model.json.GenericJsonUtil;
import com.yahoo.yolean.Exceptions;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;

import java.io.IOException;
import java.net.URI;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.logging.Logger;

import static com.yahoo.log.LogLevel.DEBUG;
import static java.util.Collections.emptyList;

/**
 * This class is used to retrieve metrics from a single Vespa node over http.
 * Keeps and serves a snapshot of the node's metrics, with a fixed TTL, to
 * avoid unnecessary load on metrics proxies.
 *
 * @author gjoranv
 */
public class NodeMetricsClient {
    private static final Logger log = Logger.getLogger(NodeMetricsClient.class.getName());

    static final Duration METRICS_TTL = Duration.ofSeconds(30);

    private final HttpClient httpClient;
    private final Node node;
    private final Clock clock;

    private List<MetricsPacket.Builder> metrics = emptyList();
    private Instant metricsTimestamp = Instant.EPOCH;
    private long snapshotsRetrieved = 0;

    public NodeMetricsClient(HttpClient httpClient, Node node, Clock clock) {
        this.httpClient = httpClient;
        this.node = node;
        this.clock = clock;
    }

    public List<MetricsPacket.Builder> getMetrics() {
        if (Instant.now(clock).isAfter(metricsTimestamp.plus(METRICS_TTL))) {
            retrieveMetrics();
        }
        return metrics;
    }

    private void retrieveMetrics() {
        log.log(DEBUG, () -> "Retrieving metrics from host " + node.metricsUri);

        try {
            String metricsJson = httpClient.execute(new HttpGet(node.metricsUri), new BasicResponseHandler());
            metrics = GenericJsonUtil.toMetricsPackets(metricsJson);
            metricsTimestamp = Instant.now(clock);
            snapshotsRetrieved ++;
            log.log(DEBUG, () -> "Successfully retrieved " + metrics.size() + " metrics packets from " + node.metricsUri);

        } catch (IOException e) {
            log.warning("Unable to retrieve metrics from " + node.metricsUri + ": " + Exceptions.toMessageString(e));
            metrics = emptyList();
        }
    }

    long snapshotsRetrieved() {
        return snapshotsRetrieved;
    }

    // TODO: move to separate file
    static class Node {
        final String configId;
        final String host;
        final int port;
        final URI metricsUri;

        public Node(String configId, String host, int port) {
            this.configId = configId;
            this.host = host;
            this.port = port;
            metricsUri = getMetricsUri(host, port);
        }

        private static URI getMetricsUri(String host, int port) {
          return URI.create("http://" + host + ":" + port + MetricsHandler.VALUES_PATH);
        }

    }

}