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
|
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.server.jetty;
import com.yahoo.concurrent.DaemonThreadFactory;
import com.yahoo.jdisc.Metric;
import org.eclipse.jetty.io.ConnectionStatistics;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandlerContainer;
import org.eclipse.jetty.server.handler.StatisticsHandler;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Reports server/connector specific metrics for Jdisc and Jetty
*
* @author bjorncs
*/
class ServerMetricReporter {
private final ScheduledExecutorService executor =
Executors.newScheduledThreadPool(1, new DaemonThreadFactory("jdisc-jetty-metric-reporter-"));
private final Metric metric;
private final Server jetty;
ServerMetricReporter(Metric metric, Server jetty) {
this.metric = metric;
this.jetty = jetty;
}
void start() {
executor.scheduleAtFixedRate(new ReporterTask(), 0, 2, TimeUnit.SECONDS);
}
void shutdown() {
try {
executor.shutdownNow();
executor.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
private class ReporterTask implements Runnable {
private final Instant timeStarted = Instant.now();
@Override
public void run() {
HttpResponseStatisticsCollector statisticsCollector = ((AbstractHandlerContainer) jetty.getHandler())
.getChildHandlerByClass(HttpResponseStatisticsCollector.class);
if (statisticsCollector != null) {
setServerMetrics(statisticsCollector);
}
// reset statisticsHandler to preserve earlier behavior
StatisticsHandler statisticsHandler = ((AbstractHandlerContainer) jetty.getHandler())
.getChildHandlerByClass(StatisticsHandler.class);
if (statisticsHandler != null) {
statisticsHandler.statsReset();
}
for (Connector connector : jetty.getConnectors()) {
setConnectorMetrics((JDiscServerConnector)connector);
}
setJettyThreadpoolMetrics();
}
private void setServerMetrics(HttpResponseStatisticsCollector statisticsCollector) {
long timeSinceStarted = System.currentTimeMillis() - timeStarted.toEpochMilli();
metric.set(MetricDefinitions.STARTED_MILLIS, timeSinceStarted, null);
addResponseMetrics(statisticsCollector);
}
private void addResponseMetrics(HttpResponseStatisticsCollector statisticsCollector) {
for (var metricEntry : statisticsCollector.takeStatistics()) {
Map<String, Object> dimensions = new HashMap<>();
dimensions.put(MetricDefinitions.METHOD_DIMENSION, metricEntry.method);
dimensions.put(MetricDefinitions.SCHEME_DIMENSION, metricEntry.scheme);
dimensions.put(MetricDefinitions.REQUEST_TYPE_DIMENSION, metricEntry.requestType);
dimensions.put(MetricDefinitions.PROTOCOL_DIMENSION, metricEntry.protocol);
metric.add(metricEntry.name, metricEntry.value, metric.createContext(dimensions));
}
}
private void setJettyThreadpoolMetrics() {
QueuedThreadPool threadpool = (QueuedThreadPool) jetty.getThreadPool();
metric.set(MetricDefinitions.JETTY_THREADPOOL_MAX_THREADS, threadpool.getMaxThreads(), null);
metric.set(MetricDefinitions.JETTY_THREADPOOL_MIN_THREADS, threadpool.getMinThreads(), null);
metric.set(MetricDefinitions.JETTY_THREADPOOL_RESERVED_THREADS, threadpool.getReservedThreads(), null);
metric.set(MetricDefinitions.JETTY_THREADPOOL_BUSY_THREADS, threadpool.getBusyThreads(), null);
metric.set(MetricDefinitions.JETTY_THREADPOOL_IDLE_THREADS, threadpool.getIdleThreads(), null);
metric.set(MetricDefinitions.JETTY_THREADPOOL_TOTAL_THREADS, threadpool.getThreads(), null);
metric.set(MetricDefinitions.JETTY_THREADPOOL_QUEUE_SIZE, threadpool.getQueueSize(), null);
}
private void setConnectorMetrics(JDiscServerConnector connector) {
ConnectionStatistics statistics = connector.getStatistics();
metric.set(MetricDefinitions.NUM_CONNECTIONS, statistics.getConnectionsTotal(), connector.getConnectorMetricContext());
metric.set(MetricDefinitions.NUM_OPEN_CONNECTIONS, statistics.getConnections(), connector.getConnectorMetricContext());
metric.set(MetricDefinitions.NUM_CONNECTIONS_OPEN_MAX, statistics.getConnectionsMax(), connector.getConnectorMetricContext());
metric.set(MetricDefinitions.CONNECTION_DURATION_MAX, statistics.getConnectionDurationMax(), connector.getConnectorMetricContext());
metric.set(MetricDefinitions.CONNECTION_DURATION_MEAN, statistics.getConnectionDurationMean(), connector.getConnectorMetricContext());
metric.set(MetricDefinitions.CONNECTION_DURATION_STD_DEV, statistics.getConnectionDurationStdDev(), connector.getConnectorMetricContext());
}
}
}
|