Skip to content

Commit c76ecff

Browse files
committed
Add padStart formatting function
Signed-off-by: Drew Corlin <[email protected]>
1 parent 1fb2061 commit c76ecff

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

packages/jaeger-ui/src/utils/link-formatting.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ describe('getParameterAndFormatter()', () => {
2727
);
2828
});
2929

30+
test('pad_start', () => {
31+
const result = getParameterAndFormatter('traceID | pad_start 10 0');
32+
expect(result).toEqual({
33+
parameterName: 'traceID',
34+
formatFunction: expect.any(Function),
35+
});
36+
37+
expect(result.formatFunction('12345')).toEqual('0000012345');
38+
});
39+
3040
test('No function', () => {
3141
const result = getParameterAndFormatter('startTime');
3242
expect(result).toEqual({

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

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

17-
function getFormatFunctions<T = Trace[keyof Trace]>(): Record<string, (value: T) => string | T> {
17+
function getFormatFunctions<T = Trace[keyof Trace]>(): Record<
18+
string,
19+
(value: T, ...args: string[]) => string | T
20+
> {
1821
return {
1922
epoch_micros_to_date_iso: microsSinceEpoch => {
2023
if (typeof microsSinceEpoch !== 'number') {
@@ -26,6 +29,26 @@ function getFormatFunctions<T = Trace[keyof Trace]>(): Record<string, (value: T)
2629

2730
return new Date(microsSinceEpoch / 1000).toISOString();
2831
},
32+
pad_start: (value, desiredLengthString: string, padCharacter: string) => {
33+
if (typeof value !== 'string') {
34+
console.error('pad_start can only operate on strings, ignoring formatting', {
35+
value,
36+
desiredLength: desiredLengthString,
37+
padCharacter,
38+
});
39+
return value;
40+
}
41+
const desiredLength = parseInt(desiredLengthString, 10);
42+
if (Number.isNaN(desiredLength)) {
43+
console.error('pad_start needs a desired length as second argument, ignoring formatting', {
44+
value,
45+
desiredLength: desiredLengthString,
46+
padCharacter,
47+
});
48+
}
49+
50+
return value.padStart(desiredLength, padCharacter);
51+
},
2952
};
3053
}
3154

@@ -35,8 +58,12 @@ export function getParameterAndFormatter<T = Trace[keyof Trace]>(
3558
parameterName: string;
3659
formatFunction: ((value: T) => T | string) | null;
3760
} {
38-
const [parameterName, formatFunctionName] = parameter.split('|').map(part => part.trim());
39-
if (!formatFunctionName) return { parameterName, formatFunction: null };
61+
const parts = parameter.split('|').map(part => part.trim());
62+
const parameterName = parts[0];
63+
if (parts.length === 1) return { parameterName, formatFunction: null };
64+
65+
const [formatFunctionName, ...args] = parts[1].split(' ');
66+
4067
const formatFunctions = getFormatFunctions<T>();
4168

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

51-
return { parameterName, formatFunction: formatFunction ?? null };
78+
return { parameterName, formatFunction: formatFunction ? val => formatFunction(val, ...args) : null };
5279
}

packages/jaeger-ui/src/utils/stringSupplant.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export function encodedStringSupplant(
2727
const formattedValue = formatFunction && mapValue ? formatFunction(mapValue) : mapValue;
2828

2929
const value = formattedValue != null && encodeFn ? encodeFn(formattedValue) : mapValue;
30+
3031
return value == null ? '' : `${value}`;
3132
});
3233
}

0 commit comments

Comments
 (0)