/clj-performance-logger

Easy way to log the performance of a function and extra argument values while in run time.

Primary LanguageClojureOtherNOASSERTION

clj-Performance-Logger

Build Status Dependency Status Coverage Status

Performance Logger Made Easy!

clj-performance-logger is a clojure library that helps you out in logging performance metrics in production in Apache CouchDB.

Current Version

Clojars Project

With Maven:

<dependency>
  <groupId>clj-performance-logger</groupId>
  <artifactId>clj-performance-logger</artifactId>
  <version>0.1.4</version>
</dependency>

With Gradle:

compile "clj-performance-logger:clj-performance-logger:0.1.4"

Usage

In the project.clj add the dependency to clj-performance-logger with the latest stable version:

(defproject hps "0.6.2-SNAPSHOT"
  :description "D square High Performance Search"
  :url "http://git.dsquare.intra/projects/HPS/"
  :license "Copyright D square N.V."
  :dependencies [[org.clojure/clojure "1.6.0"]
                 [compojure "1.3.2"]
                 [cheshire "5.3.1"]
                 ...
                 [clj-performance-logger "0.1.4"]                                  
                 [simpledb "0.1.13"]]

In your namespace you need to require it:

(ns hps.handler  
  (:require [be.dsquare.logging :as log]))

And then initialize it and it's also a good practice ot destroy it (this will change as a component, following a nice start/stop flow). This will create the default database in case it's not created.

(defn init []  
  (log/init))

(defn destroy []  
  (log/destroy))

timelog

timelog is followed by a text that will be displayed in the log and will surround the function that we want to check the performance on:

(log/timelog "doing simple maths" (* 5 2))

This will log the following message inside CouchDB:

{:ts "2015-05-15T14:21:00.000Z"
:message "doing simple maths"
:timeMillis 0.5
:namespace "be.dsquare.handler"
:function "clojure.core/*"
:remove "remove cache"}

trace-log

This macro adds extra information (entries) to the same ongoing loging from timelog or timelog-complete.

(defn sum-fn [first-value second-value]
  (do
     (log/trace-log {:first-value   first-value
                     :second-value  second-value})        
     (* first-value second-value)))

(log/timelog "doing simple maths" (sum-fn 5 2))

This will log the following message inside CouchDB:

{:ts "2015-05-15T14:21:00.000Z"
:message "doing simple maths"
:timeMillis 0.5
:namespace "be.dsquare.handler"
:function "sum-fn"
:first-value  5
:second-value 2
:remove "remove cache"}

log-error-return

This macro surrounds a function with a try catch that logs the stacktrace, the function that caused, the error message and the customized message that you passed into CouchDB.

log-error-http

It's an specific version of the log-error-return. In this case it will return and log and http error 500.

Full example

In this example you can see a full complete example on how to use the three main macros: timelog / log-error-http and trace-log

(GET "/api/annotations/" [tagNames startDate endDate stateTypes stateNames :as request]
    (->>
      (do
        (log/trace-log {:tagNames   tagNames
                        :startDate  startDate
                        :endDate    endDate})        
        tagNames)
      (map #(invoke-one-annotation % startDate endDate))
      flatten
      (mapv #(assoc-annotation-url request %))
      json-response
      (log/timelog "GET /api/annotations/")
      log/log-error-http))

Pay attention that the log-error-http macro has to surround all the other calls, that's why it has to be the last one.

Contributors

Sponsored

This project is sponsored by D square N.V

License

BSD. See the LICENSE file at the root of this repository.