aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/metrics/SearchNodeMetricsRetriever.java
blob: 86fd2f5c7f4480a976debf061e3c44334ef6fa2a (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 Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.metrics;

import com.yahoo.config.model.api.HostInfo;
import com.yahoo.config.model.api.ServiceInfo;
import com.yahoo.vespa.config.server.application.Application;
import com.yahoo.vespa.config.server.http.v2.response.SearchNodeMetricsResponse;
import java.net.URI;
import java.util.Collection;
import java.util.function.Predicate;

public class SearchNodeMetricsRetriever {

    private final ClusterSearchNodeMetricsRetriever metricsRetriever;

    public SearchNodeMetricsRetriever() {
        this( new ClusterSearchNodeMetricsRetriever());
    }

    public SearchNodeMetricsRetriever(ClusterSearchNodeMetricsRetriever metricsRetriever) {
        this.metricsRetriever = metricsRetriever;
    }

    public SearchNodeMetricsResponse getMetrics(Application application) {
        var hosts = getHostsOfApplication(application);
        var clusterMetrics = metricsRetriever.requestMetricsGroupedByCluster(hosts);
        return new SearchNodeMetricsResponse(application.getId(), clusterMetrics);
    }

    private static Collection<URI> getHostsOfApplication(Application application) {
        return application.getModel().getHosts().stream()
                .filter(host -> host.getServices().stream().anyMatch(isSearchNode()))
                .map(HostInfo::getHostname)
                .map(SearchNodeMetricsRetriever::createMetricsProxyURI)
                .toList();
    }

    private static Predicate<ServiceInfo> isSearchNode() {
        return serviceInfo -> serviceInfo.getServiceType().equalsIgnoreCase("searchnode");
    }

    private static URI createMetricsProxyURI(String hostname) {
        return URI.create("http://" + hostname + ":19092/metrics/v2/values");
    }

}