Skip to content

Commit 12e0455

Browse files
authored
feat: Metrics API revision based on Specs and RFCs (open-telemetry#259)
* feat: metrics API revision based on specs and rfcs * fix: document default values
1 parent 50047b3 commit 12e0455

File tree

7 files changed

+158
-123
lines changed

7 files changed

+158
-123
lines changed

packages/opentelemetry-types/src/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ export * from './context/propagation/BinaryFormat';
1919
export * from './context/propagation/HttpTextFormat';
2020
export * from './distributed_context/DistributedContext';
2121
export * from './distributed_context/EntryValue';
22-
export * from './metrics/counter';
23-
export * from './metrics/gauge';
24-
export * from './metrics/measure';
25-
export * from './metrics/meter';
26-
export * from './metrics/metrics';
22+
export * from './metrics/Handle';
23+
export * from './metrics/Measure';
24+
export * from './metrics/Meter';
25+
export * from './metrics/Metric';
2726
export * from './resources/Resource';
2827
export * from './trace/attributes';
2928
export * from './trace/Event';

packages/opentelemetry-types/src/metrics/counter.ts renamed to packages/opentelemetry-types/src/metrics/Handle.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,32 @@
1414
* limitations under the License.
1515
*/
1616

17-
export interface CounterTimeseries {
18-
// Adds the given value to the current value. Values cannot be negative.
17+
/** A Handle for Counter Metric. */
18+
export interface CounterHandle {
19+
/**
20+
* Adds the given value to the current value. Values cannot be negative.
21+
* @param value the value to add
22+
*/
1923
add(value: number): void;
2024

21-
// Sets the given value. Value must be larger than the current recorded value.
25+
/**
26+
* Sets the given value. Value must be larger than the current recorded value.
27+
* @param value the new value.
28+
*/
29+
set(value: number): void;
30+
}
31+
32+
/** A Handle for Gauge Metric. */
33+
export interface GaugeHandle {
34+
/**
35+
* Adds the given value to the current value. Values can be negative.
36+
* @param value the value to add.
37+
*/
38+
add(value: number): void;
39+
40+
/**
41+
* Sets the given value. Values can be negative.
42+
* @param value the new value.
43+
*/
2244
set(value: number): void;
2345
}

packages/opentelemetry-types/src/metrics/measure.ts renamed to packages/opentelemetry-types/src/metrics/Measure.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { DistributedContext } from '../distributed_context/DistributedContext';
18+
import { SpanContext } from '../trace/span_context';
19+
1720
export enum MeasureType {
1821
DOUBLE = 0,
1922
LONG = 1,
2023
}
2124

25+
/**
26+
* Options needed for measure creation
27+
*/
2228
export interface MeasureOptions {
2329
// Description of the Measure.
2430
description?: string;
@@ -30,10 +36,20 @@ export interface MeasureOptions {
3036
type?: MeasureType;
3137
}
3238

39+
/** Measure to report instantaneous measurement of a value. */
3340
export interface Measure {
34-
// Creates a measurement with the supplied value.
35-
createMeasurement(value: number): Measurement;
41+
/**
42+
* Records the given value to this measure.
43+
* @param value the measurement to record.
44+
* @param distContext the distContext associated with the measurements.
45+
* @param spanContext the {@link SpanContext} that identifies the {@link Span}
46+
* for which the measurements are associated with.
47+
*/
48+
record(value: number): void;
49+
record(value: number, distContext: DistributedContext): void;
50+
record(
51+
value: number,
52+
distContext: DistributedContext,
53+
spanContext: SpanContext
54+
): void;
3655
}
37-
38-
// Measurement describes an individual measurement
39-
export interface Measurement {}

packages/opentelemetry-types/src/metrics/meter.ts renamed to packages/opentelemetry-types/src/metrics/Meter.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,31 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { SpanContext } from '../trace/span_context';
18-
import { DistributedContext } from '../distributed_context/DistributedContext';
19-
import { Measure, MeasureOptions, Measurement } from './measure';
20-
import { Metric, MetricOptions } from './metric';
21-
import { CounterTimeseries } from './counter';
22-
import { GaugeTimeseries } from './gauge';
23-
24-
export interface RecordOptions {
25-
// spanContext represents a measurement exemplar in the form of a SpanContext.
26-
spanContext?: SpanContext;
27-
// distributedContext overrides the current context and adds dimensions
28-
// to the measurements.
29-
distributedContext?: DistributedContext;
30-
}
17+
import { Metric, MetricOptions } from './Metric';
18+
import { CounterHandle, GaugeHandle } from './Handle';
19+
import { MeasureOptions, Measure } from './Measure';
3120

21+
/**
22+
* An interface to allow the recording metrics.
23+
*
24+
* {@link Metric}s are used for recording pre-defined aggregation (Gauge and
25+
* Counter), or raw values ({@link Measure}) in which the aggregation and labels
26+
* for the exported metric are deferred.
27+
*/
3228
export interface Meter {
33-
// Creates and returns a new @link{Measure}.
29+
/**
30+
* Creates and returns a new {@link Measure}.
31+
* @param name the name of the metric.
32+
* @param [options] the measure options.
33+
*/
3434
createMeasure(name: string, options?: MeasureOptions): Measure;
3535

36-
// Creates a new counter metric.
37-
createCounter(
38-
name: string,
39-
options?: MetricOptions
40-
): Metric<CounterTimeseries>;
36+
/**
37+
* Creates a new counter metric.
38+
* @param name the name of the metric.
39+
* @param [options] the metric options.
40+
*/
41+
createCounter(name: string, options?: MetricOptions): Metric<CounterHandle>;
4142

4243
// TODO: Measurements can have a long or double type. However, it looks like
4344
// the metric timeseries API (according to spec) accepts values instead of
@@ -47,9 +48,10 @@ export interface Meter {
4748
// be cool to only have a single interface, but maybe having two is necessary?
4849
// Maybe include the type as a metrics option? Probs a good gh issue, the same goes for Measure types.
4950

50-
// Creates a new gauge metric.
51-
createGauge(name: string, options?: MetricOptions): Metric<GaugeTimeseries>;
52-
53-
// Record a set of raw measurements.
54-
record(measurements: Measurement[], options?: RecordOptions): void;
51+
/**
52+
* Creates a new gauge metric.
53+
* @param name the name of the metric.
54+
* @param [options] the metric options.
55+
*/
56+
createGauge(name: string, options?: MetricOptions): Metric<GaugeHandle>;
5557
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Copyright 2019, OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { Resource } from '../resources/Resource';
18+
19+
/**
20+
* Options needed for metric creation
21+
*/
22+
export interface MetricOptions {
23+
/**
24+
* The description of the Metric.
25+
* @default ''
26+
*/
27+
description?: string;
28+
29+
/**
30+
* The unit of the Metric values.
31+
* @default '1'
32+
*/
33+
unit?: string;
34+
35+
/** The list of label keys for the Metric. */
36+
labelKeys?: string[];
37+
38+
/** The map of constant labels for the Metric. */
39+
constantLabels?: Map<string, string>;
40+
41+
/** The resource the Metric is associated with. */
42+
resource?: Resource;
43+
44+
/** The name of the component that reports the Metric. */
45+
component?: string;
46+
}
47+
48+
/**
49+
* Metric represents a base class for different types of metric
50+
* pre aggregations.
51+
*/
52+
export interface Metric<T> {
53+
/**
54+
* Returns a Handle associated with specified label values.
55+
* It is recommended to keep a reference to the Handle instead of always
56+
* calling this method for every operations.
57+
* @param labelValues the list of label values.
58+
*/
59+
getHandle(labelValues: string[]): T;
60+
61+
/**
62+
* Returns a Handle for a metric with all labels not set.
63+
*/
64+
getDefaultHandle(): T;
65+
66+
/**
67+
* Removes the Handle from the metric, if it is present.
68+
* @param labelValues the list of label values.
69+
*/
70+
removeHandle(labelValues: string[]): void;
71+
72+
/**
73+
* Clears all timeseries from the Metric.
74+
*/
75+
clear(): void;
76+
77+
/**
78+
* what should the callback signature be?
79+
*/
80+
setCallback(fn: () => void): void;
81+
}

packages/opentelemetry-types/src/metrics/gauge.ts

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

packages/opentelemetry-types/src/metrics/metric.ts

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

0 commit comments

Comments
 (0)