Skip to content

Commit

Permalink
Mapping fixes (#21)
Browse files Browse the repository at this point in the history
* Fix mapping

Add grouping of services
Decrease the number of services
Add annotations to tags
Show error icon when fault or throttle as well

* Some more fixes

Flatten arrays as well
Add metadata to tags
Show 0 duration when no end_time
Update tests

* Change flatten array key
  • Loading branch information
zoltanbedi authored Jul 28, 2020
1 parent 86d16d4 commit 3591b44
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 315 deletions.
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ export type XrayTraceDataSegmentDocument = {
id: string;
name: string;
start_time: number;
end_time: number;
end_time?: number;
in_progress?: boolean;
// Same as top level Id
trace_id: string;
subsegments?: XrayTraceDataSegmentDocument[];
Expand All @@ -158,4 +159,6 @@ export type XrayTraceDataSegmentDocument = {
throttle?: boolean;
http?: Http;
cause?: Cause;
annotations?: any;
metadata?: any;
};
35 changes: 25 additions & 10 deletions src/utils/flatten.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,42 @@ describe('flatten function', () => {
const awsResponse = {
http: {
request: {
url: 'http://3.23.148.72/signup',
method: 'POST',
user_agent:
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36',
client_ip: '80.98.253.126',
},
response: {
status: 409,
content_length: 0,
},
},
aws: {
resource_names: ['orders', 'products'],
},
metadata: {
http: {
dns: {
addresses: [
{
Zone: '',
IP: '4.2.123.160',
},
{
Zone: '',
IP: '22.23.14.122',
},
],
},
},
},
};

expect(flatten(awsResponse)).toEqual({
'aws.resource_names[0]': 'orders',
'aws.resource_names[1]': 'products',
'http.request.client_ip': '80.98.253.126',
'http.request.method': 'POST',
'http.request.url': 'http://3.23.148.72/signup',
'http.request.user_agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36',
'http.response.content_length': 0,
'http.response.status': 409,
'metadata.http.dns.addresses[0].IP': '4.2.123.160',
'metadata.http.dns.addresses[0].Zone': '',
'metadata.http.dns.addresses[1].IP': '22.23.14.122',
'metadata.http.dns.addresses[1].Zone': '',
});
});
});
30 changes: 16 additions & 14 deletions src/utils/flatten.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
// Copyright (c) 2014, Hugh Kennedy
// Based on code from https://github.com/hughsk/flat/blob/master/index.js
import { isPlainObject } from 'lodash';

interface Options {
delimiter?: string;
maxDepth?: number;
}

export function flatten(target: object, opts: Options = { delimiter: '.', maxDepth: 4 }): any {
let currentDepth = 1;
export function flatten(target: object): any {
const output: any = {};

function step(object: any, prev: string | null) {
Object.keys(object).forEach(key => {
const value = object[key];
const type = Object.prototype.toString.call(value);
const isObject = type === '[object Object]';
const newKey = prev ? prev + opts.delimiter + key : key;
const newKey = prev ? prev + '.' + key : key;

if (isObject && Object.keys(value).length && currentDepth < (opts.maxDepth ?? currentDepth + 1)) {
++currentDepth;
if (isPlainObject(value) && Object.keys(value).length) {
return step(value, newKey);
}

if (Array.isArray(value)) {
value.forEach((val, index) => {
const keyWithArray = `${newKey}[${index}]`;
if (isPlainObject(val)) {
step(val, keyWithArray);
} else {
output[keyWithArray] = val;
}
});
return;
}

output[newKey] = value;
});
}
Expand Down
Loading

0 comments on commit 3591b44

Please sign in to comment.