Skip to content

Commit

Permalink
Conform patterns, re #572
Browse files Browse the repository at this point in the history
  • Loading branch information
safris committed May 12, 2020
1 parent 771e5cf commit b84be2b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@

package io.opentracing.contrib.specialagent;

import io.opentracing.Scope;
import io.opentracing.Span;
import java.util.HashMap;
import java.util.Map;

import io.opentracing.Scope;
import io.opentracing.Span;

/**
* Thread local map holder for Span, Scope and counter to control stack of calls.
* Map is used to avoid suppressing of creation of new span when active span of another component exists.
* Key of the map is component name.
* Thread local map holder for Span, Scope and counter to control stack of
* calls. Map is used to avoid suppressing of creation of new span when active
* span of another component exists. Key of the map is component name.
*/
public class LocalSpanContext {
private static final ThreadLocal<Map<String, LocalSpanContext>> instance = new ThreadLocal<>();
private static final ThreadLocal<Map<String,LocalSpanContext>> instance = new ThreadLocal<>();

private final String name;
private final Span span;
private final Scope scope;
Expand All @@ -39,15 +41,14 @@ private LocalSpanContext(final String name, final Span span, final Scope scope)
}

public static LocalSpanContext get(final String name) {
final Map<String, LocalSpanContext> map = instance.get();
if (map != null)
return map.get(name);
return null;
final Map<String,LocalSpanContext> map = instance.get();
return map == null ? null : map.get(name);
}

public static void set(final String name, final Span span, final Scope scope) {
if (instance.get() == null)
instance.set(new HashMap<String, LocalSpanContext>());
instance.set(new HashMap<String,LocalSpanContext>());

instance.get().put(name, new LocalSpanContext(name, span, scope));
}

Expand All @@ -70,12 +71,13 @@ public void closeAndFinish() {
}

public void closeScope() {
final Map<String, LocalSpanContext> map = instance.get();
final Map<String,LocalSpanContext> map = instance.get();
if (map != null) {
map.remove(name);
if (map.isEmpty())
instance.remove();
}

if (scope != null)
scope.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,10 @@ public static void applyEnd(final Object thiz, final Object returned, final Thro
((Future<Result>)returned).onComplete(new Function1<Try<Result>,Object>() {
@Override
public Object apply(final Try<Result> response) {
if (response.isFailure()) {
if (response.isFailure())
OpenTracingApiUtil.setErrorTag(span, response.failed().get());
}
else {
else
span.setTag(Tags.HTTP_STATUS, response.get().header().status());
}

span.finish();
return null;
Expand Down

0 comments on commit b84be2b

Please sign in to comment.