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

Commit 84623b0

Browse files
eduardoemerysilva-fabio
authored andcommitted
feat: added spans optional parameters
1 parent 7837f51 commit 84623b0

4 files changed

Lines changed: 302 additions & 25 deletions

File tree

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

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import { Clock } from '../../internal/clock';
1818
import { debug, randomSpanId } from '../../internal/util';
1919
import { Sampler } from '../config/types';
20-
import { TraceContext, Span, MapLabels, MapObjects } from './types';
20+
import { TraceContext, Span, Annotation, Attributes,
21+
Link, MessageEvent } from './types';
2122

2223

2324
export abstract class SpanBaseModel implements Span {
@@ -28,16 +29,20 @@ export abstract class SpanBaseModel implements Span {
2829
private startedLocal = false;
2930
/** Indicates if this span was ended */
3031
private endedLocal = false;
31-
/** A set of attributes, each in the format [KEY]:[VALUE] */
32-
private attributes: MapLabels = {};
33-
/** A set of attributes on the annotation describing an event */
34-
private annotations: MapObjects = {};
3532
/** Indicates if this span was forced to end */
3633
private truncated = false;
3734
/** The Span ID of this span */
3835
readonly id: string;
39-
40-
remoteParent: string;
36+
/** A set of attributes, each in the format [KEY]:[VALUE] */
37+
attributes: Attributes = {};
38+
/** A text annotation with a set of attributes. */
39+
annotations: Annotation[] = [];
40+
/** An event describing a message sent/received between Spans. */
41+
messageEvents: MessageEvent[] = [];
42+
/** Pointers from the current span to another span */
43+
links: Link[] = [];
44+
/** If the parent span is in another process. */
45+
remoteParent: boolean;
4146
/** The span ID of this span's parent. If it's a root span, must be empty */
4247
parentSpanId: string = null;
4348
/** The resource name of the span */
@@ -56,7 +61,8 @@ export abstract class SpanBaseModel implements Span {
5661
}
5762

5863
abstract get traceId(): string;
59-
64+
65+
6066
/** Indicates if span was started. */
6167
get started(): boolean {
6268
return this.startedLocal;
@@ -120,19 +126,52 @@ export abstract class SpanBaseModel implements Span {
120126
* @param key Describes the value added.
121127
* @param value The result of an operation.
122128
*/
123-
addAtribute(key: string, value: string) {
124-
// TODO: maybe key and values must be truncate
129+
addAtribute(key: string, value: string | number | boolean) {
125130
this.attributes[key] = value;
126131
}
127132

128133
/**
129134
* Adds an annotation to the span.
130-
* @param key Describes the value added.
131-
* @param value The result of an operation.
135+
* @param description Describes the event.
136+
* @param timestamp A timestamp that maks the event.
137+
* @param attributes A set of attributes on the annotation.
138+
*/
139+
addAnnotation(description: string, timestamp: number,
140+
attributes?: Attributes) {
141+
this.annotations.push({
142+
description: description,
143+
timestamp: timestamp,
144+
attributes: attributes,
145+
} as Annotation)
146+
}
147+
148+
/**
149+
* Adds a link to the span.
150+
* @param traceId The trace ID for a trace within a project.
151+
* @param spanId The span ID for a span within a trace.
152+
* @param type The relationship of the current span relative to the linked.
153+
* @param attributes A set of attributes on the link.
154+
*/
155+
addLink(traceId: string, spanId: string, type: string,
156+
attributes?: Attributes) {
157+
this.links.push({
158+
traceId: traceId,
159+
SpanId: spanId,
160+
type: type,
161+
attributes: attributes
162+
} as Link)
163+
}
164+
165+
/**
166+
* Adds a message event to the span.
167+
* @param type The type of message event.
168+
* @param id An identifier for the message event.
132169
*/
133-
addAnotation(key: string, value: string|number|boolean) {
134-
// TODO: maybe keys and values must be truncate
135-
this.annotations[key] = value;
170+
addMessageEvent(type: string, id: string) {
171+
this.messageEvents.push({
172+
type: type,
173+
id: id,
174+
} as MessageEvent)
136175
}
137176

138177
/** Starts a span. */

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

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,41 @@ import { Sampler } from '../config/types';
1919
/** Default type for functions */
2020
export type Func<T> = (...args: any[]) => T;
2121

22-
/** Maps a label to a string. Used in spans' attributes. */
23-
export interface MapLabels { [propName: string]: string; }
22+
/** Maps a label to a string, number or boolean. */
23+
export interface Attributes { [attributeKey: string]: string|number|boolean; }
2424

25-
/** Maps a label to a string, number or boolean. Used in spans' annotations. */
26-
export interface MapObjects { [propName: string]: string|number|boolean; }
25+
/** A text annotation with a set of attributes. */
26+
export interface Annotation {
27+
/** A user-supplied message describing the event. */
28+
description: string,
29+
/** A timestamp that maks the event. */
30+
timestamp: number,
31+
/** A set of attributes on the annotation. */
32+
attributes: Attributes
33+
}
34+
35+
/** An event describing a message sent/received between Spans. */
36+
export interface MessageEvent {
37+
/** Indicates whether the message was sent or received. */
38+
type: string,
39+
/** An identifier for the MessageEvent's message. */
40+
id: string,
41+
}
42+
43+
/**
44+
* A pointer from the current span to another span in the same trace or in a
45+
* different trace.
46+
*/
47+
export interface Link {
48+
/** The trace ID for a trace within a project. */
49+
traceId: string,
50+
/** The span ID for a span within a trace. */
51+
SpanId: string,
52+
/** The relationship of the current span relative to the linked. */
53+
type: string,
54+
/** A set of attributes on the link. */
55+
attributes: Attributes
56+
}
2757

2858
/** Defines tracer configuration parameters */
2959
export interface TracerConfig {
@@ -76,7 +106,8 @@ export interface Span {
76106

77107
/** The Span ID of this span */
78108
readonly id: string;
79-
remoteParent: string;
109+
/** If the parent span is in another process. */
110+
remoteParent: boolean;
80111
/** The span ID of this span's parent. If it's a root span, must be empty */
81112
parentSpanId: string;
82113
/** The resource name of the span */
@@ -85,6 +116,14 @@ export interface Span {
85116
type: string;
86117
/** A final status for this span */
87118
status: number;
119+
/** A set of attributes, each in the format [KEY]:[VALUE] */
120+
attributes: Attributes;
121+
/** A text annotation with a set of attributes. */
122+
annotations: Annotation[];
123+
/** An event describing a message sent/received between Spans. */
124+
messageEvents: MessageEvent[];
125+
/** Pointers from the current span to another span */
126+
links: Link[];
88127
/** A sampler that will decide if the span will be sampled or not */
89128
sampler: Sampler;
90129
/** Constructs a new SpanBaseModel instance. */
@@ -118,10 +157,27 @@ export interface Span {
118157
addAtribute(key: string, value: string): void;
119158
/**
120159
* Adds an annotation to the span.
121-
* @param key Describes the value added.
122-
* @param value The result of an operation.
160+
* @param description Describes the event.
161+
* @param timestamp A timestamp that maks the event.
162+
* @param attributes A set of attributes on the annotation.
163+
*/
164+
addAnnotation(description: string, timestamp: number,
165+
attributes?: Attributes): void;
166+
/**
167+
* Adds a link to the span.
168+
* @param traceId The trace ID for a trace within a project.
169+
* @param spanId The span ID for a span within a trace.
170+
* @param type The relationship of the current span relative to the linked.
171+
* @param attributes A set of attributes on the link.
172+
*/
173+
addLink(traceId: string, spanId: string, type: string,
174+
attributes?: Attributes): void;
175+
/**
176+
* Adds a message event to the span.
177+
* @param type The type of message event.
178+
* @param id An identifier for the message event.
123179
*/
124-
addAnotation(key: string, value: string | number | boolean): void;
180+
addMessageEvent(type: string, id: string): void;
125181
/** Starts a span. */
126182
start(): void;
127183
/** Ends a span. */

packages/opencensus-core/test/test-root-span.ts

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import * as mocha from 'mocha';
2020
import {RootSpanImpl} from '../src/trace/model/rootspan';
2121
import {SpanImpl} from '../src/trace/model/span';
2222
import {TracerImpl} from '../src/trace/model/tracer';
23-
import {RootSpan, Span, TraceOptions, Tracer} from '../src/trace/model/types';
23+
import {RootSpan, Span, TraceOptions, Tracer, Link, Annotation, Attributes, MessageEvent}
24+
from '../src/trace/model/types';
2425

2526
const tracer = new TracerImpl();
2627

@@ -167,4 +168,90 @@ describe('RootSpan', () => {
167168
}
168169
});
169170
});
171+
172+
173+
/**
174+
* Should add an attrinbutes
175+
*/
176+
describe('addAtribute()', function() {
177+
it('should add an attribute', function() {
178+
const rootSpan = new RootSpanImpl(tracer);
179+
rootSpan.start();
180+
181+
['String', 'Number', 'Boolean'].map(attType => {
182+
rootSpan.addAtribute('testKey'+attType, 'testValue'+attType)
183+
assert.equal(rootSpan.attributes['testKey'+attType], 'testValue'+attType);
184+
})
185+
});
186+
});
187+
188+
/**
189+
* Should add an annotation
190+
*/
191+
describe('addAnnotation()', function() {
192+
it('should add an annotation', function() {
193+
194+
function instanceOfAnnotation(object: any): object is Annotation {
195+
return 'description' in object
196+
&& 'timestamp' in object
197+
&& 'attributes' in object
198+
}
199+
200+
const rootSpan = new RootSpanImpl(tracer);
201+
rootSpan.start();
202+
203+
rootSpan.addAnnotation('description test', Date.now(), {} as Attributes);
204+
205+
assert.ok(rootSpan.annotations.length > 0)
206+
assert.ok(instanceOfAnnotation(rootSpan.annotations[0]))
207+
});
208+
});
209+
210+
/**
211+
* Should add a Link.
212+
*/
213+
describe('addLink()', function() {
214+
it('should add a Link', function() {
215+
216+
function instanceOfLink(object: any): object is Link {
217+
return 'traceId' in object
218+
&& 'SpanId' in object
219+
&& 'type' in object
220+
}
221+
222+
const rootSpan = new RootSpanImpl(tracer);
223+
rootSpan.start();
224+
225+
const span = new SpanImpl(rootSpan);
226+
span.start();
227+
228+
const LINK_TYPE = 'CHILD_LINKED_SPAN'
229+
rootSpan.addLink(rootSpan.traceId, span.id, LINK_TYPE)
230+
231+
assert.ok(rootSpan.links.length > 0)
232+
assert.ok(instanceOfLink(rootSpan.links[0]))
233+
});
234+
});
235+
236+
/**
237+
* Should add a Message Event.
238+
*/
239+
describe('addMessageEvent()', function() {
240+
it('should add a Message Event', function() {
241+
242+
function instanceOfLink(object: any): object is Link {
243+
return 'type' in object
244+
&& 'id' in object
245+
}
246+
247+
const rootSpan = new RootSpanImpl(tracer);
248+
rootSpan.start();
249+
250+
rootSpan.addMessageEvent('TYPE_UNSPECIFIED', 'message_event_test_id')
251+
252+
assert.ok(rootSpan.messageEvents.length > 0)
253+
assert.ok(instanceOfLink(rootSpan.messageEvents[0]))
254+
});
255+
});
256+
170257
});

0 commit comments

Comments
 (0)