Skip to content

Commit

Permalink
Add pad_start link formatting function #2505 (#2504)
Browse files Browse the repository at this point in the history
Resolves #2505

Given a traceparent header like
00-00000000000000001b3ef83a5d15ed1b-61409d351b3a5d99-01, jaeger will
store (and thus display + interpolate) the trace ID as 1b3ef83a5d15ed1b.
When attempting to use the shortened trace ID to link to logs some
tracing integrations (in my case, logs using
[pino](https://www.npmjs.com/package/pino) instrumented with the
[OpenTelemetry pino
instrumentation](https://www.npmjs.com/package/@opentelemetry/instrumentation-pino))
will use the full length trace ID, making jaeger link patterns not work.

This PR adds a `pad_start` formatting function, heavily inspired by
javascript's
[String.padStart](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart)
to format the trace ID in those cases. If approved, I'll update the
documentation in https://github.com/jaegertracing/documentation to
mention this function and it's behavior.

In addition to this specific function, I'd like feedback on the approach
of passing arguments to formatting functions with the space-delimited
approach (eg `#{traceID | padStart 32 0}`).

# How was this change tested?
Unit tests and by running it locally with the following UI config
```
const DEFAULT_CONFIG = {
  "linkPatterns": [
    {
      "type": "traces",
      "url": "https://myOpenSearch.com/_dashboards/app/discover#/?_g=(time:(from:'#{startTime | epoch_micros_to_date_iso}',to:'#{endTime | epoch_micros_to_date_iso}'))&_a=(columns:!(_source),filters:!(('$state':(store:appState),meta:(alias:!n,disabled:!f,index:filebeat-all,key:json.traceId,negate:!f,params:(query:'#{traceID | pad_start 32 0}'),type:phrase),query:(match_phrase:(json.traceId:'#{traceID | pad_start 32 0}')))),index:filebeat-all)",
      "text": "Logs"
    }
  ]
};
```

Signed-off-by: Drew Corlin <drew.corlin@getgarner.com>
  • Loading branch information
drewcorlin1 authored Dec 1, 2024
1 parent d9315c6 commit a083195
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
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 };
}

0 comments on commit a083195

Please # to comment.