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

Sort span tags in alphabetical order #489

Merged
merged 5 commits into from
Dec 10, 2019
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
55 changes: 55 additions & 0 deletions packages/jaeger-ui/src/model/transform-trace-data.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2019 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 { orderTags, deduplicateTags } from './transform-trace-data';

describe('orderTags()', () => {
it('correctly orders tags', () => {
const orderedTags = orderTags(
[
nabam marked this conversation as resolved.
Show resolved Hide resolved
{ key: 'b.ip', value: '8.8.4.4' },
{ key: 'http.Status_code', value: '200' },
{ key: 'z.ip', value: '8.8.8.16' },
{ key: 'a.ip', value: '8.8.8.8' },
{ key: 'http.message', value: 'ok' },
],
['z.', 'a.', 'HTTP.']
);
expect(orderedTags).toEqual([
{ key: 'z.ip', value: '8.8.8.16' },
{ key: 'a.ip', value: '8.8.8.8' },
{ key: 'http.message', value: 'ok' },
{ key: 'http.Status_code', value: '200' },
{ key: 'b.ip', value: '8.8.4.4' },
]);
});
});

describe('deduplicateTags()', () => {
it('deduplicates tags', () => {
const tagsInfo = deduplicateTags([
{ key: 'b.ip', value: '8.8.4.4' },
{ key: 'b.ip', value: '8.8.8.8' },
{ key: 'b.ip', value: '8.8.4.4' },
{ key: 'a.ip', value: '8.8.8.8' },
]);

expect(tagsInfo.tags).toEqual([
{ key: 'b.ip', value: '8.8.4.4' },
{ key: 'b.ip', value: '8.8.8.8' },
{ key: 'a.ip', value: '8.8.8.8' },
]);
expect(tagsInfo.warnings).toEqual(['Duplicate tag "b.ip:8.8.4.4"']);
});
});
39 changes: 36 additions & 3 deletions packages/jaeger-ui/src/model/transform-trace-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
import _isEqual from 'lodash/isEqual';

import { getTraceSpanIdsAsTree } from '../selectors/trace';
import { getConfigValue } from '../utils/config/get-config';
import { KeyValuePair, Span, SpanData, Trace, TraceData } from '../types/trace';
import TreeNode from '../utils/TreeNode';

function deduplicateTags(spanTags: Array<KeyValuePair>) {
// exported for tests
export function deduplicateTags(spanTags: KeyValuePair[]) {
const warningsHash: Map<string, string> = new Map<string, string>();
const tags: Array<KeyValuePair> = spanTags.reduce<Array<KeyValuePair>>((uniqueTags, tag) => {
const tags: KeyValuePair[] = spanTags.reduce<KeyValuePair[]>((uniqueTags, tag) => {
if (!uniqueTags.some(t => t.key === tag.key && t.value === tag.value)) {
uniqueTags.push(tag);
} else {
Expand All @@ -32,6 +34,37 @@ function deduplicateTags(spanTags: Array<KeyValuePair>) {
return { tags, warnings };
}

// exported for tests
export function orderTags(spanTags: KeyValuePair[], topPrefixes?: string[]) {
const orderedTags: KeyValuePair[] = spanTags.slice();
const tp = (topPrefixes || []).map((p: string) => p.toLowerCase());

orderedTags.sort((a, b) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this should be case insensitive, so below this I would add:

const aKey = a.key.toLowerCase();
const bKey = b.key.toLowerCase();

and then use aKey and bKey in place of a.key and b.key

and above tree.walk on line 111 I would add:

const prefixes = getConfigValue('topTagPrefixes');
const topTagPrefixes = prefixes && prefixes.map(p => p.toLowerCase())`;

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 think that toLowerCase mapping can be done inside orderTags. It's a bit of performance loss but makes code more testable.

const aKey = a.key.toLowerCase();
const bKey = b.key.toLowerCase();

for (let i = 0; i < tp.length; i++) {
const p = tp[i];
if (aKey.startsWith(p) && !bKey.startsWith(p)) {
return -1;
}
if (!aKey.startsWith(p) && bKey.startsWith(p)) {
return 1;
}
}

if (aKey > bKey) {
return 1;
}
if (aKey < bKey) {
return -1;
}
return 0;
});

return orderedTags;
}

/**
* NOTE: Mutates `data` - Transform the HTTP response data into the form the app
* generally requires.
Expand Down Expand Up @@ -109,7 +142,7 @@ export default function transformTraceData(data: TraceData & { spans: SpanData[]
span.tags = span.tags || [];
span.references = span.references || [];
const tagsInfo = deduplicateTags(span.tags);
span.tags = tagsInfo.tags;
span.tags = orderTags(tagsInfo.tags, getConfigValue('topTagPrefixes'));
span.warnings = span.warnings.concat(tagsInfo.warnings);
span.references.forEach(ref => {
const refSpan = spanMap.get(ref.spanID) as Span;
Expand Down
1 change: 1 addition & 0 deletions packages/jaeger-ui/src/types/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type Config = {
menu: (ConfigMenuGroup | ConfigMenuItem)[];
search?: { maxLookback: { label: string; value: string } };
scripts?: TScript[];
topTagPrefixes?: string[];
tracking?: {
gaID: string | TNil;
trackErrors: boolean | TNil;
Expand Down