Skip to content

Commit

Permalink
Add span.type/span.subtype do external nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar authored and ogupte committed Jan 13, 2020
1 parent b02b28c commit 3ccffad
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 25 deletions.
2 changes: 2 additions & 0 deletions x-pack/legacy/plugins/apm/common/service_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export interface ServiceConnectionNode {
}
export interface ExternalConnectionNode {
'destination.address': string;
'span.type': string;
'span.subtype': string;
}

export type ConnectionNode = ServiceConnectionNode | ExternalConnectionNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { ValuesType } from 'utility-types';
import { sortBy } from 'lodash';
import {
Connection,
ConnectionNode,
ServiceConnectionNode
} from '../../../../common/service_map';
import { sortBy, isEqual } from 'lodash';
import { Connection, ConnectionNode } from '../../../../common/service_map';
import { ServiceMapAPIResponse } from '../../../../server/lib/service_map/get_service_map';
import { getAPMHref } from '../../shared/Links/apm/APMLink';

Expand All @@ -30,12 +26,9 @@ export function getCytoscapeElements(
responses: ServiceMapAPIResponse[],
search: string
) {
const destMap = responses.reduce((prev, response) => {
return {
...prev,
...response.destinationMap
};
}, {} as Record<string, ServiceConnectionNode>);
const discoveredServices = responses.flatMap(
response => response.discoveredServices
);

const serviceNodes = responses
.flatMap(response => response.services)
Expand All @@ -46,9 +39,15 @@ export function getCytoscapeElements(

// maps destination.address to service.name if possible
function getConnectionNode(node: ConnectionNode) {
const mappedNode =
('destination.address' in node && destMap[node['destination.address']]) ||
node;
let mappedNode: ConnectionNode | undefined;

if ('destination.address' in node) {
mappedNode = discoveredServices.find(map => isEqual(map.from, node))?.to;
}

if (!mappedNode) {
mappedNode = node;
}

return {
...mappedNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async function getConnectionData({
environment,
traceIds
})
: { connections: [], destinationMap: {} };
: { connections: [], discoveredServices: [] };

return {
after: nextAfter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
import {
Connection,
ServiceConnectionNode,
ConnectionNode
ConnectionNode,
ExternalConnectionNode
} from '../../../common/service_map';

export async function getServiceMapFromTraceIds({
Expand Down Expand Up @@ -66,6 +67,7 @@ export async function getServiceMapFromTraceIds({
'trace.id',
'processor.event',
'span.type',
'span.subtype',
'agent.name'
};
state.fieldsToCopy = fieldsToCopy;`
Expand Down Expand Up @@ -96,7 +98,16 @@ export async function getServiceMapFromTraceIds({
},
reduce_script: {
lang: 'painless',
source: `def processAndReturnEvent(def context, def eventId) {
source: `
def getDestination ( def event ) {
def destination = new HashMap();
destination['destination.address'] = event['destination.address'];
destination['span.type'] = event['span.type'];
destination['span.subtype'] = event['span.subtype'];
return destination;
}
def processAndReturnEvent(def context, def eventId) {
if (context.processedEvents[eventId] != null) {
return context.processedEvents[eventId];
}
Expand Down Expand Up @@ -135,7 +146,8 @@ export async function getServiceMapFromTraceIds({
|| parent['service.environment'] != event['service.environment']
)
) {
context.externalToServiceMap[parent['destination.address']] = service;
def parentDestination = getDestination(parent);
context.externalToServiceMap.put(parentDestination, service);
}
}
}
Expand All @@ -151,8 +163,7 @@ export async function getServiceMapFromTraceIds({
/* if there is an outgoing span, create a new path */
if (event['destination.address'] != null && event['destination.address'] != '') {
def outgoingLocation = new HashMap();
outgoingLocation['destination.address'] = event['destination.address'];
def outgoingLocation = getDestination(event);
def outgoingPath = new ArrayList(basePath);
outgoingPath.add(outgoingLocation);
context.paths.add(outgoingPath);
Expand Down Expand Up @@ -191,8 +202,17 @@ export async function getServiceMapFromTraceIds({
def response = new HashMap();
response.paths = paths;
response.externalToServiceMap = context.externalToServiceMap;
def discoveredServices = new HashSet();
for(entry in context.externalToServiceMap.entrySet()) {
def map = new HashMap();
map.from = entry.getKey();
map.to = entry.getValue();
discoveredServices.add(map);
}
response.discoveredServices = discoveredServices;
return response;`
}
}
Expand All @@ -205,7 +225,10 @@ export async function getServiceMapFromTraceIds({

const scriptResponse = serviceMapResponse.aggregations?.service_map.value as {
paths: ConnectionNode[][];
externalToServiceMap: Record<string, ServiceConnectionNode>;
discoveredServices: Array<{
from: ExternalConnectionNode;
to: ServiceConnectionNode;
}>;
};

let paths = scriptResponse.paths;
Expand Down Expand Up @@ -251,6 +274,6 @@ export async function getServiceMapFromTraceIds({

return {
connections,
destinationMap: scriptResponse.externalToServiceMap
discoveredServices: scriptResponse.discoveredServices
};
}

0 comments on commit 3ccffad

Please sign in to comment.