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

Correct trace name resolution #541

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
244 changes: 244 additions & 0 deletions packages/jaeger-ui/src/model/find-trace-name.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
// Copyright (c) 2020 The Jaeger Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { getTraceName } from './trace-viewer';

describe('getTraceName', () => {
const firstSpanId = 'firstSpanId';
const secondSpanId = 'secondSpanId';
const thirdSpanId = 'thirdSpanId';
const missingSpanId = 'missingSpanId';

const currentTraceId = 'currentTraceId';

const serviceName = 'serviceName';
const operationName = 'operationName';

const t = 1583758670000;

// Note: this trace has a loop S1 <- S2 <- S3 <- S1, which is the only way
// to make the algorithm return an empty string as trace name.
const spansWithNoRoots = [
swapster marked this conversation as resolved.
Show resolved Hide resolved
{
spanID: firstSpanId,
traceID: currentTraceId,
startTime: t + 200,
process: {},
references: [
{
spanID: secondSpanId,
traceID: currentTraceId,
},
],
},
{
spanID: secondSpanId,
traceID: currentTraceId,
startTime: t + 100,
process: {},
references: [
{
spanID: thirdSpanId,
traceID: currentTraceId,
},
],
},
{
spanID: thirdSpanId,
traceID: currentTraceId,
startTime: t,
process: {},
references: [
{
spanID: firstSpanId,
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
traceID: currentTraceId,
},
],
},
];
const spansWithMultipleRootsDifferentByStartTime = [
{
spanID: firstSpanId,
traceID: currentTraceId,
startTime: t + 200,
process: {},
references: [
{
spanID: thirdSpanId,
traceID: currentTraceId,
},
],
},
{
spanID: secondSpanId, // may be a root span
traceID: currentTraceId,
startTime: t + 100,
process: {},
references: [
{
spanID: missingSpanId,
traceID: currentTraceId,
},
],
},
{
spanID: thirdSpanId, // root span (as the earliest)
traceID: currentTraceId,
startTime: t,
operationName,
process: {
serviceName,
},
references: [
{
spanID: missingSpanId,
traceID: currentTraceId,
},
],
},
];
const spansWithMultipleRootsWithOneWithoutRefs = [
{
spanID: firstSpanId,
traceID: currentTraceId,
startTime: t + 200,
process: {},
references: [
{
spanID: thirdSpanId,
traceID: currentTraceId,
},
],
},
{
spanID: secondSpanId, // root span (as a span without any refs)
traceID: currentTraceId,
startTime: t + 100,
operationName,
process: {
serviceName,
},
},
{
spanID: thirdSpanId, // may be a root span
traceID: currentTraceId,
startTime: t,
process: {},
references: [
{
spanID: missingSpanId,
traceID: currentTraceId,
},
],
},
];
const spansWithOneRootWithRemoteRef = [
{
spanID: firstSpanId,
traceID: currentTraceId,
startTime: t + 200,
process: {},
references: [
{
spanID: secondSpanId,
traceID: currentTraceId,
},
],
},
{
spanID: secondSpanId,
traceID: currentTraceId,
startTime: t + 100,
process: {},
references: [
{
spanID: thirdSpanId,
traceID: currentTraceId,
},
],
},
{
spanID: thirdSpanId, // effective root span, since its parent is missing
traceID: currentTraceId,
startTime: t,
operationName,
process: {
serviceName,
},
references: [
{
spanID: missingSpanId,
traceID: currentTraceId,
},
],
},
];
const spansWithOneRootWithNoRefs = [
{
spanID: firstSpanId,
traceID: currentTraceId,
startTime: t + 200,
process: {},
references: [
{
spanID: thirdSpanId,
traceID: currentTraceId,
},
],
},
{
spanID: secondSpanId, // root span
traceID: currentTraceId,
startTime: t + 100,
operationName,
process: {
serviceName,
},
},
{
spanID: thirdSpanId,
traceID: currentTraceId,
startTime: t,
process: {},
references: [
{
spanID: secondSpanId,
traceID: currentTraceId,
},
],
},
];

const fullTraceName = `${serviceName}: ${operationName}`;

it('returns an empty string if given spans with no root among them', () => {
expect(getTraceName(spansWithNoRoots)).toEqual('');
});

it('returns an id of root span with the earliest startTime', () => {
expect(getTraceName(spansWithMultipleRootsDifferentByStartTime)).toEqual(fullTraceName);
});

it('returns an id of root span without any refs', () => {
expect(getTraceName(spansWithMultipleRootsWithOneWithoutRefs)).toEqual(fullTraceName);
});

it('returns an id of root span with remote ref', () => {
expect(getTraceName(spansWithOneRootWithRemoteRef)).toEqual(fullTraceName);
});

it('returns an id of root span with no refs', () => {
expect(getTraceName(spansWithOneRootWithNoRefs)).toEqual(fullTraceName);
});
});
19 changes: 0 additions & 19 deletions packages/jaeger-ui/src/model/trace-viewer.js

This file was deleted.

40 changes: 40 additions & 0 deletions packages/jaeger-ui/src/model/trace-viewer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2020 The Jaeger Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { Span } from '../types/trace';

type spansDict = { [index: string]: Span };

// eslint-disable-next-line import/prefer-default-export
export function getTraceName(spans: Span[]) {
const allTraceSpans: spansDict = spans.reduce((dict, span) => ({ ...dict, [span.spanID]: span }), {});
const rootSpan = spans
.filter(sp => {
if (!sp.references || !sp.references.length) {
return true;
}
const parentIDs = sp.references.filter(r => r.traceID === sp.traceID).map(r => r.spanID);

// returns true if no parent from this trace found
return !parentIDs.some(pID => Boolean(allTraceSpans[pID]));
})
.sort((sp1, sp2) => {
const sp1ParentsNum = sp1.references ? sp1.references.length : 0;
const sp2ParentsNum = sp2.references ? sp2.references.length : 0;

return sp1ParentsNum - sp2ParentsNum || sp1.startTime - sp2.startTime;
})[0];

return rootSpan ? `${rootSpan.process.serviceName}: ${rootSpan.operationName}` : '';
}
1 change: 0 additions & 1 deletion packages/jaeger-ui/tsconfig.lint.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"src/api/jaeger.js",
"src/components/SearchTracePage/SearchResults/ResultItem.markers.js",
"src/model/order-by.js",
"src/model/trace-viewer.js",
Copy link
Member

Choose a reason for hiding this comment

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

should the tsx file be added to some other linter config, or will it be automatically linted?

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 will be linted since this config file inherits another config with include option.
Please correct me if I'm wrong.

"src/selectors/process.js",
"src/selectors/span.js",
"src/selectors/trace.js",
Expand Down