|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.ambari.server.metrics.system.impl; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.List; |
| 24 | +import java.util.concurrent.Executors; |
| 25 | +import java.util.concurrent.ScheduledExecutorService; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | +import java.util.stream.Collectors; |
| 28 | + |
| 29 | +import org.apache.ambari.server.metrics.system.SingleMetric; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | +import org.springframework.web.socket.config.WebSocketMessageBrokerStats; |
| 33 | + |
| 34 | +/** |
| 35 | + * Gets the metrics about stomp stats connections and publishes to configured |
| 36 | + * Metric Sink. |
| 37 | + */ |
| 38 | +public class StompStatsMetricsSource extends AbstractMetricsSource { |
| 39 | + public static final String[] metricsTypes = { "stomp.api", "stomp.agent" }; |
| 40 | + public static final String[] poolMetrics = { "pool_size", "active_threads", "queued_tasks", "completed_tasks" }; |
| 41 | + public static final String[] webSocketMetrics = { "current_client_session", "current_web_socket_session", |
| 42 | + "current_http_stream", "current_http_polling", |
| 43 | + "total_sessions_established", "abnormally_closed_session", "connect_failure_session", |
| 44 | + "send_time_limit_exceeded", "transport_errors_sessions" }; |
| 45 | + public static final String[] stompSubProtocolMetrics = { "connect", "connected", "disconnect" }; |
| 46 | + private WebSocketMessageBrokerStats apiStompStats; |
| 47 | + private WebSocketMessageBrokerStats agentStompStats; |
| 48 | + private static final Logger LOG = LoggerFactory.getLogger(StompEventsMetricsSource.class); |
| 49 | + private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); |
| 50 | + |
| 51 | + @Override |
| 52 | + public void start() { |
| 53 | + int interval = 60; |
| 54 | + LOG.info("Starting stomp stat source."); |
| 55 | + try { |
| 56 | + executor.scheduleWithFixedDelay(new Runnable() { |
| 57 | + @Override |
| 58 | + public void run() { |
| 59 | + List<SingleMetric> events = getStompStatMetrics(); |
| 60 | + if (!events.isEmpty()) { |
| 61 | + sink.publish(events); |
| 62 | + LOG.debug("********* Published stomp stat metrics to sink **********"); |
| 63 | + } |
| 64 | + } |
| 65 | + }, interval, interval, TimeUnit.SECONDS); |
| 66 | + } catch (Exception e) { |
| 67 | + LOG.info("Throwing exception when starting stomp stat source", e); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + protected List<SingleMetric> getStompStatMetrics() { |
| 72 | + List<SingleMetric> metrics = new ArrayList<>(); |
| 73 | + populateStompStatMetrics(metrics, metricsTypes[0], apiStompStats); |
| 74 | + populateStompStatMetrics(metrics, metricsTypes[1], agentStompStats); |
| 75 | + return metrics; |
| 76 | + } |
| 77 | + |
| 78 | + private void populateStompStatMetrics(List<SingleMetric> metricsList, String metricsPrefix, |
| 79 | + WebSocketMessageBrokerStats stompStats) { |
| 80 | + parseStats(metricsList, metricsPrefix + ".websocket.", webSocketMetrics, |
| 81 | + stompStats.getWebSocketSessionStatsInfo()); |
| 82 | + parseStats(metricsList, metricsPrefix + ".stomp_sub_protocol.", stompSubProtocolMetrics, |
| 83 | + stompStats.getStompSubProtocolStatsInfo()); |
| 84 | + parseStats(metricsList, metricsPrefix + ".inbound_channel.", poolMetrics, |
| 85 | + stompStats.getClientInboundExecutorStatsInfo()); |
| 86 | + parseStats(metricsList, metricsPrefix + ".outbound_channel.", poolMetrics, |
| 87 | + stompStats.getClientOutboundExecutorStatsInfo()); |
| 88 | + parseStats(metricsList, metricsPrefix + ".sockJsScheduler.", poolMetrics, |
| 89 | + stompStats.getSockJsTaskSchedulerStatsInfo()); |
| 90 | + } |
| 91 | + |
| 92 | + private void parseStats(List<SingleMetric> metricsList, String metricsPrefix, String[] metricsNamesList, |
| 93 | + String stats) { |
| 94 | + if (stats.equals("null")) { |
| 95 | + LOG.warn("stats for " + metricsPrefix + " is null"); |
| 96 | + return; |
| 97 | + } |
| 98 | + List<Long> statsArray = getValuesFromString(stats); |
| 99 | + |
| 100 | + // sanity check to make sure we have the same number of metrics and values. |
| 101 | + if (statsArray.size() != metricsNamesList.length) { |
| 102 | + LOG.error("Number of metrics and stats do not match for " + metricsPrefix); |
| 103 | + return; |
| 104 | + } |
| 105 | + long currentTimeMillis = System.currentTimeMillis(); |
| 106 | + for (int i = 0; i < metricsNamesList.length; i++) { |
| 107 | + metricsList |
| 108 | + .add(new SingleMetric(metricsPrefix + metricsNamesList[i], statsArray.get(i), currentTimeMillis)); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private List<Long> getValuesFromString(String stats) { |
| 113 | + // '\\D+' matches one or more non-digit characters. After that it split the |
| 114 | + // string and filter out empty strings. Then we convert the string to long. |
| 115 | + return Arrays.stream(stats.split("\\D+")) |
| 116 | + .filter(s -> !s.isEmpty()) |
| 117 | + .map(Long::parseLong) |
| 118 | + .collect(Collectors.toList()); |
| 119 | + } |
| 120 | + |
| 121 | + public void setApiStompStats(WebSocketMessageBrokerStats apiStompStats) { |
| 122 | + this.apiStompStats = apiStompStats; |
| 123 | + } |
| 124 | + |
| 125 | + public void setAgentStompStats(WebSocketMessageBrokerStats agentStompStats) { |
| 126 | + this.agentStompStats = agentStompStats; |
| 127 | + } |
| 128 | +} |
0 commit comments