<commit_before>package com.github.sebruck.opencensus.akka.http

import akka.http.scaladsl.model.HttpRequest
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{Directive1, ExceptionHandler}
import com.github.sebruck.opencensus.Tracing
import com.github.sebruck.opencensus.akka.http.propagation.B3FormatPropagation
import com.typesafe.scalalogging.LazyLogging
import io.opencensus.trace.{Span, Status}

import scala.util.control.NonFatal

trait TracingDirective extends LazyLogging {

  protected def tracing: Tracing
  protected def propagation: Propagation

  /**
    * Starts a new span and sets a parent context if the request contains valid headers in the b3 format.
    * The span is ended when the request compeltes or fails with a status code which is suitable
    * to the http response code
    */
  val traceRequest: Directive1[Span] =
    extractRequest.flatMap { req =>
      val span = buildSpan(req)
      recordSuccess(span) & recordException(span) & provide(span)
    }

  private def buildSpan(req: HttpRequest): Span = {
    val name = req.uri.path.toString()

    propagation
      .extractContext(req)
      .fold(
        { error =>
          logger.warn("Extracting of parent context failed", error)
          tracing.startSpan(name)
        },
        tracing.startSpanWithRemoteParent(name, _)
      )
  }

  private def recordSuccess(span: Span) = mapResponse { response =>
    tracing.endSpan(span, StatusTranslator.translate(response.status))
    response
  }

  private def recordException(span: Span) =
    handleExceptions(ExceptionHandler {
      case NonFatal(ex) =>
        tracing.endSpan(span, Status.INTERNAL)<commit_msg>Lower log level to debug<commit_after>package com.github.sebruck.opencensus.akka.http

import akka.http.scaladsl.model.HttpRequest
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{Directive1, ExceptionHandler}
import com.github.sebruck.opencensus.Tracing
import com.github.sebruck.opencensus.akka.http.propagation.B3FormatPropagation
import com.typesafe.scalalogging.LazyLogging
import io.opencensus.trace.{Span, Status}

import scala.util.control.NonFatal

trait TracingDirective extends LazyLogging {

  protected def tracing: Tracing
  protected def propagation: Propagation

  /**
    * Starts a new span and sets a parent context if the request contains valid headers in the b3 format.
    * The span is ended when the request compeltes or fails with a status code which is suitable
    * to the http response code
    */
  val traceRequest: Directive1[Span] =
    extractRequest.flatMap { req =>
      val span = buildSpan(req)
      recordSuccess(span) & recordException(span) & provide(span)
    }

  private def buildSpan(req: HttpRequest): Span = {
    val name = req.uri.path.toString()

    propagation
      .extractContext(req)
      .fold(
        { error =>
          logger.debug("Extracting of parent context failed", error)
          tracing.startSpan(name)
        },
        tracing.startSpanWithRemoteParent(name, _)
      )
  }

  private def recordSuccess(span: Span) = mapResponse { response =>
    tracing.endSpan(span, StatusTranslator.translate(response.status))
    response
  }

  private def recordException(span: Span) =
    handleExceptions(ExceptionHandler {
      case NonFatal(ex) =>
        tracing.endSpan(span, Status.INTERNAL)
{'fraction_non_alphanumeric': 0.08693081017149616, 'fraction_numerical': 0.002365464222353637, 'mean_word_length': 3.6152796725784446, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '5455906', 'n_tokens_mistral': 1009, 'n_tokens_neox': 1001, 'n_words': 247}