Skip to content

Commit dcc1bf9

Browse files
authored
improve java timers documentation (#228)
There is still a bit of confusion about the fact that Java Timers always report values in seconds. This is an attempt to provide a more complete explanation that Java Timers do this, with references to how it is implemented in code.
1 parent bd8c487 commit dcc1bf9

File tree

11 files changed

+99
-4
lines changed

11 files changed

+99
-4
lines changed

docs/concepts/naming.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,21 @@ further filter the data to a subset in which you have an interest.
9595

9696
## Use Base Units
9797

98-
Keep measurements in base units where possible. It is better to have all timers in seconds, disk
99-
sizes in bytes, and network rates in bytes/second. This allows any SI unit prefixes applied to
100-
tick labels on a graph to have an obvious meaning, such as 1k meaning 1 kilobyte, as opposed to
101-
1 kilo-megabyte.
98+
**Keep measurements in base units, whenever and wherever possible.**
99+
100+
It is best to have timers in `seconds`, disk sizes in `bytes`, and network rates in `bytes/second`.
101+
This allows any International System of Units (SI) prefixes applied to [tick labels] on a graph to
102+
have an obvious meaning, such as:
103+
104+
* `1 m` meaning `1 millisecond`, as opposed to `1 milli-millisecond`, for timers.
105+
* `1 k` meaning `1 kilobyte`, as opposed to `1 kilo-megabyte`, for disk sizes.
106+
* `1 M` meaning `1 megabyte/second`, as opposed to `1 mega-kilobyte`, for network rates.
107+
108+
Atlas automatically applies tick labels to the Y-axis of the graph, in order to accurately report
109+
the magnitude of values, while keeping them within the view window.
110+
111+
Some meters in some clients, such as [Java Timers], will automatically constrain values to base
112+
units in their implementations.
113+
114+
[tick labels]: ../api/graph/tick.md
115+
[Java Timers]: ../spectator/lang/java/meters/timer.md#units

docs/spectator/lang/cpp/meters/percentile-timer.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## Percentile Timer
2+
13
The value is the number of seconds that have elapsed for an event, with percentile estimates.
24

35
This metric type will track the data distribution by maintaining a set of Counters. The
@@ -27,3 +29,7 @@ int main()
2729
r.CreatePercentTimer(requestLatencyMeter).Record(10);
2830
}
2931
```
32+
33+
## Units
34+
35+
See [Timer Units](timer.md#units) for an explanation.

docs/spectator/lang/cpp/meters/timer.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## Timer
2+
13
A Timer is used to measure how long (in seconds) some event is taking.
24

35
Call `Record()` with a value:
@@ -19,3 +21,10 @@ int main()
1921
r.CreateTimer(requestLatencyMeter).Record(10);
2022
}
2123
```
24+
25+
## Units
26+
27+
Ensure that you always report values in seconds (see [Use Base Units]). The API does not offer any
28+
guarantees that the value will be in seconds.
29+
30+
[Use Base Units]: ../../../../concepts/naming.md#use-base-units

docs/spectator/lang/go/meters/percentile-timer.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## Percentile Timer
2+
13
The value is the number of seconds that have elapsed for an event, with percentile estimates.
24

35
This metric type will track the data distribution by maintaining a set of Counters. The
@@ -26,3 +28,7 @@ func main() {
2628
registry.PercentileTimerWithId(requestLatency).Record(500 * time.Millisecond)
2729
}
2830
```
31+
32+
## Units
33+
34+
See [Timer Units](timer.md#units) for an explanation.

docs/spectator/lang/go/meters/timer.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## Timer
2+
13
A Timer is used to measure how long (in seconds) some event is taking.
24

35
Call `record()` with a value:
@@ -18,3 +20,11 @@ func main() {
1820
registry.TimerWithId(requestLatency).Record(500 * time.Millisecond)
1921
}
2022
```
23+
24+
## Units
25+
26+
Go Timers always report values to backends in seconds (see [Use Base Units]). The API accepts a
27+
[time.Duration] value, which is converted to seconds when reporting.
28+
29+
[Use Base Units]: ../../../../concepts/naming.md#use-base-units
30+
[time.Duration]: https://pkg.go.dev/time#Duration

docs/spectator/lang/java/meters/percentile-timer.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,6 @@ is recommended to use a monotonically increasing source for measuring the times,
4949
occasionally having bogus measurements due to time adjustments. For more information, see the
5050
[Clock documentation](../../../core/clock.md).
5151

52+
## Units
53+
54+
See [Timer Units](timer.md#units) for an explanation.

docs/spectator/lang/java/meters/timer.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,15 @@ e.g., update a different Timer, if an exception is thrown.
9292
* Being a Gauge, it is inappropriate for short tasks. In particular, Gauges are sampled and if it
9393
is not sampled during the execution, or the sampling period is a significant subset of the expected
9494
duration, then the duration value will not be meaningful.
95+
96+
## Units
97+
98+
Java Timers always report values to backends in seconds (see [Use Base Units]). The API allows
99+
for recording elapsed time in different units, so it can easily be used with the common ways of
100+
measuring elapsed time provided by the SDK, such as [System.nanoTime()] (monotonic time) and
101+
[System.currentTimeMillis()] (wall clock). Regardless of how it is recorded, when queried on the
102+
backend, the unit will be seconds.
103+
104+
[Use Base Units]: ../../../../concepts/naming.md#use-base-units
105+
[System.nanoTime()]: https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/System.html#nanoTime()
106+
[System.currentTimeMillis()]: https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/System.html#currentTimeMillis()

docs/spectator/lang/nodejs/meters/percentile-timer.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## Percentile Timer
2+
13
The value is the number of seconds that have elapsed for an event, with percentile estimates.
24

35
This metric type will track the data distribution by maintaining a set of Counters. The
@@ -26,3 +28,7 @@ void registry.pct_timer("server.requestLatency").record(process.hrtime(start));
2628

2729
The `record()` method accepts `number[]` output from `hrtime` and converts it to seconds, as a
2830
convenience for recording latencies.
31+
32+
## Units
33+
34+
See [Timer Units](timer.md#units) for an explanation.

docs/spectator/lang/nodejs/meters/timer.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## Timer
2+
13
A Timer is used to measure how long (in seconds) some event is taking.
24

35
Call `record()` with a value:
@@ -18,3 +20,15 @@ void registry.timer("server.requestLatency").record(process.hrtime(start));
1820

1921
The `record()` method accepts `number[]` output from `hrtime` and converts it to seconds, as a
2022
convenience for recording latencies.
23+
24+
## Units
25+
26+
If you use [process.hrtime()] or [process.hrtime.bigint()] to calculate the elapsed time, then the
27+
units will be converted to seconds when reporting values.
28+
29+
If you use a plain `number` value to record values, then ensure that you always report values in
30+
seconds (see [Use Base Units]). No conversion assistance is provided in this case.
31+
32+
[process.hrtime()]: https://nodejs.org/api/process.html#processhrtimetime
33+
[process.hrtime.bigint()]: https://nodejs.org/api/process.html#processhrtimebigint
34+
[Use Base Units]: ../../../../concepts/naming.md#use-base-units

docs/spectator/lang/py/meters/percentile-timer.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## Percentile Timer
2+
13
The value is the number of seconds that have elapsed for an event, with percentile estimates.
24

35
This metric type will track the data distribution by maintaining a set of Counters. The
@@ -35,3 +37,7 @@ with StopWatch(thread_sleep):
3537
```
3638

3739
[Context Manager]: https://docs.python.org/3/reference/datamodel.html#context-managers
40+
41+
## Units
42+
43+
See [Timer Units](timer.md#units) for an explanation.

0 commit comments

Comments
 (0)