我正在尝试运行一个简单的TwitterServer,其实现来自https://twitter.github.io/twitter-server/Features.html
在向http://localhost:9990/echo
发出多个请求后,我在http://localhost:9990/admin/metrics
中未看到任何指标。调试后,我发现实际StatsReceiver
为NullStatsReceiver
。我没有在文档中发现任何StatsReceiver
配置。
那么如何更改默认NullStatsReceiver
?
import com.twitter.conversions.time._
import com.twitter.finagle.http.{HttpMuxer, Request, Response, Status}
import com.twitter.finagle.Service
import com.twitter.logging.Formatter
import com.twitter.server.TwitterServer
import com.twitter.util.{Await, Future, Time}
import java.net.InetSocketAddress
object AdvancedServer extends TwitterServer {
val what = flag("what", "hello", "String to return")
val addr = flag("bind", new InetSocketAddress(0), "Bind address")
val durations = flag("alarms", (1.second, 5.second), "2 alarm durations")
val counter = statsReceiver.counter("requests_counter")
override def defaultFormatter = new Formatter(
timezone = Some("UTC"),
prefix = "<yyyy-MM-dd HH:mm:ss.SSS> [%.3s] %s: "
)
override def failfastOnFlagsNotParsed: Boolean = true
val service = new Service[Request, Response] {
def apply(request: Request) = {
log.debug("Received a request at " + Time.now)
counter.incr()
val response = Response(request.version, Status.Ok)
response.contentString = what() + "\n"
Future.value(response)
}
}
def main() {
// We could create a new http server but in this case we use the
// one already started for /admin/* endpoints.
// The `TwitterServer` trait exposes an `adminHttpServer` that serve all routes
// registered in the HttpMuxer object, we just have to add our own.
HttpMuxer.addHandler("/echo", service)
HttpMuxer.addHandler("/echo/", service)
// And wait on the server
Await.ready(adminHttpServer)
}
}
答案 0 :(得分:0)
我想我找到了解决方案。
首先需要添加SqlTransaction
依赖项。我用过v finagle-stats
。
其次,需要将计数器定义为
6.43.0
。
然后它将按预期工作。