Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
startSpan(operationName: string, options: ?startSpanOptions): Span {
options = options || {};
let references = options.references || [];
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);
it('should start a span that is the child of a span', () => {
const parent = {}
fields.references = [
new Reference(opentracing.REFERENCE_CHILD_OF, parent)
]
tracer = new Tracer({ service: 'service' })
tracer.startSpan('name', fields)
expect(Span).to.have.been.calledWithMatch(tracer, {
operationName: 'name',
parent
})
})
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', {
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;