Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit e4b773d

Browse files
committed
Add Resource, constantLabels and builder for java example
1 parent 8a3d560 commit e4b773d

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

metrics/Gauge.md

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ The `Gauge` values can be negative. This document describes the key types and th
88
The value that is published for gauges is an instantaneous measurement of an `int64` or `double` value. This API is useful when you want to manually increase and decrease values as per service requirements.
99

1010
The following general operations MUST be provided by the API:
11-
* Defining a `name`, `description`, `unit`, `labelKeys` and `constantLabels` which are fixed labels that always apply to a gauge. This should give back the gauge object to get or create time series, remove time series and clear all time series.
11+
* Defining a `name`, `description`, `unit`, `labelKeys`, `resource` and `constantLabels` which are fixed labels that always apply to a gauge. This should give back the gauge object to get or create time series, remove time series and clear all time series.
1212
* `name`: a string describing the name of the metric, e.g. "vm_cpu_cycles" or "queue_size". Names MUST be unique within the library. It is recommended to use names compatible with the intended end usage.
1313
* `description`: a string describing the metric, e.g."Virtual cycles executed on VM".
14-
* `unit`: a string describing the unit used for the metric. Follows the format described by
14+
* `unit`: a string describing the unit used for the metric (default set to "1"). Follows the format described by
1515
[Unified Code for Units of Measure](http://unitsofmeasure.org/ucum.html).
1616
* `labelKeys`: the list of the label keys to track different types of metric.
17-
* `constantLabels`: the map of label keys and label values.
17+
* `constantLabels`: the map of label keys and label values. The keys in `labelKeys` should not be the same as `constantLabels`'s keys.
18+
* `resource`: the optional associated monitored resource information.
1819
* Add a new time series with label values, which returns a `Point` (which is part of the `TimeSeries`). Each point represents an instantaneous measurement of a varying gauge value. Each Gauge Metric has one or more time series for a single metric.
1920
* `labelValues`: the list of label values. The number of label values must be the same to that of the label keys.
2021
* The `Point` class should provide functionalities to manually increment/decrement values. Example: `add(long amt)`, `set(long value)`.
@@ -29,8 +30,14 @@ private static final MetricRegistry metricRegistry = Metrics.getMetricRegistry()
2930

3031
List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "description"));
3132
List<LabelValue> labelValues = Arrays.asList(LabelValue.create("queue1"));
33+
LabelKey constantLabelKey = LabelKey.create("hostname", "hostname");
34+
LabelValue constantLabelValue = LabelValue.create("localhost");
3235

33-
LongGauge gauge = metricRegistry.addLongGauge("queue_size", "The number of jobs", "1", labelKeys);
36+
Map<LabelKey, LabelValue> constantLabels = Collections.singletonMap(constantLabelKey, constantLabelValue);
37+
38+
LongGauge gauge = metricRegistry.addLongGauge().toBuilder()
39+
.setName("queue_size").setDescription("The number of jobs")
40+
.setLabelKeys(labelKeys).setConstantLabels(constantLabels).build();
3441
LongPoint point = gauge.getOrCreateTimeSeries(labelValues);
3542

3643
void doSomeWork() {
@@ -44,16 +51,17 @@ It is recommended to keep a reference of a point for manual operations instead o
4451
The value that is published for gauges is an instantaneous measurement of an `int64` or `double` value. This gauge is self sufficient once created, so users should never need to interact with it. The value of the gauge is observed from the `object` and a `callback function`. The callback function is invoked whenever metrics are collected, meaning the reported value is up-to-date. The implementation should keep a `WeakReference` to the object and it is the user's responsibility to manage the lifetime of the object.
4552

4653
The following general operations MUST be provided by the API:
47-
* Defining a `name`, `description`, `unit`, `labelKeys` and `constantLabels` which are fixed labels that always apply to a gauge. This should give back gauge object to add new time series, remove time series and clear all time series.
54+
* Defining a `name`, `description`, `unit`, `labelKeys`, `resource` and `constantLabels` which are fixed labels that always apply to a gauge. This should give back gauge object to add new time series, remove time series and clear all time series.
4855
* `name`: a string describing the name of the metric, e.g. "vm_cpu_cycles". Names MUST be unique within the library. It is recommended to use names compatible with the intended end usage.
4956
* `description`: a string describing the metric, e.g."Virtual cycles executed on VM".
50-
* `unit`: a string describing the unit used for the metric. Follows the format described by
57+
* `unit`: a string describing the unit used for the metric (default set to "1"). Follows the format described by
5158
[Unified Code for Units of Measure](http://unitsofmeasure.org/ucum.html).
5259
* `labelKeys`: the list of the label keys to track different types of metric.
53-
* `constantLabels`: the map of label keys and label values.
60+
* `constantLabels`: the map of label keys and label values. The keys in `labelKeys` should not be the same as `constantLabels`'s keys.
61+
* `resource`: the optional associated monitored resource information.
5462
* Add a new time series with label values, an `object` and a `callback function`. The number of label values must be the same to that of the label keys.
5563
* `labelValues`: the list of label values. The number of label values must be the same to that of the label keys.
56-
* `obj`: the state object from which the function derives a measurement.
64+
* `object`: the state object from which the function derives a measurement.
5765
* `function`: the callback function to be called.
5866
* Remove a single time series from the gauge metric, if it is present.
5967
* `labelValues`: the list of label values.
@@ -65,10 +73,14 @@ private static final MetricRegistry metricRegistry = Metrics.getMetricRegistry()
6573

6674
List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "description"));
6775
List<LabelValue> labelValues = Arrays.asList(LabelValue.create("value1"));
76+
LabelKey constantLabelKey = LabelKey.create("hostname", "hostname");
77+
LabelValue constantLabelValue = LabelValue.create("localhost");
78+
79+
Map<LabelKey, LabelValue> constantLabels = Collections.singletonMap(constantLabelKey, constantLabelValue);
6880

69-
DerivedDoubleGauge gauge = metricRegistry.addDerivedDoubleGauge(
70-
"vm_cpu_cycles", "Virtual cycles executed on VM", "1", labelKeys);
71-
CpuInfo cpuInfo = new CpuInfo();
81+
DerivedDoubleGauge gauge = metricRegistry.addDerivedDoubleGauge().toBuilder()
82+
.setName("vm_cpu_cycles").setDescription("Virtual cycles executed on VM")
83+
.setLabelKeys(labelKeys).setConstantLabels(constantLabels).build();
7284

7385
gauge.createTimeSeries(labelValues, cpuInfo,
7486
new ToDoubleFunction<CpuInfo>() {

0 commit comments

Comments
 (0)