Skip to content

Commit

Permalink
Merge pull request #489 from nabam/sort-tags
Browse files Browse the repository at this point in the history
Sort span tags in alphabetical order
  • Loading branch information
everett980 committed Dec 10, 2019
2 parents 36ab156 + 129d8ac commit 1ee0cf1
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
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(
[
{ 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) => {
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

0 comments on commit 1ee0cf1

Please sign in to comment.