Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend JSON file reader to handle array of objects with resourceSpans #2254

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/jaeger-ui/src/utils/readJsonFile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,20 @@ describe('fileReader.readJsonFile', () => {
const p = readJsonFile({ file });
return expect(p).rejects.toMatchObject(expect.any(Error));
});

it('loads JSON data with single resourceSpans array successfully', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this test is needed, if you can process array of size>1 you can certainly process size=1

const obj = { resourceSpans: [{ id: 1 }, { id: 2 }, { id: 3 }] };
const file = new File([JSON.stringify(obj)], 'singleResourceSpans.json');
const result = await readJsonFile({ file });
expect(result).toEqual(JSON.stringify(obj));
});

it('loads JSON data with multiple resourceSpans arrays successfully', async () => {
const obj1 = { resourceSpans: [{ id: 1 }, { id: 2 }] };
const obj2 = { resourceSpans: [{ id: 3 }, { id: 4 }] };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a valid OTEL structure, it should be something like resourceSpans > scopeSpans > spans

const file = new File([JSON.stringify([obj1, obj2])], 'multipleResourceSpans.json');
const expectedResult = { resourceSpans: [...obj1.resourceSpans, ...obj2.resourceSpans] };
const result = await readJsonFile({ file });
expect(result).toEqual(JSON.stringify(expectedResult));
});
});
12 changes: 10 additions & 2 deletions packages/jaeger-ui/src/utils/readJsonFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ export default function readJsonFile(fileList: { file: File }): Promise<string>
return;
}
try {
const traceObj = JSON.parse(reader.result);
let traceObj = JSON.parse(reader.result);
if (Array.isArray(traceObj) && traceObj.every(obj => 'resourceSpans' in obj)) {
const mergedResourceSpans = traceObj.reduce((acc, obj) => {
acc.push(...obj.resourceSpans);
return acc;
}, []);

traceObj = { resourceSpans: mergedResourceSpans };
}
if ('resourceSpans' in traceObj) {
JaegerAPI.transformOTLP(traceObj)
.then((result: string) => {
Expand All @@ -32,7 +40,7 @@ export default function readJsonFile(fileList: { file: File }): Promise<string>
.catch(() => {
reject(new Error(`Error converting traces to OTLP`));
});
} else {
} else{
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
resolve(traceObj);
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (error: unknown) {
Expand Down
Loading