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

Commit

Permalink
Add support for Opencensus Span links to Thrift Span references (#473)
Browse files Browse the repository at this point in the history
* Add support for Opencensus Span links to Thrift Span references

* fix review comments
  • Loading branch information
mayurkale22 authored Apr 8, 2019
1 parent dff1e54 commit b916ec9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
## Unreleased

- Gauge: Add support for constant labels.
- Add support for Opencensus Span links to Thrift Span references.

## 0.0.10 - 2019-04-03
- Add optional `compressedSize` and `uncompressedSize` params to `Span.addMessageEvent`
Expand Down
34 changes: 32 additions & 2 deletions packages/opencensus-exporter-jaeger/src/jaeger-driver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {Span} from '@opencensus/core';
import {Link, LinkType, Span} from '@opencensus/core';

// tslint:disable-next-line:variable-name
export const UDPSender =
Expand Down Expand Up @@ -139,7 +139,7 @@ export function spanToThrift(span: Span): ThriftSpan {
spanId: Utils.encodeInt64(span.spanContext.spanId),
parentSpanId: parentSpan,
operationName: span.name,
references: [],
references: getThriftReference(span.links),
flags: span.spanContext.options || 0x1,
startTime:
Utils.encodeInt64(span.startTime.getTime() * 1000), // to microseconds
Expand All @@ -148,3 +148,33 @@ export function spanToThrift(span: Span): ThriftSpan {
logs: spanLogs,
};
}

function getThriftReference(links: Link[]): ThriftReference[] {
return links
.map(
(link):
ThriftReference|
null => {
const refType = getThriftType(link.type);
if (!refType) return null;

const traceId =
`00000000000000000000000000000000${link.traceId}`.slice(
-32);
const traceIdHigh = Utils.encodeInt64(traceId.slice(0, 16));
const traceIdLow = Utils.encodeInt64(traceId.slice(16));
const spanId = Utils.encodeInt64(link.spanId);
return {traceIdLow, traceIdHigh, spanId, refType};
})
.filter(ref => !!ref) as ThriftReference[];
}

function getThriftType(type: number): ThriftReferenceType|null {
if (type === LinkType.CHILD_LINKED_SPAN) {
return ThriftReferenceType.CHILD_OF;
}
if (type === LinkType.PARENT_LINKED_SPAN) {
return ThriftReferenceType.FOLLOWS_FROM;
}
return null;
}
32 changes: 27 additions & 5 deletions packages/opencensus-exporter-jaeger/test/test-jaeger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
* limitations under the License.
*/

import {CoreTracer, logger} from '@opencensus/core';
import {CoreTracer, LinkType, logger} from '@opencensus/core';
import * as assert from 'assert';
import {JaegerTraceExporter, JaegerTraceExporterOptions} from '../src/';
import {spanToThrift, ThriftUtils, UDPSender} from '../src/jaeger-driver';

const DEFAULT_BUFFER_TIMEOUT = 10; // time in milliseconds

import {JaegerTraceExporter, JaegerTraceExporterOptions,} from '../src/';
import {spanToThrift, ThriftReferenceType, ThriftUtils, UDPSender} from '../src/jaeger-driver';
import {ThriftProcess} from '../src/jaeger-driver';
import {SenderCallback} from '../src/jaeger-driver';

const DEFAULT_BUFFER_TIMEOUT = 10; // time in milliseconds

/**
* Controls if the tests will use a real network or not
Expand Down Expand Up @@ -116,6 +115,13 @@ describe('Jaeger Exporter', () => {
span.addAnnotation('something happened', {
'error': true,
});
const traceIdHigh = '6e0c63257de34c92';
const traceIdLow = 'bf9efcd03927272e';
const traceId = traceIdHigh + traceIdLow;
const spanId = '6e0c63257de34c92';
span.addLink(traceId, spanId, LinkType.CHILD_LINKED_SPAN);
span.addLink(traceId, spanId, LinkType.PARENT_LINKED_SPAN);
span.addLink(traceId, spanId, LinkType.UNSPECIFIED);
span.end();
rootSpan.end();
const thriftSpan = spanToThrift(span);
Expand Down Expand Up @@ -146,6 +152,22 @@ describe('Jaeger Exporter', () => {

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

assert.equal(thriftSpan.references.length, 2);
assert.deepEqual(thriftSpan.references, [
{
refType: ThriftReferenceType.CHILD_OF,
traceIdHigh: Buffer.from([110, 12, 99, 37, 125, 227, 76, 146]),
traceIdLow: Buffer.from([191, 158, 252, 208, 57, 39, 39, 46]),
spanId: Buffer.from([110, 12, 99, 37, 125, 227, 76, 146])
},
{
refType: ThriftReferenceType.FOLLOWS_FROM,
traceIdHigh: Buffer.from([110, 12, 99, 37, 125, 227, 76, 146]),
traceIdLow: Buffer.from([191, 158, 252, 208, 57, 39, 39, 46]),
spanId: Buffer.from([110, 12, 99, 37, 125, 227, 76, 146])
}
]);

assert.strictEqual(thriftSpan.logs.length, 1);
thriftSpan.logs.forEach((log) => {
let descriptionSeen = false;
Expand Down

0 comments on commit b916ec9

Please sign in to comment.