Skip to content

Commit cf9fb2b

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

7 files changed

Lines changed: 938 additions & 5 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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public String getAppId() {
143143
/**
144144
* Returns all counter metrics in this snapshot.
145145
*
146-
* @return an unmodifiable view of the counters map
146+
* @return a view of the counters map
147147
*/
148148
public Map<String, Long> getCounters() {
149149
return counters;
@@ -152,7 +152,7 @@ public Map<String, Long> getCounters() {
152152
/**
153153
* Returns all gauge metrics in this snapshot.
154154
*
155-
* @return an unmodifiable view of the gauges map
155+
* @return a view of the gauges map
156156
*/
157157
public Map<String, Double> getGauges() {
158158
return gauges;
@@ -161,7 +161,7 @@ public Map<String, Double> getGauges() {
161161
/**
162162
* Returns all summary metrics in this snapshot.
163163
*
164-
* @return an unmodifiable view of the summaries map
164+
* @return a view of the summaries map
165165
*/
166166
public Map<String, Double> getSummaries() {
167167
return summaries;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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.zookeeper.metrics.timeline;
20+
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import java.util.Properties;
24+
25+
/**
26+
* Mock implementation of TimelineMetricsSink for testing purposes.
27+
*
28+
* <p>This mock captures all snapshots sent to it and provides methods
29+
* to verify the snapshots in tests.</p>
30+
*/
31+
public class MockTimelineMetricsSink implements TimelineMetricsSink {
32+
33+
private final List<MetricSnapshot> snapshots = new ArrayList<>();
34+
private Properties configuration;
35+
private boolean configured = false;
36+
private boolean closed = false;
37+
private volatile boolean throwOnSend = false;
38+
private volatile boolean throwOnConfigure = false;
39+
40+
@Override
41+
public void configure(Properties configuration) throws Exception {
42+
if (throwOnConfigure) {
43+
throw new Exception("Mock configuration failure");
44+
}
45+
this.configuration = configuration;
46+
this.configured = true;
47+
}
48+
49+
@Override
50+
public void send(MetricSnapshot snapshot) throws Exception {
51+
if (throwOnSend) {
52+
throw new Exception("Mock send failure");
53+
}
54+
synchronized (snapshots) {
55+
snapshots.add(snapshot);
56+
}
57+
}
58+
59+
@Override
60+
public void close() throws Exception {
61+
this.closed = true;
62+
}
63+
64+
/**
65+
* Returns all captured snapshots.
66+
*/
67+
public List<MetricSnapshot> getSnapshots() {
68+
synchronized (snapshots) {
69+
return new ArrayList<>(snapshots);
70+
}
71+
}
72+
73+
/**
74+
* Returns the last captured snapshot, or null if none.
75+
*/
76+
public MetricSnapshot getLastSnapshot() {
77+
synchronized (snapshots) {
78+
return snapshots.isEmpty() ? null : snapshots.get(snapshots.size() - 1);
79+
}
80+
}
81+
82+
/**
83+
* Returns the number of snapshots captured.
84+
*/
85+
public int getSnapshotCount() {
86+
synchronized (snapshots) {
87+
return snapshots.size();
88+
}
89+
}
90+
91+
/**
92+
* Clears all captured snapshots.
93+
*/
94+
public void clearSnapshots() {
95+
synchronized (snapshots) {
96+
snapshots.clear();
97+
}
98+
}
99+
100+
/**
101+
* Returns the configuration passed to configure().
102+
*/
103+
public Properties getConfiguration() {
104+
return configuration;
105+
}
106+
107+
/**
108+
* Returns whether configure() was called.
109+
*/
110+
public boolean isConfigured() {
111+
return configured;
112+
}
113+
114+
/**
115+
* Returns whether close() was called.
116+
*/
117+
public boolean isClosed() {
118+
return closed;
119+
}
120+
121+
/**
122+
* Sets whether send() should throw an exception.
123+
*/
124+
public void setThrowOnSend(boolean throwOnSend) {
125+
this.throwOnSend = throwOnSend;
126+
}
127+
128+
/**
129+
* Sets whether configure() should throw an exception.
130+
*/
131+
public void setThrowOnConfigure(boolean throwOnConfigure) {
132+
this.throwOnConfigure = throwOnConfigure;
133+
}
134+
}

0 commit comments

Comments
 (0)