Skip to content

Commit 70f68bb

Browse files
ZOOKEEPER-5036: Add Timeline Metrics Provider for external metrics collection systems - Unit tests and configs
1 parent 1778c05 commit 70f68bb

9 files changed

Lines changed: 1074 additions & 151 deletions

File tree

bin/zkEnv.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ for d in "$ZOOBINDIR"/../zookeeper-metrics-providers/zookeeper-prometheus-metric
113113
CLASSPATH="$d:$CLASSPATH"
114114
done
115115

116+
#make it work for developers
117+
for d in "$ZOOBINDIR"/../zookeeper-metrics-providers/zookeeper-timeline-metrics/target/lib/*.jar; do
118+
CLASSPATH="$d:$CLASSPATH"
119+
done
120+
116121
#make it work for developers
117122
CLASSPATH="$ZOOBINDIR/../build/classes:$CLASSPATH"
118123

@@ -122,6 +127,9 @@ CLASSPATH="$ZOOBINDIR/../zookeeper-server/target/classes:$CLASSPATH"
122127
#make it work for developers
123128
CLASSPATH="$ZOOBINDIR/../zookeeper-metrics-providers/zookeeper-prometheus-metrics/target/classes:$CLASSPATH"
124129

130+
#make it work for developers
131+
CLASSPATH="$ZOOBINDIR/../zookeeper-metrics-providers/zookeeper-timeline-metrics/target/classes:$CLASSPATH"
132+
125133
case "$(uname)" in
126134
CYGWIN* | MINGW*) cygwin=true ;;
127135
*) cygwin=false ;;

zookeeper-metrics-providers/zookeeper-timeline-metrics/pom.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<parent>
2424
<groupId>org.apache.zookeeper</groupId>
2525
<artifactId>zookeeper-metrics-providers</artifactId>
26-
<version>3.9.3</version>
26+
<version>3.10.0-SNAPSHOT</version>
2727
</parent>
2828

2929
<artifactId>zookeeper-timeline-metrics</artifactId>
@@ -47,7 +47,14 @@
4747
<groupId>org.slf4j</groupId>
4848
<artifactId>slf4j-api</artifactId>
4949
</dependency>
50-
50+
51+
<!-- Dropwizard Metrics - needed for AvgMinMaxPercentileCounter -->
52+
<dependency>
53+
<groupId>io.dropwizard.metrics</groupId>
54+
<artifactId>metrics-core</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
5158
<!-- Test dependencies -->
5259
<dependency>
5360
<groupId>org.mockito</groupId>

zookeeper-metrics-providers/zookeeper-timeline-metrics/src/main/java/org/apache/zookeeper/metrics/timeline/MetricSnapshot.java

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Licensed to the Apache Software Foundation (ASF) under one
33
* or more contributor license agreements. See the NOTICE file
44
* distributed with this work for additional information
@@ -22,7 +22,7 @@
2222

2323
/**
2424
* Represents a point-in-time snapshot of ZooKeeper metrics.
25-
*
25+
*
2626
* <p>This class is a data transfer object that captures metric values at a specific
2727
* timestamp for export to Timeline/Ambari Metrics Collector. It contains three types
2828
* of metrics:</p>
@@ -31,27 +31,27 @@
3131
* <li><b>Gauges</b> - Current values that can go up or down (e.g., num_alive_connections)</li>
3232
* <li><b>Summaries</b> - Computed statistics like avg, min, max, percentiles (e.g., latency_avg)</li>
3333
* </ul>
34-
*
34+
*
3535
* <p>Instances of this class are immutable after creation and are sent to the
3636
* Timeline sink for persistence and visualization.</p>
37-
*
37+
*
3838
* @see TimelineMetricsProvider
3939
* @see TimelineMetricsSink
4040
*/
4141
public class MetricSnapshot {
42-
42+
4343
private final long timestamp;
4444
private final String hostname;
4545
private final String appId;
46-
46+
4747
// Separate collections for different metric types
4848
private final Map<String, Long> counters = new HashMap<>();
4949
private final Map<String, Double> gauges = new HashMap<>();
5050
private final Map<String, Double> summaries = new HashMap<>();
51-
51+
5252
/**
5353
* Creates a new metric snapshot.
54-
*
54+
*
5555
* @param timestamp the timestamp in milliseconds since epoch
5656
* @param hostname the hostname of the ZooKeeper server
5757
* @param appId the application ID (typically "zookeeper")
@@ -61,119 +61,119 @@ public MetricSnapshot(long timestamp, String hostname, String appId) {
6161
this.hostname = hostname;
6262
this.appId = appId;
6363
}
64-
64+
6565
/**
6666
* Adds a counter metric to the snapshot.
67-
*
67+
*
6868
* <p>Counters represent monotonically increasing values such as total requests,
6969
* total bytes received, etc.</p>
70-
*
70+
*
7171
* @param name the metric name
7272
* @param value the counter value
7373
*/
7474
public void addCounter(String name, long value) {
7575
counters.put(name, value);
7676
}
77-
77+
7878
/**
7979
* Adds a gauge metric to the snapshot.
80-
*
80+
*
8181
* <p>Gauges represent current values that can increase or decrease, such as
8282
* number of active connections, queue size, etc.</p>
83-
*
83+
*
8484
* @param name the metric name
8585
* @param value the gauge value
8686
*/
8787
public void addGauge(String name, double value) {
8888
gauges.put(name, value);
8989
}
90-
90+
9191
/**
9292
* Adds a summary metric to the snapshot.
93-
*
93+
*
9494
* <p>Summaries represent computed statistics such as averages, minimums, maximums,
9595
* and percentiles. The existing {@link org.apache.zookeeper.server.metric.AvgMinMaxCounter}
9696
* and {@link org.apache.zookeeper.server.metric.AvgMinMaxPercentileCounter} classes
9797
* already compute these values and provide them as separate metrics (e.g., "latency_avg",
9898
* "latency_min", "latency_max", "latency_p99").</p>
99-
*
99+
*
100100
* @param name the metric name (e.g., "request_latency_avg")
101101
* @param value the computed statistic value
102102
*/
103103
public void addSummary(String name, double value) {
104104
summaries.put(name, value);
105105
}
106-
106+
107107
/**
108108
* Returns the total number of metrics in this snapshot.
109-
*
109+
*
110110
* @return the sum of counters, gauges, and summaries
111111
*/
112112
public int getMetricCount() {
113113
return counters.size() + gauges.size() + summaries.size();
114114
}
115-
115+
116116
/**
117117
* Returns the timestamp of this snapshot.
118-
*
118+
*
119119
* @return timestamp in milliseconds since epoch
120120
*/
121121
public long getTimestamp() {
122122
return timestamp;
123123
}
124-
124+
125125
/**
126126
* Returns the hostname of the ZooKeeper server.
127-
*
127+
*
128128
* @return the hostname
129129
*/
130130
public String getHostname() {
131131
return hostname;
132132
}
133-
133+
134134
/**
135135
* Returns the application ID.
136-
*
136+
*
137137
* @return the application ID (typically "zookeeper")
138138
*/
139139
public String getAppId() {
140140
return appId;
141141
}
142-
142+
143143
/**
144144
* Returns all counter metrics in this snapshot.
145-
*
146-
* @return an unmodifiable view of the counters map
145+
*
146+
* @return a view of the counters map
147147
*/
148148
public Map<String, Long> getCounters() {
149149
return counters;
150150
}
151-
151+
152152
/**
153153
* Returns all gauge metrics in this snapshot.
154-
*
155-
* @return an unmodifiable view of the gauges map
154+
*
155+
* @return a view of the gauges map
156156
*/
157157
public Map<String, Double> getGauges() {
158158
return gauges;
159159
}
160-
160+
161161
/**
162162
* Returns all summary metrics in this snapshot.
163-
*
164-
* @return an unmodifiable view of the summaries map
163+
*
164+
* @return a view of the summaries map
165165
*/
166166
public Map<String, Double> getSummaries() {
167167
return summaries;
168168
}
169-
169+
170170
@Override
171171
public String toString() {
172-
return String.format("MetricSnapshot{timestamp=%d, hostname='%s', appId='%s', " +
173-
"counters=%d, gauges=%d, summaries=%d}",
172+
return String.format("MetricSnapshot{timestamp=%d, hostname='%s', appId='%s', "
173+
+ "counters=%d, gauges=%d, summaries=%d}",
174174
timestamp, hostname, appId, counters.size(), gauges.size(), summaries.size());
175175
}
176-
176+
177177
/**
178178
* Helper method to repeat a character n times (Java 8 compatible).
179179
*/
@@ -184,13 +184,13 @@ private String repeatChar(char c, int count) {
184184
}
185185
return sb.toString();
186186
}
187-
187+
188188
/**
189189
* Prints all metrics in this snapshot to a formatted string.
190-
*
190+
*
191191
* <p>This method is useful for debugging and logging. It prints all counters,
192192
* gauges, and summaries in a human-readable format.</p>
193-
*
193+
*
194194
* @return a formatted string containing all metrics
195195
*/
196196
public String printAllMetrics() {

0 commit comments

Comments
 (0)