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

Commit b916ec9

Browse files
authored
Add support for Opencensus Span links to Thrift Span references (#473)
* Add support for Opencensus Span links to Thrift Span references * fix review comments
1 parent dff1e54 commit b916ec9

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55
## Unreleased
66

77
- Gauge: Add support for constant labels.
8+
- Add support for Opencensus Span links to Thrift Span references.
89

910
## 0.0.10 - 2019-04-03
1011
- Add optional `compressedSize` and `uncompressedSize` params to `Span.addMessageEvent`

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {Span} from '@opencensus/core';
17+
import {Link, LinkType, Span} from '@opencensus/core';
1818

1919
// tslint:disable-next-line:variable-name
2020
export const UDPSender =
@@ -139,7 +139,7 @@ export function spanToThrift(span: Span): ThriftSpan {
139139
spanId: Utils.encodeInt64(span.spanContext.spanId),
140140
parentSpanId: parentSpan,
141141
operationName: span.name,
142-
references: [],
142+
references: getThriftReference(span.links),
143143
flags: span.spanContext.options || 0x1,
144144
startTime:
145145
Utils.encodeInt64(span.startTime.getTime() * 1000), // to microseconds
@@ -148,3 +148,33 @@ export function spanToThrift(span: Span): ThriftSpan {
148148
logs: spanLogs,
149149
};
150150
}
151+
152+
function getThriftReference(links: Link[]): ThriftReference[] {
153+
return links
154+
.map(
155+
(link):
156+
ThriftReference|
157+
null => {
158+
const refType = getThriftType(link.type);
159+
if (!refType) return null;
160+
161+
const traceId =
162+
`00000000000000000000000000000000${link.traceId}`.slice(
163+
-32);
164+
const traceIdHigh = Utils.encodeInt64(traceId.slice(0, 16));
165+
const traceIdLow = Utils.encodeInt64(traceId.slice(16));
166+
const spanId = Utils.encodeInt64(link.spanId);
167+
return {traceIdLow, traceIdHigh, spanId, refType};
168+
})
169+
.filter(ref => !!ref) as ThriftReference[];
170+
}
171+
172+
function getThriftType(type: number): ThriftReferenceType|null {
173+
if (type === LinkType.CHILD_LINKED_SPAN) {
174+
return ThriftReferenceType.CHILD_OF;
175+
}
176+
if (type === LinkType.PARENT_LINKED_SPAN) {
177+
return ThriftReferenceType.FOLLOWS_FROM;
178+
}
179+
return null;
180+
}

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

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

17-
import {CoreTracer, logger} from '@opencensus/core';
17+
import {CoreTracer, LinkType, logger} from '@opencensus/core';
1818
import * as assert from 'assert';
19-
import {JaegerTraceExporter, JaegerTraceExporterOptions} from '../src/';
20-
import {spanToThrift, ThriftUtils, UDPSender} from '../src/jaeger-driver';
21-
22-
const DEFAULT_BUFFER_TIMEOUT = 10; // time in milliseconds
2319

20+
import {JaegerTraceExporter, JaegerTraceExporterOptions,} from '../src/';
21+
import {spanToThrift, ThriftReferenceType, ThriftUtils, UDPSender} from '../src/jaeger-driver';
2422
import {ThriftProcess} from '../src/jaeger-driver';
2523
import {SenderCallback} from '../src/jaeger-driver';
2624

25+
const DEFAULT_BUFFER_TIMEOUT = 10; // time in milliseconds
2726

2827
/**
2928
* Controls if the tests will use a real network or not
@@ -116,6 +115,13 @@ describe('Jaeger Exporter', () => {
116115
span.addAnnotation('something happened', {
117116
'error': true,
118117
});
118+
const traceIdHigh = '6e0c63257de34c92';
119+
const traceIdLow = 'bf9efcd03927272e';
120+
const traceId = traceIdHigh + traceIdLow;
121+
const spanId = '6e0c63257de34c92';
122+
span.addLink(traceId, spanId, LinkType.CHILD_LINKED_SPAN);
123+
span.addLink(traceId, spanId, LinkType.PARENT_LINKED_SPAN);
124+
span.addLink(traceId, spanId, LinkType.UNSPECIFIED);
119125
span.end();
120126
rootSpan.end();
121127
const thriftSpan = spanToThrift(span);
@@ -146,6 +152,22 @@ describe('Jaeger Exporter', () => {
146152

147153
assert.ok(testBoolSeen && testStringSeen && testNumSeen);
148154

155+
assert.equal(thriftSpan.references.length, 2);
156+
assert.deepEqual(thriftSpan.references, [
157+
{
158+
refType: ThriftReferenceType.CHILD_OF,
159+
traceIdHigh: Buffer.from([110, 12, 99, 37, 125, 227, 76, 146]),
160+
traceIdLow: Buffer.from([191, 158, 252, 208, 57, 39, 39, 46]),
161+
spanId: Buffer.from([110, 12, 99, 37, 125, 227, 76, 146])
162+
},
163+
{
164+
refType: ThriftReferenceType.FOLLOWS_FROM,
165+
traceIdHigh: Buffer.from([110, 12, 99, 37, 125, 227, 76, 146]),
166+
traceIdLow: Buffer.from([191, 158, 252, 208, 57, 39, 39, 46]),
167+
spanId: Buffer.from([110, 12, 99, 37, 125, 227, 76, 146])
168+
}
169+
]);
170+
149171
assert.strictEqual(thriftSpan.logs.length, 1);
150172
thriftSpan.logs.forEach((log) => {
151173
let descriptionSeen = false;

0 commit comments

Comments
 (0)