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

Commit 09d4cf5

Browse files
authored
refactor: privatize ZpagesExporter#getAllTraces (#43)
1 parent 1c467f2 commit 09d4cf5

8 files changed

Lines changed: 372 additions & 228 deletions

File tree

packages/opencensus-exporter-zpages/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@types/extend": "^3.0.0",
4444
"@types/mocha": "^2.2.48",
4545
"@types/node": "^9.4.7",
46+
"axios": "^0.18.0",
4647
"gts": "^0.5.4",
4748
"mocha": "^5.0.4",
4849
"nyc": "11.6.0",

packages/opencensus-exporter-zpages/src/zpages-frontend/controllers/traceconfigz.controller.ts

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,15 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
17-
import * as tracing from '@opencensus/nodejs';
18-
import {classes} from '@opencensus/opencensus-core';
1916
import * as express from 'express';
20-
21-
import {ZpagesExporter} from '../../zpages';
22-
2317
import {TraceConfigzPageHandler} from './../page-handlers/traceconfigz.page-handler';
2418

25-
type SaveQuery = {
26-
change: string,
27-
samplingprobability: number
28-
};
29-
3019
/**
31-
* Loads the Trace config page
32-
* @param req request data
33-
* @param res response data
20+
* An Express middleware that renders the TraceConfigz view.
3421
*/
35-
export const home = (req: express.Request, res: express.Response) => {
22+
export function createTraceConfigzHandler(): express.Handler {
3623
const html = new TraceConfigzPageHandler();
37-
if (req.query) {
38-
saveChanges(req.query);
39-
}
40-
res.send(html.emitHtml());
41-
};
42-
43-
/**
44-
* Saves changes made to Trace config page
45-
* @param query request query
46-
*/
47-
const saveChanges = (query: SaveQuery) => {
48-
/** restore the config to default */
49-
if (query.change === 'restore_default') {
50-
const exporter = tracing.exporter as ZpagesExporter;
51-
tracing.tracer.sampler =
52-
new classes.Sampler().probability(exporter.defaultConfig.samplingRate);
53-
return;
54-
}
55-
56-
/** change the sampling probability value */
57-
if (query.samplingprobability) {
58-
tracing.tracer.sampler =
59-
new classes.Sampler().probability(query.samplingprobability);
60-
}
61-
};
24+
return (req: express.Request, res: express.Response) => {
25+
res.send(html.emitHtml(req.query, req.query.json === '1'));
26+
};
27+
}

packages/opencensus-exporter-zpages/src/zpages-frontend/controllers/tracez.controller.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@
1414
* limitations under the License.
1515
*/
1616

17+
import {types} from '@opencensus/opencensus-core';
1718
import * as express from 'express';
1819

1920
import {TracezPageHandler} from './../page-handlers/tracez.page-handler';
2021

21-
export const home = (req: express.Request, res: express.Response) => {
22-
const html = new TracezPageHandler();
23-
res.send(html.emitHtml(req.query));
24-
};
22+
/**
23+
* Creates an Express middleware that renders the Tracez view.
24+
* @param traceMap A span data store.
25+
*/
26+
export function createTracezHandler(traceMap: Map<string, types.Span[]>):
27+
express.Handler {
28+
return (req: express.Request, res: express.Response) => {
29+
const html = new TracezPageHandler(traceMap);
30+
res.send(html.emitHtml(req.query, req.query.json === '1'));
31+
};
32+
}

packages/opencensus-exporter-zpages/src/zpages-frontend/page-handlers/traceconfigz.page-handler.ts

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

1717
import * as tracing from '@opencensus/nodejs';
18-
import {types} from '@opencensus/opencensus-core';
18+
import {classes, types} from '@opencensus/opencensus-core';
1919

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

2222
const ejs = require('ejs');
2323

24+
export interface TraceConfigzParams {
25+
change: string;
26+
samplingprobability: number;
27+
}
28+
29+
export interface TraceConfigzData {
30+
defaultConfig: {samplingRate: number};
31+
samplingProbability: number;
32+
}
33+
2434
export class TraceConfigzPageHandler {
35+
/** Configuration defaults. Currently just the default sampling rate. */
36+
private defaultConfig: {samplingRate: number;};
37+
2538
/**
2639
* Generate Zpages Trace Config HTML Page
40+
* @param params The incoming request query.
41+
* @param json If true, JSON will be emitted instead. Used for testing only.
2742
* @returns output HTML
2843
*/
29-
emitHtml(): string {
44+
emitHtml(params: Partial<TraceConfigzParams>, json: boolean): string {
45+
if (params) {
46+
this.saveChanges(params);
47+
}
48+
49+
if (!this.defaultConfig) {
50+
this.defaultConfig = {
51+
samplingRate: TraceConfigzPageHandler.extractSamplingProbability()
52+
};
53+
}
54+
3055
/** template HTML */
3156
const traceConfigzFile =
3257
ejs.fileLoader(__dirname + '/../templates/traceconfigz.ejs', 'utf8');
3358
/** EJS render options */
3459
const options = {delimiter: '?'};
35-
/** Zpages exporter object from current instance */
36-
const exporter = tracing.exporter as ZpagesExporter;
3760
/** Current sampling rate */
38-
const samplingProbability = this.extractSamplingProbability();
61+
const samplingProbability =
62+
TraceConfigzPageHandler.extractSamplingProbability();
3963

40-
/**
41-
* Checks if the current export has a defaultConfig, otherwise creates one
42-
*/
43-
if (!exporter.defaultConfig) {
44-
exporter.defaultConfig = {samplingRate: samplingProbability} as
45-
types.TracerConfig;
64+
/** Rendering the HTML table summary */
65+
const renderParams: TraceConfigzData = {
66+
defaultConfig: this.defaultConfig,
67+
samplingProbability
68+
};
69+
if (json) {
70+
return JSON.stringify(renderParams, null, 2);
71+
} else {
72+
return ejs.render(traceConfigzFile, renderParams, options);
4673
}
74+
}
4775

48-
/** Rendering the HTML table summary */
49-
return ejs.render(
50-
traceConfigzFile,
51-
{defaultConfig: exporter.defaultConfig, samplingProbability}, options);
76+
/**
77+
* Saves changes made to Trace config page
78+
* @param query request query
79+
*/
80+
private saveChanges(query: Partial<TraceConfigzParams>): void {
81+
/** restore the config to default */
82+
if (query.change === 'restore_default') {
83+
const exporter = tracing.exporter as ZpagesExporter;
84+
tracing.tracer.sampler =
85+
new classes.Sampler().probability(this.defaultConfig!.samplingRate);
86+
return;
87+
}
88+
89+
/** change the sampling probability value */
90+
if (query.samplingprobability) {
91+
tracing.tracer.sampler =
92+
new classes.Sampler().probability(query.samplingprobability);
93+
}
5294
}
5395

5496
/**
5597
* Gets the sample rate from tracer instance
5698
* @returns the sampling probability
5799
*/
58-
private extractSamplingProbability(): number {
100+
private static extractSamplingProbability(): number {
59101
/** */
60102
const samplingProbability = tracing.tracer.sampler.description;
61103
if (samplingProbability === 'always') {

0 commit comments

Comments
 (0)