家花 spring 等

Kamon with spring and akka

我将 akka (java) 与 spring boot 结合使用。我想用 kamon 监控指标并在 grafana 仪表板上显示它们。我已经将 kamon core kamon statsd 添加到依赖项中,并为 statsd 创建了一个具有正确端口和主机名的 application.conf。没有明确的示例或教程显示我正在使用的堆栈。是否可以使用 spring boot、javaakkakamontomcat 中部署为 war 来衡量 akka 指标?

这里有一个简单的例子

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>kamon.annotation</groupId>
<artifactId>kamon-spring-boot</artifactId>
<version>0.1.0</version>


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>io.kamon</groupId>
        <artifactId>kamon-core_2.11</artifactId>
        <version>0.6.2</version>
    </dependency>
    <dependency>
        <groupId>io.kamon</groupId>
        <artifactId>kamon-annotation_2.11</artifactId>
        <version>0.6.2</version>
    </dependency>
    <dependency>
        <groupId>io.kamon</groupId>
        <artifactId>kamon-akka_2.11</artifactId>
        <version>0.6.2</version>
    </dependency>
    <dependency>
        <groupId>io.kamon</groupId>
        <artifactId>kamon-log-reporter_2.11</artifactId>
        <version>0.6.2</version>
    </dependency>
</dependencies>

<properties>
    <java.version>1.8</java.version>
</properties>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>io.kamon</id>
        <url>http://snapshots.kamon.io</url>
    </repository>
</repositories>

两个演员打乒乓球

import akka.actor.UntypedActor;

class Pinger extends UntypedActor {
 static final class PingMessage {}

 public void onReceive(Object message) throws Exception {
    if (message instanceof Ponger.PongMessage) getSender().tell(new PingMessage(), getSelf());
    else unhandled(message);
}

}

import akka.actor.UntypedActor;

class Ponger extends UntypedActor {
 static final class PongMessage {}

 public void onReceive(Object message) throws Exception {
    if (message instanceof Pinger.PingMessage) getSender().tell(new PongMessage(), getSelf());
    else unhandled(message);
 }

}

一个spring组件

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

@Component
public class PingPong {

    @PostConstruct
    public void initialize() {
        final ActorSystem system = ActorSystem.create("kamon-spring-boot-actor-system");

        final ActorRef pinger = system.actorOf(Props.create(Pinger.class), "pinger");
        final ActorRef ponger = system.actorOf(Props.create(Ponger.class), "ponger");

        pinger.tell(new Ponger.PongMessage(), ponger);
    }
}

还制作了一个简单的控制器,以展示使用 kamon-annotation 模块收集其他指标有多么简单

import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
@RequestMapping("/kamon")
@EnableKamon
public class KamonController {

  @RequestMapping("/counter")
  @ResponseBody
  @Count(name = "awesomeCounter")
  public String counter() {  return "count!!!"; }
}

应用程序主

import kamon.akka.pingpong.PingPong;
import kamon.annotation.KamonController;
import org.springframework.boot.SpringApplication;

public class KamonSpringApplication {
  public static void main(String... args) {
   Kamon.start();
   SpringApplication.run(KamonController.class, args);
  }
}

application.conf 为测试目的而简化

kamon {
  metric {
    filters {
      trace.includes = [ "**" ]
      akka-actor.includes = [ "**" ]
      akka-actor.excludes = ["*/system/**", "*/user/IO-**" ]
      akka-dispatcher.includes = [ "**" ]
      akka-dispatcher.excludes = [ ]
    }
  }
 }

构建应用程序并 运行

mvn package && java -javaagent:/path/to/aspectjweaver.jar -jar target/kamon-spring-boot-0.1.0.jar

我们应该得到这样的东西吗

+--------------------------------------------------------------------------------------------------+
|                                                                                                  |
|    Actor: kamon-spring-boot-actor-system/user/pinger                                             |
|                                                                                                  |
|   Processing Time (nanoseconds)      Time in Mailbox (nanoseconds)         Mailbox Size          |
|    Msg Count: 3393358                    Msg Count: 3393405                  Min: 0              |
|          Min: 237                              Min: 178                     Avg.: 0.0            |
|    50th Perc: 672                        50th Perc: 756                      Max: 2              |
|    90th Perc: 988                        90th Perc: 1264                                         |
|    95th Perc: 1088                       95th Perc: 1368                                         |
|    99th Perc: 1520                       99th Perc: 1848                   Error Count: 0        |
|  99.9th Perc: 20480                    99.9th Perc: 17920                                        |
|          Max: 16646144                         Max: 34865152                                     |
|                                                                                                  |
+--------------------------------------------------------------------------------------------------+

+--------------------------------------------------------------------------------------------------+
|                                                                                                  |
|    Actor: kamon-spring-boot-actor-system/user/ponger                                             |
|                                                                                                  |
|   Processing Time (nanoseconds)      Time in Mailbox (nanoseconds)         Mailbox Size          |
|    Msg Count: 3739208                    Msg Count: 3739161                  Min: 0              |
|          Min: 272                              Min: 172                     Avg.: 0.0            |
|    50th Perc: 672                        50th Perc: 732                      Max: 2              |
|    90th Perc: 976                        90th Perc: 1232                                         |
|    95th Perc: 1064                       95th Perc: 1344                                         |
|    99th Perc: 1360                       99th Perc: 1656                   Error Count: 0        |
|  99.9th Perc: 10496                    99.9th Perc: 14272                                        |
|          Max: 7766016                          Max: 30277632                                     |
|                                                                                                  |
+--------------------------------------------------------------------------------------------------+

如果我们做 2 个弯举

http://localhost:8080/kamon/counter
http://localhost:8080/kamon/counter

+--------------------------------------------------------------------------------------------------+
|                                                                                                  |
|                                         Counters                                                 |
|                                       -------------                                              |
|                             awesomeCounter  =>  2                                                |
|                                                                                                  |
|                                                                                                  |
|                                        Histograms                                                |
|                                      --------------                                              |
|                                                                                                  |
|                                      MinMaxCounters                                              |
|                                    -----------------                                             |
|                                                                                                  |
|                                          Gauges                                                  |
|                                        ----------                                                |
|                                                                                                  |
+--------------------------------------------------------------------------------------------------+

link 到完整示例。

希望对您有所帮助。