diff --git a/packages/jaeger-ui/src/components/TracePage/TraceTimelineViewer/SpanDetail/KeyValuesTable.test.js b/packages/jaeger-ui/src/components/TracePage/TraceTimelineViewer/SpanDetail/KeyValuesTable.test.js index c1326f9f24..bddabc961e 100644 --- a/packages/jaeger-ui/src/components/TracePage/TraceTimelineViewer/SpanDetail/KeyValuesTable.test.js +++ b/packages/jaeger-ui/src/components/TracePage/TraceTimelineViewer/SpanDetail/KeyValuesTable.test.js @@ -45,7 +45,12 @@ describe('LinkValue', () => { describe('', () => { let wrapper; - const data = [{ key: 'span.kind', value: 'client' }, { key: 'omg', value: 'mos-def' }]; + const data = [ + { key: 'span.kind', value: 'client' }, + { key: 'omg', value: 'mos-def' }, + { key: 'numericString', value: '12345678901234567890' }, + { key: 'jsonkey', value: JSON.stringify({ hello: 'world' }) }, + ]; beforeEach(() => { wrapper = shallow(); @@ -127,4 +132,14 @@ describe('', () => { expect(copyIcon.prop('tooltipTitle')).toBe('Copy JSON'); }); }); + + it('renders a span value containing numeric string correctly', () => { + const el = wrapper.find('.ub-inline-block'); + expect(el.length).toBe(data.length); + el.forEach((valueDiv, i) => { + if (data[i].key !== 'jsonkey') { + expect(valueDiv.html()).toMatch(`"${data[i].value}"`); + } + }); + }); }); diff --git a/packages/jaeger-ui/src/components/TracePage/TraceTimelineViewer/SpanDetail/KeyValuesTable.tsx b/packages/jaeger-ui/src/components/TracePage/TraceTimelineViewer/SpanDetail/KeyValuesTable.tsx index d9753d3de4..2b316068d8 100644 --- a/packages/jaeger-ui/src/components/TracePage/TraceTimelineViewer/SpanDetail/KeyValuesTable.tsx +++ b/packages/jaeger-ui/src/components/TracePage/TraceTimelineViewer/SpanDetail/KeyValuesTable.tsx @@ -23,11 +23,17 @@ import { KeyValuePair, Link } from '../../../../types/trace'; import './KeyValuesTable.css'; -function parseIfJson(value: any) { - try { - return JSON.parse(value); - // eslint-disable-next-line no-empty - } catch (_) {} +const jsonObjectOrArrayStartRegex = /^(\[|\{)/; + +function parseIfComplexJson(value: any) { + // if the value is a string representing actual json object or array, then use json-markup + if (typeof value === 'string' && jsonObjectOrArrayStartRegex.test(value)) { + // otherwise just return as is + try { + return JSON.parse(value); + // eslint-disable-next-line no-empty + } catch (_) {} + } return value; } @@ -66,7 +72,7 @@ export default function KeyValuesTable(props: KeyValuesTableProps) { {data.map((row, i) => { const markup = { - __html: jsonMarkup(parseIfJson(row.value)), + __html: jsonMarkup(parseIfComplexJson(row.value)), }; // eslint-disable-next-line react/no-danger const jsonTable =
;