Skip to content
Permalink
Browse files
avoid double tracing + workaround for exceptions without status
  • Loading branch information
rmannibucau committed May 20, 2018
1 parent cb6a989 commit e7ba77a2147a578e51919054214e6d3cf704308a
Showing 4 changed files with 54 additions and 2 deletions.
@@ -0,0 +1,50 @@
/*
* 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 javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.WithAnnotations;
import javax.enterprise.inject.spi.configurator.AnnotatedTypeConfigurator;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.Path;

import org.eclipse.microprofile.opentracing.Traced;

public class OpenTracingExtension implements Extension {
<T> void removeTracedFromJaxRsEndpoints(@Observes @WithAnnotations(Traced.class) final ProcessAnnotatedType<T> pat) {
if (isJaxRs(pat.getAnnotatedType())) { // we have filters with more accurate timing
final AnnotatedTypeConfigurator<T> configurator = pat.configureAnnotatedType();
configurator.remove(it -> it.annotationType() == Traced.class);
configurator.methods().stream()
.filter(m -> isJaxRs(m.getAnnotated()))
.forEach(m -> m.remove(it -> it.annotationType() == Traced.class));
}
}

private <T> boolean isJaxRs(final AnnotatedType<T> annotatedType) {
return annotatedType.getAnnotations().stream().anyMatch(it -> it.annotationType() == Path.class) ||
annotatedType.getMethods().stream().anyMatch(this::isJaxRs);
}

private <T> boolean isJaxRs(final AnnotatedMethod<? super T> m) {
return m.getAnnotations().stream().anyMatch(it -> it.annotationType().isAnnotationPresent(HttpMethod.class));
}
}
@@ -41,7 +41,8 @@ public void doFilter(final ServletRequest request, final ServletResponse respons
chain.doFilter(request, response);
} catch (final Exception ex) {
ofNullable(request.getAttribute(OpenTracingFilter.class.getName())).map(Span.class::cast).ifPresent(span -> {
Tags.HTTP_STATUS.set(span, HttpServletResponse.class.cast(response).getStatus());
final int status = HttpServletResponse.class.cast(response).getStatus();
Tags.HTTP_STATUS.set(span, status == HttpServletResponse.SC_OK ? HttpServletResponse.SC_INTERNAL_SERVER_ERROR : status);
Tags.ERROR.set(span, true);
span.log(new HashMap<String, Object>() {

@@ -0,0 +1 @@
org.apache.geronimo.microprofile.opentracing.microprofile.cdi.OpenTracingExtension
@@ -20,7 +20,7 @@
<classes>
<class name="org.eclipse.microprofile.opentracing.tck.OpentracingClientTests">
<methods>
<include name="testAnnotations" />
<include name="testException" />
</methods>
</class>
</classes>

0 comments on commit e7ba77a

Please sign in to comment.