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

Support uploading traces in OpenTelemetry format (OTLP JSON) #2145

Merged
merged 9 commits into from
Mar 4, 2024
3 changes: 3 additions & 0 deletions packages/jaeger-ui/src/api/jaeger.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ const JaegerAPI = {
fetchServiceOperations(serviceName) {
return getJSON(`${this.apiRoot}services/${encodeURIComponent(serviceName)}/operations`);
},
transformOTLP(traces) {
return getJSON(`${this.apiRoot}transform`, { method: 'POST', body: JSON.stringify(traces) });
},
fetchServiceServerOps(service) {
return getJSON(`${this.apiRoot}operations`, {
query: {
Expand Down
9 changes: 9 additions & 0 deletions packages/jaeger-ui/src/api/jaeger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ describe('fetchServiceServerOps', () => {
});
});

describe('transformOTLP', () => {
it('GETs the transformed trace of Jaeger kind when provided with OTLP', () => {
const trace = JSON.parse('{"test" : true}');
const body = { ...defaultOptions, body: JSON.stringify(trace), method: 'POST' };
JaegerAPI.transformOTLP(trace);
expect(fetchMock).toHaveBeenLastCalledWith(`${DEFAULT_API_ROOT}transform`, body);
});
});

describe('fetchTrace', () => {
const generatedTraces = traceGenerator.traces({ numberOfTraces: 5 });

Expand Down
43 changes: 43 additions & 0 deletions packages/jaeger-ui/src/utils/fixtures/otlp2jaeger-in-error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"resourceSpans": [
{
"resource": {
"attributes": []
},
"scopeSpans": [
{
"scope": {
"name": "telemetrygen"
},
"spans": [
{
"traceId": "83a9efd15c1c98a977e0711cc93ee28b",
"spanId": "e127af99e3b3e074",
"parentSpanId": "909541b92cf05311",
"name": "okey-dokey-0",
"kind": 2,
"startTimeUnixNano": "1706678909209712000",
"endTimeUnixNano": "1706678909209835000",
"attributes": [
{
"key": "net.peer.ip",
"value": {
"stringValue": "1.2.3.4"
}
},
{
"key": "peer.service",
"value": {
"stringValue": "telemetrygen-client"
}
}
],
"status": {}
}
]
}
],
"schemaUrl": "https://opentelemetry.io/schemas/1.4.0"
}
]
}
74 changes: 74 additions & 0 deletions packages/jaeger-ui/src/utils/fixtures/otlp2jaeger-in.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"resourceSpans": [
{
"resource": {
"attributes": [
{
"key": "service.name",
"value": {
"stringValue": "telemetrygen"
}
}
]
},
"scopeSpans": [
{
"scope": {
"name": "telemetrygen"
},
"spans": [
{
"traceId": "83a9efd15c1c98a977e0711cc93ee28b",
"spanId": "e127af99e3b3e074",
"parentSpanId": "909541b92cf05311",
"name": "okey-dokey-0",
"kind": 2,
"startTimeUnixNano": "1706678909209712000",
"endTimeUnixNano": "1706678909209835000",
"attributes": [
{
"key": "net.peer.ip",
"value": {
"stringValue": "1.2.3.4"
}
},
{
"key": "peer.service",
"value": {
"stringValue": "telemetrygen-client"
}
}
],
"status": {}
},
{
"traceId": "83a9efd15c1c98a977e0711cc93ee28b",
"spanId": "e127af99e3b3e074",
"parentSpanId": "909541b92cf05311",
"name": "okey-dokey-0",
"kind": 2,
"startTimeUnixNano": "1706678909209712000",
"endTimeUnixNano": "1706678909209835000",
"attributes": [
{
"key": "net.peer.ip",
"value": {
"stringValue": "1.2.3.4"
}
},
{
"key": "peer.service",
"value": {
"stringValue": "telemetrygen-client"
}
}
],
"status": {}
}
]
}
],
"schemaUrl": "https://opentelemetry.io/schemas/1.4.0"
}
]
}
98 changes: 98 additions & 0 deletions packages/jaeger-ui/src/utils/fixtures/otlp2jaeger-out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"data": [
{
"traceID": "83a9efd15c1c98a977e0711cc93ee28b",
"spans": [
{
"traceID": "83a9efd15c1c98a977e0711cc93ee28b",
"spanID": "e127af99e3b3e074",
"operationName": "okey-dokey-0",
"references": [
{
"refType": "CHILD_OF",
"traceID": "83a9efd15c1c98a977e0711cc93ee28b",
"spanID": "909541b92cf05311"
}
],
"startTime": 1706678909209712,
"duration": 123,
"tags": [
{
"key": "otel.library.name",
"type": "string",
"value": "telemetrygen"
},
{
"key": "net.peer.ip",
"type": "string",
"value": "1.2.3.4"
},
{
"key": "peer.service",
"type": "string",
"value": "telemetrygen-client"
},
{
"key": "span.kind",
"type": "string",
"value": "server"
}
],
"logs": [],
"processID": "p1",
"warnings": null
},
{
"traceID": "83a9efd15c1c98a977e0711cc93ee28b",
"spanID": "e127af99e3b3e074",
"operationName": "okey-dokey-0",
"references": [
{
"refType": "CHILD_OF",
"traceID": "83a9efd15c1c98a977e0711cc93ee28b",
"spanID": "909541b92cf05311"
}
],
"startTime": 1706678909209712,
"duration": 123,
"tags": [
{
"key": "otel.library.name",
"type": "string",
"value": "telemetrygen"
},
{
"key": "net.peer.ip",
"type": "string",
"value": "1.2.3.4"
},
{
"key": "peer.service",
"type": "string",
"value": "telemetrygen-client"
},
{
"key": "span.kind",
"type": "string",
"value": "server"
}
],
"logs": [],
"processID": "p1",
"warnings": null
}
],
"processes": {
"p1": {
"serviceName": "telemetrygen",
"tags": []
}
},
"warnings": null
}
],
"total": 0,
"limit": 0,
"offset": 0,
"errors": null
}
28 changes: 28 additions & 0 deletions packages/jaeger-ui/src/utils/readJsonFile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import fs from 'fs';
import lodash from 'lodash';
import readJsonFile from './readJsonFile';
import JaegerAPI from '../api/jaeger';

jest.spyOn(JaegerAPI, 'transformOTLP').mockImplementation(APICallRequest => {
const OTLPTrace = JSON.parse(fs.readFileSync('src/utils/fixtures/otlp2jaeger-in.json', 'utf-8'));
const jaegerTrace = JSON.parse(fs.readFileSync('src/utils/fixtures/otlp2jaeger-out.json', 'utf-8'));
if (lodash.isEqual(APICallRequest, OTLPTrace)) {
return Promise.resolve(jaegerTrace);
}
// This defines case where API call errors out even after detecting a `resourceSpan` in the request
return Promise.reject();
});

describe('fileReader.readJsonFile', () => {
it('rejects when given an invalid file', () => {
Expand All @@ -39,6 +52,21 @@ describe('fileReader.readJsonFile', () => {
return expect(p).resolves.toMatchObject(obj);
});

it('loads JSON data (OTLP), successfully', () => {
const inObj = JSON.parse(fs.readFileSync('src/utils/fixtures/otlp2jaeger-in.json', 'utf-8'));
const outObj = JSON.parse(fs.readFileSync('src/utils/fixtures/otlp2jaeger-out.json', 'utf-8'));
const file = new File([JSON.stringify(inObj)], 'foo.json');
const p = readJsonFile({ file });
return expect(p).resolves.toMatchObject(outObj);
});

it('rejects an OTLP trace', () => {
const inObj = JSON.parse(fs.readFileSync('src/utils/fixtures/otlp2jaeger-in-error.json', 'utf-8'));
const file = new File([JSON.stringify(inObj)], 'foo.json');
const p = readJsonFile({ file });
return expect(p).rejects.toMatchObject(expect.any(Error));
});

it('rejects on malform JSON', () => {
const file = new File(['not-json'], 'foo.json');
const p = readJsonFile({ file });
Expand Down
15 changes: 14 additions & 1 deletion packages/jaeger-ui/src/utils/readJsonFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import JaegerAPI from '../api/jaeger';

export default function readJsonFile(fileList: { file: File }): Promise<string> {
return new Promise((resolve, reject) => {
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
const reader = new FileReader();
Expand All @@ -21,7 +23,18 @@ export default function readJsonFile(fileList: { file: File }): Promise<string>
return;
}
try {
resolve(JSON.parse(reader.result));
const traceObj = JSON.parse(reader.result);
if ('resourceSpans' in traceObj) {
JaegerAPI.transformOTLP(traceObj)
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
.then((result: string) => {
resolve(result);
})
.catch(() => {
reject(new Error(`Error converting traces to OTLP`));
});
} else {
resolve(traceObj);
}
} catch (error: unknown) {
reject(new Error(`Error parsing JSON: ${(error as Error).message}`));
}
Expand Down
Loading