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

Commit

Permalink
opencensus-zpages: Enforce strictNullChecks and noUnusedLocals (#432)
Browse files Browse the repository at this point in the history
* opencensus-zpages: Enforce strictNullChecks and noUnusedLocals

* fix tests
  • Loading branch information
mayurkale22 committed Mar 20, 2019
1 parent d838da0 commit 234c395
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class LatencyBucketBoundaries {
case LatencyBucketBoundaries.SECONDx100_MAX:
return '>100s';
default:
return null;
throw new Error('unknown bucket boundary');
}
}

Expand All @@ -122,7 +122,7 @@ export class LatencyBucketBoundaries {
case LatencyBucketBoundaries.SECONDx100_MAX:
return 'SECONDx100_MAX';
default:
return null;
throw new Error('unknown bucket boundary');
}
}

Expand All @@ -137,7 +137,7 @@ export class LatencyBucketBoundaries {
return latency;
}
}
return null;
throw new Error('unknown bucket boundary');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,6 @@ export interface RpczData {
measuresReceived: {[key: string]: ZMeasure};
}

enum DefaultMeasures {
CLIENT_SENT_MESSAGES_PER_RPC = 'grpc.io/client/sent_messages_per_rpc',
CLIENT_SENT_BYTES_PER_RPC = 'grpc.io/client/sent_bytes_per_rpc',
CLIENT_RECEIVED_MESSAGES_PER_RPC = 'grpc.io/client/received_messages_per_rpc',
CLIENT_RECEIVED_BYTES_PER_RPC = 'grpc.io/client/received_bytes_per_rpc',
CLIENT_ROUDTRIP_LATENCY = 'grpc.io/client/roundtrip_latency',
CLIENT_SERVER_LATENCY = 'grpc.io/client/server_latency',
CLIENT_STARTED_RPCS = 'grpc.io/client/started_rpcs',
SERVER_RECEIVED_MESSAGES_PER_RPC = 'grpc.io/server/received_messages_per_rpc',
SERVER_RECEIVED_BYTES_PER_RPC = 'grpc.io/server/received_bytes_per_rpc',
SERVER_SENT_MESSAGES_PER_RPC = 'grpc.io/server/sent_messages_per_rpc',
SERVER_SENT_BYTES_PER_RPC = 'grpc.io/server/sent_bytes_per_rpc',
SERVER_SERVER_LATENCY = 'grpc.io/server/server_latency',
SERVER_STARTED_RPCS = 'grpc.io/server/started_rpcs'
}

enum DefaultViews {
CLIENT_SENT_BYTES_PER_RPC = 'grpc.io/client/sent_bytes_per_rpc',
CLIENT_RECEIVED_BYTES_PER_RPC = 'grpc.io/client/received_bytes_per_rpc',
Expand Down Expand Up @@ -129,7 +113,7 @@ export class RpczPageHandler {
method = snapshot.tagValues[serverMethodIndex].value;
zMeasures = rpczData.measuresReceived;
}
if (method) {
if (zMeasures && method) {
if (!zMeasures[method]) {
zMeasures[method] = this.newEmptyZMeasure();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ export class StatszPageHandler {
/** keeps the folders that belong to the current folder */
const folders: FolderType = {};
/** selected view to show */
let selectedView: View;
let selectedView: View|undefined;
/** keeps HTML table content */
let tableContent: string;
/** keeps the stats and view data to load UI */
let statsViewData: StatsViewData;
let statsViewData: StatsViewData|undefined;

// gets the path from user
if (params.path) {
Expand Down Expand Up @@ -129,7 +129,7 @@ export class StatszPageHandler {
if (selectedView) {
const statsData = this.getStatsData(selectedView);
const viewFile = this.loaderFile('statsz-view.ejs');
let viewContentFile: string;
let viewContentFile: string|undefined;
let statsContent: string;

switch (selectedView.aggregation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import * as tracing from '@opencensus/nodejs';
import * as ejs from 'ejs';
import * as pkgDir from 'pkg-dir';

import {ZpagesExporter} from '../../zpages';

// The directory to search for templates.
const templatesDir = `${pkgDir.sync(__dirname)}/templates`;

Expand All @@ -36,7 +34,7 @@ export interface TraceConfigzData {

export class TraceConfigzPageHandler {
/** Configuration defaults. Currently just the default sampling rate. */
private defaultConfig: {samplingRate: number;};
private defaultConfig?: {samplingRate: number;};

/**
* Generate Zpages Trace Config HTML Page
Expand Down Expand Up @@ -83,7 +81,6 @@ export class TraceConfigzPageHandler {
private saveChanges(query: Partial<TraceConfigzParams>): void {
/** restore the config to default */
if (query.change === 'restore_default') {
const exporter = tracing.exporter as ZpagesExporter;
tracing.tracer.sampler =
SamplerBuilder.getSampler(this.defaultConfig!.samplingRate);
return;
Expand All @@ -107,6 +104,7 @@ export class TraceConfigzPageHandler {
} else if (samplingProbability === 'never') {
return 0;
}
return Number(samplingProbability.match(/\((.*)\)/)[1]);
const probability = samplingProbability.match(/\((.*)\)/);
return probability ? Number(probability[1]) : 1 / 10000;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const getCanonicalCode = (status: number) => {
case 16:
return 'UNAUTHENTICATED';
default:
return null;
return 'UNKNOWN';
}
};

Expand Down
6 changes: 4 additions & 2 deletions packages/opencensus-exporter-zpages/src/zpages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class ZpagesExporter implements Exporter, StatsEventListener {
static readonly defaultOptions = {port: 8080, startServer: true};

private app: express.Application;
private server: http.Server;
private server?: http.Server;
private port: number;
private traces: Map<string, Span[]> = new Map();
private logger: Logger;
Expand Down Expand Up @@ -208,6 +208,8 @@ export class ZpagesExporter implements Exporter, StatsEventListener {
* @param callback A function that will be called when the server is stopped.
*/
stopServer(callback?: () => void) {
this.server.close(callback);
if (this.server) {
this.server.close(callback);
}
}
}
25 changes: 12 additions & 13 deletions packages/opencensus-exporter-zpages/test/test-zpages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import * as assert from 'assert';
import axios from 'axios';
import * as http from 'http';
import * as qs from 'querystring';

import {ZpagesExporter, ZpagesExporterOptions} from '../src/zpages';
import {RpczData} from '../src/zpages-frontend/page-handlers/rpcz.page-handler';
import {StatsViewData, StatszParams} from '../src/zpages-frontend/page-handlers/statsz.page-handler';
Expand Down Expand Up @@ -268,7 +267,7 @@ describe('Zpages Exporter', () => {
it('should get view information', async () => {
const view = globalStats.createView(
'test/CountView', measure, AggregationType.COUNT, tagKeys,
'A count test', null);
'A count test');
globalStats.registerView(view);
globalStats.record([measurement, measurement2], tagMap);

Expand All @@ -283,7 +282,7 @@ describe('Zpages Exporter', () => {
it('should get stats for view', async () => {
const view = globalStats.createView(
'test/CountView', measure, AggregationType.COUNT, tagKeys,
'A count test', null);
'A count test');
globalStats.registerView(view);
globalStats.record([measurement, measurement2], tagMap);

Expand All @@ -301,8 +300,8 @@ describe('Zpages Exporter', () => {
it('should get view information', async () => {
globalStats.registerExporter(zpages);
const view = globalStats.createView(
'test/SumView', measure, AggregationType.SUM, tagKeys, 'A sum test',
null);
'test/SumView', measure, AggregationType.SUM, tagKeys,
'A sum test');
globalStats.registerView(view);
globalStats.record([measurement, measurement2], tagMap);

Expand All @@ -317,8 +316,8 @@ describe('Zpages Exporter', () => {
it('should get stats for view', async () => {
globalStats.registerExporter(zpages);
const view = globalStats.createView(
'test/SumView', measure, AggregationType.SUM, tagKeys, 'A sum test',
null);
'test/SumView', measure, AggregationType.SUM, tagKeys,
'A sum test');
globalStats.registerView(view);
globalStats.record([measurement, measurement2], tagMap);

Expand All @@ -337,7 +336,7 @@ describe('Zpages Exporter', () => {
globalStats.registerExporter(zpages);
const view = globalStats.createView(
'test/LastValueView', measure, AggregationType.LAST_VALUE, tagKeys,
'A last value test', null);
'A last value test');
globalStats.registerView(view);
globalStats.record([measurement, measurement2], tagMap);

Expand All @@ -353,7 +352,7 @@ describe('Zpages Exporter', () => {
globalStats.registerExporter(zpages);
const view = globalStats.createView(
'test/LastValueView', measure, AggregationType.LAST_VALUE, tagKeys,
'A last value test', null);
'A last value test');
globalStats.registerView(view);
globalStats.record([measurement, measurement2], tagMap);

Expand Down Expand Up @@ -474,13 +473,13 @@ describe('Zpages Exporter', () => {
const view4 = globalStats.createView(
'grpc.io/client/completed_rpcs', measure3, AggregationType.COUNT,
[GRPC_CLIENT_METHOD, GRPC_CLIENT_STATUS],
'Number of completed client RPCs', null);
'Number of completed client RPCs');
globalStats.registerView(view4);

const view5 = globalStats.createView(
'grpc.io/client/started_rpcs', measure4, AggregationType.COUNT,
[GRPC_CLIENT_METHOD, GRPC_CLIENT_STATUS],
'Number of started client RPCs', null);
'Number of started client RPCs');
globalStats.registerView(view5);

const measurement = {measure, value: 22000};
Expand Down Expand Up @@ -552,13 +551,13 @@ describe('Zpages Exporter', () => {
const view4 = globalStats.createView(
'grpc.io/server/completed_rpcs', measure7, AggregationType.COUNT,
[GRPC_SERVER_METHOD, GRPC_SERVER_STATUS],
'Number of completed client RPCs', null);
'Number of completed client RPCs');
globalStats.registerView(view4);

const view5 = globalStats.createView(
'grpc.io/server/started_rpcs', measure8, AggregationType.COUNT,
[GRPC_SERVER_METHOD, GRPC_SERVER_STATUS],
'Number of started client RPCs', null);
'Number of started client RPCs');
globalStats.registerView(view5);

const measurement6 = {measure: measure5, value: 2200};
Expand Down
3 changes: 2 additions & 1 deletion packages/opencensus-exporter-zpages/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"pretty": true,
"module": "commonjs",
"target": "es6",
"strictNullChecks": false
"strictNullChecks": true,
"noUnusedLocals": true
},
"include": [
"src/**/*.ts",
Expand Down

0 comments on commit 234c395

Please sign in to comment.