summaryrefslogtreecommitdiffstats
path: root/simplemetrics/src/main/java/com/yahoo/metrics/simple/Counter.java
diff options
context:
space:
mode:
Diffstat (limited to 'simplemetrics/src/main/java/com/yahoo/metrics/simple/Counter.java')
-rw-r--r--simplemetrics/src/main/java/com/yahoo/metrics/simple/Counter.java73
1 files changed, 73 insertions, 0 deletions
diff --git a/simplemetrics/src/main/java/com/yahoo/metrics/simple/Counter.java b/simplemetrics/src/main/java/com/yahoo/metrics/simple/Counter.java
new file mode 100644
index 00000000000..a35be17e75c
--- /dev/null
+++ b/simplemetrics/src/main/java/com/yahoo/metrics/simple/Counter.java
@@ -0,0 +1,73 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.metrics.simple;
+
+import com.google.common.annotations.Beta;
+import com.yahoo.metrics.simple.UntypedMetric.AssumedType;
+
+/**
+ * A counter metric. Create a counter by declaring it with
+ * {@link MetricReceiver#declareCounter(String)} or
+ * {@link MetricReceiver#declareCounter(String, Point)}.
+ *
+ * @author steinar
+ */
+@Beta
+public class Counter {
+ private final Point defaultPosition;
+ private final String name;
+ private final MetricReceiver metricReceiver;
+
+ Counter(String name, Point defaultPosition, MetricReceiver receiver) {
+ this.name = name;
+ this.defaultPosition = defaultPosition;
+ this.metricReceiver = receiver;
+ }
+
+ /**
+ * Increase the dimension-less/zero-point value of this counter by 1.
+ */
+ public void add() {
+ add(1L, defaultPosition);
+ }
+
+ /**
+ * Add to the dimension-less/zero-point value of this counter.
+ *
+ * @param n the amount by which to increase this counter
+ */
+ public void add(long n) {
+ add(n, defaultPosition);
+ }
+
+ /**
+ * Increase this metric at the given point by 1.
+ *
+ * @param p the point in the metric space at which to increase this metric by 1
+ */
+ public void add(Point p) {
+ add(1L, p);
+ }
+
+ /**
+ * Add to this metric at the given point.
+ *
+ * @param n
+ * the amount by which to increase this counter
+ * @param p
+ * the point in the metric space at which to add to the metric
+ */
+ public void add(long n, Point p) {
+ metricReceiver.update(new Sample(new Measurement(Long.valueOf(n)), new Identifier(name, p), AssumedType.COUNTER));
+ }
+
+ /**
+ * Create a PointBuilder with default dimension values as given when this
+ * counter was declared.
+ *
+ * @return a PointBuilder reflecting the default dimension values of this
+ * counter
+ */
+ public PointBuilder builder() {
+ return new PointBuilder(defaultPosition);
+ }
+}