From 49b4cfe920adc53c62b1f4b8cec4580765e29084 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Thu, 17 Jun 2021 09:58:31 +0200 Subject: [PATCH] Added simple tracer --- .../cloud/sleuth/tracer/NoOpSpanInScope.java | 34 +++++ .../cloud/sleuth/tracer/NoOpTraceContext.java | 49 +++++++ .../tracer/SimpleCurrentTraceContext.java | 84 ++++++++++++ .../cloud/sleuth/tracer/SimpleSpan.java | 118 +++++++++++++++++ .../sleuth/tracer/SimpleSpanBuilder.java | 113 ++++++++++++++++ .../cloud/sleuth/tracer/SimpleTracer.java | 123 ++++++++++++++++++ 6 files changed, 521 insertions(+) create mode 100644 spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/NoOpSpanInScope.java create mode 100644 spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/NoOpTraceContext.java create mode 100644 spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/SimpleCurrentTraceContext.java create mode 100644 spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/SimpleSpan.java create mode 100644 spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/SimpleSpanBuilder.java create mode 100644 spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/SimpleTracer.java diff --git a/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/NoOpSpanInScope.java b/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/NoOpSpanInScope.java new file mode 100644 index 000000000..d629edc94 --- /dev/null +++ b/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/NoOpSpanInScope.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2021 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 + * + * https://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.tracer; + +import org.springframework.cloud.sleuth.Tracer; + +/** + * A noop implementation. Does nothing. + * + * @author Marcin Grzejszczak + * @since 3.0.4 + */ +public class NoOpSpanInScope implements Tracer.SpanInScope { + + @Override + public void close() { + + } + +} diff --git a/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/NoOpTraceContext.java b/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/NoOpTraceContext.java new file mode 100644 index 000000000..c4b992773 --- /dev/null +++ b/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/NoOpTraceContext.java @@ -0,0 +1,49 @@ +/* + * Copyright 2013-2021 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 + * + * https://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.tracer; + +import org.springframework.cloud.sleuth.TraceContext; + +/** + * A noop implementation. Does nothing. + * + * @author Marcin Grzejszczak + * @since 3.0.4 + */ +public class NoOpTraceContext implements TraceContext { + + @Override + public String traceId() { + return ""; + } + + @Override + public String parentId() { + return ""; + } + + @Override + public String spanId() { + return ""; + } + + @Override + public Boolean sampled() { + return false; + } + +} diff --git a/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/SimpleCurrentTraceContext.java b/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/SimpleCurrentTraceContext.java new file mode 100644 index 000000000..cc7713427 --- /dev/null +++ b/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/SimpleCurrentTraceContext.java @@ -0,0 +1,84 @@ +/* + * Copyright 2013-2021 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 + * + * https://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.tracer; + +import java.util.concurrent.Callable; +import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; + +import org.springframework.cloud.sleuth.CurrentTraceContext; +import org.springframework.cloud.sleuth.TraceContext; + +/** + * A noop implementation. Does nothing. + * + * @author Marcin Grzejszczak + * @since 3.0.4 + */ +public class SimpleCurrentTraceContext implements CurrentTraceContext { + + public TraceContext traceContext; + + @Override + public TraceContext context() { + return this.traceContext; + } + + @Override + public Scope newScope(TraceContext context) { + this.traceContext = context; + return () -> { + }; + } + + @Override + public Scope maybeScope(TraceContext context) { + this.traceContext = context; + return () -> { + }; + } + + @Override + public Callable wrap(Callable task) { + return task; + } + + @Override + public Runnable wrap(Runnable task) { + return task; + } + + @Override + public Executor wrap(Executor delegate) { + return delegate; + } + + @Override + public ExecutorService wrap(ExecutorService delegate) { + return delegate; + } + + public static SimpleCurrentTraceContext withTracer(SimpleTracer simpleTracer) { + return new SimpleCurrentTraceContext() { + @Override + public TraceContext context() { + return simpleTracer.currentSpan() != null ? simpleTracer.currentSpan().context() : null; + } + }; + } + +} diff --git a/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/SimpleSpan.java b/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/SimpleSpan.java new file mode 100644 index 000000000..29077e857 --- /dev/null +++ b/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/SimpleSpan.java @@ -0,0 +1,118 @@ +/* + * Copyright 2013-2021 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 + * + * https://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.tracer; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.springframework.cloud.sleuth.Span; +import org.springframework.cloud.sleuth.TraceContext; + +/** + * A noop implementation. Does nothing. + * + * @author Marcin Grzejszczak + * @since 3.0.4 + */ +public class SimpleSpan implements Span { + + public Map tags = new HashMap<>(); + + public boolean started; + + public boolean ended; + + public Throwable throwable; + + public String remoteServiceName; + + public Kind spanKind; + + public List events = new ArrayList<>(); + + public String name; + + public String ip; + + public int port; + + @Override + public boolean isNoop() { + return true; + } + + @Override + public TraceContext context() { + return new NoOpTraceContext(); + } + + @Override + public SimpleSpan start() { + this.started = true; + return this; + } + + @Override + public SimpleSpan name(String name) { + this.name = name; + return this; + } + + @Override + public SimpleSpan event(String value) { + this.events.add(value); + return this; + } + + @Override + public SimpleSpan tag(String key, String value) { + this.tags.put(key, value); + return this; + } + + @Override + public SimpleSpan error(Throwable throwable) { + this.throwable = throwable; + return this; + } + + @Override + public void end() { + this.ended = true; + } + + @Override + public void abandon() { + + } + + @Override + public Span remoteServiceName(String remoteServiceName) { + this.remoteServiceName = remoteServiceName; + return this; + } + + @Override + public String toString() { + return "SimpleSpan{" + "tags=" + tags + ", started=" + started + ", ended=" + ended + ", throwable=" + throwable + + ", remoteServiceName='" + remoteServiceName + '\'' + ", spanKind=" + spanKind + ", events=" + events + + ", name='" + name + '\'' + '}'; + } + +} diff --git a/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/SimpleSpanBuilder.java b/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/SimpleSpanBuilder.java new file mode 100644 index 000000000..ae160b945 --- /dev/null +++ b/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/SimpleSpanBuilder.java @@ -0,0 +1,113 @@ +/* + * Copyright 2013-2021 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 + * + * https://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.tracer; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.springframework.cloud.sleuth.Span; +import org.springframework.cloud.sleuth.TraceContext; + +/** + * A noop implementation. Does nothing. + * + * @author Marcin Grzejszczak + * @since 3.0.4 + */ +public class SimpleSpanBuilder implements Span.Builder { + + public List events = new ArrayList<>(); + + public Map tags = new HashMap<>(); + + public Throwable error; + + public Span.Kind spanKind; + + public String remoteServiceName; + + public String name; + + public SimpleTracer simpleTracer; + + public SimpleSpanBuilder(SimpleTracer simpleTracer) { + this.simpleTracer = simpleTracer; + } + + @Override + public Span.Builder setParent(TraceContext context) { + return this; + } + + @Override + public Span.Builder setNoParent() { + return this; + } + + @Override + public Span.Builder name(String name) { + this.name = name; + return this; + } + + @Override + public Span.Builder event(String value) { + this.events.add(value); + return this; + } + + @Override + public Span.Builder tag(String key, String value) { + this.tags.put(key, value); + return this; + } + + @Override + public Span.Builder error(Throwable throwable) { + this.error = throwable; + return this; + } + + @Override + public Span.Builder kind(Span.Kind spanKind) { + this.spanKind = spanKind; + return this; + } + + @Override + public Span.Builder remoteServiceName(String remoteServiceName) { + this.remoteServiceName = remoteServiceName; + return this; + } + + @Override + public Span start() { + SimpleSpan span = new SimpleSpan(); + this.tags.forEach(span::tag); + this.events.forEach(span::event); + span.remoteServiceName(this.remoteServiceName); + span.error(this.error); + span.spanKind = this.spanKind; + span.name(this.name); + span.start(); + simpleTracer.spans.add(span); + return span; + } + +} diff --git a/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/SimpleTracer.java b/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/SimpleTracer.java new file mode 100644 index 000000000..0999ee328 --- /dev/null +++ b/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/tracer/SimpleTracer.java @@ -0,0 +1,123 @@ +/* + * Copyright 2013-2021 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 + * + * https://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.tracer; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.assertj.core.api.BDDAssertions; + +import org.springframework.cloud.sleuth.BaggageInScope; +import org.springframework.cloud.sleuth.ScopedSpan; +import org.springframework.cloud.sleuth.Span; +import org.springframework.cloud.sleuth.SpanCustomizer; +import org.springframework.cloud.sleuth.TraceContext; +import org.springframework.cloud.sleuth.Tracer; + +/** + * A noop implementation. Does nothing. + * + * @author Marcin Grzejszczak + * @since 3.0.4 + */ +public class SimpleTracer implements Tracer { + + public List spans = new ArrayList<>(); + + @Override + public Span nextSpan(Span parent) { + return new SimpleSpan(); + } + + public SimpleSpan getOnlySpan() { + BDDAssertions.then(this.spans).hasSize(1); + SimpleSpan span = this.spans.get(0); + BDDAssertions.then(span.started).as("Span must be started").isTrue(); + BDDAssertions.then(span.ended).as("Span must be finished").isTrue(); + return span; + } + + public SimpleSpan getLastSpan() { + BDDAssertions.then(this.spans).isNotEmpty(); + SimpleSpan span = this.spans.get(this.spans.size() - 1); + BDDAssertions.then(span.started).as("Span must be started").isTrue(); + return span; + } + + @Override + public SpanInScope withSpan(Span span) { + return new NoOpSpanInScope(); + } + + @Override + public SpanCustomizer currentSpanCustomizer() { + return null; + } + + @Override + public Span currentSpan() { + if (this.spans.isEmpty()) { + return null; + } + return this.spans.get(spans.size() - 1); + } + + @Override + public SimpleSpan nextSpan() { + final SimpleSpan span = new SimpleSpan(); + this.spans.add(span); + return span; + } + + @Override + public ScopedSpan startScopedSpan(String name) { + return null; + } + + @Override + public Span.Builder spanBuilder() { + return new SimpleSpanBuilder(this); + } + + @Override + public Map getAllBaggage() { + return new HashMap<>(); + } + + @Override + public BaggageInScope getBaggage(String name) { + return null; + } + + @Override + public BaggageInScope getBaggage(TraceContext traceContext, String name) { + return null; + } + + @Override + public BaggageInScope createBaggage(String name) { + return null; + } + + @Override + public BaggageInScope createBaggage(String name, String value) { + return null; + } + +}