All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.twitter.finagle.zipkin.thriftscala.Scribe$FinagleClient.scala Maven / Gradle / Ivy

There is a newer version: 18.2.0
Show newest version
/**
 * Generated by Scrooge
 *   version: 4.10.0
 *   rev: 93b5383f7afff1301467b8941baf6ab35959a15c
 *   built at: 20160907-143258
 */
package com.twitter.finagle.zipkin.thriftscala

import com.twitter.finagle.SourcedException
import com.twitter.finagle.{service => ctfs}
import com.twitter.finagle.stats.{NullStatsReceiver, StatsReceiver}
import com.twitter.finagle.thrift.{Protocols, ThriftClientRequest}
import com.twitter.scrooge.{ThriftStruct, ThriftStructCodec}
import com.twitter.util.{Future, Return, Throw, Throwables}
import java.nio.ByteBuffer
import java.util.Arrays
import org.apache.thrift.protocol._
import org.apache.thrift.TApplicationException
import org.apache.thrift.transport.{TMemoryBuffer, TMemoryInputTransport}
import scala.collection.{Map, Set}
import scala.language.higherKinds


@javax.annotation.Generated(value = Array("com.twitter.scrooge.Compiler"))
class Scribe$FinagleClient(
    val service: com.twitter.finagle.Service[ThriftClientRequest, Array[Byte]],
    val protocolFactory: TProtocolFactory,
    val serviceName: String,
    stats: StatsReceiver,
    responseClassifier: ctfs.ResponseClassifier)
  extends Scribe[Future] {

  def this(
    service: com.twitter.finagle.Service[ThriftClientRequest, Array[Byte]],
    protocolFactory: TProtocolFactory = Protocols.binaryFactory(),
    serviceName: String = "Scribe",
    stats: StatsReceiver = NullStatsReceiver
  ) = this(
    service,
    protocolFactory,
    serviceName,
    stats,
    ctfs.ResponseClassifier.Default
  )

  import Scribe._

  protected def encodeRequest(name: String, args: ThriftStruct): ThriftClientRequest = {
    val buf = new TMemoryBuffer(512)
    val oprot = protocolFactory.getProtocol(buf)

    oprot.writeMessageBegin(new TMessage(name, TMessageType.CALL, 0))
    args.write(oprot)
    oprot.writeMessageEnd()

    val bytes = Arrays.copyOfRange(buf.getArray, 0, buf.length)
    new ThriftClientRequest(bytes, false)
  }

  protected def decodeResponse[T <: ThriftStruct](
    resBytes: Array[Byte],
    codec: ThriftStructCodec[T]
  ): T = {
    val iprot = protocolFactory.getProtocol(new TMemoryInputTransport(resBytes))
    val msg = iprot.readMessageBegin()
    try {
      if (msg.`type` == TMessageType.EXCEPTION) {
        val exception = TApplicationException.read(iprot) match {
          case sourced: SourcedException =>
            if (serviceName != "") sourced.serviceName = serviceName
            sourced
          case e => e
        }
        throw exception
      } else {
        codec.decode(iprot)
      }
    } finally {
      iprot.readMessageEnd()
    }
  }

  protected def missingResult(name: String) = {
    new TApplicationException(
      TApplicationException.MISSING_RESULT,
      name + " failed: unknown result"
    )
  }

  protected def setServiceName(ex: Throwable): Throwable =
    if (this.serviceName == "") ex
    else {
      ex match {
        case se: SourcedException =>
          se.serviceName = this.serviceName
          se
        case _ => ex
      }
    }

  // ----- end boilerplate.

  private[this] val scopedStats = if (serviceName != "") stats.scope(serviceName) else stats
  private[this] object __stats_log {
    val RequestsCounter = scopedStats.scope("Log").counter("requests")
    val SuccessCounter = scopedStats.scope("Log").counter("success")
    val FailuresCounter = scopedStats.scope("Log").counter("failures")
    val FailuresScope = scopedStats.scope("Log").scope("failures")
  }
  
  def log(messages: Seq[com.twitter.finagle.zipkin.thriftscala.LogEntry] = Seq[com.twitter.finagle.zipkin.thriftscala.LogEntry]()): Future[com.twitter.finagle.zipkin.thriftscala.ResultCode] = {
    __stats_log.RequestsCounter.incr()
    val inputArgs = Log.Args(messages)
    val replyDeserializer: Array[Byte] => _root_.com.twitter.util.Try[com.twitter.finagle.zipkin.thriftscala.ResultCode] =
      response => {
        val decodeResult: _root_.com.twitter.util.Try[Log.Result] =
          _root_.com.twitter.util.Try {
            decodeResponse(response, Log.Result)
          }
  
        decodeResult match {
          case t@_root_.com.twitter.util.Throw(_) =>
            t.cast[com.twitter.finagle.zipkin.thriftscala.ResultCode]
          case  _root_.com.twitter.util.Return(result) =>
            val serviceException: Throwable =
              null
  
            if (result.success.isDefined)
              _root_.com.twitter.util.Return(result.success.get)
            else if (serviceException != null)
              _root_.com.twitter.util.Throw(serviceException)
            else
              _root_.com.twitter.util.Throw(missingResult("Log"))
        }
      }
  
    val serdeCtx = new _root_.com.twitter.finagle.thrift.DeserializeCtx[com.twitter.finagle.zipkin.thriftscala.ResultCode](inputArgs, replyDeserializer)
    _root_.com.twitter.finagle.context.Contexts.local.let(
      _root_.com.twitter.finagle.thrift.DeserializeCtx.Key,
      serdeCtx
    ) {
      val serialized = encodeRequest("Log", inputArgs)
      this.service(serialized).flatMap { response =>
        Future.const(serdeCtx.deserialize(response))
      }.respond { response =>
        val responseClass = responseClassifier.applyOrElse(
          ctfs.ReqRep(inputArgs, response),
          ctfs.ResponseClassifier.Default)
        responseClass match {
          case ctfs.ResponseClass.Successful(_) =>
            __stats_log.SuccessCounter.incr()
          case ctfs.ResponseClass.Failed(_) =>
            __stats_log.FailuresCounter.incr()
            response match {
              case Throw(ex) =>
                setServiceName(ex)
                __stats_log.FailuresScope.counter(Throwables.mkString(ex): _*).incr()
              case _ =>
            }
        }
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy