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

Commit 532a28e

Browse files
djonathascardosokjin
authored andcommitted
feat: add stats core
1 parent 77036f4 commit 532a28e

9 files changed

Lines changed: 760 additions & 4 deletions

File tree

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

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

1717
import * as loggerTypes from '../common/types';
18+
import {Measure, Measurement, View} from '../stats/model/types';
1819
import * as modelTypes from '../trace/model/types';
1920

2021
import {ExporterBuffer} from './exporter-buffer';
@@ -45,17 +46,14 @@ export class ConsoleExporter implements types.Exporter {
4546
this.buffer = new ExporterBuffer(this, config);
4647
this.logger = config.logger;
4748
}
48-
4949
onStartSpan(root: modelTypes.RootSpan) {}
50-
5150
/**
5251
* Event called when a span is ended.
5352
* @param root Ended span.
5453
*/
5554
onEndSpan(root: modelTypes.RootSpan) {
5655
this.buffer.addToBuffer(root);
5756
}
58-
5957
/**
6058
* Sends the spans information to the console.
6159
* @param rootSpans
@@ -77,3 +75,25 @@ export class ConsoleExporter implements types.Exporter {
7775
return Promise.resolve();
7876
}
7977
}
78+
79+
/** Exporter that receives stats data and shows in the log console. */
80+
export class ConsoleStatsExporter implements types.StatsExporter {
81+
/**
82+
* Event called when a view is registered
83+
* @param view registered view
84+
* @param measure registered measure
85+
*/
86+
onRegisterView(view: View, measure: Measure) {
87+
console.log(
88+
`View registered: ${view.name}, Measure registered: ${measure.name}`);
89+
}
90+
/**
91+
* Event called when a measurement is recorded
92+
* @param view recorded view from measurement
93+
* @param measurement recorded measurement
94+
*/
95+
onRecord(view: View, measurement: Measurement) {
96+
console.log(`Measurement recorded: ${measurement.measure.name}, value: ${
97+
measurement.value}`);
98+
}
99+
}

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

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

1717

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

@@ -29,4 +29,19 @@ export interface Exporter extends modelTypes.SpanEventListener {
2929
publish(rootSpans: modelTypes.RootSpan[]): Promise<number|string|void>;
3030
}
3131

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+
}
46+
3247
export type ExporterConfig = configTypes.BufferConfig;

packages/opencensus-core/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ export * from './trace/instrumentation/base-plugin';
4040
export * from './exporters/exporter-buffer';
4141
export * from './exporters/console-exporter';
4242

43+
// STATS CLASSES
44+
45+
// domain models impls
46+
export * from './stats/model/aggregation';
47+
export * from './stats/model/measure';
48+
export * from './stats/model/stats-manager';
49+
50+
// interfaces
51+
export * from './stats/model/types';
52+
4353
// logger
4454
import * as logger from './common/console-logger';
4555
export {logger};
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* Copyright 2018, OpenCensus 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+
* http://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 {Aggregation, HistogramBucket, Measurement} from './types';
18+
19+
/** Counts the number of recorded measurements. */
20+
export class AggregationCount implements Aggregation {
21+
measurements: Measurement[] = [];
22+
23+
getValue(): number {
24+
return this.measurements.length;
25+
}
26+
27+
getType(): string {
28+
return 'COUNT';
29+
}
30+
}
31+
32+
/** Returns the recorded measurements values summed. */
33+
export class AggregationSum implements Aggregation {
34+
measurements: Measurement[] = [];
35+
36+
getValue(): number {
37+
let sum = 0;
38+
for (const measurement of this.measurements) {
39+
sum += measurement.value;
40+
}
41+
return sum;
42+
}
43+
44+
getType(): string {
45+
return 'SUM';
46+
}
47+
}
48+
49+
/** Returns the last recorded measurement value. */
50+
export class AggregationLastValue implements Aggregation {
51+
measurements: Measurement[] = [];
52+
53+
getValue(): number {
54+
return this.measurements[this.measurements.length - 1].value;
55+
}
56+
57+
getType(): string {
58+
return 'LAST VALUE';
59+
}
60+
}
61+
62+
/**
63+
* Returns a histogram distribution. A distribution Aggregation may contain a
64+
* histogram of the values in the population.
65+
*/
66+
export class AggregationDistribution implements Aggregation {
67+
private bucketBoundaries: number[];
68+
measurements: Measurement[] = [];
69+
70+
constructor(bucketBoundaries: number[]) {
71+
this.bucketBoundaries = bucketBoundaries;
72+
}
73+
74+
getValue(): HistogramBucket[] {
75+
const histograms = this.createHistogramBase();
76+
77+
/** loop to calculate the bucket sizes */
78+
for (const measurement of this.measurements) {
79+
for (const histogram of histograms) {
80+
if (measurement.value < histogram.range.max) {
81+
histogram.bucketCount += 1;
82+
break;
83+
}
84+
}
85+
}
86+
87+
return histograms;
88+
}
89+
90+
getType(): string {
91+
return 'DISTRIBUTION';
92+
}
93+
94+
/**
95+
* Creates the initial histogram with the bucket boundaries
96+
*/
97+
private createHistogramBase(): HistogramBucket[] {
98+
const histogram: HistogramBucket[] = [];
99+
const bucketBoundaries = this.bucketBoundaries;
100+
101+
// pushing the first value
102+
histogram.push(
103+
{range: {min: -Infinity, max: bucketBoundaries[0]}, bucketCount: 0});
104+
105+
// pushing the second value to the penultimate value
106+
for (let i = 1; i < bucketBoundaries.length; i++) {
107+
histogram.push({
108+
range: {min: bucketBoundaries[i - 1], max: bucketBoundaries[i]},
109+
bucketCount: 0
110+
});
111+
}
112+
113+
// pushing the last value
114+
histogram.push({
115+
range:
116+
{min: bucketBoundaries[bucketBoundaries.length - 1], max: Infinity},
117+
bucketCount: 0
118+
});
119+
120+
return histogram;
121+
}
122+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright 2018, OpenCensus 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+
* http://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 {Measure} from './types';
18+
19+
/** Measure class for double type */
20+
export class MeasureDouble implements Measure {
21+
name: string;
22+
description: string;
23+
unit: string;
24+
type: string;
25+
26+
constructor(name: string, description: string, unit: string) {
27+
this.name = name;
28+
this.description = description;
29+
this.unit = unit;
30+
this.type = 'DOUBLE';
31+
}
32+
}
33+
34+
/** 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;
45+
this.type = 'INT64';
46+
}
47+
}

0 commit comments

Comments
 (0)