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

[APM] Fix service maps #192859

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function addMessagingConnections(
const newConnections = messagingDestinations
.map((node) => {
const matchedService = discoveredServices.find(
({ from }) =>
({ from, to }) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you using this to?

Copy link
Contributor

Choose a reason for hiding this comment

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

You're right, it's not used there

node[SPAN_DESTINATION_SERVICE_RESOURCE] === from[SPAN_DESTINATION_SERVICE_RESOURCE]
)?.to;
if (matchedService) {
Expand Down Expand Up @@ -86,9 +86,25 @@ export function getAllNodes(
return allNodes;
}

export function getServiceNodes(allNodes: ConnectionNode[]) {
export function getServiceNodes(
allNodes: ConnectionNode[],
discoveredServices: Array<{
from: ExternalConnectionNode;
to: ServiceConnectionNode;
}>
) {
const connectionFromDiscoveredServices = discoveredServices
.filter(({ from, to }) => {
return (
allNodes.some((node) => node.id === getConnectionNodeId(from)) &&
!allNodes.some((node) => node.id === to[SERVICE_NAME])
);
})
.map(({ to }) => ({ ...to }));
// List of nodes that are services
const serviceNodes = allNodes.filter((node) => SERVICE_NAME in node) as ServiceConnectionNode[];
const serviceNodes = [...allNodes, ...connectionFromDiscoveredServices].filter(
(node) => SERVICE_NAME in node
) as ServiceConnectionNode[];

return serviceNodes;
}
Expand All @@ -108,12 +124,16 @@ export function transformServiceMapResponses({
const { discoveredServices, services, connections, anomalies } = response;
const allConnections = addMessagingConnections(connections, discoveredServices);
const allNodes = getAllNodes(services, allConnections);
const serviceNodes = getServiceNodes(allNodes);
const serviceNodes = getServiceNodes(allNodes, discoveredServices);

// List of nodes that are externals
const externalNodes = allNodes.filter(
(node) => SPAN_DESTINATION_SERVICE_RESOURCE in node
) as ExternalConnectionNode[];
const externalNodes = [
...new Set(
allNodes.filter(
(node) => SPAN_DESTINATION_SERVICE_RESOURCE in node
) as ExternalConnectionNode[]
),
];
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const externalNodes = [
...new Set(
allNodes.filter(
(node) => SPAN_DESTINATION_SERVICE_RESOURCE in node
) as ExternalConnectionNode[]
),
];
const externalNodes = Array.from(new Set(
allNodes.filter(
(node) => SPAN_DESTINATION_SERVICE_RESOURCE in node
) as ExternalConnectionNode[]
)
);


// 1. Map external nodes to internal services
// 2. Collapse external nodes into one node based on span.destination.service.resource
Expand Down Expand Up @@ -202,9 +222,10 @@ export function transformServiceMapResponses({
})
.filter((connection) => connection.source !== connection.target);

const nodes = mappedConnections
.flatMap((connection) => [connection.sourceData, connection.targetData])
.concat(serviceNodes);
const nodes = mappedConnections.flatMap((connection) => [
connection.sourceData,
connection.targetData,
]);

const dedupedNodes: typeof nodes = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,13 @@ export default function serviceMapsApiTests({ getService }: FtrProviderContext)

it('returns the correct data', () => {
const elements: Array<{ data: Record<string, any> }> = response.body.elements;

const serviceNames = uniq(
elements
.filter((element) => element.data['service.name'] !== undefined)
.map((element) => element.data['service.name'])
).sort();

expectSnapshot(serviceNames).toMatchInline(`
Array [
"auditbeat",
"opbeans-dotnet",
"opbeans-go",
"opbeans-java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export default function ApiTest({ getService }: FtrProviderContext) {
}

registry.when('Service Map', { config: 'trial', archives: [] }, () => {
// FLAKY: https://github.com/elastic/kibana/issues/176982
describe('optional kuery param', () => {
before(async () => {
const events = timerange(start, end)
Expand Down