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

Commit 67e5bb8

Browse files
Cristian Gonzálezdraffensperger
authored andcommitted
Update @opencensus/web-types with @opencensus/core on commit 27de0b1 (#76)
1 parent aeb6c8a commit 67e5bb8

14 files changed

Lines changed: 220 additions & 34 deletions

File tree

packages/opencensus-web-types/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"scripts": {
88
"build": "npm run compile",
99
"clean": "rimraf build/*",
10-
"copytypes": "node scripts/copy-types.js '36f63e6dee9e6ad9af5ece9c5458b47786d41f06' && npm run fix",
10+
"copytypes": "node scripts/copy-types.js '27de0b1e3422df31af936a0dba457728ff4bffc2' && npm run fix",
1111
"check": "gts check",
1212
"compile": "tsc -p .",
1313
"fix": "gts fix",

packages/opencensus-web-types/scripts/copy-types.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ async function logAndExec(cmd, options) {
105105
function getPatchedContents(srcFile, contents) {
106106
contents = updateCopyright(srcFile, contents);
107107
contents = fixNodeJsEventEmitterType(srcFile, contents);
108+
contents = fixStatsImportInternalLibrary(srcFile, contents);
109+
contents = fixSemicolonError(srcFile, contents);
108110
return contents;
109111
}
110112

@@ -146,3 +148,43 @@ function fixNodeJsEventEmitterType(srcFile, contents) {
146148
contents = lines.join('\n');
147149
return contents;
148150
}
151+
152+
/**
153+
* Replaces the `cls` import and usage and adds a new type to use instead of that import.
154+
* This function might be provisional until @opencensus/core updates with this change.
155+
*
156+
*/
157+
function fixStatsImportInternalLibrary(srcFile, contents){
158+
if(contents.indexOf('import * as cls from \'../internal/cls\';') === -1){
159+
return contents;
160+
}
161+
162+
console.log(` Using Fix stats import internal library for: ${srcFile}`);
163+
164+
const contentToAdd = [
165+
"/** Default type for functions */",
166+
"// tslint:disable:no-any",
167+
"export type Func<T> = (...args: any[]) => T;",
168+
"\n",
169+
"/** Main interface for stats. */"
170+
].join('\n');
171+
172+
contents = contents.replace("import * as cls from '../internal/cls';\n", "");
173+
contents = contents.replace('/** Main interface for stats. */', contentToAdd);
174+
contents = contents.replace('cls.Func', 'Func');
175+
return contents;
176+
}
177+
178+
/**
179+
* Adds a Semicolon in a specific location in order to perform `npm run fix` automatically without errors.
180+
* This function might be provisional until @opencensus/core updates with this change.
181+
*/
182+
function fixSemicolonError(srcFile, contents){
183+
if(contents.indexOf('[pluginName: string]: string\n') === -1){
184+
return contents;
185+
}
186+
187+
console.log(` Using fix semicolon error for: ${srcFile}`);
188+
189+
return contents.replace('[pluginName: string]: string\n', '[pluginName: string]: string;\n');
190+
}

packages/opencensus-web-types/src/common/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// tslint:disable:no-any
1818
export type LogFunction = (message: any, ...args: any[]) => void;
1919

20-
/** Defines an logger interface. */
20+
/** Defines a logger interface. */
2121
export interface Logger {
2222
/** Logger verbosity level. If omitted, `debug` level is assumed. */
2323
level?: string;
@@ -28,7 +28,7 @@ export interface Logger {
2828
debug: LogFunction;
2929
}
3030

31-
/** Defines an logger options interface. */
31+
/** Defines a logger options interface. */
3232
export interface LoggerOptions {
3333
level?: string;
3434
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ export interface StatsEventListener {
3939
* @param view The registered view
4040
*/
4141
onRegisterView(view: View): void;
42+
4243
/**
4344
* Is called whenever a new measurement is recorded.
4445
* @deprecated since version 0.0.9 - use {@link start} instead
4546
* @param views The views related to the measurement
4647
* @param measurement The recorded measurement
48+
* @param tags The tags to which the value is applied
4749
*/
4850
onRecord(
4951
views: View[],

packages/opencensus-web-types/src/metrics/export/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Copyright 2019, OpenCensus Authors
33
*
4-
* Licensed under the Apache License, Version 2.0 the "License";
4+
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*

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

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import { Metric } from '../metrics/export/types';
1919
import { TagMap } from '../tags/tag-map';
2020
import { TagKey, TagValue } from '../tags/types';
2121

22+
/** Default type for functions */
23+
// tslint:disable:no-any
24+
export type Func<T> = (...args: any[]) => T;
25+
2226
/** Main interface for stats. */
2327
export interface Stats {
2428
/**
@@ -29,7 +33,7 @@ export interface Stats {
2933
* @param tagKeys The view columns (tag keys)
3034
* @param description The view description
3135
* @param bucketBoundaries The view bucket boundaries for a distribution
32-
* aggregation type
36+
* aggregation type
3337
*/
3438
createView(
3539
name: string,
@@ -75,10 +79,17 @@ export interface Stats {
7579
* Updates all views with the new measurements.
7680
* @param measurements A list of measurements to record
7781
* @param tags optional The tags to which the value is applied.
78-
* tags could either be explicitly passed to the method, or implicitly
79-
* read from current execution context.
82+
* tags could either be explicitly passed to the method, or implicitly
83+
* read from current execution context.
84+
* @param attachments optional The contextual information associated with an
85+
* example value. The contextual information is represented as key - value
86+
* string pairs.
8087
*/
81-
record(measurements: Measurement[], tags?: TagMap): void;
88+
record(
89+
measurements: Measurement[],
90+
tags?: TagMap,
91+
attachments?: { [key: string]: string }
92+
): void;
8293

8394
/**
8495
* Remove all registered Views and exporters from the stats.
@@ -103,6 +114,18 @@ export interface Stats {
103114
* @param exporter An stats exporter
104115
*/
105116
unregisterExporter(exporter: StatsEventListener): void;
117+
118+
/**
119+
* Enters the scope of code where the given `TagMap` is in the current context
120+
* (replacing the previous `TagMap`).
121+
* @param tags The TagMap to be set to the current context.
122+
* @param fn Callback function.
123+
* @returns The callback return.
124+
*/
125+
withTagContext<T>(tags: TagMap, fn: Func<T>): T;
126+
127+
/** Gets the current tag context. */
128+
getCurrentTagContext(): TagMap;
106129
}
107130

108131
/**
@@ -181,7 +204,7 @@ export interface View {
181204
/**
182205
* The end time for this view - represents the last time a value was recorded
183206
*/
184-
endTime: number;
207+
endTime?: number;
185208
/** true if the view was registered */
186209
registered: boolean;
187210
/**
@@ -191,13 +214,20 @@ export interface View {
191214
* Measurements with measurement type INT64 will have its value truncated.
192215
* @param measurement The measurement to record
193216
* @param tags The tags to which the value is applied
217+
* @param attachments optional The contextual information associated with an
218+
* example value. THe contextual information is represented as key - value
219+
* string pairs.
194220
*/
195-
recordMeasurement(measurement: Measurement, tags: TagMap): void;
221+
recordMeasurement(
222+
measurement: Measurement,
223+
tags: TagMap,
224+
attachments?: { [key: string]: string }
225+
): void;
196226
/**
197227
* Returns a snapshot of an AggregationData for that tags/labels values.
198228
* @param tagValues The desired data's tag values.
199229
*/
200-
getSnapshot(tagValues: TagValue[]): AggregationData;
230+
getSnapshot(tagValues: Array<TagValue | null>): AggregationData;
201231
/** Gets the view's tag keys */
202232
getColumns(): TagKey[];
203233
/** Gets view`s metric */
@@ -220,7 +250,7 @@ export interface AggregationMetadata {
220250
/** The aggregation type of the aggregation data */
221251
readonly type: AggregationType;
222252
/** The tagValues that this AggregationData collects and aggregates */
223-
readonly tagValues: TagValue[];
253+
readonly tagValues: Array<TagValue | null>;
224254
/** The latest timestamp a new data point was recorded */
225255
timestamp: number;
226256
}
@@ -279,6 +309,25 @@ export interface DistributionData extends AggregationMetadata {
279309
buckets: Bucket[];
280310
/** Buckets count */
281311
bucketCounts?: number[];
312+
/** If the distribution does not have a histogram, then omit this field. */
313+
exemplars?: StatsExemplar[];
314+
}
315+
316+
/**
317+
* Exemplars are example points that may be used to annotate aggregated
318+
* Distribution values. They are metadata that gives information about a
319+
* particular value added to a Distribution bucket.
320+
*/
321+
export interface StatsExemplar {
322+
/**
323+
* Value of the exemplar point. It determines which bucket the exemplar
324+
* belongs to.
325+
*/
326+
readonly value: number;
327+
/** The observation (sampling) time of the above value. */
328+
readonly timestamp: number;
329+
/** Contextual information about the example value. */
330+
readonly attachments: { [key: string]: string };
282331
}
283332

284333
export type Bucket = number;
Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Copyright 2019, OpenCensus Authors
33
*
4-
* Licensed under the Apache License, Version 2.0 the "License";
4+
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
@@ -14,33 +14,86 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { TagKey, TagValue } from './types';
17+
import {
18+
TagKey,
19+
TagMetadata,
20+
TagTtl,
21+
TagValue,
22+
TagValueWithMetadata,
23+
} from './types';
1824
import { isValidTagKey, isValidTagValue } from './validation';
1925

20-
/** TagMap is maps of TagKey -> TagValue */
26+
const UNLIMITED_PROPAGATION_MD = {
27+
tagTtl: TagTtl.UNLIMITED_PROPAGATION,
28+
};
29+
30+
/** TagMap is maps of TagKey -> TagValueWithMetadata */
2131
export class TagMap {
22-
// A map mapping TagKey to to its respective TagValue.
23-
private readonly registeredTags: Map<TagKey, TagValue> = new Map();
32+
// A map mapping TagKey to to its respective TagValueWithMetadata.
33+
private readonly registeredTags: Map<
34+
TagKey,
35+
TagValueWithMetadata
36+
> = new Map();
2437

25-
/** Adds the key/value pair regardless of whether the key is present. */
26-
set(tagKey: TagKey, tagValue: TagValue): void {
38+
/**
39+
* Adds the key/value pair regardless of whether the key is present.
40+
* @param tagKey The TagKey which will be set.
41+
* @param tagValue The TagValue to set for the given key.
42+
* @param tagMetadata The TagMetadata associated with this Tag.
43+
*/
44+
set(tagKey: TagKey, tagValue: TagValue, tagMetadata?: TagMetadata): void {
2745
if (!isValidTagKey(tagKey)) {
2846
throw new Error(`Invalid TagKey name: ${tagKey.name}`);
2947
}
30-
3148
if (!isValidTagValue(tagValue)) {
3249
throw new Error(`Invalid TagValue: ${tagValue.value}`);
3350
}
34-
this.registeredTags.set(tagKey, tagValue);
51+
let existingKey;
52+
for (const key of this.registeredTags.keys()) {
53+
if (key.name === tagKey.name) {
54+
existingKey = key;
55+
break;
56+
}
57+
}
58+
if (existingKey) this.registeredTags.delete(existingKey);
59+
const valueWithMetadata = this.getValueWithMetadata(tagValue, tagMetadata);
60+
this.registeredTags.set(tagKey, valueWithMetadata);
3561
}
3662

37-
/** Deletes a tag from the map if the key is in the map. */
63+
/**
64+
* Deletes a tag from the map if the key is in the map.
65+
* @param tagKey The TagKey which will be removed.
66+
*/
3867
delete(tagKey: TagKey): void {
3968
this.registeredTags.delete(tagKey);
4069
}
4170

42-
/** Gets the tags map. */
71+
/** Gets the tags map without metadata. */
4372
get tags() {
73+
const tagsWithoutMetadata: Map<TagKey, TagValue> = new Map();
74+
for (const [tagKey, valueWithMetadata] of this.registeredTags) {
75+
tagsWithoutMetadata.set(tagKey, valueWithMetadata.tagValue);
76+
}
77+
return tagsWithoutMetadata;
78+
}
79+
80+
/** Gets the tags map with metadata. */
81+
get tagsWithMetadata() {
4482
return this.registeredTags;
4583
}
84+
85+
/**
86+
* Constructs a new TagValueWithMetadata using tagValue and tagMetadata.
87+
* For backwards-compatibility this method still produces propagating Tags
88+
* (UNLIMITED_PROPAGATION) if tagMetadata is not provided or missing.
89+
*/
90+
private getValueWithMetadata(
91+
tagValue: TagValue,
92+
tagMetadata?: TagMetadata
93+
): TagValueWithMetadata {
94+
if (tagMetadata) {
95+
return { tagValue, tagMetadata };
96+
}
97+
return { tagValue, tagMetadata: UNLIMITED_PROPAGATION_MD };
98+
}
4699
}

packages/opencensus-web-types/src/tags/types.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,38 @@ export interface TagValue {
2525
/** The value of a tag. */
2626
readonly value: string;
2727
}
28+
29+
/** TagValueWithMetadata holds a TagValue and a TagMetadata. */
30+
export interface TagValueWithMetadata {
31+
/** The tag value */
32+
readonly tagValue: TagValue;
33+
/** The metadata for the tag */
34+
readonly tagMetadata: TagMetadata;
35+
}
36+
37+
/**
38+
* TagMetadata contains properties associated with a Tag.
39+
* Anytime a sender serializes a tag, sends it over the wire and receiver
40+
* deserializes the tag then the tag is considered to have travelled one hop.
41+
* There could be one or more proxy(ies) between sender and receiver. Proxies
42+
* are treated as transparent entities and they do not create additional hops.
43+
*/
44+
export interface TagMetadata {
45+
/**
46+
* For now, only special values of TagTtl are supported. In future,
47+
* additional properties may be added to address specific situations.
48+
*/
49+
readonly tagTtl: number;
50+
}
51+
52+
/** TagTtl is an integer that represents number of hops a tag can propagate */
53+
export enum TagTtl {
54+
/**
55+
* NO_PROPAGATION is considered to have local scope and is used within the
56+
* process it created.
57+
*/
58+
NO_PROPAGATION = 0,
59+
60+
/** UNLIMITED_PROPAGATION can propagate unlimited hops. */
61+
UNLIMITED_PROPAGATION = -1,
62+
}

packages/opencensus-web-types/src/tags/validation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Copyright 2019, OpenCensus Authors
33
*
4-
* Licensed under the Apache License, Version 2.0 the "License";
4+
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*

0 commit comments

Comments
 (0)