Skip to content

Commit 3685d72

Browse files
Bryce Andersonjenkins
Bryce Anderson
authored and
jenkins
committed
util-stats: Remove metric instantiation methods from MetricBuilder
Problem We have a StatsReceiver that lives on MetricBuilder. It doesn't need to be there. It's currently used mostly for supporting expressions and their convenient `.build()` method which hunts for a stats receiver in the MetricSchema to register the expression with. Solution In order to unwind this we first need to remove the metric creation methods from MetricBuilder. In the future we'll make expressions register themselves with a global instance of a StatsReceiver or even a new interface that feels more purpose driven. Differential Revision: https://phabricator.twitter.biz/D859036
1 parent 20e5f80 commit 3685d72

File tree

5 files changed

+38
-96
lines changed

5 files changed

+38
-96
lines changed

CHANGELOG.rst

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ Note that ``PHAB_ID=#`` and ``RB_ID=#`` correspond to associated messages in com
77
Unreleased
88
----------
99

10+
Breaking API Changes
11+
~~~~~~~~~~~~~~~~~~~~
12+
13+
* util-stats: The metric instantiation methods have been removed from `MetricBuilder`. Use the methods on
14+
`StatsReceiver` to instantiate metrics instead. ``PHAB_ID=D859036``
15+
1016
22.3.0
1117
------
1218

util-jvm/src/main/scala/com/twitter/jvm/JvmStats.scala

+32-20
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,22 @@ object JvmStats {
8080
case compilation =>
8181
val compilationStats = stats.scope("compilation")
8282
gauges.add(
83-
compilationStats.metricBuilder(GaugeType).withCounterishGauge.gauge("time_msec") {
83+
compilationStats.addGauge(
84+
compilationStats.metricBuilder(GaugeType).withCounterishGauge.withName("time_msec")) {
8485
compilation.getTotalCompilationTime().toFloat
8586
})
8687
}
8788

8889
val classes = ManagementFactory.getClassLoadingMXBean()
8990
val classLoadingStats = stats.scope("classes")
9091
gauges.add(
91-
classLoadingStats.metricBuilder(GaugeType).withCounterishGauge.gauge("total_loaded") {
92+
classLoadingStats.addGauge(
93+
classLoadingStats.metricBuilder(GaugeType).withCounterishGauge.withName("total_loaded")) {
9294
classes.getTotalLoadedClassCount().toFloat
9395
})
9496
gauges.add(
95-
classLoadingStats.metricBuilder(GaugeType).withCounterishGauge.gauge("total_unloaded") {
97+
classLoadingStats.addGauge(
98+
classLoadingStats.metricBuilder(GaugeType).withCounterishGauge.withName("total_unloaded")) {
9699
classes.getUnloadedClassCount().toFloat
97100
})
98101
gauges.add(
@@ -142,15 +145,20 @@ object JvmStats {
142145
}
143146

144147
val spStats = stats.scope("safepoint")
145-
gauges.add(spStats.metricBuilder(GaugeType).withCounterishGauge.gauge("sync_time_millis") {
146-
jvm.safepoint.syncTimeMillis.toFloat
147-
})
148-
gauges.add(spStats.metricBuilder(GaugeType).withCounterishGauge.gauge("total_time_millis") {
149-
jvm.safepoint.totalTimeMillis.toFloat
150-
})
151-
gauges.add(spStats.metricBuilder(GaugeType).withCounterishGauge.gauge("count") {
152-
jvm.safepoint.count.toFloat
153-
})
148+
gauges.add(
149+
spStats.addGauge(
150+
spStats.metricBuilder(GaugeType).withCounterishGauge.withName("sync_time_millis")) {
151+
jvm.safepoint.syncTimeMillis.toFloat
152+
})
153+
gauges.add(
154+
spStats.addGauge(
155+
spStats.metricBuilder(GaugeType).withCounterishGauge.withName("total_time_millis")) {
156+
jvm.safepoint.totalTimeMillis.toFloat
157+
})
158+
gauges.add(
159+
spStats.addGauge(spStats.metricBuilder(GaugeType).withCounterishGauge.withName("count")) {
160+
jvm.safepoint.count.toFloat
161+
})
154162

155163
ManagementFactory.getPlatformMXBeans(classOf[BufferPoolMXBean]) match {
156164
case null =>
@@ -169,10 +177,12 @@ object JvmStats {
169177
gcPool.foreach { gc =>
170178
val name = gc.getName.regexSub("""[^\w]""".r) { m => "_" }
171179
val poolCycles =
172-
gcStats.metricBuilder(GaugeType).withCounterishGauge.gauge(name, "cycles") {
180+
gcStats.addGauge(
181+
gcStats.metricBuilder(GaugeType).withCounterishGauge.withName(name, "cycles")) {
173182
gc.getCollectionCount.toFloat
174183
}
175-
val poolMsec = gcStats.metricBuilder(GaugeType).withCounterishGauge.gauge(name, "msec") {
184+
val poolMsec = gcStats.addGauge(
185+
gcStats.metricBuilder(GaugeType).withCounterishGauge.withName(name, "msec")) {
176186
gc.getCollectionTime.toFloat
177187
}
178188

@@ -195,12 +205,14 @@ object JvmStats {
195205
}
196206

197207
// note, these could be -1 if the collector doesn't have support for it.
198-
val cycles = gcStats.metricBuilder(GaugeType).withCounterishGauge.gauge("cycles") {
199-
gcPool.map(_.getCollectionCount).filter(_ > 0).sum.toFloat
200-
}
201-
val msec = gcStats.metricBuilder(GaugeType).withCounterishGauge.gauge("msec") {
202-
gcPool.map(_.getCollectionTime).filter(_ > 0).sum.toFloat
203-
}
208+
val cycles =
209+
gcStats.addGauge(gcStats.metricBuilder(GaugeType).withCounterishGauge.withName("cycles")) {
210+
gcPool.map(_.getCollectionCount).filter(_ > 0).sum.toFloat
211+
}
212+
val msec =
213+
gcStats.addGauge(gcStats.metricBuilder(GaugeType).withCounterishGauge.withName("msec")) {
214+
gcPool.map(_.getCollectionTime).filter(_ > 0).sum.toFloat
215+
}
204216

205217
gauges.add(cycles)
206218
gauges.add(msec)

util-stats/src/main/scala/com/twitter/finagle/stats/MetricBuilder.scala

-45
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.twitter.finagle.stats.MetricBuilder.GaugeType
66
import com.twitter.finagle.stats.MetricBuilder.HistogramType
77
import com.twitter.finagle.stats.MetricBuilder.MetricType
88
import com.twitter.finagle.stats.MetricBuilder.UnlatchedCounter
9-
import java.util.function.Supplier
109
import scala.annotation.varargs
1110

1211
/**
@@ -303,50 +302,6 @@ class MetricBuilder private (
303302
private[MetricBuilder] final def counterishGaugeSchema: MetricBuilder =
304303
this.copy(metricType = CounterishGaugeType)
305304

306-
/**
307-
* Produce a counter as described by the builder inside the underlying StatsReceiver.
308-
* @return the counter created.
309-
*/
310-
@deprecated("Use the StatsReceiver interface for instantiation.", "2022-03-28")
311-
@varargs
312-
final def counter(name: String*): Counter = {
313-
this.statsReceiver.validateMetricType(this, CounterType)
314-
this.statsReceiver.counter(this.withName(name: _*))
315-
}
316-
317-
/**
318-
* Produce a gauge as described by the builder inside the underlying StatsReceiver.
319-
* @return the gauge created.
320-
*/
321-
@deprecated("Use the StatsReceiver interface for instantiation.", "2022-03-28")
322-
final def gauge(name: String*)(f: => Float): Gauge = {
323-
this.statsReceiver.validateMetricType(this, GaugeType)
324-
this.statsReceiver.addGauge(this.withName(name: _*))(f)
325-
}
326-
327-
/**
328-
* Produce a gauge as described by the builder inside the underlying StatsReceiver.
329-
* This API is for Java compatibility
330-
* @return the gauge created.
331-
*/
332-
@deprecated("Use the StatsReceiver interface for instantiation.", "2022-03-28")
333-
@varargs
334-
final def gauge(f: Supplier[Float], name: String*): Gauge = {
335-
this.statsReceiver.validateMetricType(this, GaugeType)
336-
this.statsReceiver.addGauge(this.withName(name: _*))(f.get())
337-
}
338-
339-
/**
340-
* Produce a histogram as described by the builder inside the underlying StatsReceiver.
341-
* @return the histogram created.
342-
*/
343-
@deprecated("Use the StatsReceiver interface for instantiation.", "2022-03-28")
344-
@varargs
345-
final def histogram(name: String*): Stat = {
346-
this.statsReceiver.validateMetricType(this, HistogramType)
347-
this.statsReceiver.stat(this.withName(name: _*))
348-
}
349-
350305
def canEqual(other: Any): Boolean = other.isInstanceOf[MetricBuilder]
351306

352307
override def equals(other: Any): Boolean = other match {

util-stats/src/test/java/com/twitter/compiletest/MetricBuilderCompilationTest.java

-15
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44

55
import scala.Some;
66

7-
import com.twitter.finagle.stats.Counter;
8-
import com.twitter.finagle.stats.Gauge;
97
import com.twitter.finagle.stats.InMemoryStatsReceiver;
108
import com.twitter.finagle.stats.MetricBuilder;
119
import com.twitter.finagle.stats.MetricTypes;
1210
import com.twitter.finagle.stats.Microseconds;
1311
import com.twitter.finagle.stats.NoRoleSpecified;
14-
import com.twitter.finagle.stats.Stat;
1512
import com.twitter.finagle.stats.StatsReceiver;
1613
import com.twitter.finagle.stats.Verbosity;
1714

@@ -45,16 +42,4 @@ public void testWithMethods() {
4542
.withRelativeName("cool", "name")
4643
.withPercentiles(0.99, 0.88, 0.77);
4744
}
48-
49-
@Test
50-
public void testConstructingMetrics() {
51-
StatsReceiver sr = new InMemoryStatsReceiver();
52-
MetricBuilder gmb = sr.metricBuilder(MetricTypes.GAUGE_TYPE);
53-
MetricBuilder smb = sr.metricBuilder(MetricTypes.HISTOGRAM_TYPE);
54-
MetricBuilder cmb = sr.metricBuilder(MetricTypes.COUNTER_TYPE);
55-
56-
Gauge g = gmb.gauge(() -> 3.0f, "my", "cool", "gauge");
57-
Stat s = smb.histogram("my", "cool", "histogram");
58-
Counter c = cmb.counter("my", "cool", "counter");
59-
}
6045
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.twitter.finagle.stats
22

33
import com.twitter.finagle.stats.MetricBuilder.CounterType
4-
import com.twitter.finagle.stats.MetricBuilder.CounterishGaugeType
5-
import com.twitter.finagle.stats.MetricBuilder.GaugeType
64
import org.scalatest.funsuite.AnyFunSuite
75

86
class MetricBuilderTest extends AnyFunSuite {
@@ -18,18 +16,4 @@ class MetricBuilderTest extends AnyFunSuite {
1816
metadataScopeSeparator.setSeparator("_aTerriblyLongStringForSomeReason_")
1917
assert(mb.toString.contains("foo_aTerriblyLongStringForSomeReason_bar"))
2018
}
21-
22-
test("the .gauge() api builds CounterishGauge vs Gauge where appropriate") {
23-
val mb = MetricBuilder(
24-
name = Seq("c_gauge"),
25-
metricType = GaugeType,
26-
statsReceiver = new InMemoryStatsReceiver
27-
).withCounterishGauge
28-
29-
assert(mb.metricType == CounterishGaugeType)
30-
31-
val g = mb.gauge("c_gauge") { 1.0.toFloat }
32-
33-
assert(g.metadata.toMetricBuilder.get.metricType == CounterishGaugeType)
34-
}
3519
}

0 commit comments

Comments
 (0)