kamon文档翻译(一)

来源:互联网 发布:用别人身份证开淘宝店 编辑:程序博客网 时间:2024/06/08 00:05

原文 http://kamon.io/documentation/get-started/

从kamon开始

kamon以一个核心模块和一些可选模块发布。核心模块由具有所有metric recording和trace manipulation 功能的API构成,可选模块提供了字节码形式的基本组建,并且可以报告你的应用程序的性能。你可以在你的应用程序中将所有模块作为简单的库添加到依赖中,此外,如果你使用的是基本组建模块(可选模块),当你在运行应用时,需要将AspectJ Weaver agent 包含在JVM启动参数中。

将你所使用的模块包含在项目中

所有的kamon模块都可以在maven central中获得,你只需要将其作为编译依赖添加到项目中即可。根据你所使用的项目管理工具分别由不同的添加方式。你只需要知道我们的group id是io.kamon,artifacts是以模块名命名的。下面是使用常见构建工具的例子。

通过SBT构建:
SBT:libraryDependencies ++= Seq(  "io.kamon" %% "kamon-core" % "0.6.0",  // [Optional]  "io.kamon" %% "kamon-statsd" % "0.6.0",  // [Optional]  "io.kamon" %% "kamon-datadog" % "0.6.0"  // ... and so on with all the modules you need.)
通过Maven构建:
<dependencies>  <dependency>    <groupId>io.kamon</groupId>    <artifactId>kamon-core_2.11</artifactId>    <version>0.6.0</version>  </dependency></dependencies>

目前有通过SBT交叉版本特性的基于Scala 2.10和Scala 2.11 的两个最新发布版本,也就是说artifacts的后缀包含特定的scala版本_2.10 和 _2.11。如果你不是用scala,并且不熟悉这些版本,就选择版本最高的,目前是2.11,也就是上面Maven例子中的。你可以在本页左侧面板列表中找到所有的kamon 模块。

开始Kamon

为了使用metrics和tracing API,并保证所有的kamon模块都已经加载,你需要通过使用Kamon.start(..)方法启动kamon。这是个一次性操作,你应该尽可能早的在你的应用程序中启动它,以保证所有基本组建和用到的kamon API 能够顺利的工作。
Scala:

 object get GetStarted extends App {  Kamon.start()  val someHistogram = Kamon.metrics.histogram("some-histogram")  val someCounter = Kamon.metrics.counter("some-counter")  someHistogram.record(42)  someHistogram.record(50)  someCounter.increment()  // This application wont terminate unless you shutdown Kamon.  Kamon.shutdown()}

Java:

public class GetStarted {  public static void main(String[] args) {    Kamon.start();    final Histogram someHistogram = Kamon.metrics().histogram("some-histogram");    final Counter someCounter = Kamon.metrics().counter("some-counter");    someHistogram.record(42);    someHistogram.record(50);    someCounter.increment();    // This application wont terminate unless you shutdown Kamon.    Kamon.shutdown();  }}

当你使用kamon时,还可以提供一个常用的配置对象。参考configuration 一节,快速浏览如何配置kamon。在使用完kamon时,记得使用kamon.shutdown( )方法关闭。

可选:通过AspectJ Weaver启动你的应用

如果你的应用程序中用到的任何一个基本组建模块需要使用AspectJ,这一步才是必须的。如果没有用到,请跳过这节。
通过AspectJ weaver启动你的应用非常简单,只需要将JVM启动参数 -javaagent 指向weaver文件所在的目录就可以了。如何实现这一步根据你的发布方式不同而不同,下面是几个简单的例子。

手动方式:
To manually add the agent on application startup you just needto add the -javaagent:/path/to/aspectj-weaver.jar as show here:java -javaagent:~/.aspectj/aspectj-weaver.jar your-app.jar
sbt-aspectj-runner:
//Add the aspectj-runner plugin to project/plugins.sbtaddSbtPlugin("io.kamon" % "aspectj-runner" % "0.1.3")// Run!aspectj-runner:run
sbt-aspectj:
// Include the sbt-aspectj (https://github.com/sbt/sbt-aspectj)// plugin in your build by adding the following line to your// `project/plugins.sbt` file.////  addSbtPlugin("com.typesafe.sbt" % "sbt-aspectj" % "0.10.0")//// Bring the sbt-aspectj settings into this buildaspectjSettings// Here we are effectively adding the `-javaagent` JVM startup// option with the location of the AspectJ Weaver provided by// the sbt-aspectj plugin.javaOptions in run <++= AspectjKeys.weaverOptions in Aspectj// We need to ensure that the JVM is forked for the// AspectJ Weaver to kick in properly and do it's magic.fork in run := true

为了防止你需要使用AspectJ Weaver但是你没有正确配置,Kamon会在日志中打印很多日志和一些显著的错误信息,但是Kamon不会因为这个错误而终止应用程序。
如果你的发布方式没有在上面列出,并且不会太嫩家weaver,请联系我们.

Enjoy!

现在你已经掌握了使用kamon的必要知识了,在kamon初始化之后,所有的模块都会自动启动,你不需要一个一个地启动他们。
但是,kamon不仅仅是你代码里直方图和计数器的测量组建,我们可以做得更多。请参考文档中的metrics 和 tracing 模块。学习如何使用这些核心特性和其他可选模块的绝体使用细节和建议的文档。