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 1 commit
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 @@ -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
60 changes: 60 additions & 0 deletions 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,35 @@ 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('traceID', trace)).toEqual({
key: 'traceID',
value: trace.traceID,
});
});

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,18 +384,42 @@ describe('computeLinks()', () => {
url: 'http://example.com/?myKey=#{myOtherKey}&myKey=#{myKey}',
text: 'second link (#{myOtherKey})',
},
{
type: 'tags',
key: 'myThirdKey',
url: 'http://example.com/?myKey1=#{myKey}&myKey=#{myThirdKey}&traceID=#{traceID}&startTime=#{startTime}',
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it's a good idea that you can reference trace-level attributes without qualification. I can see it for traceID, but why does startTime refer to trace-level and not span-level?

My preference would be to use trace.traceID, trace.startTime, etc. to clearly indicate the scope, not to try to infer it automatically.

Copy link
Member

Choose a reason for hiding this comment

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

Reading more into the existing code I am not a big fan of how it automatically ascends into ancestor spans when looking for tags. #1179

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed - now trace level are accessible only with trace.<data> qualification in template variables.

text: 'third link (#{myThirdKey}) for traceID - #{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: {}, tags: [{ key: 'myThirdKey', value: 'valueOfThirdMyKey' }] },
Copy link
Member

Choose a reason for hiding this comment

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

seem redundant adding a new span, just add a new tag to one of the other spans

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed - added a new to existing span.

];
spans[1].references = [
{
refType: 'CHILD_OF',
span: spans[0],
},
];
spans[2].references = [
{
refType: 'CHILD_OF',
span: spans[0],
},
];

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 +434,12 @@ describe('computeLinks()', () => {
text: 'second link (valueOfMy+Other+Key)',
},
]);
expect(computeLinks(linkPatterns, spans[2], spans[2].tags, 0, trace)).toEqual([
{
url: 'http://example.com/?myKey1=valueOfMyKey&myKey=valueOfThirdMyKey&traceID=trc1&startTime=1000',
text: 'third link (valueOfThirdMyKey) for traceID - trc1',
},
]);
});
});

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

return undefined;
}

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 = (Object.keys(trace) as (keyof Trace)[]).filter(
key => typeof trace[key] === 'string' || typeof trace[key] === 'number'
);

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

return undefined;
}

Expand Down Expand Up @@ -152,7 +168,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 @@ -170,15 +187,23 @@ export function computeLinks(
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);
}

// look up in trace for matching keys
if(!entry) {
entry = getParameterInTrace(parameter, trace);
}

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 +223,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