From 8f5f553376080c9e27d82f5f2e4a3a4b34f7d0de Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Wed, 10 Feb 2016 11:56:19 +0100 Subject: [PATCH] Removed unnecessary constructors --- .../springframework/cloud/sleuth/Span.java | 135 +++++++++++++++--- .../cloud/sleuth/SpanName.java | 7 +- .../sleuth/instrument/web/TraceFilter.java | 11 +- .../cloud/sleuth/SpanNameTest.java | 28 ++++ .../cloud/sleuth/SpanTest.java | 35 ++++- 5 files changed, 183 insertions(+), 33 deletions(-) diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/Span.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/Span.java index 309aad05c..c49283246 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/Span.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/Span.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2015 the original author or authors. + * Copyright 2013-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ package org.springframework.cloud.sleuth; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; @@ -26,9 +27,7 @@ import java.util.Map; import org.springframework.util.Assert; -import lombok.Builder; import lombok.Getter; -import lombok.Singular; /** * Class for gathering and reporting statistics about a block of execution. @@ -45,7 +44,6 @@ import lombok.Singular; * like scoped tracers. Sleuth spans are DTOs, whose sole responsibility is the current * span in the trace tree. */ -@Builder(toBuilder = true) @Getter public class Span { @@ -63,21 +61,15 @@ public class Span { private long end = 0; private final SpanName name; private final long traceId; - @Singular private List parents = new ArrayList<>(); private final long spanId; private boolean remote = false; private boolean exportable = true; private final Map tags = new LinkedHashMap<>(); private final String processId; - @Singular private final List logs = new ArrayList<>(); private final Span savedSpan; - public static Span.SpanBuilder builder() { - return new Span().toBuilder(); - } - public Span(Span current, Span savedSpan) { this.begin = current.getBegin(); this.end = current.getEnd(); @@ -114,15 +106,8 @@ public class Span { this.savedSpan = savedSpan; } - // for serialization - private Span() { - this.begin = 0; - this.name = SpanName.NO_NAME; - this.traceId = 0; - this.spanId = 0; - this.processId = null; - this.parents = new ArrayList<>(); - this.savedSpan = null; + public static SpanBuilder builder() { + return new SpanBuilder(); } /** @@ -326,4 +311,116 @@ public class Span { return false; return true; } + + public static class SpanBuilder { + private long begin; + private long end; + private SpanName name; + private long traceId; + private ArrayList parents = new ArrayList<>(); + private long spanId; + private boolean remote; + private boolean exportable = true; + private String processId; + private Span savedSpan; + private List logs = new ArrayList<>(); + private Map tags = new LinkedHashMap<>(); + + SpanBuilder() { + } + + public Span.SpanBuilder begin(long begin) { + this.begin = begin; + return this; + } + + public Span.SpanBuilder end(long end) { + this.end = end; + return this; + } + + public Span.SpanBuilder name(SpanName name) { + this.name = name; + return this; + } + + public Span.SpanBuilder traceId(long traceId) { + this.traceId = traceId; + return this; + } + + public Span.SpanBuilder parent(Long parent) { + this.parents.add(parent); + return this; + } + + public Span.SpanBuilder parents(Collection parents) { + this.parents.addAll(parents); + return this; + } + + public Span.SpanBuilder log(Log log) { + this.logs.add(log); + return this; + } + + public Span.SpanBuilder logs(Collection logs) { + this.logs.addAll(logs); + return this; + } + + public Span.SpanBuilder tag(String tagKey, String tagValue) { + this.tags.put(tagKey, tagValue); + return this; + } + + public Span.SpanBuilder tags(Map tags) { + this.tags.putAll(tags); + return this; + } + + public Span.SpanBuilder spanId(long spanId) { + this.spanId = spanId; + return this; + } + + public Span.SpanBuilder remote(boolean remote) { + this.remote = remote; + return this; + } + + public Span.SpanBuilder exportable(boolean exportable) { + this.exportable = exportable; + return this; + } + + public Span.SpanBuilder processId(String processId) { + this.processId = processId; + return this; + } + + public Span.SpanBuilder savedSpan(Span savedSpan) { + this.savedSpan = savedSpan; + return this; + } + + public Span build() { + Span span = new Span(this.begin, this.end, this.name, this.traceId, + this.parents, this.spanId, this.remote, this.exportable, + this.processId, this.savedSpan); + span.logs.addAll(this.logs); + span.tags.putAll(this.tags); + return span; + } + + public String toString() { + return "org.springframework.cloud.sleuth.Span.SpanBuilder(begin=" + this.begin + + ", end=" + this.end + ", name=" + this.name + ", traceId=" + + this.traceId + ", parents=" + this.parents + ", spanId=" + + this.spanId + ", remote=" + this.remote + ", exportable=" + + this.exportable + ", processId=" + this.processId + ", logs=" + + this.logs + ", tags=" + this.tags + ", savedSpan=" + this.savedSpan + + ")"; + } + } } diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/SpanName.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/SpanName.java index 37696da70..36381527e 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/SpanName.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/SpanName.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2015 the original author or authors. + * Copyright 2013-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,11 +48,6 @@ public class SpanName { public final String address; public final String fragment; - // serialization - SpanName() { - this("", "", ""); - } - public SpanName(String component, String address) { this(component, address, ""); } diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceFilter.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceFilter.java index 005953f65..200616e5d 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceFilter.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2015 the original author or authors. + * Copyright 2013-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,8 +15,6 @@ */ package org.springframework.cloud.sleuth.instrument.web; -import static org.springframework.util.StringUtils.hasText; - import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -46,6 +44,8 @@ import org.springframework.util.StringUtils; import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.util.UrlPathHelper; +import static org.springframework.util.StringUtils.hasText; + /** * Filter that takes the value of the {@link Span#SPAN_ID_NAME} and * {@link Span#TRACE_ID_NAME} header from either request or response and uses them to @@ -58,7 +58,7 @@ import org.springframework.web.util.UrlPathHelper; * * @see Tracer * @see TraceKeys - * @see TraceWebAutoConfiguration#traceFilter(TraceFilter) + * @see TraceWebAutoConfiguration#traceFilter * * @author Jakub Nabrdalik, 4financeIT * @author Tomasz Nurkiewicz, 4financeIT @@ -119,8 +119,7 @@ public class TraceFilter extends OncePerRequestFilter } String protocol = "http"; - String address = uri; - SpanName name = new SpanName(protocol, address); + SpanName name = new SpanName(protocol, uri); if (spanFromRequest == null) { if (hasHeader(request, response, Span.TRACE_ID_NAME)) { long traceId = Span diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/SpanNameTest.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/SpanNameTest.java index 4fc1215c7..82e958e14 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/SpanNameTest.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/SpanNameTest.java @@ -1,7 +1,26 @@ +/* + * Copyright 2013-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.cloud.sleuth; import org.junit.Test; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + import static org.springframework.cloud.sleuth.assertions.SleuthAssertions.then; /** @@ -48,4 +67,13 @@ public class SpanNameTest { .hasFragmentEqualTo("async:asd=123#4444"); } + @Test public void should_properly_serialize_object() throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + + String serializedName = objectMapper + .writeValueAsString(SpanName.fromString("async:foo#method=bar")); + + then(serializedName).isEqualTo( + "{\"component\":\"async\",\"address\":\"foo\",\"fragment\":\"method=bar\"}"); + } } \ No newline at end of file diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/SpanTest.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/SpanTest.java index d510155ca..04662a87a 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/SpanTest.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/SpanTest.java @@ -1,9 +1,28 @@ +/* + * Copyright 2013-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.cloud.sleuth; import java.util.Collections; import org.junit.Test; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + import static org.assertj.core.api.BDDAssertions.then; /** @@ -36,17 +55,29 @@ public class SpanTest { Span.fromHex(null); } - @Test(expected = UnsupportedOperationException.class) public void getAnnotationsReadOnly() { + @Test(expected = UnsupportedOperationException.class) + public void getAnnotationsReadOnly() { Span span = new Span(1, 2, new SpanName("http", "name"), 1L, Collections.emptyList(), 2L, true, true, "process"); span.tags().put("a", "b"); } - @Test(expected = UnsupportedOperationException.class) public void getTimelineAnnotationsReadOnly() { + @Test(expected = UnsupportedOperationException.class) + public void getTimelineAnnotationsReadOnly() { Span span = new Span(1, 2, new SpanName("http", "name"), 1L, Collections.emptyList(), 2L, true, true, "process"); span.logs().add(new Log(1, "1")); } + + @Test public void should_properly_serialize_object() throws JsonProcessingException { + Span span = new Span(1, 2, new SpanName("http", "name"), 1L, + Collections.emptyList(), 2L, true, true, "process"); + ObjectMapper objectMapper = new ObjectMapper(); + + String serializedName = objectMapper.writeValueAsString(span); + + then(serializedName).isNotEmpty(); + } } \ No newline at end of file