From 872a331dd9d74d66872f1518493fd32ad17ffdab Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Fri, 16 Mar 2018 17:43:16 +0800 Subject: [PATCH] Fixes endpoint formatting, notably undefined references This omits dummy port zero and undefined IPs when viewing span details --- zipkin-ui/js/component_ui/traceToMustache.js | 18 ++++++------- .../test/component_ui/traceToMustache.test.js | 25 +++++++++++++++++-- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/zipkin-ui/js/component_ui/traceToMustache.js b/zipkin-ui/js/component_ui/traceToMustache.js index 5baea9bb3db..3926faf5d74 100644 --- a/zipkin-ui/js/component_ui/traceToMustache.js +++ b/zipkin-ui/js/component_ui/traceToMustache.js @@ -79,17 +79,15 @@ function toSpanDepths(spans) { return treeDepths(entry, 1); } -export function formatEndpoint({ipv4, ipv6, port = 0, serviceName = ''}) { - if (serviceName) { - if (ipv6) { - return `[${ipv6}]:${port} (${serviceName})`; - } - return `${ipv4}:${port} (${serviceName})`; - } - if (ipv6) { - return `[${ipv6}]:${port}`; +export function formatEndpoint({ipv4, ipv6, port, serviceName}) { + if (ipv4 || ipv6) { + const ip = ipv6 ? `[${ipv6}]` : ipv4; // arbitrarily prefer ipv6 + const portString = port ? `:${port}` : ''; + const serviceNameString = serviceName ? ` (${serviceName})` : ''; + return ip + portString + serviceNameString; + } else { + return serviceName || ''; } - return `${ipv4}:${port}`; } export default function traceToMustache(trace, logsUrl = undefined) { diff --git a/zipkin-ui/test/component_ui/traceToMustache.test.js b/zipkin-ui/test/component_ui/traceToMustache.test.js index dd5be2729ae..e17e87c3737 100644 --- a/zipkin-ui/test/component_ui/traceToMustache.test.js +++ b/zipkin-ui/test/component_ui/traceToMustache.test.js @@ -198,20 +198,37 @@ describe('formatEndpoint', () => { formatEndpoint({ipv4: '150.151.152.153', port: 5000}).should.equal('150.151.152.153:5000'); }); - it('should use 0 as default port', () => { - formatEndpoint({ipv4: '150.151.152.153'}).should.equal('150.151.152.153:0'); + it('should not use port when missing or zero', () => { + formatEndpoint({ipv4: '150.151.152.153'}).should.equal('150.151.152.153'); + formatEndpoint({ipv4: '150.151.152.153', port: 0}).should.equal('150.151.152.153'); }); it('should put service name in parenthesis', () => { formatEndpoint({ipv4: '150.151.152.153', port: 9042, serviceName: 'cassandra'}).should.equal( '150.151.152.153:9042 (cassandra)' ); + formatEndpoint({ipv4: '150.151.152.153', serviceName: 'cassandra'}).should.equal( + '150.151.152.153 (cassandra)' + ); }); it('should not show empty service name', () => { formatEndpoint({ipv4: '150.151.152.153', port: 9042, serviceName: ''}).should.equal( '150.151.152.153:9042' ); + formatEndpoint({ipv4: '150.151.152.153', serviceName: ''}).should.equal( + '150.151.152.153' + ); + }); + + it('should show service name missing IP', () => { + formatEndpoint({serviceName: 'rabbit'}).should.equal( + 'rabbit' + ); + }); + + it('should not crash on no data', () => { + formatEndpoint({}).should.equal(''); }); it('should put ipv6 in brackets', () => { @@ -222,5 +239,9 @@ describe('formatEndpoint', () => { formatEndpoint({ipv6: '2001:db8::c001', port: 9042}).should.equal( '[2001:db8::c001]:9042' ); + + formatEndpoint({ipv6: '2001:db8::c001'}).should.equal( + '[2001:db8::c001]' + ); }); });