Skip to content

Commit

Permalink
[APM] Don't mutating the original waterfall item while reparenting sp…
Browse files Browse the repository at this point in the history
…ans (elastic#65840) (elastic#66026)

* do not mutate the waterfall items

* addressing pr comments

Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
cauemarcondes and elasticmachine authored May 11, 2020
1 parent abe7b0f commit cc20632
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,28 +236,33 @@ const getWaterfallItems = (items: TraceAPIResponse['trace']['items']) =>
}
});

/**
* Changes the parent_id of items based on the child.id property.
* Solves the problem of Inferred spans that are created as child of trace spans
* when it actually should be its parent.
* @param waterfallItems
*/
const reparentSpans = (waterfallItems: IWaterfallItem[]) => {
function reparentSpans(waterfallItems: IWaterfallItem[]) {
// find children that needs to be re-parented and map them to their correct parent id
const childIdToParentIdMapping = Object.fromEntries(
flatten(
waterfallItems.map(waterfallItem => {
if (waterfallItem.docType === 'span') {
const childIds = waterfallItem.doc.child?.id ?? [];
return childIds.map(id => [id, waterfallItem.id]);
}
return [];
})
)
);

// update parent id for children that needs it or return unchanged
return waterfallItems.map(waterfallItem => {
if (waterfallItem.docType === 'span') {
const childId = waterfallItem.doc.child?.id;
if (childId) {
childId.forEach(id => {
const item = waterfallItems.find(_item => _item.id === id);
if (item) {
item.parentId = waterfallItem.id;
}
});
}
const newParentId = childIdToParentIdMapping[waterfallItem.id];
if (newParentId) {
return {
...waterfallItem,
parentId: newParentId
};
}

return waterfallItem;
});
};
}

const getChildrenGroupedByParentId = (waterfallItems: IWaterfallItem[]) =>
groupBy(waterfallItems, item => (item.parentId ? item.parentId : ROOT_ID));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2027,7 +2027,7 @@ export const inferredSpans = {
id: '41226ae63af4f235',
type: 'unknown'
},
child: { ids: ['8d80de06aa11a6fc'] }
child: { id: ['8d80de06aa11a6fc'] }
},
{
container: {
Expand Down

0 comments on commit cc20632

Please sign in to comment.