Skip to content

Commit

Permalink
refactor: improve collector exporter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mwear committed Jun 10, 2020
1 parent 3c35345 commit 8b8795d
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 148 deletions.
166 changes: 18 additions & 148 deletions packages/opentelemetry-exporter-collector/test/common/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
import { Attributes, TimedEvent, TraceFlags } from '@opentelemetry/api';
import * as assert from 'assert';
import * as transform from '../../src/transform';
import { ensureSpanIsCorrect, mockedReadableSpan } from '../helper';
import {
ensureSpanIsCorrect,
mockedReadableSpan,
mockedResources,
mockedInstrumentationLibraries,
multiResourceTrace,
multiInstrumentationLibraryTrace,
} from '../helper';
import { Resource } from '@opentelemetry/resources';
import { InstrumentationLibrary } from '@opentelemetry/core';
import { ReadableSpan } from '@opentelemetry/tracing';

describe('transform', () => {
describe('toCollectorAttributes', () => {
Expand Down Expand Up @@ -124,171 +129,36 @@ describe('transform', () => {

describe('groupSpans', () => {
it('should group by resource', () => {
const resource1: Resource = new Resource({ name: 'resource 1' });
const resource2: Resource = new Resource({ name: 'resource 2' });
const instrumentationLibrary: InstrumentationLibrary = {
name: 'lib1',
version: '0.0.1',
};

const span1: ReadableSpan = {
name: 'span1',
kind: 0,
spanContext: {
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
spanId: '5e107261f64fa53e',
traceFlags: TraceFlags.SAMPLED,
},
parentSpanId: '78a8915098864388',
startTime: [1574120165, 429803070],
endTime: [1574120165, 438688070],
ended: true,
status: { code: 0 },
attributes: {},
links: [],
events: [],
duration: [0, 8885000],
resource: resource1,
instrumentationLibrary,
};

const span2: ReadableSpan = {
name: 'span2',
kind: 0,
spanContext: {
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
spanId: 'f64fa53e5e107261',
traceFlags: TraceFlags.SAMPLED,
},
parentSpanId: '86438878a8915098',
startTime: [1575120165, 439803070],
endTime: [1575120165, 448688070],
ended: true,
status: { code: 0 },
attributes: {},
links: [],
events: [],
duration: [0, 8775000],
resource: resource2,
instrumentationLibrary,
};

const span3: ReadableSpan = {
name: 'span3',
kind: 0,
spanContext: {
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
spanId: '07261f64fa53e5e1',
traceFlags: TraceFlags.SAMPLED,
},
parentSpanId: 'a891578098864388',
startTime: [1575120165, 439803070],
endTime: [1575120165, 448688070],
ended: true,
status: { code: 0 },
attributes: {},
links: [],
events: [],
duration: [0, 8775000],
resource: resource2,
instrumentationLibrary,
};
const [resource1, resource2] = mockedResources;
const [instrumentationLibrary] = mockedInstrumentationLibraries;
const [span1, span2, span3] = multiResourceTrace;

const expected = new Map([
[resource1, new Map([[instrumentationLibrary, [span1]]])],
[resource2, new Map([[instrumentationLibrary, [span2, span3]]])],
]);

const result = transform.groupSpans([span1, span2, span3]);
const result = transform.groupSpans(multiResourceTrace);

assert.deepStrictEqual(result, expected);
});

it('should group by instrumentation library', () => {
const resource: Resource = new Resource({ name: 'resource 1' });
const instrumentationLibrary1: InstrumentationLibrary = {
name: 'lib1',
version: '0.0.1',
};
const instrumentationLibrary2: InstrumentationLibrary = {
name: 'lib2',
version: '0.0.2',
};

const span1: ReadableSpan = {
name: 'span1',
kind: 0,
spanContext: {
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
spanId: '5e107261f64fa53e',
traceFlags: TraceFlags.SAMPLED,
},
parentSpanId: '78a8915098864388',
startTime: [1574120165, 429803070],
endTime: [1574120165, 438688070],
ended: true,
status: { code: 0 },
attributes: {},
links: [],
events: [],
duration: [0, 8885000],
resource,
instrumentationLibrary: instrumentationLibrary1,
};

const span2: ReadableSpan = {
name: 'span2',
kind: 0,
spanContext: {
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
spanId: 'f64fa53e5e107261',
traceFlags: TraceFlags.SAMPLED,
},
parentSpanId: '78a8915098864388',
startTime: [1575120165, 439803070],
endTime: [1575120165, 448688070],
ended: true,
status: { code: 0 },
attributes: {},
links: [],
events: [],
duration: [0, 8775000],
resource,
instrumentationLibrary: instrumentationLibrary1,
};

const span3: ReadableSpan = {
name: 'span3',
kind: 0,
spanContext: {
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
spanId: '07261f64fa53e5e1',
traceFlags: TraceFlags.SAMPLED,
},
parentSpanId: 'a891578098864388',
startTime: [1575120165, 439803070],
endTime: [1575120165, 448688070],
ended: true,
status: { code: 0 },
attributes: {},
links: [],
events: [],
duration: [0, 8775000],
resource: resource,
instrumentationLibrary: instrumentationLibrary2,
};
const [resource] = mockedResources;
const [lib1, lib2] = mockedInstrumentationLibraries;
const [span1, span2, span3] = multiInstrumentationLibraryTrace;

const expected = new Map([
[
resource,
new Map([
[instrumentationLibrary1, [span1, span2]],
[instrumentationLibrary2, [span3]],
[lib1, [span1, span2]],
[lib2, [span3]],
]),
],
]);

const result = transform.groupSpans([span1, span2, span3]);
const result = transform.groupSpans(multiInstrumentationLibraryTrace);

assert.deepStrictEqual(result, expected);
});
Expand Down
110 changes: 110 additions & 0 deletions packages/opentelemetry-exporter-collector/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Resource } from '@opentelemetry/resources';
import * as assert from 'assert';
import { opentelemetryProto } from '../src/types';
import * as collectorTypes from '../src/types';
import { InstrumentationLibrary } from '@opentelemetry/core';

if (typeof Buffer === 'undefined') {
(window as any).Buffer = {
Expand Down Expand Up @@ -108,6 +109,115 @@ export const mockedReadableSpan: ReadableSpan = {
instrumentationLibrary: { name: 'default', version: '0.0.1' },
};

export const mockedResources: Resource[] = [
new Resource({ name: 'resource 1' }),
new Resource({ name: 'resource 2' }),
];

export const mockedInstrumentationLibraries: InstrumentationLibrary[] = [
{
name: 'lib1',
version: '0.0.1',
},
{
name: 'lib2',
version: '0.0.2',
},
];

export const basicTrace: ReadableSpan[] = [
{
name: 'span1',
kind: 0,
spanContext: {
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
spanId: '5e107261f64fa53e',
traceFlags: TraceFlags.SAMPLED,
},
parentSpanId: '78a8915098864388',
startTime: [1574120165, 429803070],
endTime: [1574120165, 438688070],
ended: true,
status: { code: 0 },
attributes: {},
links: [],
events: [],
duration: [0, 8885000],
resource: mockedResources[0],
instrumentationLibrary: mockedInstrumentationLibraries[0],
},
{
name: 'span2',
kind: 0,
spanContext: {
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
spanId: 'f64fa53e5e107261',
traceFlags: TraceFlags.SAMPLED,
},
parentSpanId: '78a8915098864388',
startTime: [1575120165, 439803070],
endTime: [1575120165, 448688070],
ended: true,
status: { code: 0 },
attributes: {},
links: [],
events: [],
duration: [0, 8775000],
resource: mockedResources[0],
instrumentationLibrary: mockedInstrumentationLibraries[0],
},
{
name: 'span3',
kind: 0,
spanContext: {
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
spanId: '07261f64fa53e5e1',
traceFlags: TraceFlags.SAMPLED,
},
parentSpanId: 'a891578098864388',
startTime: [1575120165, 439803070],
endTime: [1575120165, 448688070],
ended: true,
status: { code: 0 },
attributes: {},
links: [],
events: [],
duration: [0, 8775000],
resource: mockedResources[0],
instrumentationLibrary: mockedInstrumentationLibraries[0],
},
];

export const multiResourceTrace: ReadableSpan[] = [
{
...basicTrace[0],
resource: mockedResources[0],
},
{
...basicTrace[1],
resource: mockedResources[1],
},
{
...basicTrace[2],
resource: mockedResources[1],
},
];

export const multiInstrumentationLibraryTrace: ReadableSpan[] = [
{
...basicTrace[0],
instrumentationLibrary: mockedInstrumentationLibraries[0],
},
{
...basicTrace[1],
instrumentationLibrary: mockedInstrumentationLibraries[0],
},
{
...basicTrace[2],
instrumentationLibrary: mockedInstrumentationLibraries[1],
},
];

export function ensureExportedEventsAreCorrect(
events: opentelemetryProto.trace.v1.Span.Event[]
) {
Expand Down

0 comments on commit 8b8795d

Please sign in to comment.