|
| 1 | +/* |
| 2 | + * Copyright 2018 Expedia, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import * as opentracing from 'opentracing'; |
| 17 | + |
| 18 | +import { expect } from 'chai'; |
| 19 | +import Tracer from '../src/tracer'; |
| 20 | +import InMemoryDispatcher from '../src/dispatchers/in_memory'; |
| 21 | +import StartSpanFields from '../src/start_span_fields'; |
| 22 | +import {isUndefined} from 'util'; |
| 23 | +import SpanContext from '../src/span_context'; |
| 24 | + |
| 25 | +const dummyServiceName = 'my-service'; |
| 26 | +const dummyOperation = 'my-service-operation'; |
| 27 | +const downstreamOperation = 'downstream'; |
| 28 | +const commonTags = { |
| 29 | + version: '1.1' |
| 30 | +}; |
| 31 | + |
| 32 | +const expectSpansInStore = (inMemSpanStore: InMemoryDispatcher, expectedCount: number): void => { |
| 33 | + expect(inMemSpanStore.spans().length).eq(expectedCount); |
| 34 | + inMemSpanStore.spans().forEach(receivedSpan => { |
| 35 | + expect(receivedSpan.isFinished()).eq(true); |
| 36 | + const receivedSpanTag = receivedSpan.tags()[0]; |
| 37 | + expect(receivedSpanTag.key).eq('version'); |
| 38 | + expect(receivedSpanTag.value).eq('1.1'); |
| 39 | + expect(receivedSpan.serviceName()).eq(dummyServiceName); |
| 40 | + expect(isUndefined(receivedSpan.context().traceId())).eq(false); |
| 41 | + expect(isUndefined(receivedSpan.context().spanId())).eq(false); |
| 42 | + }); |
| 43 | +}; |
| 44 | + |
| 45 | +describe('Tracer tests', () => { |
| 46 | + |
| 47 | + describe('Tracer#startSpan', () => { |
| 48 | + it('should start a span and dispatch when finished', () => { |
| 49 | + const inMemSpanStore = new InMemoryDispatcher(); |
| 50 | + const tracer = new Tracer(dummyServiceName, inMemSpanStore, commonTags); |
| 51 | + expect(tracer.serviceName()).equal(dummyServiceName); |
| 52 | + const span = tracer.startSpan(dummyOperation); |
| 53 | + expect(span.isFinished()).eq(false); |
| 54 | + expect(inMemSpanStore.spans().length).equal(0); |
| 55 | + span.finish(); |
| 56 | + expectSpansInStore(inMemSpanStore, 1); |
| 57 | + const receivedSpan = inMemSpanStore.spans()[0]; |
| 58 | + expect(receivedSpan.operationName()).eq(dummyOperation); |
| 59 | + expect(isUndefined(receivedSpan.context().parentSpanId())).eq(true); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should start and dispatch server and client spans', () => { |
| 63 | + const inMemSpanStore = new InMemoryDispatcher(); |
| 64 | + const tracer = new Tracer(dummyServiceName, inMemSpanStore, commonTags); |
| 65 | + |
| 66 | + const startServerSpanFields = new StartSpanFields(); |
| 67 | + startServerSpanFields.tags = { 'span.kind': 'server'}; |
| 68 | + const serverSpan = tracer.startSpan(dummyOperation, startServerSpanFields); |
| 69 | + |
| 70 | + const startClientSpanFields = new StartSpanFields(); |
| 71 | + startClientSpanFields.childOf = serverSpan.context(); |
| 72 | + startClientSpanFields.tags = { 'span.kind': 'client' }; |
| 73 | + const clientSpan = tracer.startSpan(downstreamOperation, startClientSpanFields); |
| 74 | + |
| 75 | + expect(serverSpan.isFinished()).eq(false); |
| 76 | + expect(inMemSpanStore.spans().length).equal(0); |
| 77 | + serverSpan.finish(); |
| 78 | + clientSpan.finish(); |
| 79 | + |
| 80 | + expectSpansInStore(inMemSpanStore, 2); |
| 81 | + |
| 82 | + expect(inMemSpanStore.spans().map(span => span.operationName())).includes(downstreamOperation); |
| 83 | + expect(inMemSpanStore.spans().map(span => span.operationName())).includes(dummyOperation); |
| 84 | + |
| 85 | + const receivedClientSpan = inMemSpanStore.spans().filter(span => { |
| 86 | + return span.tags().filter(tag => tag.key === 'span.kind' && tag.value === 'client').length > 0 |
| 87 | + })[0]; |
| 88 | + const receivedServerSpan = inMemSpanStore.spans().filter(span => { |
| 89 | + return span.tags().filter(tag => tag.key === 'span.kind' && tag.value === 'server').length > 0 |
| 90 | + })[0]; |
| 91 | + |
| 92 | + expect(receivedClientSpan.context().parentSpanId()).eq(receivedServerSpan.context().spanId()); |
| 93 | + expect(isUndefined(receivedServerSpan.context().parentSpanId())).eq(true); |
| 94 | + expect(receivedServerSpan.context().traceId()).eq(receivedClientSpan.context().traceId()); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should inject the span in the carrier', () => { |
| 98 | + const inMemSpanStore = new InMemoryDispatcher(); |
| 99 | + const tracer = new Tracer(dummyServiceName, inMemSpanStore, commonTags); |
| 100 | + const spanContext = new SpanContext('a', 'b', 'c'); |
| 101 | + const carrier = {}; |
| 102 | + tracer.inject(spanContext, opentracing.FORMAT_TEXT_MAP, carrier); |
| 103 | + expect(JSON.stringify(carrier)).eq('{"Trace-ID":"a","Span-ID":"b","Parent-ID":"c"}'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should extract the span from the carrier', () => { |
| 107 | + const inMemSpanStore = new InMemoryDispatcher(); |
| 108 | + const tracer = new Tracer(dummyServiceName, inMemSpanStore, commonTags); |
| 109 | + const carrier = {'Trace-ID': 'a', 'Span-ID': 'b', 'Parent-ID': 'c'}; |
| 110 | + const spanContext = tracer.extract(opentracing.FORMAT_TEXT_MAP, carrier); |
| 111 | + expect(JSON.stringify(spanContext)).eq('{"_traceId":"a","_spanId":"b","_parentSpanId":"c","_baggage":{}}'); |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
0 commit comments