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

Allow uploading JSON-per-line OTLP data #2380

Merged
merged 8 commits into from
Jul 17, 2024

Conversation

BenzeneAlcohol
Copy link
Contributor

OpenTelemetry specifies that JSON line files can contain valid JSON objects which are seperated with a newline character. The existing readJsonFile function couldn't handle those instances, this PR fixes that, along with test cases to verify.

Which problem is this PR solving?

Resolves #2225

Description of the changes

A new try catch block was inserted, for files that weren't parsed by the usual JSON.parse, would be parsed by the catch block assuming the objects are seperated by newline (which is mentioned in opentelemetry)

How was this change tested?

Test cases, and manual testing

Checklist

OpenTelemetry specifies that JSON line files can contain valid
JSON objects which are seperated with a newline character.
The existing readJsonFile function couldn't handle those
instances, this PR fixes that, along with test cases to
verify.

Signed-off-by: Muthukumar <muthuku37@gmail.com>
@BenzeneAlcohol BenzeneAlcohol requested a review from a team as a code owner July 13, 2024 07:36
@BenzeneAlcohol BenzeneAlcohol requested review from albertteoh and removed request for a team July 13, 2024 07:36
@BenzeneAlcohol BenzeneAlcohol changed the title Resolves #2225 Resolves #2225 - Importing OTLP "File Exporter" JSON fails Jul 13, 2024
Copy link

codecov bot commented Jul 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.61%. Comparing base (1704a9a) to head (9139065).
Report is 8 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2380   +/-   ##
=======================================
  Coverage   96.60%   96.61%           
=======================================
  Files         254      254           
  Lines        7662     7679   +17     
  Branches     1994     1997    +3     
=======================================
+ Hits         7402     7419   +17     
  Misses        260      260           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

OpenTelemetry specifies that JSON line files can contain valid
JSON objects which are seperated with a newline character.
The existing readJsonFile function couldn't handle those
instances, this PR fixes that, along with test cases to
verify.

Signed-off-by: Muthukumar <muthuku37@gmail.com>
Signed-off-by: Muthukumar <muthuku37@gmail.com>
@BenzeneAlcohol BenzeneAlcohol changed the title Resolves #2225 - Importing OTLP "File Exporter" JSON fails changelog: update readJsonFile.tsx Jul 14, 2024
Copy link
Member

@yurishkuro yurishkuro left a comment

Choose a reason for hiding this comment

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

lgtm, minor nits

packages/jaeger-ui/src/utils/readJsonFile.test.js Outdated Show resolved Hide resolved
packages/jaeger-ui/src/utils/readJsonFile.tsx Outdated Show resolved Hide resolved
Signed-off-by: Muthukumar <muthuku37@gmail.com>
yurishkuro
yurishkuro previously approved these changes Jul 15, 2024
@yurishkuro yurishkuro changed the title changelog: update readJsonFile.tsx Allow uploading JSON-per-line OTLP data Jul 15, 2024
@yurishkuro yurishkuro added the changelog:bugfix-or-minor-feature 🐞 Bug fixes, Minor Improvements label Jul 15, 2024
Signed-off-by: Yuri Shkuro <github@ysh.us>
@BenzeneAlcohol
Copy link
Contributor Author

Woops, I didn't lint the file after my last commit. I'll do it.

Signed-off-by: Yuri Shkuro <github@ysh.us>
@yurishkuro
Copy link
Member

I just pushed some clean-up

@BenzeneAlcohol
Copy link
Contributor Author

cool

@yurishkuro
Copy link
Member

when I run the test manually I am getting very odd result:

 11:31:23  ✘ 1  ~/dev/jaegertracing/jaeger-ui/packages/jaeger-ui   fix/readjson ✘ ✹ 
$ yarn test src/utils/readJsonFile.test.js
yarn run v1.22.22
$ jest --maxWorkers=50% src/utils/readJsonFile.test.js
 PASS  src/utils/readJsonFile.test.js
  fileReader.readJsonFile
    ✓ rejects when given an invalid file (1 ms)
    ✓ does not throw when given an invalid file
    ✓ loads JSON data, successfully (10 ms)
    ✓ loads JSON data (OTLP), successfully (3 ms)
    ✓ rejects an OTLP trace (1 ms)
    ✓ rejects malformed JSON
    ✓ loads JSON-per-line data (1 ms)

Test Suites: 1 passed, 1 total
Tests:       7 passed, 7 total
Snapshots:   0 total
Time:        1.497 s, estimated 6 s
Ran all test suites matching /src\/utils\/readJsonFile.test.js/i.
/Users/ysh/dev/jaegertracing/jaeger-ui/node_modules/expect/build/index.js:113
  const err = new JestAssertionError();
              ^

JestAssertionError: expect(received).resolves.toBeDefined()

Received promise rejected instead of resolved
Rejected to value: [Error: Error converting traces to OTLP]
    at expect (/Users/ysh/dev/jaegertracing/jaeger-ui/node_modules/expect/build/index.js:113:15)
    at Object.<anonymous> (/Users/ysh/dev/jaegertracing/jaeger-ui/packages/jaeger-ui/src/utils/readJsonFile.test.js:93:5)
    at Promise.then.completed (/Users/ysh/dev/jaegertracing/jaeger-ui/node_modules/jest-circus/build/utils.js:298:28)
    at new Promise (<anonymous>)
    at callAsyncCircusFn (/Users/ysh/dev/jaegertracing/jaeger-ui/node_modules/jest-circus/build/utils.js:231:10)
    at _callCircusTest (/Users/ysh/dev/jaegertracing/jaeger-ui/node_modules/jest-circus/build/run.js:316:40)
    at _runTest (/Users/ysh/dev/jaegertracing/jaeger-ui/node_modules/jest-circus/build/run.js:252:3)
    at _runTestsForDescribeBlock (/Users/ysh/dev/jaegertracing/jaeger-ui/node_modules/jest-circus/build/run.js:126:9)
    at _runTestsForDescribeBlock (/Users/ysh/dev/jaegertracing/jaeger-ui/node_modules/jest-circus/build/run.js:121:9)
    at run (/Users/ysh/dev/jaegertracing/jaeger-ui/node_modules/jest-circus/build/run.js:71:3)
    at runAndTransformResultsToJestFormat (/Users/ysh/dev/jaegertracing/jaeger-ui/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)
    at jestAdapter (/Users/ysh/dev/jaegertracing/jaeger-ui/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:74:19)
    at runTestInternal (/Users/ysh/dev/jaegertracing/jaeger-ui/node_modules/jest-runner/build/runTest.js:281:16)
    at runTest (/Users/ysh/dev/jaegertracing/jaeger-ui/node_modules/jest-runner/build/runTest.js:341:7) {
  matcherResult: undefined
}

Node.js v20.12.2
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Here the test is marked as succeeded, yet an exception is logged indicating that the match condition was not met.

I am also wondering how the jest.spyOn at the top of the file affect the execution.

@BenzeneAlcohol
Copy link
Contributor Author

BenzeneAlcohol commented Jul 15, 2024

const fs = require('fs');

function tryParseMultiLineInput(input) {
    const jsonStrings = input.split('\n').filter(line => line.trim() !== '');
    const parsedObjects = [];

    jsonStrings.forEach((jsonString, index) => {
        try {
            const traceObj = JSON.parse(jsonString.trim());
            console.log("l10")
            parsedObjects.push(traceObj);
        } catch (error) {
            throw new Error(`Error parsing JSON at line ${index + 1}: ${error.message}`);
        }
    });
    console.log("L16")
    return parsedObjects;
}

function readJsonFile(fileList) {
    return new Promise((resolve, reject) => {
        try {
            const fileContent = fs.readFileSync(fileList.file.name, 'utf-8');
            let traceObj;
            try {
                traceObj = JSON.parse(fileContent);
            } catch (error) {
                traceObj = tryParseMultiLineInput(fileContent);
            }

            if (Array.isArray(traceObj) && traceObj.every(obj => 'resourceSpans' in obj)) {
                console.log("32")
                const mergedResourceSpans = traceObj.reduce((acc, obj) => {
                    console.log("34")
                    acc.push(...obj.resourceSpans);
                    return acc;
                }, []);

                traceObj = { resourceSpans: mergedResourceSpans };
            }

            if ('resourceSpans' in traceObj) {
                console.log("43")
                resolve(traceObj);
            } else {
                reject(new Error('Invalid JSON format'));
            }
        } catch (error) {
            reject(new Error(`Error reading the file: ${error.message}`));
        }
    });
}

const fileList = { file: { name: './multi.json.txt', type: 'application/json' } };

readJsonFile(fileList)
    .then(result => {
        console.log('Successfully parsed JSON:', result);
    })
    .catch(error => {
        console.error('Error:', error);
    });

If you try this sample test I wrote with plain node (just to emulate whats happening), all the lines get logged without any concern? The lines that sentry says is missing gets logged by the test function. It has got something to do with jest? I'll continue debugging.

@BenzeneAlcohol
Copy link
Contributor Author

BenzeneAlcohol commented Jul 15, 2024

It has something to do with jest and fs module, I feel. I've been debugging for a while, however no success. Will look into this tomorrow.

Additionally @yurishkuro , views on using mock functions from Jest? That way, we do not have to talk with the file system from the test file at all. I believe mock functions should solve the issue.

Mock is used instead of transform function in
test cases.

Signed-off-by: Muthukumar <muthuku37@gmail.com>
@BenzeneAlcohol
Copy link
Contributor Author

@yurishkuro Review once again please, the test case file. Only the tests are changed, but hopefully now everything should work as expected.

@yurishkuro yurishkuro merged commit 8772ad6 into jaegertracing:main Jul 17, 2024
9 checks passed
@yurishkuro
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:bugfix-or-minor-feature 🐞 Bug fixes, Minor Improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Importing OTLP "File Exporter" JSON fails
2 participants