<commit_before>package io.opentracing.contrib.jaxrs2.server;

import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.contrib.jaxrs2.internal.CastUtils;
import io.opentracing.contrib.jaxrs2.internal.SpanWrapper;
import io.opentracing.tag.Tags;
import io.opentracing.util.GlobalTracer;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Filter which finishes span after server processing. It is required to be registered.
 *
 * @author <NAME>
 */
@WebFilter(urlPatterns = "/*", asyncSupported = true)
public class SpanFinishingFilter implements Filter {

  private final Tracer tracer;

  public SpanFinishingFilter() {
    this(GlobalTracer.get());
  }

  public SpanFinishingFilter(Tracer tracer){
    this.tracer = tracer;
  }

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {

    HttpServletResponse httpResponse = (HttpServletResponse)response;
    HttpServletRequest httpRequest = (HttpServletRequest)request;

    try {
      chain.doFilter(request, response);
    } catch (Exception ex) {
      SpanWrapper spanWrapper = getSpanWrapper(httpRequest);
      if (spanWrapper != null) {
        Tags.HTTP_STATUS.set(spanWrapper.get(), httpResponse.getStatus());
        addExceptionLogs(spanWrapper.get(), ex);
        throw ex;
      }
    } finally {
      Scope scope = tracer.scopeManager().active();
      if (scope != null) {
        scope.close();
      }
      SpanWrapper spanWrapper = getSpanWrapper(httpRequest);
      if (spanWrapper != null) {
        if (request.isAsyncStarted()) {
          request.getAsyncContext().addListener(new SpanFinisher(spanWrapper), request, response);
        } else {
          spanWrapper.finish();
        }
      }
    }
  }
<commit_msg>SpanFinishingFilter should always re-throw the caught exception (#75)<commit_after>package io.opentracing.contrib.jaxrs2.server;

import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.contrib.jaxrs2.internal.CastUtils;
import io.opentracing.contrib.jaxrs2.internal.SpanWrapper;
import io.opentracing.tag.Tags;
import io.opentracing.util.GlobalTracer;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Filter which finishes span after server processing. It is required to be registered.
 *
 * @author <NAME>
 */
@WebFilter(urlPatterns = "/*", asyncSupported = true)
public class SpanFinishingFilter implements Filter {

  private final Tracer tracer;

  public SpanFinishingFilter() {
    this(GlobalTracer.get());
  }

  public SpanFinishingFilter(Tracer tracer){
    this.tracer = tracer;
  }

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {

    HttpServletResponse httpResponse = (HttpServletResponse)response;
    HttpServletRequest httpRequest = (HttpServletRequest)request;

    try {
      chain.doFilter(request, response);
    } catch (Exception ex) {
      SpanWrapper spanWrapper = getSpanWrapper(httpRequest);
      if (spanWrapper != null) {
        Tags.HTTP_STATUS.set(spanWrapper.get(), httpResponse.getStatus());
        addExceptionLogs(spanWrapper.get(), ex);
      }
      throw ex;
    } finally {
      Scope scope = tracer.scopeManager().active();
      if (scope != null) {
        scope.close();
      }
      SpanWrapper spanWrapper = getSpanWrapper(httpRequest);
      if (spanWrapper != null) {
        if (request.isAsyncStarted()) {
          request.getAsyncContext().addListener(new SpanFinisher(spanWrapper), request, response);
        } else {
          spanWrapper.finish();
        }
      }
    }
  }

{'fraction_non_alphanumeric': 0.1004983388704319, 'fraction_numerical': 0.0016611295681063123, 'mean_word_length': 4.075869336143309, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5715667', 'n_tokens_mistral': 1351, 'n_tokens_neox': 1353, 'n_words': 295}