Added simple tracer

This commit is contained in:
Marcin Grzejszczak
2021-06-17 09:58:31 +02:00
parent 81322756a4
commit 49b4cfe920
6 changed files with 521 additions and 0 deletions

View File

@@ -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() {
}
}

View File

@@ -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;
}
}

View File

@@ -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 <C> Callable<C> wrap(Callable<C> 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;
}
};
}
}

View File

@@ -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<String, String> tags = new HashMap<>();
public boolean started;
public boolean ended;
public Throwable throwable;
public String remoteServiceName;
public Kind spanKind;
public List<String> 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 + '\'' + '}';
}
}

View File

@@ -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<String> events = new ArrayList<>();
public Map<String, String> 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;
}
}

View File

@@ -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<SimpleSpan> 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<String, String> 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;
}
}