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

com.twitter.server.handler.MetricQueryHandler.scala Maven / Gradle / Ivy

There is a newer version: 18.9.1
Show newest version
package com.twitter.server.handler

import com.twitter.finagle.Service
import com.twitter.finagle.http.{Request, Response}
import com.twitter.io.Buf
import com.twitter.server.util.HttpUtils.{newResponse, parse}
import com.twitter.server.util.{JsonConverter, MetricSource}
import com.twitter.util.Future

private object MetricQueryHandler {
  def render(title: String, keys: Set[String]): String =
    s"""
        
        
        
    ${ (for (key <- keys.toSeq.sorted) yield { s"""
  • $key
  • """ }).mkString("\n") }
""" } /** * A handler which accepts metrics queries via http query strings and returns * json encoded metrics. */ class MetricQueryHandler(source: MetricSource = new MetricSource) extends Service[Request, Response] { import MetricQueryHandler._ private[this] def query(keys: Seq[String]) = for (k <- keys; e <- source.get(k)) yield e def apply(req: Request): Future[Response] = { val (_, params) = parse(req.uri) params.getOrElse("m", Nil) match { case Nil => newResponse( contentType = "text/html;charset=UTF-8", content = Buf.Utf8(render("Test", source.keySet)) ) case someKeys => newResponse( contentType = "application/json;charset=UTF-8", content = Buf.Utf8(JsonConverter.writeToString(query(someKeys))) ) } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy