Adds RSocket instrumentation

coauthor @olegdokuka

fixes gh-1677
This commit is contained in:
Marcin Grzejszczak
2021-04-29 13:32:58 +02:00
parent ba8da46e20
commit 216d681b04
42 changed files with 2494 additions and 460 deletions

View File

@@ -41,4 +41,9 @@ public class SpanAndScope {
return this.scope;
}
@Override
public String toString() {
return "SpanAndScope{" + "span=" + this.span + '}';
}
}

View File

@@ -51,4 +51,47 @@ public interface TraceContext {
*/
Boolean sampled();
/**
* Builder for {@link TraceContext}.
*
* @since 3.1.0
*/
interface Builder {
/**
* Sets trace id on the trace context.
* @param traceId trace id
* @return this
*/
TraceContext.Builder traceId(String traceId);
/**
* Sets parent id on the trace context.
* @param parentId parent trace id
* @return this
*/
TraceContext.Builder parentId(String parentId);
/**
* Sets span id on the trace context.
* @param spanId span id
* @return this
*/
TraceContext.Builder spanId(String spanId);
/**
* Sets sampled on the trace context.
* @param sampled if span is sampled
* @return this
*/
TraceContext.Builder sampled(Boolean sampled);
/**
* Builds the trace context.
* @return trace context
*/
TraceContext build();
}
}

View File

@@ -134,6 +134,12 @@ public interface Tracer extends BaggageManager {
*/
Span.Builder spanBuilder();
/**
* Builder for {@link TraceContext}.
* @return a trace context builder
*/
TraceContext.Builder traceContextBuilder();
/**
* Allows to customize the current span in scope.
* @return current span customizer

View File

@@ -0,0 +1,91 @@
/*
* 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;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.lang.Nullable;
/**
* Represents a {@link Span} stored in thread local.
*
* @author Marcin Grzejszczak
* @since 3.1.0
*/
public interface WithThreadLocalSpan {
/**
* Logger.
*/
Log log = LogFactory.getLog(WithThreadLocalSpan.class);
/**
* Sets the span in thread local scope.
* @param span span to put in thread local
*/
default void setSpanInScope(Span span) {
getThreadLocalSpan().set(span);
if (log.isDebugEnabled()) {
log.debug("Put span in scope " + span);
}
}
/**
* Finishes the thread local span.
* @param error potential error to be stored in span
*/
default void finishSpan(@Nullable Throwable error) {
SpanAndScope spanAndScope = takeSpanFromThreadLocal();
if (spanAndScope == null) {
return;
}
Span span = spanAndScope.getSpan();
Tracer.SpanInScope scope = spanAndScope.getScope();
if (span.isNoop()) {
if (log.isDebugEnabled()) {
log.debug("Span " + span + " is noop - will stope the scope");
}
scope.close();
return;
}
if (error != null) { // an error occurred, adding error to span
span.error(error);
}
if (log.isDebugEnabled()) {
log.debug("Will finish the span and its corresponding scope " + span);
}
span.end();
scope.close();
}
/**
* Takes a span from thread local and restores the previous one if present.
* @return span from a thread local span
*/
default SpanAndScope takeSpanFromThreadLocal() {
SpanAndScope span = getThreadLocalSpan().get();
if (log.isDebugEnabled()) {
log.debug("Took span [" + span + "] from thread local");
}
getThreadLocalSpan().remove();
return span;
}
ThreadLocalSpan getThreadLocalSpan();
}