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

Commit d6930df

Browse files
finished zipkin basic implementation
2 parents 50b920d + 1cff931 commit d6930df

4 files changed

Lines changed: 133 additions & 117 deletions

File tree

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

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

17-
import {Clock} from '../../internal/clock'
18-
import {Trace} from './trace'
19-
import {debug, randomSpanId} from '../../internal/util'
20-
import {TraceBaseModel} from '../types/tracetypes'
17+
import { Clock } from '../../internal/clock'
18+
import { Trace } from './trace'
19+
import { debug, randomSpanId } from '../../internal/util'
20+
import { TraceBaseModel } from '../types/tracetypes'
2121

2222

2323
export class Span extends TraceBaseModel {
2424

25-
private trace: TraceBaseModel;
26-
// private _parentSpanId: string;
27-
28-
constructor(trace: TraceBaseModel) {
29-
super()
30-
this.trace = trace;
25+
private trace: TraceBaseModel;
26+
// private _parentSpanId: string;
27+
28+
constructor(trace: TraceBaseModel) {
29+
super()
30+
this.trace = trace;
3131
}
3232

33-
public get traceId() : string {
33+
public get traceId(): string {
3434
return this.trace.traceId;
3535
}
3636

37-
public get parentSpanId() : string {
37+
public get parentSpanId(): string {
3838
return this.trace.id;
3939
}
40-
40+
4141
public get traceContext() {
4242
return {
43-
traceId: this.traceId.toString(),
44-
spanId: this.id.toString(),
45-
options: 1 // always traced
43+
traceId: this.traceId.toString(),
44+
spanId: this.id.toString(),
45+
options: 1 // always traced
4646
};
4747
}
48-
48+
4949
public start() {
5050
super.start();
51-
debug('starting span %o',
52-
{id: this.id,
51+
debug('starting span %o',
52+
{
53+
id: this.id,
5354
traceId: this.traceId,
54-
name: this.name})
55+
name: this.name
56+
})
5557
}
5658

5759
public end(): void {
5860
super.end();
59-
debug('ending span %o',
60-
{spanId: this.id,
61-
traceId: this.trace.id,
62-
name: this.name ,
63-
startTime: this.startTime,
64-
endTime: this.endTime,
65-
duration: this.duration}
61+
debug('ending span %o',
62+
{
63+
spanId: this.id,
64+
traceId: this.trace.id,
65+
name: this.name,
66+
startTime: this.startTime,
67+
endTime: this.endTime,
68+
duration: this.duration
69+
}
6670
)
67-
71+
6872
}
69-
73+
7074

7175
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class Trace extends TraceBaseModel {
4141

4242
public start() {
4343
super.start()
44-
debug('starting trace %o', { id: this.id })
44+
debug('starting trace %o', { traceId: this.traceId })
4545
}
4646

4747
public end() {
@@ -54,7 +54,13 @@ export class Trace extends TraceBaseModel {
5454
})
5555

5656
debug('ending trace %o',
57-
{ id: this.id, name: this.name, startTime: this.startTime, endTime: this.endTime, duration: this.duration })
57+
{
58+
id: this.id,
59+
name: this.name,
60+
startTime: this.startTime,
61+
endTime: this.endTime,
62+
duration: this.duration
63+
})
5864
}
5965

6066
public startSpan(name: string, type: string) {

packages/opencensus-core/src/trace/model/tracer.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515
*/
1616

1717
import * as cls from '../../internal/cls'
18-
import {Trace} from './trace'
19-
import {Span} from './span'
20-
import {debug} from '../../internal/util'
21-
import {Stackdriver} from '../../exporters/stackdriver/stackdriver'
22-
import {StackdriverOptions} from '../../exporters/stackdriver/options'
18+
import { Trace } from './trace'
19+
import { Span } from './span'
20+
import { debug } from '../../internal/util'
21+
import { Stackdriver } from '../../exporters/stackdriver/stackdriver'
22+
import { StackdriverOptions } from '../../exporters/stackdriver/options'
2323
import { Exporter } from '../../exporters/exporter'
2424

2525
export type Func<T> = (...args: any[]) => T;
2626

2727
export class Tracer {
2828

2929
readonly PLUGINS = ['http', 'https', 'mongodb-core', 'express'];
30-
30+
3131
private _active: boolean;
3232
private contextManager: cls.Namespace;
3333
private exporter: Exporter;
@@ -40,15 +40,15 @@ export class Tracer {
4040
this.contextManager = cls.createNamespace();
4141
}
4242

43-
public get currentTrace(): Trace {
44-
return this.contextManager.get('trace');
43+
public get currentTrace(): Trace {
44+
return this.contextManager.get('trace');
4545
}
4646

47-
private setCurrentTrace(trace:Trace) {
48-
this.contextManager.set('trace', trace);
47+
private setCurrentTrace(trace: Trace) {
48+
this.contextManager.set('trace', trace);
4949
}
5050

51-
public start(config?:Object): Tracer {
51+
public start(config?: Object): Tracer {
5252
this._active = true;
5353
return this;
5454
}
@@ -65,22 +65,22 @@ export class Tracer {
6565
}
6666

6767
public endTrace(): void {
68-
if (!this.currentTrace){
68+
if (!this.currentTrace) {
6969
return debug('cannot end trace - no active trace found')
7070
}
7171
this.currentTrace.end();
7272
this.addEndedTrace(this.currentTrace);
7373
//this.clearCurrentTrace();
7474
}
75-
75+
7676
public clearCurrentTrace() {
7777
this.setCurrentTrace(null);
7878
}
7979

80-
public startSpan(name:string, type: string): Span {
80+
public startSpan(name: string, type: string): Span {
8181
let newSpan: Span = null;
82-
if (!this.currentTrace) {
83-
debug('no current trace found - cannot start a new span');
82+
if (!this.currentTrace) {
83+
debug('no current trace found - cannot start a new span');
8484
} else {
8585
newSpan = this.currentTrace.startSpan(name, type);
8686
}
@@ -97,35 +97,35 @@ export class Tracer {
9797
if (this.active) {
9898
//TODO: temp solution
9999
//this.endedTraces.push(trace);
100-
this.exporter.emit(this.currentTrace);
100+
this.exporter.writeTrace(this.currentTrace);
101101
}
102102
}
103103

104104
public wrap<T>(fn: Func<T>): Func<T> {
105105
if (!this.active) {
106-
return fn;
107-
}
108-
106+
return fn;
107+
}
108+
109109
// This is safe because isActive checks the value of this.namespace.
110110
const namespace = this.contextManager as cls.Namespace;
111111
return namespace.bind<T>(fn);
112112
}
113-
113+
114114
public wrapEmitter(emitter: NodeJS.EventEmitter): void {
115-
if (!this.active) {
116-
return;
117-
}
115+
if (!this.active) {
116+
return;
117+
}
118118

119119
// This is safe because isActive checks the value of this.namespace.
120120
const namespace = this.contextManager as cls.Namespace;
121121
namespace.bindEmitter(emitter);
122122
}
123123

124-
public registerExporter(exporter:Exporter) {
124+
public registerExporter(exporter: Exporter) {
125125
this.exporter = exporter;
126126
}
127127

128-
}
128+
}
129129

130130

131131

0 commit comments

Comments
 (0)