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
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>
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 */
4141public 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