|
| 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 | + |
| 17 | +import { expect } from 'chai'; |
| 18 | +import Tracer from '../tracer'; |
| 19 | +import InMemoryDispatcher from '../dispatchers/in_memory'; |
| 20 | +import StartSpanFields from '../start_span_fields'; |
| 21 | +import {isUndefined} from 'util'; |
| 22 | + |
| 23 | +describe('Tracer tests', () => { |
| 24 | + const dummyServiceName = 'my-service'; |
| 25 | + const dummyOperation = 'my-service-operation'; |
| 26 | + const downstreamOperation = 'downstream'; |
| 27 | + const commonTags = { |
| 28 | + version: '1.1' |
| 29 | + }; |
| 30 | + |
| 31 | + describe('Tracer#startSpan', () => { |
| 32 | + it('should start a span and dispatch when finished', () => { |
| 33 | + const inMemSpanStore = new InMemoryDispatcher(); |
| 34 | + const tracer = new Tracer(dummyServiceName, inMemSpanStore, commonTags); |
| 35 | + expect(tracer.serviceName()).equal(dummyServiceName); |
| 36 | + const span = tracer.startSpan(dummyOperation); |
| 37 | + expect(span.isFinished()).eq(false); |
| 38 | + expect(inMemSpanStore.spans().length).equal(0); |
| 39 | + span.finish(); |
| 40 | + expect(inMemSpanStore.spans().length).equal(1); |
| 41 | + const receivedSpan = inMemSpanStore.spans()[0]; |
| 42 | + expect(receivedSpan.isFinished()).eq(true); |
| 43 | + expect(receivedSpan.serviceName()).eq(dummyServiceName); |
| 44 | + expect(receivedSpan.operationName()).eq(dummyOperation); |
| 45 | + expect(isUndefined(receivedSpan.context().traceId())).eq(false); |
| 46 | + expect(isUndefined(receivedSpan.context().spanId())).eq(false); |
| 47 | + expect(isUndefined(receivedSpan.context().parentSpanId())).eq(true); |
| 48 | + const receivedSpanTag = receivedSpan.tags()[0]; |
| 49 | + expect(receivedSpanTag.key).eq('version'); |
| 50 | + expect(receivedSpanTag.value).eq('1.1'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should start and dispatch server and client spans', () => { |
| 54 | + const inMemSpanStore = new InMemoryDispatcher(); |
| 55 | + const tracer = new Tracer(dummyServiceName, inMemSpanStore, commonTags); |
| 56 | + expect(tracer.serviceName()).equal(dummyServiceName); |
| 57 | + const serverSpan = tracer.startSpan(dummyOperation); |
| 58 | + const startSpanFields = new StartSpanFields(); |
| 59 | + startSpanFields.childOf = serverSpan.context(); |
| 60 | + startSpanFields.tags = { 'span.kind': 'client' }; |
| 61 | + const clientSpan = tracer.startSpan(downstreamOperation, startSpanFields); |
| 62 | + |
| 63 | + expect(serverSpan.isFinished()).eq(false); |
| 64 | + expect(inMemSpanStore.spans().length).equal(0); |
| 65 | + serverSpan.finish(); |
| 66 | + clientSpan.finish(); |
| 67 | + |
| 68 | + expect(inMemSpanStore.spans().length).equal(2); |
| 69 | + inMemSpanStore.spans().forEach(receivedSpan => { |
| 70 | + expect(receivedSpan.isFinished()).eq(true); |
| 71 | + const receivedSpanTag = receivedSpan.tags()[0]; |
| 72 | + expect(receivedSpanTag.key).eq('version'); |
| 73 | + expect(receivedSpanTag.value).eq('1.1'); |
| 74 | + expect(receivedSpan.serviceName()).eq(dummyServiceName); |
| 75 | + }); |
| 76 | + expect(inMemSpanStore.spans().map(span => span.operationName())).includes(downstreamOperation); |
| 77 | + expect(inMemSpanStore.spans().map(span => span.operationName())).includes(dummyOperation); |
| 78 | + const receivedClientSpan = inMemSpanStore.spans().filter(span => span.tags().filter(tag => tag.key === 'span.kind' && tag.value === 'client').length > 0)[0]; |
| 79 | + const receivedServerSpan = inMemSpanStore.spans().filter(span => span.tags().filter(tag => tag.key === 'span.kind').length <= 0)[0]; |
| 80 | + expect(receivedClientSpan.context().parentSpanId()).eq(receivedServerSpan.context().spanId()); |
| 81 | + expect(isUndefined(receivedServerSpan.context().parentSpanId())).eq(true); |
| 82 | + expect(receivedServerSpan.context().traceId()).eq(receivedClientSpan.context().traceId()); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments