Skip to content

Commit

Permalink
Merge pull request #440 from ExpediaDotCom/service-tag-revert
Browse files Browse the repository at this point in the history
revert changes made for service tag serviceName override
  • Loading branch information
Jeff Baker authored Nov 29, 2018
2 parents 841b74f + f11c515 commit 0b7aa74
Show file tree
Hide file tree
Showing 4 changed files with 555 additions and 574 deletions.
139 changes: 69 additions & 70 deletions server/connectors/traces/haystack/search/searchResultsTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,114 +18,113 @@ const _ = require('lodash');
const transformer = {};

function calculateEndToEndDuration(spans) {
const startTime = spans
.map(span => span.startTime)
.reduce((earliest, cur) => Math.min(earliest, cur));
const endTime = spans
.map(span => (span.startTime + span.duration))
.reduce((latest, cur) => Math.max(latest, cur));

const difference = endTime - startTime;
return difference || 1;
const startTime = spans
.map(span => span.startTime)
.reduce((earliest, cur) => Math.min(earliest, cur));
const endTime = spans
.map(span => (span.startTime + span.duration))
.reduce((latest, cur) => Math.max(latest, cur));

const difference = endTime - startTime;
return difference || 1;
}

function calculateShadowDuration(spans) {
if (!spans.length) return 0;
if (!spans.length) return 0;

const shadows = _.flatMap(spans, span => [{time: span.startTime, value: 1}, {time: span.startTime + span.duration, value: -1}]);
const shadows = _.flatMap(spans, span => [{time: span.startTime, value: 1}, {time: span.startTime + span.duration, value: -1}]);

const sortedShadows = shadows.sort((a, b) => a.time - b.time);
const sortedShadows = shadows.sort((a, b) => a.time - b.time);

let runningCount = 0;
let lastStartTimestamp = sortedShadows[0].time;
let runningShadowDuration = 0;
let runningCount = 0;
let lastStartTimestamp = sortedShadows[0].time;
let runningShadowDuration = 0;

for (let i = 0; i < sortedShadows.length; i += 1) {
if (runningCount === 1 && sortedShadows[i].value === -1) {
runningShadowDuration += sortedShadows[i].time - lastStartTimestamp;
}
for (let i = 0; i < sortedShadows.length; i += 1) {
if (runningCount === 1 && sortedShadows[i].value === -1) {
runningShadowDuration += sortedShadows[i].time - lastStartTimestamp;
}

if (runningCount === 0 && sortedShadows[i].value === 1) {
lastStartTimestamp = sortedShadows[i].time;
}
if (runningCount === 0 && sortedShadows[i].value === 1) {
lastStartTimestamp = sortedShadows[i].time;
}

runningCount += sortedShadows[i].value;
}
runningCount += sortedShadows[i].value;
}

return runningShadowDuration;
return runningShadowDuration;
}

function findTag(tags, tagName) {
const foundTag = tags.find(tag => tag.key && tag.key.toLowerCase() === tagName);
return foundTag && foundTag.value;
const foundTag = tags.find(tag => tag.key && tag.key.toLowerCase() === tagName);
return foundTag && foundTag.value;
}

function isSpanError(span) {
return findTag(span.tags, 'error') === 'true' || findTag(span.tags, 'error') === true;
return findTag(span.tags, 'error') === 'true' || findTag(span.tags, 'error') === true;
}

function createServicesSummary(trace) {
const services = _.countBy(trace, span => findTag(span.tags, 'service') || span.serviceName);
const services = _.countBy(trace, span => span.serviceName);

return _.keys(services).map(service => ({
name: service,
spanCount: services[service]
}));
return _.keys(services).map(service => ({
name: service,
spanCount: services[service]
}));
}

function createQueriedServiceSummary(trace, serviceName, endToEndDuration) {
const serviceSpans = trace.filter(span => span.serviceName === serviceName);
const serviceSpans = trace.filter(span => span.serviceName === serviceName);

const serviceShadowDuration = calculateShadowDuration(serviceSpans);
const percent = Math.ceil((serviceShadowDuration / endToEndDuration) * 100);
const serviceShadowDuration = calculateShadowDuration(serviceSpans);
const percent = Math.ceil((serviceShadowDuration / endToEndDuration) * 100);

return serviceName && serviceSpans && {
return serviceName && serviceSpans && {
duration: serviceShadowDuration,
durationPercent: percent,
error: serviceSpans.some(span => isSpanError(span))
};
};
}

function createQueriedOperationSummary(trace, operationName, endToEndDuration) {
const operationSpans = trace.filter(span => operationName && span.operationName && (span.operationName.toLowerCase() === operationName.toLowerCase()));
const operationShadowDuration = calculateShadowDuration(operationSpans);
const percent = Math.floor((operationShadowDuration / endToEndDuration) * 100);
const operationSpans = trace.filter(span => operationName && span.operationName && (span.operationName.toLowerCase() === operationName.toLowerCase()));
const operationShadowDuration = calculateShadowDuration(operationSpans);
const percent = Math.floor((operationShadowDuration / endToEndDuration) * 100);

return operationName && operationSpans && {
return operationName && operationSpans && {
duration: operationShadowDuration,
durationPercent: percent,
error: operationSpans.some(span => isSpanError(span))
};
};
}

function toSearchResult(trace, query) {
const rootSpan = trace.find(span => !span.parentSpanId);
const serviceName = findTag(rootSpan.tags, 'service') || rootSpan.serviceName;
const root = {
url: findTag(rootSpan.tags, 'url') || '',
serviceName,
operationName: rootSpan.operationName,
duration: rootSpan.duration,
error: isSpanError(rootSpan)
};

const services = createServicesSummary(trace);

const endToEndDuration = calculateEndToEndDuration(trace);
const queriedService = createQueriedServiceSummary(trace, query.serviceName, endToEndDuration);
const queriedOperation = createQueriedOperationSummary(trace, query.operationName, endToEndDuration);

return {
traceId: rootSpan.traceId,
spanCount: trace.length,
errorSpanCount: trace.filter(span => isSpanError(span)).length,
services,
root,
queriedService,
queriedOperation,
startTime: rootSpan.startTime, // start time of the root span
duration: endToEndDuration // end-to-end duration
};
const rootSpan = trace.find(span => !span.parentSpanId);
const root = {
url: findTag(rootSpan.tags, 'url') || '',
serviceName: rootSpan.serviceName,
operationName: rootSpan.operationName,
duration: rootSpan.duration,
error: isSpanError(rootSpan)
};

const services = createServicesSummary(trace);

const endToEndDuration = calculateEndToEndDuration(trace);
const queriedService = createQueriedServiceSummary(trace, query.serviceName, endToEndDuration);
const queriedOperation = createQueriedOperationSummary(trace, query.operationName, endToEndDuration);

return {
traceId: rootSpan.traceId,
spanCount: trace.length,
errorSpanCount: trace.filter(span => isSpanError(span)).length,
services,
root,
queriedService,
queriedOperation,
startTime: rootSpan.startTime, // start time of the root span
duration: endToEndDuration // end-to-end duration
};
}

transformer.transform = (traces, query) => traces.map(trace => toSearchResult(trace, query));
Expand Down
Loading

0 comments on commit 0b7aa74

Please sign in to comment.