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

Commit 096eb70

Browse files
authored
Exporter/Prometheus: Enforce strictNullChecks and noUnusedLocals (#419)
1 parent 5dfea38 commit 096eb70

File tree

8 files changed

+22
-20
lines changed

8 files changed

+22
-20
lines changed

examples/stats/exporter/prometheus.js

-2
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,11 @@ lineReader.on('line', function (line) {
117117

118118
globalStats.record([{
119119
measure: mLineLengths,
120-
tags,
121120
value: processedLine.length
122121
}], tags);
123122

124123
globalStats.record([{
125124
measure: mLatencyMs,
126-
tags,
127125
value: sinceInMilliseconds(endNanoseconds, startNanoseconds)
128126
}], tags);
129127
} catch (err) {

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export interface View {
197197
* Returns a snapshot of an AggregationData for that tags/labels values.
198198
* @param tagValues The desired data's tag values.
199199
*/
200-
getSnapshot(tagValues: TagValue[]): AggregationData;
200+
getSnapshot(tagValues: Array<TagValue|null>): AggregationData;
201201
/** Gets the view's tag keys */
202202
getColumns(): TagKey[];
203203
/** Gets view`s metric */

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ export class BaseView implements View {
257257
* @param tags The desired data's tags
258258
* @returns {AggregationData}
259259
*/
260-
getSnapshot(tagValues: TagValue[]): AggregationData {
260+
getSnapshot(tagValues: Array<TagValue|null>): AggregationData {
261261
return this.tagValueAggregationMap[this.encodeTagValues(tagValues)];
262262
}
263263

packages/opencensus-exporter-prometheus/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
# OpenCensus Prometheus Exporter for Node.js
1+
# OpenCensus Prometheus Stats Exporter
22
[![Gitter chat][gitter-image]][gitter-url] ![Node Version][node-img] [![NPM Published Version][npm-img]][npm-url] ![dependencies Status][dependencies-status] ![devDependencies Status][devdependencies-status] ![Apache License][license-image]
33

4-
The OpenCensus Prometheus Exporter allows the user to send collected stats with [OpenCensus Core](https://github.com/census-instrumentation/opencensus-core) to Prometheus.
4+
The OpenCensus Prometheus Stats Exporter allows the user to send collected stats with [OpenCensus Core](https://github.com/census-instrumentation/opencensus-core) to Prometheus.
5+
6+
[Prometheus](https://prometheus.io/) is a monitoring system that collects metrics, by scraping exposed endpoints at regular intervals, evaluating rule expressions. It can also trigger alerts if certain conditions are met. For assistance setting up Prometheus, [Click here](https://opencensus.io/codelabs/prometheus/#0) for a guided codelab.
57

68
This package is still at an early stage of development, and is subject to change.
79

packages/opencensus-exporter-prometheus/src/prometheus-stats.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class PrometheusStatsExporter implements StatsEventListener {
4747
private prefix: string;
4848
private port: number;
4949
private app = express();
50-
private server: http.Server;
50+
private server?: http.Server;
5151
// Registry instance from Prometheus to keep the metrics
5252
private registry = new Registry();
5353

@@ -105,7 +105,10 @@ export class PrometheusStatsExporter implements StatsEventListener {
105105
const labels: labelValues = {};
106106
columns.forEach((tagKey) => {
107107
if (tags.has(tagKey)) {
108-
labels[tagKey.name] = tags.get(tagKey).value;
108+
const tagValue = tags.get(tagKey);
109+
if (tagValue) {
110+
labels[tagKey.name] = tagValue.value;
111+
}
109112
}
110113
});
111114
return labels;
@@ -149,8 +152,7 @@ export class PrometheusStatsExporter implements StatsEventListener {
149152
metric = new Histogram(distribution);
150153
break;
151154
default:
152-
this.logger.error('Aggregation %s is not supported', view.aggregation);
153-
return null;
155+
this.logger.error(`Aggregation ${view.aggregation} is not supported`);
154156
}
155157

156158
this.registry.registerMetric(metric);

packages/opencensus-exporter-prometheus/test/test-prometheus-stats.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import {AggregationType, globalStats, Measure, MeasureUnit, TagMap} from '@opencensus/core';
1818
import * as assert from 'assert';
1919
import * as http from 'http';
20-
2120
import {PrometheusStatsExporter} from '../src/';
2221

2322
describe('Prometheus Stats Exporter', () => {
@@ -48,7 +47,7 @@ describe('Prometheus Stats Exporter', () => {
4847
it('should create a count aggregation', (done) => {
4948
const view = globalStats.createView(
5049
'ocnodemetrics/countview', measure, AggregationType.COUNT, tagKeys,
51-
'A count aggregation example', null);
50+
'A count aggregation example');
5251
const measurement = {measure, value: 2};
5352
const measurement2 = {measure, value: 3};
5453
globalStats.registerView(view);
@@ -75,7 +74,7 @@ describe('Prometheus Stats Exporter', () => {
7574
globalStats.createMeasureDouble('testMeasureDouble', MeasureUnit.UNIT);
7675
const view = globalStats.createView(
7776
'ocnodemetrics/sumview', measure, AggregationType.SUM, tagKeys,
78-
'A sum aggregation example', null);
77+
'A sum aggregation example');
7978
const measurement = {measure, value: 2};
8079
const measurement2 = {measure, value: 3};
8180
globalStats.registerView(view);
@@ -102,7 +101,7 @@ describe('Prometheus Stats Exporter', () => {
102101
globalStats.createMeasureDouble('testMeasureDouble', MeasureUnit.UNIT);
103102
const view = globalStats.createView(
104103
'ocnodemetrics/sumview1', measure, AggregationType.SUM, tagKeys,
105-
'A sum aggregation example', null);
104+
'A sum aggregation example');
106105
const measurement = {measure, value: 2};
107106
const measurement2 = {measure, value: 3};
108107
globalStats.registerView(view);
@@ -128,7 +127,7 @@ describe('Prometheus Stats Exporter', () => {
128127
globalStats.createMeasureDouble('testMeasureDouble', MeasureUnit.UNIT);
129128
const view = globalStats.createView(
130129
'ocnodemetrics/lastvalueview', measure, AggregationType.LAST_VALUE,
131-
tagKeys, 'A last value aggregation example', null);
130+
tagKeys, 'A last value aggregation example');
132131
const measurement = {measure, value: 2};
133132
const measurement2 = {measure, value: 3};
134133
globalStats.registerView(view);
@@ -247,7 +246,7 @@ describe('Prometheus Stats Exporter with prefix option', () => {
247246
it('should create a count aggregation with le labels', (done) => {
248247
const view = globalStats.createView(
249248
'test/key-1', measure, AggregationType.COUNT, tagKeys,
250-
'A count aggregation example', null);
249+
'A count aggregation example');
251250
const measurement = {measure, value: 2};
252251
const measurement2 = {measure, value: 3};
253252
globalStats.registerView(view);

packages/opencensus-exporter-prometheus/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"pretty": true,
77
"module": "commonjs",
88
"target": "es6",
9-
"strictNullChecks": false
9+
"strictNullChecks": true,
10+
"noUnusedLocals": true
1011
},
1112
"exclude": [
1213
"node_modules"

0 commit comments

Comments
 (0)