Advertisement
Guest User

Untitled

a guest
Jan 15th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.28 KB | None | 0 0
  1. package ru.yandex.vertis.statistics.datamart
  2.  
  3. import akka.actor.{ActorSystem, Props}
  4. import akka.io.IO
  5. import akka.pattern.ask
  6. import akka.util.Timeout
  7. import com.codahale.metrics.MetricRegistry
  8. import ru.yandex.common.monitoring.graphite.GraphiteReporterEnabler
  9. import ru.yandex.util.spray.monitoring.MonitoringDirectives
  10. import ru.yandex.vertis.statistics.app.AppDependencies.metricsRegistry
  11. import ru.yandex.vertis.statistics.app.control.Metered
  12. import ru.yandex.vertis.statistics.app.{AppDependencies, Environment}
  13. import spray.can.Http
  14. import spray.http.{HttpRequest, HttpResponse, StatusCodes}
  15. import spray.routing._
  16. import spray.routing.directives.LogEntry
  17.  
  18. import scala.concurrent.ExecutionContext
  19. import scala.concurrent.duration._
  20.  
  21. //noinspection ScalaStyle
  22. object hello extends App with Metered with MonitoringDirectives{
  23.  
  24.   override def marker: String = "hello"
  25.  
  26.   override def metricRegistry: MetricRegistry = AppDependencies.metricsRegistry
  27.  
  28.   override def serviceName: String = "hello"
  29.  
  30.   override implicit val ec: ExecutionContext = AppDependencies.auxiliaryExecutor
  31.   implicit val system = ActorSystem("my-system")
  32.   implicit val timeout: Timeout = Timeout(20.seconds)
  33.  
  34.   def requestMethodAndResponseStatusAsInfo(req: HttpRequest): Any => Option[LogEntry] = {
  35.     case res: HttpResponse =>
  36.       res.message.status.intValue match {
  37.         case so if so % 100 == 2 => response2xxCounter.mark()
  38.         case so if so % 100 == 4 => response4xxCounter.mark()
  39.         case so if so % 100 == 5 => response5xxCounter.mark()
  40.         case _ =>
  41.       }
  42.       println(res.message.status.intValue)
  43.       None
  44.     case e =>
  45.       println(e.getClass.getCanonicalName)
  46.       println("other")
  47.       None // other kind of responses
  48.   }
  49.  
  50.   var wrapper = monitorRoute(requestMethodAndResponseStatusAsInfo _)
  51.  
  52.   val response5xxCounter = meter("received 5xx")
  53.   val response2xxCounter = meter("received 2xx")
  54.   val response4xxCounter = meter("received 4xx")
  55.  
  56.   GraphiteReporterEnabler.enable(
  57.     metricsRegistry,
  58.     "hello_from_my_dev",
  59.     Environment.hostName)
  60.  
  61.  
  62.   IO(Http)(system) ask Http.Bind(
  63.     listener = system.actorOf(
  64.       Props(
  65.         new MyService()
  66.       ),
  67.       "root"
  68.     ),
  69.     interface = "0.0.0.0",
  70.     port = 8080
  71.   )
  72.  
  73.   class MyService extends HttpServiceActor {
  74.  
  75.     implicit val specificExceptionHandler = ExceptionHandler {
  76.       case e: NoSuchElementException =>
  77.         ctx => ctx.complete(StatusCodes.NotFound)
  78.       case e: IllegalArgumentException =>
  79.         ctx => ctx.complete(StatusCodes.BadRequest)
  80.       case e: UnsupportedOperationException =>
  81.         ctx => ctx.complete(StatusCodes.NotImplemented)
  82.       case other =>
  83.         ctx => {
  84.           println("hadled")
  85.           response5xxCounter.mark()
  86.           ctx.complete(StatusCodes.InternalServerError)
  87.         }
  88.     }
  89.  
  90.     implicit val rs: RoutingSettings = RoutingSettings.default(system)
  91.  
  92.     def receive = runRoute {
  93.       wrapper {
  94.         path("hello") {
  95.           get {
  96.             complete {
  97.               throw new IllegalStateException("hello ")
  98.             }
  99.           }
  100.         } ~
  101.           path("hi") {
  102.             get {
  103.               complete {
  104.                 "hello"
  105.               }
  106.             }
  107.           }
  108.       }
  109.     }
  110.   }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement