Skip to content

Commit 2799a8e

Browse files
authored
Merge pull request #3 from asteurer/add-metrics
Add metrics and refactor tracing
2 parents 885283c + 7af71fc commit 2799a8e

File tree

9 files changed

+431
-72
lines changed

9 files changed

+431
-72
lines changed

.github/workflows/main.yml

Lines changed: 0 additions & 14 deletions
This file was deleted.

wit/deps/example-dep/example-api.wit

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package wasi:clocks@0.2.0;
2+
3+
/// WASI Monotonic Clock is a clock API intended to let users measure elapsed
4+
/// time.
5+
///
6+
/// It is intended to be portable at least between Unix-family platforms and
7+
/// Windows.
8+
///
9+
/// A monotonic clock is a clock which has an unspecified initial value, and
10+
/// successive reads of the clock will produce non-decreasing values.
11+
///
12+
/// It is intended for measuring elapsed time.
13+
interface monotonic-clock {
14+
use wasi:io/poll@0.2.0.{pollable};
15+
16+
/// An instant in time, in nanoseconds. An instant is relative to an
17+
/// unspecified initial value, and can only be compared to instances from
18+
/// the same monotonic-clock.
19+
type instant = u64;
20+
21+
/// A duration of time, in nanoseconds.
22+
type duration = u64;
23+
24+
/// Read the current value of the clock.
25+
///
26+
/// The clock is monotonic, therefore calling this function repeatedly will
27+
/// produce a sequence of non-decreasing values.
28+
now: func() -> instant;
29+
30+
/// Query the resolution of the clock. Returns the duration of time
31+
/// corresponding to a clock tick.
32+
resolution: func() -> duration;
33+
34+
/// Create a `pollable` which will resolve once the specified instant
35+
/// occured.
36+
subscribe-instant: func(when: instant) -> pollable;
37+
38+
/// Create a `pollable` which will resolve once the given duration has
39+
/// elapsed, starting at the time at which this function was called.
40+
/// occured.
41+
subscribe-duration: func(when: duration) -> pollable;
42+
}
43+
44+
/// WASI Wall Clock is a clock API intended to let users query the current
45+
/// time. The name "wall" makes an analogy to a "clock on the wall", which
46+
/// is not necessarily monotonic as it may be reset.
47+
///
48+
/// It is intended to be portable at least between Unix-family platforms and
49+
/// Windows.
50+
///
51+
/// A wall clock is a clock which measures the date and time according to
52+
/// some external reference.
53+
///
54+
/// External references may be reset, so this clock is not necessarily
55+
/// monotonic, making it unsuitable for measuring elapsed time.
56+
///
57+
/// It is intended for reporting the current date and time for humans.
58+
interface wall-clock {
59+
/// A time and date in seconds plus nanoseconds.
60+
record datetime {
61+
seconds: u64,
62+
nanoseconds: u32,
63+
}
64+
65+
/// Read the current value of the clock.
66+
///
67+
/// This clock is not monotonic, therefore calling this function repeatedly
68+
/// will not necessarily produce a sequence of non-decreasing values.
69+
///
70+
/// The returned timestamps represent the number of seconds since
71+
/// 1970-01-01T00:00:00Z, also known as [POSIX's Seconds Since the Epoch],
72+
/// also known as [Unix Time].
73+
///
74+
/// The nanoseconds field of the output is always less than 1000000000.
75+
///
76+
/// [POSIX's Seconds Since the Epoch]: https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xbd_chap04.html#tag_21_04_16
77+
/// [Unix Time]: https://en.wikipedia.org/wiki/Unix_time
78+
now: func() -> datetime;
79+
80+
/// Query the resolution of the clock.
81+
///
82+
/// The nanoseconds field of the output is always less than 1000000000.
83+
resolution: func() -> datetime;
84+
}
85+
86+
world imports {
87+
import wasi:io/poll@0.2.0;
88+
import monotonic-clock;
89+
import wall-clock;
90+
}

wit/deps/wasi-io-0.2.0/package.wit

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package wasi:io@0.2.0;
2+
3+
interface poll {
4+
resource pollable {
5+
ready: func() -> bool;
6+
block: func();
7+
}
8+
9+
poll: func(in: list<borrow<pollable>>) -> list<u32>;
10+
}
11+

wit/metrics.wit

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
interface metrics {
2+
use wasi:clocks/wall-clock@0.2.0.{datetime};
3+
use wasi:clocks/monotonic-clock@0.2.0.{duration};
4+
use types.{key-value, instrumentation-scope};
5+
use tracing.{span-id, trace-id};
6+
7+
/// Exports a resource's metric data.
8+
%export: func(metrics: resource-metrics) -> result<_, error>;
9+
10+
/// An error resulting from `export` being called.
11+
type error = string;
12+
13+
/// A collection of `scope-metrics` and the associated `resource` that created them.
14+
record resource-metrics {
15+
/// The entity that collected the metrics.
16+
%resource: %resource,
17+
/// The collection of metrics with unique `instrumentation-scope`s.
18+
scope-metrics: list<scope-metrics>,
19+
}
20+
21+
/// An immutable representation of the entity producing telemetry as attributes.
22+
record %resource {
23+
/// Attributes that identify the resource.
24+
attributes: list<key-value>,
25+
/// The schema URL to be recorded in the emitted resource.
26+
schema-url: option<string>,
27+
}
28+
29+
/// A collection of `metric`s produced by a meter.
30+
record scope-metrics {
31+
/// The instrumentation scope that the meter was created with.
32+
scope: instrumentation-scope,
33+
/// The list of aggregations created by the meter.
34+
metrics: list<metric>,
35+
}
36+
37+
/// A collection of one or more aggregated time series from a metric.
38+
record metric {
39+
/// The name of the metric that created this data.
40+
name: string,
41+
/// The description of the metric, which can be used in documentation.
42+
description: string,
43+
/// The unit in which the metric reports.
44+
unit: string,
45+
/// The aggregated data from a metric.
46+
data: metric-data
47+
}
48+
49+
/// Metric data for all types.
50+
variant metric-data {
51+
/// Metric data for an f64 gauge.
52+
f64-gauge(gauge),
53+
/// Metric data for an f64 sum.
54+
f64-sum(sum),
55+
/// Metric data for an f64 histogram.
56+
f64-histogram(histogram),
57+
/// Metric data for an f64 exponential-histogram.
58+
f64-exponential-histogram(exponential-histogram),
59+
/// Metric data for an u64 gauge.
60+
u64-gauge(gauge),
61+
/// Metric data for an u64 sum.
62+
u64-sum(sum),
63+
/// Metric data for an u64 histogram.
64+
u64-histogram(histogram),
65+
/// Metric data for an u64 exponential-histogram.
66+
u64-exponential-histogram(exponential-histogram),
67+
/// Metric data for an s64 gauge.
68+
s64-gauge(gauge),
69+
/// Metric data for an s64 sum.
70+
s64-sum(sum),
71+
/// Metric data for an s64 histogram.
72+
s64-histogram(histogram),
73+
/// Metric data for an s64 exponential-histogram.
74+
s64-exponential-histogram(exponential-histogram),
75+
}
76+
77+
/// A measurement of the current value of an instrument.
78+
record gauge {
79+
/// Represents individual aggregated measurements with unique attributes.
80+
data-points: list<gauge-data-point>,
81+
/// The time when the time series was started.
82+
start-time: option<datetime>,
83+
/// The time when the time series was recorded.
84+
time: datetime,
85+
}
86+
87+
/// A single data point in a time series to be associated with a `gauge`.
88+
record gauge-data-point {
89+
/// `attributes` is the set of key value pairs that uniquely identify the
90+
/// time series.
91+
attributes: list<key-value>,
92+
/// The value of this data point.
93+
value: metric-number,
94+
/// The sampled `exemplar`s collected during the time series.
95+
exemplars: list<exemplar>,
96+
}
97+
98+
/// Represents the sum of all measurements of values from an instrument.
99+
record sum {
100+
/// Represents individual aggregated measurements with unique attributes.
101+
data-points: list<sum-data-point>,
102+
/// The time when the time series was started.
103+
start-time: datetime,
104+
/// The time when the time series was recorded.
105+
time: datetime,
106+
/// Describes if the aggregation is reported as the change from the last report
107+
/// time, or the cumulative changes since a fixed start time.
108+
temporality: temporality,
109+
/// Whether this aggregation only increases or decreases.
110+
is-monotonic: bool,
111+
}
112+
113+
/// A single data point in a time series to be associated with a `sum`.
114+
record sum-data-point {
115+
/// `attributes` is the set of key value pairs that uniquely identify the
116+
/// time series.
117+
attributes: list<key-value>,
118+
/// The value of this data point.
119+
value: metric-number,
120+
/// The sampled `exemplar`s collected during the time series.
121+
exemplars: list<exemplar>,
122+
}
123+
124+
/// Represents the histogram of all measurements of values from an instrument.
125+
record histogram {
126+
/// Individual aggregated measurements with unique attributes.
127+
data-points: list<histogram-data-point>,
128+
/// The time when the time series was started.
129+
start-time: datetime,
130+
/// The time when the time series was recorded.
131+
time: datetime,
132+
/// Describes if the aggregation is reported as the change from the last report
133+
/// time, or the cumulative changes since a fixed start time.
134+
temporality: temporality,
135+
}
136+
137+
/// A single data point in a time series to be associated with a `histogram`.
138+
record histogram-data-point {
139+
/// The set of key value pairs that uniquely identify the time series.
140+
attributes: list<key-value>,
141+
/// The number of updates this histogram has been calculated with.
142+
count: u64,
143+
/// The upper bounds of the buckets of the histogram.
144+
bounds: list<f64>,
145+
/// The count of each of the buckets.
146+
bucket-counts: list<u64>,
147+
/// The minimum value recorded.
148+
min: option<metric-number>,
149+
/// The maximum value recorded.
150+
max: option<metric-number>,
151+
/// The sum of the values recorded
152+
sum: metric-number,
153+
/// The sampled `exemplar`s collected during the time series.
154+
exemplars: list<exemplar>,
155+
}
156+
157+
/// The histogram of all measurements of values from an instrument.
158+
record exponential-histogram {
159+
/// The individual aggregated measurements with unique attributes.
160+
data-points: list<exponential-histogram-data-point>,
161+
/// When the time series was started.
162+
start-time: datetime,
163+
/// The time when the time series was recorded.
164+
time: datetime,
165+
/// Describes if the aggregation is reported as the change from the last report
166+
/// time, or the cumulative changes since a fixed start time.
167+
temporality: temporality,
168+
}
169+
170+
/// A single data point in a time series to be associated with an `exponential-histogram `.
171+
record exponential-histogram-data-point {
172+
/// The set of key value pairs that uniquely identify the time series.
173+
attributes: list<key-value>,
174+
/// The number of updates this histogram has been calculated with.
175+
count: u64,
176+
/// The minimum value recorded.
177+
min: option<metric-number>,
178+
/// The maximum value recorded.
179+
max: option<metric-number>,
180+
/// The maximum value recorded.
181+
sum: metric-number,
182+
/// Describes the resolution of the histogram.
183+
///
184+
/// Boundaries are located at powers of the base, where:
185+
///
186+
/// base = 2 ^ (2 ^ -scale)
187+
scale: s8,
188+
/// The number of values whose absolute value is less than or equal to
189+
/// `zero_threshold`.
190+
///
191+
/// When `zero_threshold` is `0`, this is the number of values that cannot be
192+
/// expressed using the standard exponential formula as well as values that have
193+
/// been rounded to zero.
194+
zero-count: u64,
195+
/// The range of positive value bucket counts.
196+
positive-bucket: exponential-bucket,
197+
/// The range of negative value bucket counts.
198+
negative-bucket: exponential-bucket,
199+
/// The width of the zero region.
200+
///
201+
/// Where the zero region is defined as the closed interval
202+
/// [-zero_threshold, zero_threshold].
203+
zero-threshold: f64,
204+
/// The sampled exemplars collected during the time series.
205+
exemplars: list<exemplar>,
206+
}
207+
208+
/// A set of bucket counts, encoded in a contiguous array of counts.
209+
record exponential-bucket {
210+
/// The bucket index of the first entry in the `counts` list.
211+
offset: s32,
212+
/// A list where `counts[i]` carries the count of the bucket at index `offset + i`.
213+
///
214+
/// `counts[i]` is the count of values greater than base^(offset+i) and less than
215+
/// or equal to base^(offset+i+1).
216+
counts: list<u64>,
217+
}
218+
219+
/// A measurement sampled from a time series providing a typical example.
220+
record exemplar {
221+
/// The attributes recorded with the measurement but filtered out of the
222+
/// time series' aggregated data.
223+
filtered-attributes: list<key-value>,
224+
/// The time when the measurement was recorded.
225+
time: datetime,
226+
/// The measured value.
227+
value: metric-number,
228+
/// The ID of the span that was active during the measurement.
229+
///
230+
/// If no span was active or the span was not sampled this will be empty.
231+
span-id: span-id,
232+
/// The ID of the trace the active span belonged to during the measurement.
233+
///
234+
/// If no span was active or the span was not sampled this will be empty.
235+
trace-id: trace-id,
236+
}
237+
238+
/// Defines the window that an aggregation was calculated over.
239+
enum temporality {
240+
/// A measurement interval that continues to expand forward in time from a
241+
/// starting point.
242+
///
243+
/// New measurements are added to all previous measurements since a start time.
244+
///
245+
/// This is the default temporality.
246+
cumulative,
247+
/// A measurement interval that resets each cycle.
248+
///
249+
/// Measurements from one cycle are recorded independently, measurements from
250+
/// other cycles do not affect them.
251+
delta,
252+
/// Configures Synchronous Counter and Histogram instruments to use
253+
/// Delta aggregation temporality, which allows them to shed memory
254+
/// following a cardinality explosion, thus use less memory.
255+
low-memory,
256+
}
257+
258+
/// The number types available for any given instrument.
259+
variant metric-number {
260+
%f64(f64),
261+
%s64(s64),
262+
%u64(u64),
263+
}
264+
}

0 commit comments

Comments
 (0)