forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 2
/
AbstractMetricsHandler.java
134 lines (118 loc) · 4.76 KB
/
AbstractMetricsHandler.java
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.flink.runtime.rest.handler.legacy.metrics;
import org.apache.flink.runtime.jobmaster.JobManagerGateway;
import org.apache.flink.runtime.rest.handler.legacy.AbstractJsonRequestHandler;
import org.apache.flink.runtime.rest.handler.legacy.JsonFactory;
import org.apache.flink.util.FlinkException;
import org.apache.flink.util.Preconditions;
import com.fasterxml.jackson.core.JsonGenerator;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.Executor;
/**
* Abstract request handler that returns a list of all available metrics or the values for a set of metrics.
*
* <p>If the query parameters do not contain a "get" parameter the list of all metrics is returned.
* {@code [ { "id" : "X" } ] }
*
* <p>If the query parameters do contain a "get" parameter a comma-separate list of metric names is expected as a value.
* {@code /get?X,Y}
* The handler will then return a list containing the values of the requested metrics.
* {@code [ { "id" : "X", "value" : "S" }, { "id" : "Y", "value" : "T" } ] }
*/
public abstract class AbstractMetricsHandler extends AbstractJsonRequestHandler {
private final MetricFetcher fetcher;
public AbstractMetricsHandler(Executor executor, MetricFetcher fetcher) {
super(executor);
this.fetcher = Preconditions.checkNotNull(fetcher);
}
@Override
public CompletableFuture<String> handleJsonRequest(Map<String, String> pathParams, Map<String, String> queryParams, JobManagerGateway jobManagerGateway) {
return CompletableFuture.supplyAsync(
() -> {
fetcher.update();
String requestedMetricsList = queryParams.get("get");
try {
return requestedMetricsList != null
? getMetricsValues(pathParams, requestedMetricsList)
: getAvailableMetricsList(pathParams);
} catch (IOException e) {
throw new CompletionException(new FlinkException("Could not retrieve metrics.", e));
}
},
executor);
}
/**
* Returns a Map containing the metrics belonging to the entity pointed to by the path parameters.
*
* @param pathParams REST path parameters
* @param metrics MetricStore containing all metrics
* @return Map containing metrics, or null if no metric exists
*/
protected abstract Map<String, String> getMapFor(Map<String, String> pathParams, MetricStore metrics);
private String getMetricsValues(Map<String, String> pathParams, String requestedMetricsList) throws IOException {
if (requestedMetricsList.isEmpty()) {
/*
* The WebInterface doesn't check whether the list of available metrics was empty. This can lead to a
* request for which the "get" parameter is an empty string.
*/
return "";
}
Map<String, String> metrics = getMapFor(pathParams, fetcher.getMetricStore());
if (metrics == null) {
return "";
}
String[] requestedMetrics = requestedMetricsList.split(",");
StringWriter writer = new StringWriter();
JsonGenerator gen = JsonFactory.JACKSON_FACTORY.createGenerator(writer);
gen.writeStartArray();
for (String requestedMetric : requestedMetrics) {
Object metricValue = metrics.get(requestedMetric);
if (metricValue != null) {
gen.writeStartObject();
gen.writeStringField("id", requestedMetric);
gen.writeStringField("value", metricValue.toString());
gen.writeEndObject();
}
}
gen.writeEndArray();
gen.close();
return writer.toString();
}
private String getAvailableMetricsList(Map<String, String> pathParams) throws IOException {
Map<String, String> metrics = getMapFor(pathParams, fetcher.getMetricStore());
if (metrics == null) {
return "";
}
StringWriter writer = new StringWriter();
JsonGenerator gen = JsonFactory.JACKSON_FACTORY.createGenerator(writer);
gen.writeStartArray();
for (String m : metrics.keySet()) {
gen.writeStartObject();
gen.writeStringField("id", m);
gen.writeEndObject();
}
gen.writeEndArray();
gen.close();
return writer.toString();
}
}