Search code examples
javaspringspring-bootakkakamon

Kamon with spring and akka


I'm using akka (java) in combination with spring boot. I would like to monitor metrics with kamon and show them on a grafana dashboard. I've included kamon core kamon statsd to the dependencies and created an application.conf with the correct port and hostname for statsd. There are no clear examples or tutorials that show the stack i'm using. Is it possible to measure the akka metrics using spring boot, java, akka and kamon deployed as war in tomcat?


Solution

  • Here a simple example

    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>
    

    two actors to play a ping pong game

    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);
     }
    

    }

    a spring component

    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);
        }
    }
    

    also make a simple controller in order to show how simple is using the kamon-annotation module to gather some other metrics

    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!!!"; }
    }
    

    the application main

    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 simplified for testing purposes

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

    build the application and run

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

    should we get something like this

    +--------------------------------------------------------------------------------------------------+
    |                                                                                                  |
    |    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                                     |
    |                                                                                                  |
    +--------------------------------------------------------------------------------------------------+
    

    and if we do 2 curls

    http://localhost:8080/kamon/counter
    http://localhost:8080/kamon/counter
    
    +--------------------------------------------------------------------------------------------------+
    |                                                                                                  |
    |                                         Counters                                                 |
    |                                       -------------                                              |
    |                             awesomeCounter  =>  2                                                |
    |                                                                                                  |
    |                                                                                                  |
    |                                        Histograms                                                |
    |                                      --------------                                              |
    |                                                                                                  |
    |                                      MinMaxCounters                                              |
    |                                    -----------------                                             |
    |                                                                                                  |
    |                                          Gauges                                                  |
    |                                        ----------                                                |
    |                                                                                                  |
    +--------------------------------------------------------------------------------------------------+
    

    link to the complete example.

    Hope this can help you.