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

Commit 702b0d0

Browse files
committed
refactor: apply code guidelines rules to stackdriver.ts file5
2 parents 014afa6 + 7f2ef63 commit 702b0d0

9 files changed

Lines changed: 462 additions & 408 deletions

File tree

packages/opencensus-core/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
"compile": "npm run script compile-auto compile-auto-strict",
1414
"clean": "rimraf build/*",
1515
"fix": "gts fix",
16-
"script": "ts-node -P ./scripts ./scripts"
16+
"script": "ts-node -P ./scripts ./scripts",
17+
"check": "gts check",
18+
"prepare": "npm run compile",
19+
"pretest": "npm run compile",
20+
"posttest": "npm run check"
1721
},
1822
"keywords": [
1923
"opencensus",
@@ -46,13 +50,15 @@
4650
"@types/semver": "^5.5.0",
4751
"@types/shimmer": "^1.0.1",
4852
"@types/uuid": "^3.4.3",
53+
"gts": "^0.5.4",
54+
"ncp": "^2.0.0",
4955
"google-cloud": "^0.57.0",
5056
"googleapis": "^27.0.0",
5157
"gts": "^0.5.1",
5258
"mocha": "^5.0.4",
5359
"ncp": "^2.0.0",
5460
"ts-node": "^4.0.0",
55-
"typescript": "^2.7.2"
61+
"typescript": "~2.6.1"
5662
},
5763
"dependencies": {
5864
"async_hooks": "^1.0.0",

packages/opencensus-core/src/exporters

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
17+
/**
18+
* TODO: Interface to exporters options
19+
*/
1820
export interface ExporterOptions {
1921

2022
}

packages/opencensus-core/src/internal/clock.ts

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,67 +15,58 @@
1515
*/
1616

1717
export class Clock {
18-
private _ended: boolean;
19-
private _startTime: Date;
20-
private _hrtime: [number, number];
21-
private diff: [number,number];
18+
private endedLocal: boolean;
19+
private startTimeLocal: Date;
20+
private hrtimeLocal: [number, number];
21+
private diff: [number, number];
2222

23-
constructor() {
24-
this._ended = false
25-
this._startTime = new Date()
26-
this._hrtime = process.hrtime()
27-
this.diff = null
28-
}
23+
constructor() {
24+
this.endedLocal = false;
25+
this.startTimeLocal = new Date();
26+
this.hrtimeLocal = process.hrtime();
27+
this.diff = null;
28+
}
2929

30-
public end(): void {
31-
if (this._ended){
32-
return
33-
}
34-
this.diff = process.hrtime(this._hrtime)
35-
this._ended = true
36-
}
37-
38-
public get duration(): number {
39-
if (!this._ended){
40-
return null
41-
}
42-
var ns = this.diff[0] * 1e9 + this.diff[1]
43-
return ns / 1e6
44-
}
45-
46-
public offset(timer: Clock): number {
47-
var a = timer.hrtime
48-
var b = this.hrtime
49-
var ns = (b[0] - a[0]) * 1e9 + (b[1] - a[1])
50-
return ns / 1e6
30+
end(): void {
31+
if (this.endedLocal) {
32+
return;
5133
}
34+
this.diff = process.hrtime(this.hrtimeLocal);
35+
this.endedLocal = true;
36+
}
5237

53-
public get hrtime() : [number, number] {
54-
return this._hrtime;
55-
}
56-
57-
public get startTime(): Date {
58-
return this._startTime;
38+
get duration(): number {
39+
if (!this.endedLocal) {
40+
return null;
5941
}
60-
61-
public get endTime(): Date {
62-
let result: Date = null;
63-
if(this.ended) {
64-
result= new Date(this.startTime.getTime() + this.duration);
65-
}
66-
return result;
67-
}
68-
69-
public get ended(): boolean {
70-
return this._ended;
71-
}
72-
73-
}
74-
75-
76-
42+
const ns = this.diff[0] * 1e9 + this.diff[1];
43+
return ns / 1e6;
44+
}
7745

46+
offset(timer: Clock): number {
47+
const a = timer.hrtime;
48+
const b = this.hrtime;
49+
const ns = (b[0] - a[0]) * 1e9 + (b[1] - a[1]);
50+
return ns / 1e6;
51+
}
7852

53+
get hrtime(): [number, number] {
54+
return this.hrtimeLocal;
55+
}
7956

57+
get startTime(): Date {
58+
return this.startTimeLocal;
59+
}
8060

61+
get endTime(): Date {
62+
let result: Date = null;
63+
if (this.ended) {
64+
result = new Date(this.startTime.getTime() + this.duration);
65+
}
66+
return result;
67+
}
8168

69+
get ended(): boolean {
70+
return this.endedLocal;
71+
}
72+
}

packages/opencensus-core/src/trace/instrumentation/types.ts

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

17-
export interface Plugin <T> {
18-
applyPatch(module: any, tracer: T, version: string): void;
17+
/**
18+
* Interface Plugin to apply patch.
19+
*/
20+
export interface Plugin<T> {
21+
applyPatch(module: {}, tracer: T, version: string): void;
1922
}
23+
/**
24+
* This class represent the base to patch plugin
25+
*/
26+
export abstract class BasePlugin<T> {
27+
module: {};
28+
moduleName: string;
29+
tracer: T;
30+
version: string;
2031

21-
export abstract class BasePlugin <T> {
22-
23-
public module: any;
24-
public moduleName: string;
25-
public tracer: T;
26-
public version: string;
27-
28-
constructor (moduleName: string) {
29-
this.moduleName = moduleName;
30-
}
31-
32-
setPluginContext(http: any, tracer: T, version: string) {
33-
this.module = http;
34-
this.tracer = tracer;
35-
this.version = version;
36-
}
37-
32+
constructor(moduleName: string) {
33+
this.moduleName = moduleName;
34+
}
35+
/**
36+
* Set modified plugin to the context.
37+
* @param http object module to set on context
38+
* @param tracer tracer relating to context
39+
* @param version module version description
40+
*/
41+
setPluginContext(http: {}, tracer: T, version: string) {
42+
this.module = http;
43+
this.tracer = tracer;
44+
this.version = version;
45+
}
3846
}

packages/opencensus-core/src/trace/model/root-span.ts

Lines changed: 96 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -14,91 +14,115 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Span } from './span'
18-
import { Clock } from '../../internal/clock'
1917
import * as uuid from 'uuid';
20-
import { debug } from '../../internal/util'
21-
import { SpanBaseModel, TraceOptions, TraceContext, OnEndSpanEventListener } from '../types/tracetypes'
22-
import { Tracer } from './tracer'
2318

24-
export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
19+
import {Clock} from '../../internal/clock';
20+
import {debug} from '../../internal/util';
21+
import {OnEndSpanEventListener, SpanBaseModel, TraceContext, TraceOptions} from '../types/tracetypes';
22+
import {Span} from './span';
23+
import {Tracer} from './tracer';
2524

26-
private tracer: Tracer;
27-
private _spans: Span[];
28-
private _traceId: string;
25+
/** Defines a root span */
26+
export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
27+
private tracer: Tracer;
28+
readonly traceId: string;
29+
private spansLocal: Span[] = [];
2930

30-
//TODO - improve root name setup
31-
constructor(tracer: Tracer, context?: TraceOptions) {
32-
super()
33-
this.tracer = tracer;
34-
this._traceId = context && context.traceContext && context.traceContext.traceId ? context.traceContext.traceId : (uuid.v4().split('-').join(''));
35-
this.name = context && context.name ? context.name : 'undefined';
36-
if (context && context.traceContext) {
37-
this.setParentSpanId(context.traceContext.spanId || '')
38-
}
39-
this._spans = [];
31+
/**
32+
* Constructs a new RootSpan instance.
33+
* @param tracer
34+
* @param context
35+
*/
36+
constructor(tracer: Tracer, context?: TraceOptions) {
37+
super();
38+
this.tracer = tracer;
39+
this.traceId =
40+
context && context.traceContext && context.traceContext.traceId ?
41+
context.traceContext.traceId :
42+
(uuid.v4().split('-').join(''));
43+
// TODO - improve root name setup
44+
this.name = context && context.name ? context.name : 'undefined';
45+
if (context && context.traceContext) {
46+
this.setParentSpanId(context.traceContext.spanId || '');
4047
}
48+
}
4149

42-
public get spans() {
43-
return this._spans;
44-
}
50+
/** Returns a list of the trace's spans. */
51+
get spans() {
52+
return this.spansLocal;
53+
}
4554

46-
public get traceId() {
47-
return this._traceId;
48-
}
55+
/** Starts the root span. */
56+
start() {
57+
super.start();
58+
debug('starting %s %o', this._className, {
59+
traceId: this.traceId,
60+
id: this.id,
61+
parentSpanId: this.getParentSpanId()
62+
});
63+
}
4964

50-
public start() {
51-
super.start()
52-
debug('starting %s %o', this._className, { traceId: this.traceId, id: this.id, parentSpanId: this.getParentSpanId() })
53-
}
65+
/** Ends the root span. */
66+
end() {
67+
super.end();
5468

55-
public end() {
56-
super.end()
69+
// TODO - Define logic for list of spans
70+
this.spansLocal.map(span => {
71+
if (span.ended || !span.started) return;
72+
span.truncate();
73+
});
5774

58-
//TODO - Define logic for list of spans
59-
this._spans.forEach(function (span) {
60-
if (span.ended || !span.started) return
61-
span.truncate()
62-
})
75+
debug('ending %s %o', this._className, {
76+
id: this.id,
77+
traceId: this.traceId,
78+
name: this.name,
79+
startTime: this.startTime,
80+
endTime: this.endTime,
81+
duration: this.duration
82+
});
6383

64-
debug('ending %s %o',
65-
this._className,
66-
{
67-
id: this.id,
68-
traceId: this.traceId,
69-
name: this.name,
70-
startTime: this.startTime,
71-
endTime: this.endTime,
72-
duration: this.duration
73-
})
84+
this.tracer.onEndSpan(this);
85+
}
7486

75-
this.tracer.onEndSpan(this)
76-
}
87+
/**
88+
* Happens when a span is ended.
89+
* @param span
90+
*/
91+
onEndSpan(span: Span) {
92+
debug('%s notified ending by %o', {id: span.id, name: span.name});
93+
}
7794

78-
public onEndSpan(span: Span) {
79-
debug('%s notified ending by %o', { id: span.id, name: span.name })
95+
/**
96+
* Starts a span inside the respective trace.
97+
* @param name Span's name
98+
* @param type Spans's type
99+
* @param parentSpanId Span's parent ID
100+
*/
101+
startSpan(name: string, type: string, parentSpanId?: string) {
102+
if (!this.started) {
103+
debug(
104+
'calling %s.startSpan() on un-started %s %o', this._className,
105+
this._className, {id: this.id, name: this.name, type: this.type});
106+
return;
80107
}
81-
82-
public startSpan(name: string, type: string, parentSpanId?: string) {
83-
if (!this.started) {
84-
debug('calling %s.startSpan() on un-started %s %o',
85-
this._className, this._className,
86-
{ id: this.id, name: this.name, type: this.type })
87-
return
88-
}
89-
if (this.ended) {
90-
debug('calling %s.startSpan() on ended %s %o',
91-
this._className, this._className,
92-
{ id: this.id, name: this.name, type: this.type })
93-
return
94-
}
95-
let newSpan = new Span(this);
96-
if (name) { newSpan.name = name }
97-
if (type) { newSpan.type = type }
98-
if (type) { newSpan.setParentSpanId(parentSpanId || '') }
99-
newSpan.start();
100-
this._spans.push(newSpan);
101-
return newSpan;
108+
if (this.ended) {
109+
debug(
110+
'calling %s.startSpan() on ended %s %o', this._className,
111+
this._className, {id: this.id, name: this.name, type: this.type});
112+
return;
113+
}
114+
const newSpan = new Span(this);
115+
if (name) {
116+
newSpan.name = name;
117+
}
118+
if (type) {
119+
newSpan.type = type;
102120
}
121+
if (type) {
122+
newSpan.setParentSpanId(parentSpanId || '');
123+
}
124+
newSpan.start();
125+
this.spansLocal.push(newSpan);
126+
return newSpan;
127+
}
103128
}
104-

0 commit comments

Comments
 (0)