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

Make traceID, startTime, endTime, duration and traceName available for Link Patterns #1178

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { DEFAULT_HEIGHTS, VirtualizedTraceViewImpl } from './VirtualizedTraceVie
import traceGenerator from '../../../demo/trace-generators';
import transformTraceData from '../../../model/transform-trace-data';
import updateUiFindSpy from '../../../utils/update-ui-find';
import * as getLinks from '../../../model/link-patterns';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
import * as getLinks from '../../../model/link-patterns';
import * as linkPatterns from '../../../model/link-patterns';


jest.mock('./SpanTreeOffset');
jest.mock('../../../utils/update-ui-find');
Expand Down Expand Up @@ -430,4 +431,22 @@ describe('<VirtualizedTraceViewImpl>', () => {
expect(focusUiFindMatchesMock).toHaveBeenLastCalledWith(trace, spanName, false);
});
});

describe('linksGetter()', () => {
let getLinksSpy;

beforeAll(() => {
getLinksSpy = jest.spyOn(getLinks, 'default');
});

afterAll(() => {
getLinksSpy.mockRestore();
});

it('calls getLinks with expected params', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stale title

const span = trace.spans[1];
instance.linksGetter(span, span.tags, 0);
expect(getLinksSpy).toHaveBeenCalledWith(span, span.tags, 0, trace);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be better to validate the actual value returned, which, as I understand, is [{url, text}]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find a clean way to initialize linkPatterns for tests, so I exported the const processedLinks in link-patterns.tsx.

});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ export class VirtualizedTraceViewImpl extends React.Component<VirtualizedTraceVi
return DEFAULT_HEIGHTS.detail;
};

linksGetter = (span: Span, items: KeyValuePair[], itemIndex: number) => getLinks(span, items, itemIndex);
linksGetter = (span: Span, items: KeyValuePair[], itemIndex: number) => {
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
const { trace } = this.props;
return getLinks(span, items, itemIndex, trace);
};

renderRow = (key: string, style: React.CSSProperties, index: number, attrs: {}) => {
const { isDetail, span, spanIndex } = this.getRowStates()[index];
Expand Down
63 changes: 62 additions & 1 deletion packages/jaeger-ui/src/model/link-patterns.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
createTestFunction,
getParameterInArray,
getParameterInAncestor,
getParameterInTrace,
processLinkPattern,
computeLinks,
createGetLinks,
Expand Down Expand Up @@ -296,6 +297,31 @@ describe('getParameterInAncestor()', () => {
});
});

describe('getParameterInTrace()', () => {
const trace = {
processes: [],
traceName: 'theTrace',
traceID: 'trc1',
spans: [],
startTime: 1000,
endTime: 3000,
duration: 2000,
services: [],
};

it('returns an entry that is present', () => {
expect(getParameterInTrace('startTime', trace)).toEqual(trace.startTime);
});

it('returns undefined when the entry cannot be found', () => {
expect(getParameterInTrace('someThingElse', trace)).toBeUndefined();
});

it('returns undefined when there is no trace', () => {
expect(getParameterInTrace('traceID')).toBeUndefined();
});
});

describe('computeTraceLink()', () => {
const linkPatterns = [
{
Expand Down Expand Up @@ -354,11 +380,29 @@ describe('computeLinks()', () => {
url: 'http://example.com/?myKey=#{myOtherKey}&myKey=#{myKey}',
text: 'second link (#{myOtherKey})',
},
{
type: 'logs',
key: 'myThirdKey',
url:
'http://example.com/?myKey1=#{myKey}&myKey=#{myThirdKey}&traceID=#{trace.traceID}&startTime=#{trace.startTime}',
text: 'third link (#{myThirdKey}) for traceID - #{trace.traceID}',
},
].map(processLinkPattern);

const spans = [
{ depth: 0, process: {}, tags: [{ key: 'myKey', value: 'valueOfMyKey' }] },
{ depth: 1, process: {}, logs: [{ fields: [{ key: 'myOtherKey', value: 'valueOfMy+Other+Key' }] }] },
{
depth: 1,
process: {},
logs: [
{
fields: [
{ key: 'myOtherKey', value: 'valueOfMy+Other+Key' },
{ key: 'myThirdKey', value: 'valueOfThirdMyKey' },
],
},
],
},
];
spans[1].references = [
{
Expand All @@ -367,6 +411,17 @@ describe('computeLinks()', () => {
},
];

const trace = {
processes: [],
traceName: 'theTrace',
traceID: 'trc1',
spans: [],
startTime: 1000,
endTime: 3000,
duration: 2000,
services: [],
};

it('correctly computes links', () => {
expect(computeLinks(linkPatterns, spans[0], spans[0].tags, 0)).toEqual([
{
Expand All @@ -380,6 +435,12 @@ describe('computeLinks()', () => {
text: 'second link (valueOfMy+Other+Key)',
},
]);
expect(computeLinks(linkPatterns, spans[1], spans[1].logs[0].fields, 1, trace)).toEqual([
{
url: 'http://example.com/?myKey1=valueOfMyKey&myKey=valueOfThirdMyKey&traceID=trc1&startTime=1000',
text: 'third link (valueOfThirdMyKey) for traceID - trc1',
},
]);
});
});

Expand Down
59 changes: 45 additions & 14 deletions packages/jaeger-ui/src/model/link-patterns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,27 @@ export function getParameterInAncestor(name: string, span: Span) {
}
currentSpan = getParent(currentSpan);
}

return undefined;
}

const getValidTraceKeys = memoize(10)((trace: Trace) => {
const validKeys = (Object.keys(trace) as (keyof Trace)[]).filter(
key => typeof trace[key] === 'string' || typeof trace[key] === 'number'
);
return validKeys;
});

export function getParameterInTrace(name: string, trace: Trace | undefined) {
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
if (trace) {
const validTraceKeys = getValidTraceKeys(trace);

const key = name as keyof Trace;
if (validTraceKeys.includes(key)) {
return trace[key];
}
}

return undefined;
}

Expand All @@ -119,20 +140,17 @@ function callTemplate(template: ProcessedTemplate, data: any) {

export function computeTraceLink(linkPatterns: ProcessedLinkPattern[], trace: Trace) {
const result: TLinksRV = [];
const validKeys = (Object.keys(trace) as (keyof Trace)[]).filter(
key => typeof trace[key] === 'string' || typeof trace[key] === 'number'
);

linkPatterns
.filter(pattern => pattern.type('traces'))
.forEach(pattern => {
const parameterValues: Record<string, any> = {};
const allParameters = pattern.parameters.every(parameter => {
const key = parameter as keyof Trace;
if (validKeys.includes(key)) {
const val = getParameterInTrace(parameter, trace);
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
if (val) {
// At this point is safe to access to trace object using parameter variable because
// we validated parameter against validKeys, this implies that parameter a keyof Trace.
parameterValues[parameter] = trace[key];
parameterValues[parameter] = val;
return true;
}
return false;
Expand All @@ -152,7 +170,8 @@ export function computeLinks(
linkPatterns: ProcessedLinkPattern[],
span: Span,
items: KeyValuePair[],
itemIndex: number
itemIndex: number,
trace: Trace | undefined
) {
const item = items[itemIndex];
let type = 'logs';
Expand All @@ -169,16 +188,28 @@ export function computeLinks(
if (pattern.type(type) && pattern.key(item.key) && pattern.value(item.value)) {
const parameterValues: Record<string, any> = {};
const allParameters = pattern.parameters.every(parameter => {
let entry = getParameterInArray(parameter, items);
if (!entry && !processTags) {
// do not look in ancestors for process tags because the same object may appear in different places in the hierarchy
// and the cache in getLinks uses that object as a key
entry = getParameterInAncestor(parameter, span);
let entry;

if (parameter.startsWith('trace.')) {
const traceVal = getParameterInTrace(parameter.split('trace.')[1], trace);
if (traceVal) {
entry = { key: parameter, value: traceVal };
}
} else {
entry = getParameterInArray(parameter, items);

if (!entry && !processTags) {
// do not look in ancestors for process tags because the same object may appear in different places in the hierarchy
// and the cache in getLinks uses that object as a key
entry = getParameterInAncestor(parameter, span);
}
}

if (entry) {
parameterValues[parameter] = entry.value;
return true;
}

// eslint-disable-next-line no-console
console.warn(
`Skipping link pattern, missing parameter ${parameter} for key ${item.key} in ${type}.`,
Expand All @@ -198,14 +229,14 @@ export function computeLinks(
}

export function createGetLinks(linkPatterns: ProcessedLinkPattern[], cache: WeakMap<KeyValuePair, Link[]>) {
return (span: Span, items: KeyValuePair[], itemIndex: number) => {
return (span: Span, items: KeyValuePair[], itemIndex: number, trace: Trace | undefined) => {
if (linkPatterns.length === 0) {
return [];
}
const item = items[itemIndex];
let result = cache.get(item);
if (!result) {
result = computeLinks(linkPatterns, span, items, itemIndex);
result = computeLinks(linkPatterns, span, items, itemIndex, trace);
cache.set(item, result);
}
return result;
Expand Down