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

Commit 64b104b

Browse files
tcolgatekjin
authored andcommitted
feat: add support for annotations and messageevents to jaeger (#78)
* feature: add support for annotations and messageevents to jaeger * refactor: lint check
1 parent e35be89 commit 64b104b

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

packages/opencensus-exporter-jaeger/src/jaeger-driver/index.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,40 @@ export type SenderCallback = (numSpans: number, err?: string) => void;
4848
* @param span
4949
*/
5050
export function spanToThrift(span: Span) {
51-
let spanTags = [];
51+
const tags = [];
5252
if (span.attributes) {
53-
const tags = [];
5453
Object.keys(span.attributes).forEach(key => {
5554
tags.push({'key': key, 'value': span.attributes[key]});
5655
});
57-
spanTags = ThriftUtils.getThriftTags(tags);
5856
}
5957

60-
const spanLogs = [];
58+
const logs = [];
59+
if (span.messageEvents) {
60+
span.messageEvents.forEach(msg => {
61+
logs.push({
62+
timestamp: msg.timestamp,
63+
fields: [
64+
{'key': 'message.id', 'value': msg.id},
65+
{'key': 'message.type', 'value': msg.type}
66+
],
67+
});
68+
});
69+
}
70+
71+
if (span.annotations) {
72+
span.annotations.forEach(ann => {
73+
const tags = [];
74+
Object.keys(ann.attributes).forEach(key => {
75+
tags.push({'key': key, 'value': ann.attributes[key]});
76+
});
77+
tags.push({'key': 'description', 'value': ann.description});
78+
logs.push({
79+
timestamp: ann.timestamp,
80+
fields: tags,
81+
});
82+
});
83+
}
84+
6185
const unsigned = true;
6286
const parentSpan = span.parentSpanId ? Utils.encodeInt64(span.parentSpanId) :
6387
ThriftUtils.emptyBuffer;
@@ -67,6 +91,8 @@ export function spanToThrift(span: Span) {
6791

6892
const high = traceId.slice(0, 16);
6993
const low = traceId.slice(16);
94+
const spanTags = ThriftUtils.getThriftTags(tags);
95+
const spanLogs = ThriftUtils.getThriftLogs(logs);
7096

7197
return {
7298
traceIdLow: Utils.encodeInt64(low),

packages/opencensus-exporter-jaeger/test/test-jaeger.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,60 @@ describe('Jaeger Exporter', () => {
8080
it('should encode as thrift', () => {
8181
return tracer.startRootSpan({name: 'root-s01'}, (rootSpan) => {
8282
const span = tracer.startChildSpan('child-s01');
83+
span.addAttribute('testBool', true);
84+
span.addAttribute('testString', 'here');
85+
span.addAttribute('testNum', 3.142);
86+
span.addAnnotation('something happened', {
87+
'error': true,
88+
});
8389
span.end();
8490
rootSpan.end();
8591
const thriftSpan = spanToThrift(span);
8692
const result = ThriftUtils._thrift.Span.rw.toBuffer(thriftSpan);
8793
assert.strictEqual(result.err, null);
94+
95+
assert.strictEqual(thriftSpan.tags.length, 3);
96+
let testBoolSeen = false;
97+
let testStringSeen = false;
98+
let testNumSeen = false;
99+
thriftSpan.tags.forEach((tag) => {
100+
if (tag.key === 'testBool' && tag.vType === 'BOOL' &&
101+
tag.vBool === true) {
102+
testBoolSeen = true;
103+
return;
104+
}
105+
if (tag.key === 'testString' && tag.vType === 'STRING' &&
106+
tag.vStr === 'here') {
107+
testStringSeen = true;
108+
return;
109+
}
110+
if (tag.key === 'testNum' && tag.vType === 'DOUBLE' &&
111+
tag.vDouble === 3.142) {
112+
testNumSeen = true;
113+
return;
114+
}
115+
});
116+
117+
assert.strictEqual(true, testBoolSeen && testStringSeen && testNumSeen);
118+
119+
assert.strictEqual(thriftSpan.logs.length, 1);
120+
thriftSpan.logs.forEach((log) => {
121+
let descriptionSeen = false;
122+
let errorSeen = false;
123+
log.fields.forEach((field) => {
124+
if (field.key === 'description' && field.vType === 'STRING' &&
125+
field.vStr === 'something happened') {
126+
descriptionSeen = true;
127+
return;
128+
}
129+
if (field.key === 'error' && field.vType === 'BOOL' &&
130+
field.vBool === true) {
131+
errorSeen = true;
132+
return;
133+
}
134+
assert.strictEqual(true, descriptionSeen && errorSeen);
135+
});
136+
});
88137
});
89138
});
90139
});

0 commit comments

Comments
 (0)