aboutsummaryrefslogtreecommitdiffstats
path: root/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/RemoteMetricsFetcher.java
blob: f375fd1586fc5bbbcabbba3c2bce8e75e05b0f86 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.metricsproxy.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutionException;

/**
 * Fetch metrics for a given vespa service
 *
 * @author Jo Kristian Bergum
 */
public class RemoteMetricsFetcher extends HttpMetricFetcher {

    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 void getMetrics(MetricsParser.Consumer consumer, int fetchCount) {
        try (InputStream stream = getJson()) {
            createMetrics(stream, consumer, fetchCount);
        } catch (IOException | InterruptedException | ExecutionException e) {
        }
    }

    void createMetrics(String data, MetricsParser.Consumer consumer, int fetchCount) {
        try {
            MetricsParser.parse(data, consumer);
        } catch (Exception e) {
            handleException(e, data, fetchCount);
        }
    }
    private void createMetrics(InputStream data, MetricsParser.Consumer consumer, int fetchCount) throws IOException {
        try {
            MetricsParser.parse(data, consumer);
        } catch (Exception e) {
            handleException(e, data, fetchCount);
            while (data.read() != -1) {}
        }
    }
}