aboutsummaryrefslogtreecommitdiffstats
path: root/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/VespaServices.java
blob: 5b9e1e6765775fb3843df92565422bc551edc767 (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
// Copyright Yahoo. 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.core.MonitoringConfig;
import ai.vespa.metricsproxy.metric.model.DimensionId;
import ai.vespa.metricsproxy.service.VespaServicesConfig.Service;
import com.yahoo.component.annotation.Inject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import static ai.vespa.metricsproxy.core.MetricsConsumers.toUnmodifiableLinkedMap;
import static ai.vespa.metricsproxy.metric.model.DimensionId.toDimensionId;
import static java.util.logging.Level.FINE;

/**
 * Creates representations for the Vespa services running on the node,
 * and provides methods for updating and getting them.
 *
 * @author gjoranv
 */
public class VespaServices {

    private static final Logger log = Logger.getLogger(VespaServices.class.getName());

    public static final String ALL_SERVICES = "all";

    private final ConfigSentinelClient sentinel;
    private final List<VespaService> services;

    @Inject
    public VespaServices(VespaServicesConfig config, MonitoringConfig monitoringConfig, ConfigSentinelClient sentinel) {
        this.sentinel = sentinel;
        this.services = createServices(config, monitoringConfig.systemName());
        updateServices(services);
    }

    public VespaServices(List<VespaService> services) {
        this.services = services;
        sentinel = null;
    }

    private List<VespaService> createServices(VespaServicesConfig servicesConfig, String monitoringSystemName) {
        List<VespaService> services = new ArrayList<>();
        for (Service s : servicesConfig.service()) {
            log.log(FINE, () -> "Creating service " + s.name());
            VespaService vespaService = VespaService.create(s.name(), s.configId(), s.port(), monitoringSystemName,
                                                            createServiceDimensions(s));
            services.add(vespaService);
        }
        log.log(FINE, () -> "Created new services: " + services.size());
        return services;
    }

    /**
     * Sets 'alive=false' for services that are no longer running.
     * Note that the status is updated in-place for the given services.
     */
    public final void updateServices(List<VespaService> services) {
        if (sentinel != null) {
            log.log(FINE, "Updating services ");
            sentinel.updateServiceStatuses(services);
        }
    }

    /**
     * Get all known vespa services
     *
     * @return A list of VespaService objects
     */
    public List<VespaService> getVespaServices() {
        return List.copyOf(services);
    }

    /**
     * @param id The configid
     * @return A list with size 1 as there should only be one service with the given configid
     */
    public List<VespaService> getInstancesById(String id) {
        List<VespaService> myServices = new ArrayList<>();
        for (VespaService s : services) {
            if (s.getConfigId().equals(id)) {
                myServices.add(s);
            }
        }

        return myServices;
    }

    /**
     * Get services matching pattern for the name used in the monitoring system.
     *
     * @param service name in monitoring system + service name, without index, e.g: vespa.container
     * @return A list of VespaServices
     */
    public List<VespaService> getMonitoringServices(String service) {
        if (service.equalsIgnoreCase(ALL_SERVICES))
            return services;

        List<VespaService> myServices = new ArrayList<>();
        for (VespaService s : services) {
            log.log(FINE, () -> "getMonitoringServices. service=" + service + ", checking against " + s + ", which has monitoring name " + s.getMonitoringName());
            if (s.getMonitoringName().id.equalsIgnoreCase(service)) {
                myServices.add(s);
            }
        }

        return myServices;
    }

    private static Map<DimensionId, String> createServiceDimensions(Service service) {
        return service.dimension().stream().collect(
                toUnmodifiableLinkedMap(dim -> toDimensionId(dim.key()), Service.Dimension::value));
    }

}