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

Add pad_start link formatting function #2505 #2504

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
45 changes: 37 additions & 8 deletions packages/jaeger-ui/src/utils/link-formatting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,45 @@
import { getParameterAndFormatter } from './link-formatting';

describe('getParameterAndFormatter()', () => {
test('epoch_micros_to_date_iso', () => {
const result = getParameterAndFormatter('startTime | epoch_micros_to_date_iso');
expect(result).toEqual({
parameterName: 'startTime',
formatFunction: expect.any(Function),
describe('epoch_micros_to_date_iso', () => {
test('epoch_micros_to_date_iso', () => {
const result = getParameterAndFormatter('startTime | epoch_micros_to_date_iso');
expect(result).toEqual({
parameterName: 'startTime',
formatFunction: expect.any(Function),
});

expect(result.formatFunction(new Date('2020-01-01').getTime() * 1000)).toEqual(
'2020-01-01T00:00:00.000Z'
);
});

test('Non date', () => {
const result = getParameterAndFormatter('startTime | epoch_micros_to_date_iso');
expect(result.formatFunction('Not a date value')).toEqual('Not a date value');
});
});

describe('pad_start', () => {
test('Valid desired length', () => {
const result = getParameterAndFormatter('traceID | pad_start 10 0');
expect(result).toEqual({
parameterName: 'traceID',
formatFunction: expect.any(Function),
});

expect(result.formatFunction('12345')).toEqual('0000012345');
});

expect(result.formatFunction(new Date('2020-01-01').getTime() * 1000)).toEqual(
'2020-01-01T00:00:00.000Z'
);
test('Invalid desired length', () => {
const result = getParameterAndFormatter('traceID | pad_start invalid 0');
expect(result.formatFunction('12345')).toEqual('12345');
});

test('Invalid input', () => {
const result = getParameterAndFormatter('traceID | pad_start 32 0');
expect(result.formatFunction(12345)).toEqual(12345);
});
});

test('No function', () => {
Expand Down
35 changes: 31 additions & 4 deletions packages/jaeger-ui/src/utils/link-formatting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

import { Trace } from '../types/trace';

function getFormatFunctions<T = Trace[keyof Trace]>(): Record<string, (value: T) => string | T> {
function getFormatFunctions<T = Trace[keyof Trace]>(): Record<
string,
(value: T, ...args: string[]) => string | T
> {
return {
epoch_micros_to_date_iso: microsSinceEpoch => {
if (typeof microsSinceEpoch !== 'number') {
Expand All @@ -26,6 +29,26 @@ function getFormatFunctions<T = Trace[keyof Trace]>(): Record<string, (value: T)

return new Date(microsSinceEpoch / 1000).toISOString();
},
pad_start: (value, desiredLengthString: string, padCharacter: string) => {
if (typeof value !== 'string') {
console.error('pad_start can only operate on strings, ignoring formatting', {
value,
desiredLength: desiredLengthString,
padCharacter,
});
return value;
}
const desiredLength = parseInt(desiredLengthString, 10);
if (Number.isNaN(desiredLength)) {
console.error('pad_start needs a desired length as second argument, ignoring formatting', {
value,
desiredLength: desiredLengthString,
padCharacter,
});
}

return value.padStart(desiredLength, padCharacter);
},
};
}

Expand All @@ -35,8 +58,12 @@ export function getParameterAndFormatter<T = Trace[keyof Trace]>(
parameterName: string;
formatFunction: ((value: T) => T | string) | null;
} {
const [parameterName, formatFunctionName] = parameter.split('|').map(part => part.trim());
if (!formatFunctionName) return { parameterName, formatFunction: null };
const parts = parameter.split('|').map(part => part.trim());
const parameterName = parts[0];
if (parts.length === 1) return { parameterName, formatFunction: null };

const [formatFunctionName, ...args] = parts[1].split(' ');

const formatFunctions = getFormatFunctions<T>();

const formatFunction = formatFunctions[formatFunctionName];
Expand All @@ -48,5 +75,5 @@ export function getParameterAndFormatter<T = Trace[keyof Trace]>(
});
}

return { parameterName, formatFunction: formatFunction ?? null };
return { parameterName, formatFunction: formatFunction ? val => formatFunction(val, ...args) : null };
}
Loading