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

Commit 77eada8

Browse files
authored
Update stats, zpages, prometheus packages with new Tags API (#307)
* Use Tags API * replace not found key values by null and fix nit
1 parent de34faf commit 77eada8

28 files changed

+689
-466
lines changed

CHANGELOG.md

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55
## Unreleased
66
- Add Metrics API.
77
- Add Resource API.
8+
- Add Tags API.
89
- Add Gauges (`DoubleGauge`, `LongGauge`, `DerivedDoubleGauge`, `DerivedLongGauge`) APIs.
910
- Add support for supplying instrumentation configuration via tracing option. Option argument added to instrumentation interface.
1011
- Add ignoreIncomingPaths and ignoreOutgoingUrls support to the http and https tracing instrumentations.
@@ -14,26 +15,82 @@ All notable changes to this project will be documented in this file.
1415

1516
- Modify `Logger` interface: `level` made optional, `silly` removed.
1617
- The ```new Stats()``` has been deprecated on Stats class. The global singleton ```globalStats``` object should be used instead. Also, ```registerView()``` is separated out from ```createView()```.
18+
- Use ```TagKey```, ```TagValue``` and ```TagMap``` to create the tag keys, tag values.
1719

1820
##### Old code
1921
```js
2022
const { Stats } = require("@opencensus/core");
2123
const stats = new Stats();
2224

25+
// Counts/groups the lengths of lines read in.
26+
const mLineLengths = stats.createMeasureInt64(
27+
"demo/line_lengths",
28+
MeasureUnit.BYTE,
29+
"The distribution of line lengths"
30+
);
31+
32+
// Create tag keys
33+
const tagKeys = ["method", "status"];
34+
2335
// Create and register the view
24-
stats.createView(...);
36+
stats.createView(
37+
"demo/lines_in",
38+
mLineLengths,
39+
AggregationType.COUNT,
40+
tagKeys,
41+
"The number of lines from standard input"
42+
);
43+
44+
// Records measurements
45+
stats.record({
46+
measure: mLineLengths,
47+
tags,
48+
value: 2
49+
});
50+
2551
```
2652

2753
##### New code
2854
```js
29-
// Get the global singleton stats object
55+
// Gets the global stats instance
3056
const { globalStats } = require("@opencensus/core");
3157

32-
// Create the view
33-
const view = globalStats.createView(...);
34-
35-
// register the view
58+
// Counts/groups the lengths of lines read in.
59+
const mLineLengths = globalStats.createMeasureInt64(
60+
"demo/line_lengths",
61+
MeasureUnit.BYTE,
62+
"The distribution of line lengths"
63+
);
64+
65+
// Creates the method and status key
66+
const methodKey = {name: "method"};
67+
const statusKey = {name: "status"};
68+
69+
// Creates the view
70+
const view = globalStats.createView(
71+
"demo/lines_in",
72+
mLineLengths,
73+
AggregationType.COUNT,
74+
[methodKey, statusKey],
75+
"The number of lines from standard input"
76+
);
77+
78+
// Registers the view
3679
globalStats.registerView(view);
80+
81+
// Creates tags map -> key/value pair
82+
const tagMap = new TagMap();
83+
tagMap.set(methodKey, {value: 'REPL'});
84+
tagMap.set(statusKey, {value: 'OK'});
85+
86+
// Creates measurements (measure + value)
87+
const measurements = [{
88+
measure: mLineLengths,
89+
value: 2
90+
}];
91+
92+
// Records measurement with tagMap
93+
globalStats.record(measurements, tagMap);
3794
```
3895

3996
## 0.0.8 - 2018-12-14

packages/opencensus-core/src/exporters/console-exporter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import * as loggerTypes from '../common/types';
1818
import {Measurement, View} from '../stats/types';
19+
import {TagKey, TagValue} from '../tags/types';
1920
import * as modelTypes from '../trace/model/types';
2021

2122
import {ExporterBuffer} from './exporter-buffer';
@@ -93,7 +94,8 @@ export class ConsoleStatsExporter implements types.StatsEventListener {
9394
* @param view recorded view from measurement
9495
* @param measurement recorded measurement
9596
*/
96-
onRecord(views: View[], measurement: Measurement) {
97+
onRecord(
98+
views: View[], measurement: Measurement, tags: Map<TagKey, TagValue>) {
9799
console.log(`Measurement recorded: ${measurement.measure.name}`);
98100
}
99101

packages/opencensus-core/src/exporters/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import {Measurement, View} from '../stats/types';
18+
import {TagKey, TagValue} from '../tags/types';
1819
import * as configTypes from '../trace/config/types';
1920
import * as modelTypes from '../trace/model/types';
2021

@@ -44,7 +45,9 @@ export interface StatsEventListener {
4445
* @param views The views related to the measurement
4546
* @param measurement The recorded measurement
4647
*/
47-
onRecord(views: View[], measurement: Measurement): void;
48+
onRecord(
49+
views: View[], measurement: Measurement,
50+
tags: Map<TagKey, TagValue>): void;
4851

4952
/**
5053
* Starts the exporter that polls Metric from Metrics library and send

packages/opencensus-core/src/stats/metric-utils.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
*/
1616

1717

18-
import {LabelKey, LabelValue, MetricDescriptor, MetricDescriptorType} from '../metrics/export/types';
18+
import {LabelValue, MetricDescriptor, MetricDescriptorType} from '../metrics/export/types';
19+
import {TagValue} from '../tags/types';
1920

20-
import {AggregationType, Measure, MeasureType, Tags, View} from './types';
21+
import {AggregationType, Measure, MeasureType, View} from './types';
2122

2223
/** Utils to convert Stats data models to Metric data models */
2324
export class MetricUtils {
@@ -61,25 +62,24 @@ export class MetricUtils {
6162
* @returns {MetricDescriptor}
6263
*/
6364
static viewToMetricDescriptor(view: View): MetricDescriptor {
64-
// TODO(mayurkale): add description
6565
return {
6666
name: view.name,
6767
description: view.description,
6868
unit: view.measure.unit,
6969
type: MetricUtils.getType(view.measure, view.aggregation),
7070
labelKeys: view.getColumns().map(
71-
tag => ({key: tag, description: ''} as LabelKey))
71+
// TODO(mayurkale): add description
72+
tagKey => ({key: tagKey.name, description: ''}))
7273
};
7374
}
7475

7576
/**
76-
* Converts tags to label values.
77-
* @param tags
77+
* Converts tag values to label values.
78+
* @param tagValues
7879
* @returns {LabelValue[]} List of label values
7980
*/
80-
static tagsToLabelValues(tags: Tags): LabelValue[] {
81-
return Object.keys(tags).map(key => {
82-
return {value: tags[key]} as LabelValue;
83-
});
81+
static tagValuesToLabelValues(tagValues: TagValue[]): LabelValue[] {
82+
return tagValues.map(
83+
(tagValue) => ({value: tagValue ? tagValue.value : null}));
8484
}
8585
}

packages/opencensus-core/src/stats/recorder.ts

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

17+
import {TagKey, TagValue} from '../tags/types';
1718
import {AggregationData, AggregationType, CountData, DistributionData, LastValueData, Measurement, MeasureType, SumData} from './types';
1819

20+
const UNKNOWN_TAG_VALUE: TagValue = null;
21+
1922
export class Recorder {
2023
static addMeasurement(
2124
aggregationData: AggregationData,
@@ -40,6 +43,15 @@ export class Recorder {
4043
}
4144
}
4245

46+
/** Gets the tag values from tags and columns */
47+
static getTagValues(tags: Map<TagKey, TagValue>, columns: TagKey[]):
48+
TagValue[] {
49+
return columns.map(
50+
(tagKey) =>
51+
(tags.get(tagKey) ||
52+
/** replace not found key values by null. */ UNKNOWN_TAG_VALUE));
53+
}
54+
4355
private static addToDistribution(
4456
distributionData: DistributionData, value: number): DistributionData {
4557
distributionData.count += 1;

packages/opencensus-core/src/stats/stats.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import * as loggerTypes from '../common/types';
1919
import {StatsEventListener} from '../exporters/types';
2020
import {Metric} from '../metrics/export/types';
2121
import {Metrics} from '../metrics/metrics';
22+
import {TagMap} from '../tags/tag-map';
23+
import {TagKey} from '../tags/types';
2224

2325
import {MetricProducerForStats} from './metric-producer';
2426
import {AggregationType, Measure, Measurement, MeasureType, MeasureUnit, Stats, View} from './types';
@@ -83,7 +85,7 @@ export class BaseStats implements Stats {
8385
*/
8486
createView(
8587
name: string, measure: Measure, aggregation: AggregationType,
86-
tagKeys: string[], description: string,
88+
tagKeys: TagKey[], description: string,
8789
bucketBoundaries?: number[]): View {
8890
const view = new BaseView(
8991
name, measure, aggregation, tagKeys, description, bucketBoundaries);
@@ -156,27 +158,35 @@ export class BaseStats implements Stats {
156158
/**
157159
* Updates all views with the new measurements.
158160
* @param measurements A list of measurements to record
161+
* @param tags optional The tags to which the value is applied.
162+
* tags could either be explicitly passed to the method, or implicitly
163+
* read from current execution context.
159164
*/
160-
record(...measurements: Measurement[]): void {
165+
record(measurements: Measurement[], tags?: TagMap): void {
161166
if (this.hasNegativeValue(measurements)) {
162167
this.logger.warn(`Dropping measurments ${measurements}, value to record
163168
must be non-negative.`);
164169
return;
165170
}
166171

172+
if (!tags) {
173+
// TODO(mayurkale): read tags current execution context
174+
tags = new TagMap();
175+
}
176+
167177
for (const measurement of measurements) {
168178
const views = this.registeredViews[measurement.measure.name];
169179
if (!views) {
170180
break;
171181
}
172182
// Updates all views
173183
for (const view of views) {
174-
view.recordMeasurement(measurement);
184+
view.recordMeasurement(measurement, tags);
175185
}
176186

177187
// Notifies all exporters
178188
for (const exporter of this.statsEventListeners) {
179-
exporter.onRecord(views, measurement);
189+
exporter.onRecord(views, measurement, tags.tags);
180190
}
181191
}
182192
}

packages/opencensus-core/src/stats/types.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import {StatsEventListener} from '../exporters/types';
1818
import {Metric} from '../metrics/export/types';
19+
import {TagMap} from '../tags/tag-map';
20+
import {TagKey, TagValue} from '../tags/types';
1921

2022
/** Main interface for stats. */
2123
export interface Stats {
@@ -31,7 +33,7 @@ export interface Stats {
3133
*/
3234
createView(
3335
name: string, measure: Measure, aggregation: AggregationType,
34-
tagKeys: string[], description: string,
36+
tagKeys: TagKey[], description: string,
3537
bucketBoundaries?: number[]): View;
3638

3739
/**
@@ -62,8 +64,11 @@ export interface Stats {
6264
/**
6365
* Updates all views with the new measurements.
6466
* @param measurements A list of measurements to record
67+
* @param tags optional The tags to which the value is applied.
68+
* tags could either be explicitly passed to the method, or implicitly
69+
* read from current execution context.
6570
*/
66-
record(...measurements: Measurement[]): void;
71+
record(measurements: Measurement[], tags?: TagMap): void;
6772

6873
/**
6974
* Remove all registered Views and exporters from the stats.
@@ -83,12 +88,6 @@ export interface Stats {
8388
registerExporter(exporter: StatsEventListener): void;
8489
}
8590

86-
87-
/** Tags are maps of names -> values */
88-
export interface Tags {
89-
[key: string]: string;
90-
}
91-
9291
/**
9392
* Describes the type of the individual values/measurements recorded by an
9493
* application. It includes information such as the type of measurement, the
@@ -140,8 +139,6 @@ export interface Measurement {
140139
* up to Number.MAX_SAFE_INTERGER.
141140
*/
142141
readonly value: number;
143-
/** The tags to which the value is applied */
144-
readonly tags: Tags;
145142
}
146143

147144
/**
@@ -176,15 +173,16 @@ export interface View {
176173
*
177174
* Measurements with measurement type INT64 will have its value truncated.
178175
* @param measurement The measurement to record
176+
* @param tags The tags to which the value is applied
179177
*/
180-
recordMeasurement(measurement: Measurement): void;
178+
recordMeasurement(measurement: Measurement, tags: TagMap): void;
181179
/**
182180
* Returns a snapshot of an AggregationData for that tags/labels values.
183-
* @param tags The desired data's tags
181+
* @param tagValues The desired data's tag values.
184182
*/
185-
getSnapshot(tags: Tags): AggregationData;
183+
getSnapshot(tagValues: TagValue[]): AggregationData;
186184
/** Gets the view's tag keys */
187-
getColumns(): string[];
185+
getColumns(): TagKey[];
188186
/** Gets view`s metric */
189187
getMetric(start: number): Metric;
190188
}
@@ -204,8 +202,8 @@ export enum AggregationType {
204202
export interface AggregationMetadata {
205203
/** The aggregation type of the aggregation data */
206204
readonly type: AggregationType;
207-
/** The tags/labels that this AggregationData collects and aggregates */
208-
readonly tags: Tags;
205+
/** The tagValues that this AggregationData collects and aggregates */
206+
readonly tagValues: TagValue[];
209207
/** The latest timestamp a new data point was recorded */
210208
timestamp: number;
211209
}

0 commit comments

Comments
 (0)