|
| 1 | +// Copyright (c) 2017 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import { isEqual as _isEqual } from 'lodash'; |
| 16 | + |
| 17 | +import { getTraceSpanIdsAsTree } from '../selectors/trace'; |
| 18 | +import { TraceKeyValuePair, TraceSpan, Trace, TraceResponse, TraceProcess } from '../types'; |
| 19 | +import TreeNode from '../utils/TreeNode'; |
| 20 | +import { getConfigValue } from '../utils/config/get-config'; |
| 21 | + |
| 22 | +import { getTraceName } from './trace-viewer'; |
| 23 | + |
| 24 | +function deduplicateTags(tags: TraceKeyValuePair[]) { |
| 25 | + const warningsHash: Map<string, string> = new Map<string, string>(); |
| 26 | + const dedupedTags: TraceKeyValuePair[] = tags.reduce<TraceKeyValuePair[]>((uniqueTags, tag) => { |
| 27 | + if (!uniqueTags.some((t) => t.key === tag.key && t.value === tag.value)) { |
| 28 | + uniqueTags.push(tag); |
| 29 | + } else { |
| 30 | + warningsHash.set(`${tag.key}:${tag.value}`, `Duplicate tag "${tag.key}:${tag.value}"`); |
| 31 | + } |
| 32 | + return uniqueTags; |
| 33 | + }, []); |
| 34 | + const warnings = Array.from(warningsHash.values()); |
| 35 | + return { dedupedTags, warnings }; |
| 36 | +} |
| 37 | + |
| 38 | +function orderTags(tags: TraceKeyValuePair[], topPrefixes?: string[]) { |
| 39 | + const orderedTags: TraceKeyValuePair[] = tags?.slice() ?? []; |
| 40 | + const tp = (topPrefixes || []).map((p: string) => p.toLowerCase()); |
| 41 | + |
| 42 | + orderedTags.sort((a, b) => { |
| 43 | + const aKey = a.key.toLowerCase(); |
| 44 | + const bKey = b.key.toLowerCase(); |
| 45 | + |
| 46 | + for (let i = 0; i < tp.length; i++) { |
| 47 | + const p = tp[i]; |
| 48 | + if (aKey.startsWith(p) && !bKey.startsWith(p)) { |
| 49 | + return -1; |
| 50 | + } |
| 51 | + if (!aKey.startsWith(p) && bKey.startsWith(p)) { |
| 52 | + return 1; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (aKey > bKey) { |
| 57 | + return 1; |
| 58 | + } |
| 59 | + if (aKey < bKey) { |
| 60 | + return -1; |
| 61 | + } |
| 62 | + return 0; |
| 63 | + }); |
| 64 | + |
| 65 | + return orderedTags; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * NOTE: Mutates `data` - Transform the HTTP response data into the form the app |
| 70 | + * generally requires. |
| 71 | + */ |
| 72 | +export default function transformTraceData(data: TraceResponse | undefined): Trace | null { |
| 73 | + if (!data?.traceID) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + const traceID = data.traceID.toLowerCase(); |
| 77 | + |
| 78 | + let traceEndTime = 0; |
| 79 | + let traceStartTime = Number.MAX_SAFE_INTEGER; |
| 80 | + const spanIdCounts = new Map(); |
| 81 | + const spanMap = new Map<string, TraceSpan>(); |
| 82 | + // filter out spans with empty start times |
| 83 | + // eslint-disable-next-line no-param-reassign |
| 84 | + data.spans = data.spans.filter((span) => Boolean(span.startTime)); |
| 85 | + |
| 86 | + // Sort process tags |
| 87 | + data.processes = Object.entries(data.processes).reduce<Record<string, TraceProcess>>((processes, [id, process]) => { |
| 88 | + processes[id] = { |
| 89 | + ...process, |
| 90 | + tags: orderTags(process.tags), |
| 91 | + }; |
| 92 | + return processes; |
| 93 | + }, {}); |
| 94 | + |
| 95 | + const max = data.spans.length; |
| 96 | + for (let i = 0; i < max; i++) { |
| 97 | + const span: TraceSpan = data.spans[i] as TraceSpan; |
| 98 | + const { startTime, duration, processID } = span; |
| 99 | + |
| 100 | + let spanID = span.spanID; |
| 101 | + // check for start / end time for the trace |
| 102 | + if (startTime < traceStartTime) { |
| 103 | + traceStartTime = startTime; |
| 104 | + } |
| 105 | + if (startTime + duration > traceEndTime) { |
| 106 | + traceEndTime = startTime + duration; |
| 107 | + } |
| 108 | + // make sure span IDs are unique |
| 109 | + const idCount = spanIdCounts.get(spanID); |
| 110 | + if (idCount != null) { |
| 111 | + // eslint-disable-next-line no-console |
| 112 | + console.warn(`Dupe spanID, ${idCount + 1} x ${spanID}`, span, spanMap.get(spanID)); |
| 113 | + if (_isEqual(span, spanMap.get(spanID))) { |
| 114 | + // eslint-disable-next-line no-console |
| 115 | + console.warn('\t two spans with same ID have `isEqual(...) === true`'); |
| 116 | + } |
| 117 | + spanIdCounts.set(spanID, idCount + 1); |
| 118 | + spanID = `${spanID}_${idCount}`; |
| 119 | + span.spanID = spanID; |
| 120 | + } else { |
| 121 | + spanIdCounts.set(spanID, 1); |
| 122 | + } |
| 123 | + span.process = data.processes[processID]; |
| 124 | + spanMap.set(spanID, span); |
| 125 | + } |
| 126 | + // tree is necessary to sort the spans, so children follow parents, and |
| 127 | + // siblings are sorted by start time |
| 128 | + const tree = getTraceSpanIdsAsTree(data, spanMap); |
| 129 | + const spans: TraceSpan[] = []; |
| 130 | + const svcCounts: Record<string, number> = {}; |
| 131 | + |
| 132 | + tree.walk((spanID: string, node: TreeNode<string>, depth = 0) => { |
| 133 | + if (spanID === '__root__') { |
| 134 | + return; |
| 135 | + } |
| 136 | + if (typeof spanID !== 'string') { |
| 137 | + return; |
| 138 | + } |
| 139 | + const span = spanMap.get(spanID); |
| 140 | + if (!span) { |
| 141 | + return; |
| 142 | + } |
| 143 | + const { serviceName } = span.process; |
| 144 | + svcCounts[serviceName] = (svcCounts[serviceName] || 0) + 1; |
| 145 | + span.relativeStartTime = span.startTime - traceStartTime; |
| 146 | + span.depth = depth - 1; |
| 147 | + span.hasChildren = node.children.length > 0; |
| 148 | + span.childSpanCount = node.children.length; |
| 149 | + span.warnings = span.warnings || []; |
| 150 | + span.tags = span.tags || []; |
| 151 | + span.references = span.references || []; |
| 152 | + |
| 153 | + span.childSpanIds = node.children |
| 154 | + .slice() |
| 155 | + .sort((a, b) => { |
| 156 | + const spanA = spanMap.get(a.value)!; |
| 157 | + const spanB = spanMap.get(b.value)!; |
| 158 | + return spanB.startTime + spanB.duration - (spanA.startTime + spanA.duration); |
| 159 | + }) |
| 160 | + .map((each) => each.value); |
| 161 | + |
| 162 | + const tagsInfo = deduplicateTags(span.tags); |
| 163 | + span.tags = orderTags(tagsInfo.dedupedTags, getConfigValue('topTagPrefixes')); |
| 164 | + span.warnings = span.warnings.concat(tagsInfo.warnings); |
| 165 | + span.references.forEach((ref, index) => { |
| 166 | + const refSpan = spanMap.get(ref.spanID); |
| 167 | + if (refSpan) { |
| 168 | + // eslint-disable-next-line no-param-reassign |
| 169 | + ref.span = refSpan; |
| 170 | + if (index > 0) { |
| 171 | + // Don't take into account the parent, just other references. |
| 172 | + refSpan.subsidiarilyReferencedBy = refSpan.subsidiarilyReferencedBy || []; |
| 173 | + refSpan.subsidiarilyReferencedBy.push({ |
| 174 | + spanID, |
| 175 | + traceID, |
| 176 | + span, |
| 177 | + refType: ref.refType, |
| 178 | + }); |
| 179 | + } |
| 180 | + } |
| 181 | + }); |
| 182 | + spans.push(span); |
| 183 | + }); |
| 184 | + const traceName = getTraceName(spans); |
| 185 | + const services = Object.keys(svcCounts).map((name) => ({ name, numberOfSpans: svcCounts[name] })); |
| 186 | + return { |
| 187 | + services, |
| 188 | + spans, |
| 189 | + traceID, |
| 190 | + traceName, |
| 191 | + // can't use spread operator for intersection types |
| 192 | + // repl: https://goo.gl/4Z23MJ |
| 193 | + // issue: https://github.com/facebook/flow/issues/1511 |
| 194 | + processes: data.processes, |
| 195 | + duration: traceEndTime - traceStartTime, |
| 196 | + startTime: traceStartTime, |
| 197 | + endTime: traceEndTime, |
| 198 | + }; |
| 199 | +} |
0 commit comments