-
Notifications
You must be signed in to change notification settings - Fork 129
Add a monitor for the OpenMetrics endpoint #584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
gateway-ha/src/main/java/io/trino/gateway/ha/clustermonitor/ClusterStatsMetricsMonitor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| /* | ||
| * Licensed 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 io.trino.gateway.ha.clustermonitor; | ||
|
|
||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.common.collect.ImmutableSet; | ||
| import io.airlift.http.client.HttpClient; | ||
| import io.airlift.http.client.HttpUriBuilder; | ||
| import io.airlift.http.client.Request; | ||
| import io.airlift.http.client.Response; | ||
| import io.airlift.http.client.ResponseHandler; | ||
| import io.airlift.http.client.UnexpectedResponseException; | ||
| import io.airlift.log.Logger; | ||
| import io.trino.gateway.ha.config.BackendStateConfiguration; | ||
| import io.trino.gateway.ha.config.MonitorConfiguration; | ||
| import io.trino.gateway.ha.config.ProxyBackendConfiguration; | ||
| import io.trino.gateway.ha.security.util.BasicCredentials; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.util.Arrays; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import static com.google.common.base.Strings.isNullOrEmpty; | ||
| import static com.google.common.collect.ImmutableMap.toImmutableMap; | ||
| import static io.airlift.http.client.HttpUriBuilder.uriBuilderFrom; | ||
| import static io.airlift.http.client.Request.Builder.prepareGet; | ||
| import static io.airlift.http.client.ResponseHandlerUtils.propagate; | ||
| import static io.trino.gateway.ha.clustermonitor.MonitorUtils.shouldRetry; | ||
| import static java.lang.String.format; | ||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class ClusterStatsMetricsMonitor | ||
| implements ClusterStatsMonitor | ||
| { | ||
| public static final String RUNNING_QUERIES_METRIC = "trino_execution_name_QueryManager_RunningQueries"; | ||
| public static final String QUEUED_QUERIES_METRIC = "trino_execution_name_QueryManager_QueuedQueries"; | ||
| private static final Logger log = Logger.get(ClusterStatsMetricsMonitor.class); | ||
|
|
||
| private final HttpClient httpClient; | ||
| private final int retries; | ||
| private final MetricsResponseHandler metricsResponseHandler; | ||
| private final Header identityHeader; | ||
| private final String metricsEndpoint; | ||
| private final ImmutableSet<String> metricNames; | ||
| private final Map<String, Float> metricMinimumValues; | ||
| private final Map<String, Float> metricMaximumValues; | ||
|
|
||
| public ClusterStatsMetricsMonitor(HttpClient httpClient, BackendStateConfiguration backendStateConfiguration, MonitorConfiguration monitorConfiguration) | ||
| { | ||
| this.httpClient = requireNonNull(httpClient, "httpClient is null"); | ||
| retries = monitorConfiguration.getRetries(); | ||
| if (!isNullOrEmpty(backendStateConfiguration.getPassword())) { | ||
| identityHeader = new Header("Authorization", | ||
| new BasicCredentials(backendStateConfiguration.getUsername(), backendStateConfiguration.getPassword()).getBasicAuthHeader()); | ||
| } | ||
| else { | ||
| identityHeader = new Header("X-Trino-User", backendStateConfiguration.getUsername()); | ||
| } | ||
| metricsEndpoint = monitorConfiguration.getMetricsEndpoint(); | ||
| metricMinimumValues = ImmutableMap.copyOf(monitorConfiguration.getMetricMinimumValues()); | ||
| metricMaximumValues = ImmutableMap.copyOf(monitorConfiguration.getMetricMaximumValues()); | ||
| metricNames = ImmutableSet.<String>builder() | ||
| .add(RUNNING_QUERIES_METRIC, QUEUED_QUERIES_METRIC) | ||
| .addAll(metricMinimumValues.keySet()) | ||
| .addAll(metricMaximumValues.keySet()) | ||
| .build(); | ||
| metricsResponseHandler = new MetricsResponseHandler(metricNames); | ||
| } | ||
|
|
||
| private static ClusterStats getUnhealthyStats(ProxyBackendConfiguration backend) | ||
| { | ||
| return ClusterStats.builder(backend.getName()) | ||
| .trinoStatus(TrinoStatus.UNHEALTHY) | ||
| .proxyTo(backend.getProxyTo()) | ||
| .externalUrl(backend.getExternalUrl()) | ||
| .routingGroup(backend.getRoutingGroup()) | ||
| .build(); | ||
| } | ||
|
|
||
| @Override | ||
| public ClusterStats monitor(ProxyBackendConfiguration backend) | ||
| { | ||
| Map<String, String> metrics = getMetrics(backend.getProxyTo(), retries); | ||
| if (metrics.isEmpty()) { | ||
| log.error("No metrics available for %s!", backend.getName()); | ||
| return getUnhealthyStats(backend); | ||
| } | ||
|
|
||
| for (Map.Entry<String, Float> entry : metricMinimumValues.entrySet()) { | ||
| if (!metrics.containsKey(entry.getKey()) | ||
| || Float.parseFloat(metrics.get(entry.getKey())) < entry.getValue()) { | ||
| log.warn("Health metric value below min for cluster %s: %s=%s", backend.getName(), entry.getKey(), metrics.get(entry.getKey())); | ||
| return getUnhealthyStats(backend); | ||
| } | ||
| } | ||
|
|
||
| for (Map.Entry<String, Float> entry : metricMaximumValues.entrySet()) { | ||
| if (!metrics.containsKey(entry.getKey()) | ||
| || Float.parseFloat(metrics.get(entry.getKey())) > entry.getValue()) { | ||
| log.warn("Health metric value over max for cluster %s: %s=%s", backend.getName(), entry.getKey(), metrics.get(entry.getKey())); | ||
| return getUnhealthyStats(backend); | ||
| } | ||
| } | ||
| return ClusterStats.builder(backend.getName()) | ||
| .trinoStatus(TrinoStatus.HEALTHY) | ||
| .runningQueryCount((int) Float.parseFloat(metrics.get(RUNNING_QUERIES_METRIC))) | ||
| .queuedQueryCount((int) Float.parseFloat(metrics.get(QUEUED_QUERIES_METRIC))) | ||
| .proxyTo(backend.getProxyTo()) | ||
| .externalUrl(backend.getExternalUrl()) | ||
| .routingGroup(backend.getRoutingGroup()) | ||
| .build(); | ||
| } | ||
|
|
||
| private Map<String, String> getMetrics(String baseUrl, int retriesRemaining) | ||
| { | ||
| HttpUriBuilder uri = uriBuilderFrom(URI.create(baseUrl)).appendPath(metricsEndpoint); | ||
| for (String metric : metricNames) { | ||
| uri.addParameter("name[]", metric); | ||
| } | ||
|
|
||
| Request request = prepareGet() | ||
| .setUri(uri.build()) | ||
| .addHeader(identityHeader.name, identityHeader.value) | ||
| .addHeader("Content-Type", "application/openmetrics-text; version=1.0.0; charset=utf-8") | ||
| .build(); | ||
| try { | ||
| return httpClient.execute(request, metricsResponseHandler); | ||
| } | ||
| catch (UnexpectedResponseException e) { | ||
| if (shouldRetry(e.getStatusCode())) { | ||
| if (retriesRemaining > 0) { | ||
| log.warn("Retrying health check on error: %s, ", e.toString()); | ||
| return getMetrics(baseUrl, retriesRemaining - 1); | ||
| } | ||
| log.error("Encountered error %s, no retries remaining", e.toString()); | ||
| } | ||
| log.error(e, "Health check failed with non-retryable response.\n%s", e.toString()); | ||
| } | ||
| catch (Exception e) { | ||
| log.error(e, "Exception checking %s for health", request.getUri()); | ||
| } | ||
| return ImmutableMap.of(); | ||
| } | ||
|
|
||
| private static class MetricsResponseHandler | ||
| implements ResponseHandler<Map<String, String>, RuntimeException> | ||
| { | ||
| private final ImmutableSet<String> requiredKeys; | ||
|
|
||
| public MetricsResponseHandler(Set<String> requiredKeys) | ||
| { | ||
| this.requiredKeys = ImmutableSet.copyOf(requiredKeys); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, String> handleException(Request request, Exception exception) | ||
| throws RuntimeException | ||
| { | ||
| throw propagate(request, exception); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, String> handle(Request request, Response response) | ||
| throws RuntimeException | ||
| { | ||
| try { | ||
| String responseBody = new String(response.getInputStream().readAllBytes(), UTF_8); | ||
| Map<String, String> metrics = Arrays.stream(responseBody.split("\n")) | ||
| .filter(line -> !line.startsWith("#")) | ||
| .collect(toImmutableMap(s -> s.split(" ")[0], s -> s.split(" ")[1])); | ||
| if (!metrics.keySet().containsAll(requiredKeys)) { | ||
| throw new UnexpectedResponseException( | ||
| format("Request is missing required keys: \n%s\nin response: '%s'", String.join("\n", requiredKeys), responseBody), | ||
| request, | ||
| response); | ||
| } | ||
| return metrics; | ||
| } | ||
| catch (IOException e) { | ||
| throw new UnexpectedResponseException(request, response); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private record Header(String name, String value) {} | ||
willmostly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
31 changes: 31 additions & 0 deletions
31
gateway-ha/src/main/java/io/trino/gateway/ha/clustermonitor/MonitorUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Licensed 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 io.trino.gateway.ha.clustermonitor; | ||
|
|
||
| import static java.net.HttpURLConnection.HTTP_BAD_GATEWAY; | ||
| import static java.net.HttpURLConnection.HTTP_GATEWAY_TIMEOUT; | ||
| import static java.net.HttpURLConnection.HTTP_UNAVAILABLE; | ||
|
|
||
| public final class MonitorUtils | ||
| { | ||
| private MonitorUtils() {} | ||
|
|
||
| public static boolean shouldRetry(int statusCode) | ||
| { | ||
| return switch (statusCode) { | ||
| case HTTP_BAD_GATEWAY, HTTP_UNAVAILABLE, HTTP_GATEWAY_TIMEOUT -> true; | ||
| default -> false; | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,5 +19,6 @@ public enum ClusterStatsMonitorType | |
| INFO_API, | ||
| UI_API, | ||
| JDBC, | ||
| JMX | ||
| JMX, | ||
| METRICS | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.