Skip to content

Commit

Permalink
Add padStart formatting function
Browse files Browse the repository at this point in the history
Signed-off-by: Drew Corlin <[email protected]>
  • Loading branch information
drewcorlin1 committed Nov 30, 2024
1 parent d9315c6 commit 1234b1d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
10 changes: 10 additions & 0 deletions packages/jaeger-ui/src/utils/link-formatting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ describe('getParameterAndFormatter()', () => {
);
});

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

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

test('No function', () => {
const result = getParameterAndFormatter('startTime');
expect(result).toEqual({
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', {

Check warning on line 34 in packages/jaeger-ui/src/utils/link-formatting.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jaeger-ui/src/utils/link-formatting.tsx#L34

Added line #L34 was not covered by tests
value,
desiredLength: desiredLengthString,
padCharacter,
});
return value;

Check warning on line 39 in packages/jaeger-ui/src/utils/link-formatting.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jaeger-ui/src/utils/link-formatting.tsx#L39

Added line #L39 was not covered by tests
}
const desiredLength = parseInt(desiredLengthString, 10);
if (Number.isNaN(desiredLength)) {
console.error('pad_start needs a desired length as second argument, ignoring formatting', {

Check warning on line 43 in packages/jaeger-ui/src/utils/link-formatting.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jaeger-ui/src/utils/link-formatting.tsx#L43

Added line #L43 was not covered by tests
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 };
}
1 change: 1 addition & 0 deletions packages/jaeger-ui/src/utils/stringSupplant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function encodedStringSupplant(
const formattedValue = formatFunction && mapValue ? formatFunction(mapValue) : mapValue;

const value = formattedValue != null && encodeFn ? encodeFn(formattedValue) : mapValue;

return value == null ? '' : `${value}`;
});
}
Expand Down

0 comments on commit 1234b1d

Please sign in to comment.