Recipes Java - Thomas-S-B/Telemee GitHub Wiki

Some recipes to use telemee in Java - more to come.

A quick Java/Maven example:

1 - Include the TelemeeJavaClient (available on mavencentral) into your pom:

<dependency>
    <groupId>de.struller-baumann</groupId>
    <artifactId>TelemeeJavaClient</artifactId>
    <version>0.01</version>
</dependency>

2 - Write a Telemee-Helper-class:

import de.strullerbaumann.telemeejavaclient.boundary.TelemeeJavaClient;
import de.strullerbaumann.telemeejavaclient.entity.Channel;
import de.strullerbaumann.telemeejavaclient.entity.ChannelAttribute;
import de.strullerbaumann.telemeejavaclient.entity.TelemeeApp;

public class Telemee {
   private final TelemeeJavaClient tj = new TelemeeJavaClient();
   private final TelemeeApp TEST_APP = new TelemeeApp("my testapp");
   private final Channel TEST_CHANNEL = new Channel("my test channel");
   private final ChannelAttribute TEST_CHANNELATTRIBUTE_CURRENTTIME = new ChannelAttribute("CurrentTime in ms");
   private final ChannelAttribute TEST_CHANNELATTRIBUTE_X = new ChannelAttribute("X");
   private final ChannelAttribute TEST_CHANNELATTRIBUTE_Y = new ChannelAttribute("Y");

   public Telemee() {
      tj.init();
      tj.setLogLevel(TelemeeJavaClient.INFO);
   }

   public synchronized void log(long x, long y, String message) {
      tj.forTelemeeApp(TEST_APP)
              .forChannel(TEST_CHANNEL)
              .startLogEntry(message, TelemeeJavaClient.INFO)
              .forChannelAttribute(TEST_CHANNELATTRIBUTE_CURRENTTIME)
              .log(System.currentTimeMillis())
              .forChannelAttribute(TEST_CHANNELATTRIBUTE_X)
              .log(x)
              .forChannelAttribute(TEST_CHANNELATTRIBUTE_Y)
              .log(y)
              .endLogEntry()
              .send();
   }

}

3 - Use it in your javacode:

Telemee telemee = new Telemee();
...
telemee.log(123, 456, "This is a testmessage");
...

Logging methodcalls and performance in Java EE5/6/7

Write an interceptor, which creates the telemeelogs:

import java.io.Serializable;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

public class TelemeeInterceptor implements Serializable {

   @Inject
   Telemee telemee;

   @AroundInvoke
   public Object intercept(InvocationContext ctx) throws Exception {
      long start = System.nanoTime();
      Object result = ctx.proceed();
      long duration = System.nanoTime() - start;
      telemee.logPerformance(ctx.getMethod().getName(), TimeUnit.MILLISECONDS.convert(duration, TimeUnit.NANOSECONDS), start);
      return result;
   }
}

Then for convenience a telemee-singleton EJB:

import de.strullerbaumann.telemeejavaclient.boundary.TelemeeJavaClient;
import de.strullerbaumann.telemeejavaclient.entity.Channel;
import de.strullerbaumann.telemeejavaclient.entity.ChannelAttribute;
import de.strullerbaumann.telemeejavaclient.entity.TelemeeApp;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.enterprise.context.ApplicationScoped;

@Singleton
@Startup
@ApplicationScoped
@TransactionManagement(TransactionManagementType.BEAN)
public class Telemee {

   private final TelemeeJavaClient tj = new TelemeeJavaClient();
   private final TelemeeApp TEST_APP = new TelemeeApp("My EE-App to test");
   private final Channel PERFORMANCE_CHANNEL = new Channel("Performance methods");
   private final ChannelAttribute TEST_CHANNELATTRIBUTE_CURRENTTIME = new ChannelAttribute("CurrentTime in ms");
   private final ChannelAttribute TEST_CHANNELATTRIBUTE_METHODNAME = new ChannelAttribute("Methodname");
   private final ChannelAttribute TEST_CHANNELATTRIBUTE_DURATION = new ChannelAttribute("Duration in ms");

   @PostConstruct
   public void init() {
      tj.init();
      tj.setLogLevel(TelemeeJavaClient.INFO);
   }

   public void logPerformance(String methodName, long duration, long start) {
      tj.forTelemeeApp(TEST_APP)
              .forChannel(PERFORMANCE_CHANNEL)
              .startLogEntry("Performance method " + methodName, TelemeeJavaClient.INFO)
              .forChannelAttribute(TEST_CHANNELATTRIBUTE_CURRENTTIME)
              .log(start)
              .forChannelAttribute(TEST_CHANNELATTRIBUTE_METHODNAME)
              .log(methodName)
              .forChannelAttribute(TEST_CHANNELATTRIBUTE_DURATION)
              .log(duration)
              .endLogEntry()
              .sendAsync();
   }

}

Activate/Bind the interceptor to the class(es) you want to measure:

...
@Interceptors(TelemeeInterceptor.class)
public class BeanWeWantToMeasure implements Serializable {
...

Open http://localhost:8080/telemee to browse the generated data.
Now you can get graphs similar like this linechart (you can group by the methodname):

Linechart 1

You can see in a chronology way which methods are called and also their performance.

⚠️ **GitHub.com Fallback** ⚠️