Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let userTags = options.tags;
let startTime = options.startTime || this.now();
// followsFromIsParent is used to ensure that CHILD_OF reference is preferred
// as a parent even if it comes after FOLLOWS_FROM reference.
let followsFromIsParent = false;
let parent: ?SpanContext = options.childOf instanceof Span ? options.childOf.context() : options.childOf;
// If there is no childOf in options, then search list of references
for (let i = 0; i < references.length; i++) {
let ref: Reference = references[i];
if (ref.type() === opentracing.REFERENCE_CHILD_OF) {
if (!parent || followsFromIsParent) {
parent = ref.referencedContext();
break;
}
} else if (ref.type() === opentracing.REFERENCE_FOLLOWS_FROM) {
if (!parent) {
parent = ref.referencedContext();
followsFromIsParent = true;
}
}
}
let ctx: SpanContext;
let internalTags: any = {};
let hasValidParent = false;
const isRpcServer = Boolean(userTags && userTags[otTags.SPAN_KIND] === otTags.SPAN_KIND_RPC_SERVER);
if (!parent || !parent.isValid) {
if (this._traceId128bit) {
let randomId = Utils.getRandom128();
ctx = new SpanContext(randomId, randomId.slice(-8));
} else {
describe('span reference tests', () => {
let tracer = new Tracer('test-service-name', new InMemoryReporter(), new ConstSampler(true));
let parentContext = tracer.startSpan('just-used-for-context').context();
let childOfContext = tracer.startSpan('just-used-for-context').context();
let childOfRef = new opentracing.Reference(opentracing.REFERENCE_CHILD_OF, childOfContext);
let followsFromContext = tracer.startSpan('just-used-for-context').context();
let followsFromRef = new opentracing.Reference(opentracing.REFERENCE_FOLLOWS_FROM, followsFromContext);
let options = [
{ childOf: null, references: [], expectedTraceId: null, expectedParentId: null },
{
childOf: parentContext,
references: [childOfRef, followsFromRef],
expectedTraceId: parentContext.traceId,
expectedParentId: parentContext.parentId,
},
];
_.each(options, o => {
it('should serialize span references', done => {
const span = tracer.startSpan('bender', {
childOf: o.childOf,
references: o.references,
describe('span reference tests', () => {
let tracer = new Tracer('test-service-name', new InMemoryReporter(), new ConstSampler(true));
let parentContext = tracer.startSpan('just-used-for-context').context();
let childOfContext = tracer.startSpan('just-used-for-context').context();
let childOfRef = new opentracing.Reference(opentracing.REFERENCE_CHILD_OF, childOfContext);
let followsFromContext = tracer.startSpan('just-used-for-context').context();
let followsFromRef = new opentracing.Reference(opentracing.REFERENCE_FOLLOWS_FROM, followsFromContext);
let options = [
{ childOf: null, references: [], expectedTraceId: null, expectedParentId: null },
{
childOf: null,
references: [childOfRef, followsFromRef],
expectedTraceId: childOfContext.traceId,
expectedParentId: childOfContext.parentId,
},
];
_.each(options, o => {
it('should serialize span references', done => {
let span = tracer.startSpan('bender', {
childOf: o.childOf,
references: o.references,
it('should start a span that follows from a span', () => {
const parent = {}
fields.references = [
new Reference(opentracing.REFERENCE_FOLLOWS_FROM, parent)
]
tracer = new Tracer({ service: 'service' })
tracer.startSpan('name', fields)
expect(Span).to.have.been.calledWithMatch(tracer, {
operationName: 'name',
parent
})
})
it('should ignore additional follow references', () => {
const parent = {}
fields.references = [
new Reference(opentracing.REFERENCE_FOLLOWS_FROM, parent),
new Reference(opentracing.REFERENCE_FOLLOWS_FROM, {})
]
tracer = new Tracer({ service: 'service' })
tracer.startSpan('name', fields)
expect(Span).to.have.been.calledWithMatch(tracer, {
operationName: 'name',
parent
})
})
describe('start a child span represented as a separate span from parent, using childOf and references', () => {
let nextId = 0;
const getId = () => Utils.encodeInt64(nextId++);
const traceId = getId();
const flags = 1;
const parentContext = SpanContext.withBinaryIds(traceId, getId(), null, flags);
const childOfContext = SpanContext.withBinaryIds(traceId, getId(), null, flags);
const childOfRef = new opentracing.Reference(opentracing.REFERENCE_CHILD_OF, childOfContext);
const followsFromContext = SpanContext.withBinaryIds(traceId, getId(), null, flags);
const followsFromRef = new opentracing.Reference(opentracing.REFERENCE_FOLLOWS_FROM, followsFromContext);
const testCases = [
{
message: 'starts a span based on childOf',
spanOptions: {
childOf: parentContext,
references: [],
},
verify: parentContext,
},
{
message: 'starts a span based on childOf, ignoring FOLLOWS_FROM',
spanOptions: {
childOf: parentContext,
references: [followsFromRef],
},
function getParent (references) {
let parent = null
if (references) {
for (let i = 0; i < references.length; i++) {
const ref = references[i]
if (ref.type() === opentracing.REFERENCE_CHILD_OF) {
parent = ref.referencedContext()
break
} else if (ref.type() === opentracing.REFERENCE_FOLLOWS_FROM) {
if (!parent) {
parent = ref.referencedContext()
}
}
}
}
return parent
}
function Span(tracer, name, fields) {
opentracing.Span.call(this);
this.tracerImpl = tracer;
var parentContext;
if (fields && fields.references) {
for (var i = 0, length = fields.references.length; i < length; i++) {
var reference = fields.references[i];
if (
reference.type() === opentracing.REFERENCE_CHILD_OF ||
reference.type() === opentracing.REFERENCE_FOLLOWS_FROM
) {
parentContext = reference.referencedContext();
}
}
}
var spanId = tracingUtil.generateRandomSpanId();
var traceId = (parentContext ? parentContext.t : null) || spanId;
var parentId = (parentContext ? parentContext.s : null) || undefined;
this._contextImpl = new opentracing.SpanContext();
this._contextImpl.s = spanId;
this._contextImpl.t = traceId;
this._contextImpl.baggage = copyBaggage(parentContext && parentContext.baggage);
this._contextImpl.samplingPriority = parentContext ? parentContext.samplingPriority : 1;
this.span = {
static spanRefsToThriftRefs(refs: Array): Array {
let thriftRefs = [];
for (let i = 0; i < refs.length; i++) {
let refEnum;
let ref = refs[i];
let context = refs[i].referencedContext();
if (ref.type() === opentracing.REFERENCE_CHILD_OF) {
refEnum = ThriftUtils._thrift.SpanRefType.CHILD_OF;
} else if (ref.type() === opentracing.REFERENCE_FOLLOWS_FROM) {
refEnum = ThriftUtils._thrift.SpanRefType.FOLLOWS_FROM;
} else {
continue;
}
thriftRefs.push({
refType: refEnum,
traceIdLow: ThriftUtils.getTraceIdLow(context.traceId),
traceIdHigh: ThriftUtils.getTraceIdHigh(context.traceId),
spanId: context.spanId,
});
}
return thriftRefs;
}