summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@verizonmedia.com>2020-02-25 09:53:10 +0100
committerJon Bratseth <bratseth@verizonmedia.com>2020-02-25 09:53:10 +0100
commitd06558911d8194da799a630d1b55abfae8ab836b (patch)
tree676dea7b77ad15d96ac07194e59971f4b9d021a6
parent7391d147691f8a907f9beb2c2659d57a352de80c (diff)
Fetch metrics from a container
-rw-r--r--config-model/src/main/java/com/yahoo/vespa/model/admin/monitoring/MetricsConsumer.java5
-rw-r--r--node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/NodeMetricsHttpFetcher.java65
2 files changed, 64 insertions, 6 deletions
diff --git a/config-model/src/main/java/com/yahoo/vespa/model/admin/monitoring/MetricsConsumer.java b/config-model/src/main/java/com/yahoo/vespa/model/admin/monitoring/MetricsConsumer.java
index 529ed6ecf67..403788e3af6 100644
--- a/config-model/src/main/java/com/yahoo/vespa/model/admin/monitoring/MetricsConsumer.java
+++ b/config-model/src/main/java/com/yahoo/vespa/model/admin/monitoring/MetricsConsumer.java
@@ -13,12 +13,13 @@ import java.util.Objects;
*/
@Immutable
public class MetricsConsumer {
+
private final String id;
private final MetricSet metricSet;
/**
- * @param id The consumer
- * @param metricSet The metrics for this consumer
+ * @param id the consumer
+ * @param metricSet the metrics for this consumer
*/
public MetricsConsumer(String id, MetricSet metricSet) {
this.id = Objects.requireNonNull(id, "A consumer must have a non-null id.");;
diff --git a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/NodeMetricsHttpFetcher.java b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/NodeMetricsHttpFetcher.java
index 480184798fd..5ac96a94a08 100644
--- a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/NodeMetricsHttpFetcher.java
+++ b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/NodeMetricsHttpFetcher.java
@@ -1,23 +1,80 @@
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.autoscale;
+import ai.vespa.util.http.VespaHttpClientBuilder;
+import com.yahoo.component.AbstractComponent;
import com.yahoo.config.provision.ApplicationId;
+import com.yahoo.vespa.applicationmodel.HostName;
+import com.yahoo.vespa.hosted.provision.Node;
+import com.yahoo.vespa.hosted.provision.NodeRepository;
+import com.yahoo.vespa.orchestrator.HostNameNotFoundException;
+import com.yahoo.vespa.orchestrator.Orchestrator;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.BasicResponseHandler;
+import org.apache.http.impl.client.CloseableHttpClient;
+import java.io.IOException;
+import java.io.UncheckedIOException;
import java.util.Collection;
+import java.util.logging.Level;
+import java.util.logging.Logger;
/**
* Fetches node metrics over the metrics/v2 API
*
* @author bratseth
*/
-public class NodeMetricsHttpFetcher implements NodeMetrics {
+public class NodeMetricsHttpFetcher extends AbstractComponent implements NodeMetrics {
- private static final String apiPath = "/metrics/v2";
+ private static final Logger log = Logger.getLogger(NodeMetricsHttpFetcher.class.getName());
+
+ private static final String apiPath = "/metrics/v2/values";
+
+ private final NodeRepository nodeRepository;
+ private final Orchestrator orchestrator;
+ private final CloseableHttpClient httpClient;
+
+ public NodeMetricsHttpFetcher(NodeRepository nodeRepository, Orchestrator orchestrator) {
+ this.nodeRepository = nodeRepository;
+ this.orchestrator = orchestrator;
+ httpClient = VespaHttpClientBuilder.createWithBasicConnectionManager().build();
+ }
@Override
public Collection<MetricValue> fetchMetrics(ApplicationId application) {
- String response = ""; // TODO: Fetch the "system" metrics set
- return new MetricsResponse(response).metrics();
+ Node metricsV2Container = nodeRepository.list()
+ .state(Node.State.active)
+ .container()
+ .filter(node -> expectedUp(node))
+ .asList().get(0);
+ String url = "https://" + metricsV2Container.hostname() + ":" + 4443 + apiPath + "?consumer=vespa-consumer-metrics";
+
+ try {
+ String response = httpClient.execute(new HttpGet(url), new BasicResponseHandler());
+ return new MetricsResponse(response).metrics();
+ }
+ catch (IOException e) {
+ throw new UncheckedIOException("Failed to get metrics on " + url, e);
+ }
+ }
+
+ @Override
+ public void deconstruct() {
+ try {
+ httpClient.close();
+ }
+ catch (IOException e) {
+ log.log(Level.WARNING, "Exception deconstructing", e);
+ }
+ }
+
+ private boolean expectedUp(Node node) {
+ try {
+ return ! orchestrator.getNodeStatus(new HostName(node.hostname())).isSuspended();
+ }
+ catch (HostNameNotFoundException e) {
+ return false;
+ }
}
}