/clockwork

Clockwork is a library used to communicate with Clockwork browser extension

Primary LanguageGoMIT LicenseMIT

checks & tests codecov Go Report Card GPL3 license

Server-side component for Clockwork browser extension written in GO

Attach and use data-sources to clockwork instance.

package main
import (

"github.com/coretrix/clockwork"
"github.com/go-redis/redis"
)


func main()  {
 client := redis.NewClient(&redis.Options{
 		Addr:     "127.0.0.1:6379", //your address
 		Password: "", // your password
 		DB:       0, 
 	})
 	
 var redisDataProvider clockwork.DataProviderInterface
 redisDataProvider = clockwork.RedisDataProvider{RedisStorageProvider: client}
 profiler := clockwork.Clockwork{DataProvider: redisDataProvider}
}

Mysql data source

var mysqlDataSource dataSource.QueryLoggerDataSourceInterface = &dataSource.MysqlDataSource{}
profiler.SetDatabaseDataSource(mysqlDataSource)

var bind1 []interface{}
var bind2 []interface{}
bind2 = append(bind2, 1, 2, "test param")
mysqlDataSource.LogQuery("mysql", "SELECT * FROM users", 12.224, bind1)
mysqlDataSource.LogQuery("mysql", "SELECT * FROM address where id = ?", 1, bind2)

Redis data source

var redisDataSource dataSource.CommandLoggerDataSourceInterface = &dataSource.RedisDataSource{}
profiler.AddDataSource(redisDataSource)

redisDataSource.LogCommand("hSet", "test_key_1", 0.12)
redisDataSource.LogCommand("hGet", "test_key_2", 0.15)

Cache data source

var cacheDataSource dataSource.CacheLoggerDataSourceInterface = &dataSource.CacheDataSource{}
profiler.AddDataSource(cacheDataSource)

cacheDataSource.LogCache("miss", "price", "30.10$", 12.22, 3000)

Timeline data source

var timelineDataSource dataSource.TimelineLoggerDataSourceInterface = &dataSource.TimelineDataSource{}
profiler.SetTimeLineDataSource(timelineDataSource)

profiler.GetTimeLineDataSource().StartEvent("Event_11", "My first event desc")
//put some logic here
profiler.GetTimeLineDataSource().EndEvent("Event_11")

profiler.GetTimeLineDataSource().StartEvent("Event_22", "My second event desc")
//put some logic here
profiler.GetTimeLineDataSource().EndEvent("Event_22")

Request data source

var requestDataSource dataSource.RequestLoggerDataSourceInterface = &dataSource.RequestResponseDataSource{}
profiler.SetRequestDataSource(requestDataSource)

//in the begining of request
profiler.GetRequestDataSource().SetStartTime(time.Now())
profiler.GetRequestDataSource().StartMemoryUsage()
profiler.GetRequestDataSource().SetController("HomeController", "IndexAction")
profiler.GetRequestDataSource().SetMiddleware([]string{"Authorize", "Normalization", "Guard", "Handler"})

//at the end of request	
profiler.GetRequestDataSource().SetResponseTime(time.Now())
profiler.GetRequestDataSource().SetResponseStatus(200)

Logger(debugger) data source

var loggerDataSource dataSource.LoggerDataSourceInterface = &dataSource.LoggerDataSource{}
profiler.SetLoggerDataSource(loggerDataSource)

profiler.GetLoggerDataSource().LogDebugString("test payment", "payment method works")
test1 := make([]string, 2)
test1[0] = "item1"
test1[1] = "item 2"
profiler.GetLoggerDataSource().LogDebugSlice("cart", test1)

users := make(map[string]string)
users["Adam"] = "33 ages"
users["Fred"] = "15 ages"
profiler.GetLoggerDataSource().LogDebugMap("users", users)	

Before end of the request

You should call this method

profiler.SaveData()

The last thing u need to do is to send 2 special headers into the response:

c.Writer.Header().Set("X-Clockwork-Id", profiler.GetUniqueID())
c.Writer.Header().Set("X-Clockwork-Version", "4.0.13")