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

Commit 9b02ad4

Browse files
silva-fabiokjin
authored andcommitted
feat: add metrics interfaces and classes, create view classe, general refactoring, jsdoc and others
1 parent 532a28e commit 9b02ad4

25 files changed

Lines changed: 3165 additions & 903 deletions
161 KB
Loading

packages/opencensus-core/package-lock.json

Lines changed: 863 additions & 269 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
},
5555
"dependencies": {
5656
"continuation-local-storage": "^3.2.1",
57+
"hdr-histogram-js": "^1.1.4",
5758
"log-driver": "^1.2.7",
5859
"semver": "^5.5.0",
5960
"shimmer": "^1.2.0",

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,16 @@ export class ConsoleStatsExporter implements types.StatsExporter {
8383
* @param view registered view
8484
* @param measure registered measure
8585
*/
86-
onRegisterView(view: View, measure: Measure) {
87-
console.log(
88-
`View registered: ${view.name}, Measure registered: ${measure.name}`);
86+
onRegisterView(view: View) {
87+
console.log(`View registered: ${view.name}, Measure registered: ${
88+
view.measure.name}`);
8989
}
9090
/**
9191
* Event called when a measurement is recorded
9292
* @param view recorded view from measurement
9393
* @param measurement recorded measurement
9494
*/
95-
onRecord(view: View, measurement: Measurement) {
96-
console.log(`Measurement recorded: ${measurement.measure.name}, value: ${
97-
measurement.value}`);
95+
onRecord(view: View) {
96+
console.log(`Measurement recorded: ${view.measure.name}`);
9897
}
9998
}

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

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

1717

18-
import {Measure, Measurement, View} from '../stats/model/types';
18+
import {Measure, Measurement, View, ViewEventListener} from '../stats/model/types';
1919
import * as configTypes from '../trace/config/types';
2020
import * as modelTypes from '../trace/model/types';
2121

@@ -25,23 +25,10 @@ export interface Exporter extends modelTypes.SpanEventListener {
2525
* Sends a list of root spans to the service.
2626
* @param rootSpans A list of root spans to publish.
2727
*/
28-
2928
publish(rootSpans: modelTypes.RootSpan[]): Promise<number|string|void>;
3029
}
3130

32-
export interface StatsExporter {
33-
/**
34-
* Event called when a view is registered
35-
* @param view registered view
36-
* @param measure registered measure
37-
*/
38-
onRegisterView(view: View, measure: Measure): void;
39-
/**
40-
* Event called when a measurement is recorded
41-
* @param view recorded view from measurement
42-
* @param measurement recorded measurement
43-
*/
44-
onRecord(view: View, measurement: Measurement): void;
45-
}
31+
/** Define a StatsExporter interface */
32+
export interface StatsExporter extends ViewEventListener {}
4633

4734
export type ExporterConfig = configTypes.BufferConfig;

packages/opencensus-core/src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ export * from './exporters/console-exporter';
4343
// STATS CLASSES
4444

4545
// domain models impls
46-
export * from './stats/model/aggregation';
46+
export * from './stats/model/metrics/counter';
47+
export * from './stats/model/metrics/gauge';
48+
export * from './stats/model/metrics/histogram';
49+
export * from './stats/model/metrics/metric';
4750
export * from './stats/model/measure';
48-
export * from './stats/model/stats-manager';
51+
export * from './stats/model/view';
52+
export * from './stats/stats';
4953

5054
// interfaces
55+
export * from './stats/model/metrics/types';
5156
export * from './stats/model/types';
5257

5358
// logger

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

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

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

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

17-
import {Measure} from './types';
17+
import {Measure, MeasureUnit, RegisteredMeasures} from './types';
18+
1819

1920
/** Measure class for double type */
20-
export class MeasureDouble implements Measure {
21-
name: string;
22-
description: string;
23-
unit: string;
24-
type: string;
21+
abstract class AbstractMeasure implements Measure {
22+
readonly name: string;
23+
readonly description: string;
24+
readonly unit: MeasureUnit;
25+
abstract get type(): string;
2526

26-
constructor(name: string, description: string, unit: string) {
27+
constructor(name: string, description: string, unit: MeasureUnit) {
2728
this.name = name;
2829
this.description = description;
2930
this.unit = unit;
31+
}
32+
}
33+
34+
/** Measure class for double type */
35+
class MeasureDouble extends AbstractMeasure {
36+
readonly type: string;
37+
constructor(name: string, description: string, unit: MeasureUnit) {
38+
super(name, description, unit);
3039
this.type = 'DOUBLE';
3140
}
3241
}
3342

3443
/** Measure class for int64 type */
35-
export class MeasureInt64 implements Measure {
36-
name: string;
37-
description: string;
38-
unit: string;
39-
type: string;
40-
41-
constructor(name: string, description: string, unit: string) {
42-
this.name = name;
43-
this.description = description;
44-
this.unit = unit;
44+
class MeasureInt64 extends AbstractMeasure {
45+
readonly type: string;
46+
constructor(name: string, description: string, unit: MeasureUnit) {
47+
super(name, description, unit);
4548
this.type = 'INT64';
4649
}
4750
}
51+
52+
/**
53+
* Simple Mesure Builder and Register
54+
*/
55+
export class MeasureManager {
56+
private static registeredMeasures: RegisteredMeasures = {};
57+
58+
private static registerMeasure(measure: Measure) {
59+
const existingMeasure = MeasureManager.registeredMeasures[measure.name];
60+
if (existingMeasure) {
61+
return;
62+
}
63+
MeasureManager.registeredMeasures[measure.name] = measure;
64+
}
65+
66+
/** Factory method that createas a Measure of type DOUBLE */
67+
static createMeasureDouble(
68+
name: string, description: string, unit: MeasureUnit): Measure {
69+
const newMesure = new MeasureDouble(name, description, unit);
70+
MeasureManager.registerMeasure(newMesure);
71+
return newMesure;
72+
}
73+
74+
/** Factory method that createas a Measure of type INT64 */
75+
static createMeasureInt64(
76+
name: string, description: string, unit: MeasureUnit): Measure {
77+
const newMesure = new MeasureInt64(name, description, unit);
78+
MeasureManager.registerMeasure(newMesure);
79+
return newMesure;
80+
}
81+
}

0 commit comments

Comments
 (0)