Skip to content
Permalink
Browse files
wiring threadlocals and fixing some lifecycles
  • Loading branch information
rmannibucau committed May 19, 2018
1 parent f6cc6b3 commit 231b5044e71fab03cccb7672ef3dfbdf72df9492
Showing 6 changed files with 107 additions and 11 deletions.
@@ -31,11 +31,13 @@ public class ScopeManagerImpl implements ScopeManager {
public Scope activate(final Span span, final boolean finishSpanOnClose) {
final Thread thread = Thread.currentThread();
final Scope oldScope = current.get();
return new ScopeImpl(() -> {
final ScopeImpl newScope = new ScopeImpl(() -> {
if (Thread.currentThread() == thread) {
current.set(oldScope);
} // else error?
}, span, finishSpanOnClose);
current.set(newScope);
return newScope;
}

@Override
@@ -110,8 +110,8 @@ public Tracer.SpanBuilder withStartTimestamp(final long microseconds) {
}

@Override
public Scope startActive(boolean finishSpanOnClose) {
return new ScopeImpl(null, startManual(), finishSpanOnClose);
public Scope startActive(final boolean finishSpanOnClose) {
return tracer.scopeManager().activate(startManual(), finishSpanOnClose);
}

@Override
@@ -60,8 +60,9 @@ public SpanImpl(final String operationName, final long startTimestamp, final Col

@Override
public Span log(final long timestampMicroseconds, final Map<String, ?> fields) {
final Log log = new Log(timestampMicroseconds, fields);
synchronized (logs) {
logs.add(new Log(timestampMicroseconds, fields));
logs.add(log);
}
return this;
}
@@ -149,7 +150,7 @@ public String operationName() {

@Deprecated // TCK compat
public Object parentId() {
return parentId;
return parentId == null ? 0L : parentId;
}

@Deprecated // TCK compat
@@ -0,0 +1,98 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.geronimo.microprofile.opentracing.microprofile.cdi;

import static java.util.Objects.requireNonNull;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.annotation.Priority;
import javax.enterprise.inject.Intercepted;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

import org.eclipse.microprofile.opentracing.Traced;

import io.opentracing.Scope;
import io.opentracing.Tracer;

@Traced
@Interceptor
@Priority(Interceptor.Priority.LIBRARY_BEFORE)
public class TracedInterceptor implements Serializable {
@Inject
private Tracer tracer;

@Inject
@Intercepted
private Bean<?> bean;

@Inject
private BeanManager beanManager;

private transient ConcurrentMap<Method, Meta> metas = new ConcurrentHashMap<>();

@AroundInvoke
public Object trace(final InvocationContext context) throws Exception {
final Method method = context.getMethod();
Meta meta = metas.get(method);
if (meta == null) {
final AnnotatedType<?> annotatedType = beanManager.createAnnotatedType(bean.getBeanClass());
final Traced traced = requireNonNull(annotatedType.getMethods().stream()
.filter(m -> m.getJavaMember().equals(method))
.findFirst().map(m -> m.getAnnotation(Traced.class))
.orElseGet(() -> annotatedType.getAnnotation(Traced.class)), "no @Traced found on " + method);
meta = new Meta(
traced.value(),
Optional.of(traced.operationName())
.filter(v -> !v.isEmpty())
.orElseGet(() -> method.getDeclaringClass().getName() + "." + method.getName()));
metas.putIfAbsent(method, meta); // no big deal to not use the same meta instance
}
if (!meta.traced) {
return context.proceed();
}

final Tracer.SpanBuilder spanBuilder = tracer.buildSpan(meta.operationName);
final Scope parent = tracer.scopeManager().active();
if (parent != null) {
spanBuilder.asChildOf(parent.span());
}
try (final Scope scope = spanBuilder.startActive(true)) {
return context.proceed();
}
}

private static class Meta {
private final boolean traced;
private final String operationName;

private Meta(final boolean traced, final String name) {
this.traced = traced;
this.operationName = name;
}
}
}
@@ -18,7 +18,6 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.tag.Tags;
@@ -53,7 +52,6 @@ public void doFilter(final ServletRequest request, final ServletResponse respons
});
throw ex;
} finally {
ofNullable(tracer.scopeManager().active()).ifPresent(Scope::close);
ofNullable(request.getAttribute(OpenTracingFilter.class.getName())).map(Span.class::cast).ifPresent(span -> {
if (request.isAsyncStarted()) {
request.getAsyncContext().addListener(new AsyncListener() {
@@ -54,10 +54,7 @@ public void filter(final ContainerRequestContext context) {
.orElseGet(() -> tracer.extract(Format.Builtin.HTTP_HEADERS, new HeaderTextMap<>(context.getHeaders()))))
.ifPresent(builder::asChildOf);

final Span span = builder.startActive(false/* filter does */).span();
if (span == null) {
return;
}
final Span span = builder.startActive(true).span();
if (!"true".equalsIgnoreCase(
String.valueOf(context.getProperty("org.apache.geronimo.microprofile.opentracing.server.skipDefaultSpanTags")))) {
Tags.HTTP_METHOD.set(span, context.getMethod());

0 comments on commit 231b504

Please sign in to comment.