Skip to content

Commit 8b8795d

Browse files
committed
refactor: improve collector exporter tests
1 parent 3c35345 commit 8b8795d

2 files changed

Lines changed: 128 additions & 148 deletions

File tree

packages/opentelemetry-exporter-collector/test/common/transform.test.ts

Lines changed: 18 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717
import { Attributes, TimedEvent, TraceFlags } from '@opentelemetry/api';
1818
import * as assert from 'assert';
1919
import * as transform from '../../src/transform';
20-
import { ensureSpanIsCorrect, mockedReadableSpan } from '../helper';
20+
import {
21+
ensureSpanIsCorrect,
22+
mockedReadableSpan,
23+
mockedResources,
24+
mockedInstrumentationLibraries,
25+
multiResourceTrace,
26+
multiInstrumentationLibraryTrace,
27+
} from '../helper';
2128
import { Resource } from '@opentelemetry/resources';
22-
import { InstrumentationLibrary } from '@opentelemetry/core';
23-
import { ReadableSpan } from '@opentelemetry/tracing';
2429

2530
describe('transform', () => {
2631
describe('toCollectorAttributes', () => {
@@ -124,171 +129,36 @@ describe('transform', () => {
124129

125130
describe('groupSpans', () => {
126131
it('should group by resource', () => {
127-
const resource1: Resource = new Resource({ name: 'resource 1' });
128-
const resource2: Resource = new Resource({ name: 'resource 2' });
129-
const instrumentationLibrary: InstrumentationLibrary = {
130-
name: 'lib1',
131-
version: '0.0.1',
132-
};
133-
134-
const span1: ReadableSpan = {
135-
name: 'span1',
136-
kind: 0,
137-
spanContext: {
138-
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
139-
spanId: '5e107261f64fa53e',
140-
traceFlags: TraceFlags.SAMPLED,
141-
},
142-
parentSpanId: '78a8915098864388',
143-
startTime: [1574120165, 429803070],
144-
endTime: [1574120165, 438688070],
145-
ended: true,
146-
status: { code: 0 },
147-
attributes: {},
148-
links: [],
149-
events: [],
150-
duration: [0, 8885000],
151-
resource: resource1,
152-
instrumentationLibrary,
153-
};
154-
155-
const span2: ReadableSpan = {
156-
name: 'span2',
157-
kind: 0,
158-
spanContext: {
159-
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
160-
spanId: 'f64fa53e5e107261',
161-
traceFlags: TraceFlags.SAMPLED,
162-
},
163-
parentSpanId: '86438878a8915098',
164-
startTime: [1575120165, 439803070],
165-
endTime: [1575120165, 448688070],
166-
ended: true,
167-
status: { code: 0 },
168-
attributes: {},
169-
links: [],
170-
events: [],
171-
duration: [0, 8775000],
172-
resource: resource2,
173-
instrumentationLibrary,
174-
};
175-
176-
const span3: ReadableSpan = {
177-
name: 'span3',
178-
kind: 0,
179-
spanContext: {
180-
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
181-
spanId: '07261f64fa53e5e1',
182-
traceFlags: TraceFlags.SAMPLED,
183-
},
184-
parentSpanId: 'a891578098864388',
185-
startTime: [1575120165, 439803070],
186-
endTime: [1575120165, 448688070],
187-
ended: true,
188-
status: { code: 0 },
189-
attributes: {},
190-
links: [],
191-
events: [],
192-
duration: [0, 8775000],
193-
resource: resource2,
194-
instrumentationLibrary,
195-
};
132+
const [resource1, resource2] = mockedResources;
133+
const [instrumentationLibrary] = mockedInstrumentationLibraries;
134+
const [span1, span2, span3] = multiResourceTrace;
196135

197136
const expected = new Map([
198137
[resource1, new Map([[instrumentationLibrary, [span1]]])],
199138
[resource2, new Map([[instrumentationLibrary, [span2, span3]]])],
200139
]);
201140

202-
const result = transform.groupSpans([span1, span2, span3]);
141+
const result = transform.groupSpans(multiResourceTrace);
203142

204143
assert.deepStrictEqual(result, expected);
205144
});
206145

207146
it('should group by instrumentation library', () => {
208-
const resource: Resource = new Resource({ name: 'resource 1' });
209-
const instrumentationLibrary1: InstrumentationLibrary = {
210-
name: 'lib1',
211-
version: '0.0.1',
212-
};
213-
const instrumentationLibrary2: InstrumentationLibrary = {
214-
name: 'lib2',
215-
version: '0.0.2',
216-
};
217-
218-
const span1: ReadableSpan = {
219-
name: 'span1',
220-
kind: 0,
221-
spanContext: {
222-
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
223-
spanId: '5e107261f64fa53e',
224-
traceFlags: TraceFlags.SAMPLED,
225-
},
226-
parentSpanId: '78a8915098864388',
227-
startTime: [1574120165, 429803070],
228-
endTime: [1574120165, 438688070],
229-
ended: true,
230-
status: { code: 0 },
231-
attributes: {},
232-
links: [],
233-
events: [],
234-
duration: [0, 8885000],
235-
resource,
236-
instrumentationLibrary: instrumentationLibrary1,
237-
};
238-
239-
const span2: ReadableSpan = {
240-
name: 'span2',
241-
kind: 0,
242-
spanContext: {
243-
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
244-
spanId: 'f64fa53e5e107261',
245-
traceFlags: TraceFlags.SAMPLED,
246-
},
247-
parentSpanId: '78a8915098864388',
248-
startTime: [1575120165, 439803070],
249-
endTime: [1575120165, 448688070],
250-
ended: true,
251-
status: { code: 0 },
252-
attributes: {},
253-
links: [],
254-
events: [],
255-
duration: [0, 8775000],
256-
resource,
257-
instrumentationLibrary: instrumentationLibrary1,
258-
};
259-
260-
const span3: ReadableSpan = {
261-
name: 'span3',
262-
kind: 0,
263-
spanContext: {
264-
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
265-
spanId: '07261f64fa53e5e1',
266-
traceFlags: TraceFlags.SAMPLED,
267-
},
268-
parentSpanId: 'a891578098864388',
269-
startTime: [1575120165, 439803070],
270-
endTime: [1575120165, 448688070],
271-
ended: true,
272-
status: { code: 0 },
273-
attributes: {},
274-
links: [],
275-
events: [],
276-
duration: [0, 8775000],
277-
resource: resource,
278-
instrumentationLibrary: instrumentationLibrary2,
279-
};
147+
const [resource] = mockedResources;
148+
const [lib1, lib2] = mockedInstrumentationLibraries;
149+
const [span1, span2, span3] = multiInstrumentationLibraryTrace;
280150

281151
const expected = new Map([
282152
[
283153
resource,
284154
new Map([
285-
[instrumentationLibrary1, [span1, span2]],
286-
[instrumentationLibrary2, [span3]],
155+
[lib1, [span1, span2]],
156+
[lib2, [span3]],
287157
]),
288158
],
289159
]);
290160

291-
const result = transform.groupSpans([span1, span2, span3]);
161+
const result = transform.groupSpans(multiInstrumentationLibraryTrace);
292162

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

packages/opentelemetry-exporter-collector/test/helper.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { Resource } from '@opentelemetry/resources';
2020
import * as assert from 'assert';
2121
import { opentelemetryProto } from '../src/types';
2222
import * as collectorTypes from '../src/types';
23+
import { InstrumentationLibrary } from '@opentelemetry/core';
2324

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

112+
export const mockedResources: Resource[] = [
113+
new Resource({ name: 'resource 1' }),
114+
new Resource({ name: 'resource 2' }),
115+
];
116+
117+
export const mockedInstrumentationLibraries: InstrumentationLibrary[] = [
118+
{
119+
name: 'lib1',
120+
version: '0.0.1',
121+
},
122+
{
123+
name: 'lib2',
124+
version: '0.0.2',
125+
},
126+
];
127+
128+
export const basicTrace: ReadableSpan[] = [
129+
{
130+
name: 'span1',
131+
kind: 0,
132+
spanContext: {
133+
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
134+
spanId: '5e107261f64fa53e',
135+
traceFlags: TraceFlags.SAMPLED,
136+
},
137+
parentSpanId: '78a8915098864388',
138+
startTime: [1574120165, 429803070],
139+
endTime: [1574120165, 438688070],
140+
ended: true,
141+
status: { code: 0 },
142+
attributes: {},
143+
links: [],
144+
events: [],
145+
duration: [0, 8885000],
146+
resource: mockedResources[0],
147+
instrumentationLibrary: mockedInstrumentationLibraries[0],
148+
},
149+
{
150+
name: 'span2',
151+
kind: 0,
152+
spanContext: {
153+
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
154+
spanId: 'f64fa53e5e107261',
155+
traceFlags: TraceFlags.SAMPLED,
156+
},
157+
parentSpanId: '78a8915098864388',
158+
startTime: [1575120165, 439803070],
159+
endTime: [1575120165, 448688070],
160+
ended: true,
161+
status: { code: 0 },
162+
attributes: {},
163+
links: [],
164+
events: [],
165+
duration: [0, 8775000],
166+
resource: mockedResources[0],
167+
instrumentationLibrary: mockedInstrumentationLibraries[0],
168+
},
169+
{
170+
name: 'span3',
171+
kind: 0,
172+
spanContext: {
173+
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
174+
spanId: '07261f64fa53e5e1',
175+
traceFlags: TraceFlags.SAMPLED,
176+
},
177+
parentSpanId: 'a891578098864388',
178+
startTime: [1575120165, 439803070],
179+
endTime: [1575120165, 448688070],
180+
ended: true,
181+
status: { code: 0 },
182+
attributes: {},
183+
links: [],
184+
events: [],
185+
duration: [0, 8775000],
186+
resource: mockedResources[0],
187+
instrumentationLibrary: mockedInstrumentationLibraries[0],
188+
},
189+
];
190+
191+
export const multiResourceTrace: ReadableSpan[] = [
192+
{
193+
...basicTrace[0],
194+
resource: mockedResources[0],
195+
},
196+
{
197+
...basicTrace[1],
198+
resource: mockedResources[1],
199+
},
200+
{
201+
...basicTrace[2],
202+
resource: mockedResources[1],
203+
},
204+
];
205+
206+
export const multiInstrumentationLibraryTrace: ReadableSpan[] = [
207+
{
208+
...basicTrace[0],
209+
instrumentationLibrary: mockedInstrumentationLibraries[0],
210+
},
211+
{
212+
...basicTrace[1],
213+
instrumentationLibrary: mockedInstrumentationLibraries[0],
214+
},
215+
{
216+
...basicTrace[2],
217+
instrumentationLibrary: mockedInstrumentationLibraries[1],
218+
},
219+
];
220+
111221
export function ensureExportedEventsAreCorrect(
112222
events: opentelemetryProto.trace.v1.Span.Event[]
113223
) {

0 commit comments

Comments
 (0)