<commit_before>package com.twitter.finagle.b3.thrift

import org.apache.thrift.protocol.TBinaryProtocol
import org.apache.thrift.transport.TIOStreamTransport

import java.net.InetSocketAddress
import java.io.ByteArrayOutputStream
import java.nio.ByteBuffer

import com.twitter.conversions.time._
import com.twitter.util.Base64StringEncoder
import com.twitter.finagle.tracing._
import com.twitter.finagle.stats.{NullStatsReceiver, StatsReceiver}
import com.twitter.finagle.builder.ClientBuilder
import com.twitter.finagle.thrift.{ThriftClientFramedCodec, ThriftClientRequest, thrift}

import collection.mutable.{ArrayBuffer, HashMap, SynchronizedMap}
import scala.collection.JavaConversions._
import com.twitter.finagle.{Service, SimpleFilter, tracing}
import com.twitter.finagle.service.TimeoutFilter

object BigBrotherBirdTracer {
  // to make sure we only create one instance of the tracer
  // per host and port
  private[this] val map =
    new HashMap[String, BigBrotherBirdTracer] with SynchronizedMap[String, BigBrotherBirdTracer]

  /**
   * @param scribeHost Host to send trace data to
   * @param scribePort Port to send trace data to
   * @param statsReceiver Where to log information about tracing success/failures
   * @param sampleRate How much data to collect. Default sample rate 0.1%. Max is 1, min 0.
   */
  def apply(scribeHost: String = "localhost",
            scribePort: Int = 1463,
            statsReceiver: StatsReceiver = NullStatsReceiver,
            sampleRate: Float = 0.001f): Tracer.Factory = {

    val tracer = map.getOrElseUpdate(scribeHost + ":" + scribePort, {
      new BigBrotherBirdTracer(scribeHost, scribePort, statsReceiver.scope("b3"), sampleRate)
    })

    h => {
      tracer.acquire()
      h.onClose {
        tracer.release()
      }
      tracer
    }
  }
}

/**
 * Receives the Finagle generated traces, samples them
 * and sends off the survivors to BigBrotherBird via scribe.
 * @param client The Thrift client used to send traces to scribe
 * @param statsReceiver We generate stats to keep track of traces sent, failures and so on
 */
private[thrift] class BigBrotherBirdTracer(
  scribeHost: String,
  scribePort: Int,
  statsReceiver: StatsReceiver,
  initialSampleRate: Float
) extends Tracer
{
  private[this] val protocolFactory = new TBinaryProtocol.Factory()
  private[this] val TraceCategory = "b3" // scribe category

  // this sends off spans after the deadline is hit, no matter if it ended naturally or not.
  private[this] val spanMap = new DeadlineSpanMap(this, 120.seconds, statsReceiver)<commit_msg>[split] Add alternate apply for creating b3 tracers in java<commit_after>package com.twitter.finagle.b3.thrift

import org.apache.thrift.protocol.TBinaryProtocol
import org.apache.thrift.transport.TIOStreamTransport

import java.net.InetSocketAddress
import java.io.ByteArrayOutputStream
import java.nio.ByteBuffer

import com.twitter.conversions.time._
import com.twitter.util.Base64StringEncoder
import com.twitter.finagle.tracing._
import com.twitter.finagle.stats.{NullStatsReceiver, StatsReceiver}
import com.twitter.finagle.builder.ClientBuilder
import com.twitter.finagle.thrift.{ThriftClientFramedCodec, ThriftClientRequest, thrift}

import collection.mutable.{ArrayBuffer, HashMap, SynchronizedMap}
import scala.collection.JavaConversions._
import com.twitter.finagle.{Service, SimpleFilter, tracing}
import com.twitter.finagle.service.TimeoutFilter

object BigBrotherBirdTracer {
  // to make sure we only create one instance of the tracer
  // per host and port
  private[this] val map =
    new HashMap[String, BigBrotherBirdTracer] with SynchronizedMap[String, BigBrotherBirdTracer]

  /**
   * @param scribeHost Host to send trace data to
   * @param scribePort Port to send trace data to
   * @param statsReceiver Where to log information about tracing success/failures
   * @param sampleRate How much data to collect. Default sample rate 0.1%. Max is 1, min 0.
   */
  def apply(scribeHost: String = "localhost",
            scribePort: Int = 1463,
            statsReceiver: StatsReceiver = NullStatsReceiver,
            sampleRate: Float = 0.001f): Tracer.Factory = {

    val tracer = map.getOrElseUpdate(scribeHost + ":" + scribePort, {
      new BigBrotherBirdTracer(scribeHost, scribePort, statsReceiver.scope("b3"), sampleRate)
    })

    h => {
      tracer.acquire()
      h.onClose {
        tracer.release()
      }
      tracer
    }
  }

  /**
   * Util method since named parameters can't be called from Java
   * @param sr
   */
  def apply(sr: StatsReceiver): Tracer.Factory = apply(statsReceiver = sr)
}

/**
 * Receives the Finagle generated traces, samples them
 * and sends off the survivors to BigBrotherBird via scribe.
 * @param client The Thrift client used to send traces to scribe
 * @param statsReceiver We generate stats to keep track of traces sent, failures and so on
 */
private[thrift] class BigBrotherBirdTracer(
  scribeHost: String,
  scribePort: Int,
  statsReceiver: StatsReceiver,
  initialSampleRate: Float
) extends Tracer
{
  private[this] val protocolFactory = new TBinaryProtocol.Factory()
  private[this] val TraceCategory = "b3" // scribe category

  // this sends off spans after the deadline is hit, no matter if it ended naturally or not.
  private[this] val spanMap = new DeadlineSpanMap(this, 120.seconds, statsReceiver)
{'fraction_non_alphanumeric': 0.08319343406080955, 'fraction_numerical': 0.007647826897966797, 'mean_word_length': 4.121298949379178, 'pattern_counts': {'":': 2, '<': 3, '<?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': '3440152', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1527, 'n_words': 474}