Skip to content

Commit 2b879e1

Browse files
authored
Fix histogram metrics (#348)
1 parent fd08695 commit 2b879e1

File tree

4 files changed

+32
-30
lines changed

4 files changed

+32
-30
lines changed

pkg/gofr/container/container.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,18 @@ func (c *Container) registerFrameworkMetrics() {
115115
c.Metrics().NewGauge("app_go_numGC", "Number of completed Garbage Collector cycles.")
116116
c.Metrics().NewGauge("app_go_sys", "Number of total bytes of memory.")
117117

118-
histogramBuckets := []float64{.001, .003, .005, .01, .02, .03, .05, .1, .2, .3, .5, .75, 1, 2, 3, 5, 10, 30}
119-
120118
// http metrics
121-
c.Metrics().NewHistogram("app_http_response", "Response time of http requests in seconds.", histogramBuckets...)
122-
c.Metrics().NewHistogram("app_http_service_response", "Response time of http service requests in seconds.", histogramBuckets...)
119+
httpBuckets := []float64{.001, .003, .005, .01, .02, .03, .05, .1, .2, .3, .5, .75, 1, 2, 3, 5, 10, 30}
120+
c.Metrics().NewHistogram("app_http_response", "Response time of http requests in seconds.", httpBuckets...)
121+
c.Metrics().NewHistogram("app_http_service_response", "Response time of http service requests in seconds.", httpBuckets...)
123122

124123
// redis metrics
125-
c.Metrics().NewHistogram("app_redis_stats", "Observes the response time for Redis commands.", histogramBuckets...)
124+
redisBuckets := []float64{50, 75, 100, 125, 150, 200, 300, 500, 750, 1000, 1250, 1500, 2000, 2500, 3000}
125+
c.Metrics().NewHistogram("app_redis_stats", "Response time of Redis commands in microseconds.", redisBuckets...)
126126

127127
// sql metrics
128-
c.Metrics().NewHistogram("app_sql_stats", "Observes the response time for SQL queries.", histogramBuckets...)
128+
sqlBuckets := []float64{50, 75, 100, 125, 150, 200, 300, 500, 750, 1000, 2000, 3000, 4000, 5000, 7500, 10000}
129+
c.Metrics().NewHistogram("app_sql_stats", "Response time of SQL queries in microseconds.", sqlBuckets...)
129130
c.Metrics().NewGauge("app_sql_open_connections", "Number of open SQL connections.")
130131
c.Metrics().NewGauge("app_sql_inUse_connections", "Number of inUse SQL connections.")
131132
}

pkg/gofr/datasource/redis/hook.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ func (ql QueryLog) String() string {
4444

4545
// logQuery logs the Redis query information.
4646
func (r *redisHook) logQuery(start time.Time, query string, args ...interface{}) {
47-
duration := time.Since(start)
47+
duration := time.Since(start).Microseconds()
4848

4949
r.logger.Debug(QueryLog{
5050
Query: query,
51-
Duration: duration.Microseconds(),
51+
Duration: duration,
5252
Args: args,
5353
})
5454

5555
r.metrics.RecordHistogram(context.Background(), "app_redis_stats",
56-
duration.Seconds(), "type", query)
56+
float64(duration), "type", query)
5757
}
5858

5959
// DialHook implements the redis.DialHook interface.

pkg/gofr/datasource/sql/db.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ type Log struct {
2828
}
2929

3030
func (d *DB) logQuery(start time.Time, queryType, query string, args ...interface{}) {
31-
duration := time.Since(start)
31+
duration := time.Since(start).Microseconds()
3232

3333
d.logger.Debug(Log{
3434
Type: queryType,
3535
Query: query,
36-
Duration: duration.Microseconds(),
36+
Duration: duration,
3737
Args: args,
3838
})
3939

40-
d.metrics.RecordHistogram(context.Background(), "app_sql_stats",
41-
duration.Seconds(), "type", getOperationType(query))
40+
d.metrics.RecordHistogram(context.Background(), "app_sql_stats", float64(duration),
41+
"type", getOperationType(query))
4242
}
4343

4444
func getOperationType(query string) string {
@@ -94,16 +94,17 @@ type Tx struct {
9494
}
9595

9696
func (t *Tx) logQuery(start time.Time, queryType, query string, args ...interface{}) {
97-
duration := time.Since(start)
97+
duration := time.Since(start).Microseconds()
9898

9999
t.logger.Debug(Log{
100100
Type: queryType,
101101
Query: query,
102-
Duration: duration.Microseconds(),
102+
Duration: duration,
103103
Args: args,
104104
})
105105

106-
t.metrics.RecordHistogram(context.Background(), "app_sql_stats", duration.Seconds())
106+
t.metrics.RecordHistogram(context.Background(), "app_sql_stats", float64(duration),
107+
"type", getOperationType(query))
107108
}
108109

109110
func (t *Tx) Query(query string, args ...interface{}) (*sql.Rows, error) {

pkg/gofr/datasource/sql/db_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ func TestTx_Query(t *testing.T) {
602602
mock.ExpectQuery("SELECT 1").
603603
WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow("1"))
604604
mockMetrics.EXPECT().RecordHistogram(gomock.Any(), "app_sql_stats",
605-
gomock.Any())
605+
gomock.Any(), "type", "SELECT")
606606

607607
rows, err = tx.Query("SELECT 1")
608608
assert.Nil(t, err)
@@ -632,7 +632,7 @@ func TestTx_QueryError(t *testing.T) {
632632
mock.ExpectQuery("SELECT ").
633633
WillReturnError(errSyntax)
634634
mockMetrics.EXPECT().RecordHistogram(gomock.Any(), "app_sql_stats",
635-
gomock.Any())
635+
gomock.Any(), "type", "SELECT")
636636

637637
rows, err = tx.Query("SELECT")
638638
if !assert.Nil(t, rows) {
@@ -665,7 +665,7 @@ func TestTx_QueryRow(t *testing.T) {
665665
mock.ExpectQuery("SELECT name FROM employee WHERE id = ?").WithArgs(1).
666666
WillReturnRows(sqlmock.NewRows([]string{"name"}).AddRow("jhon"))
667667
mockMetrics.EXPECT().RecordHistogram(gomock.Any(), "app_sql_stats",
668-
gomock.Any())
668+
gomock.Any(), "type", "SELECT")
669669

670670
row = tx.QueryRow("SELECT name FROM employee WHERE id = ?", 1)
671671
assert.NotNil(t, row)
@@ -692,7 +692,7 @@ func TestTx_QueryRowContext(t *testing.T) {
692692

693693
mock.ExpectQuery("SELECT name FROM employee WHERE id = ?").WithArgs(1)
694694
mockMetrics.EXPECT().RecordHistogram(gomock.Any(), "app_sql_stats",
695-
gomock.Any())
695+
gomock.Any(), "type", "SELECT")
696696

697697
row = tx.QueryRowContext(context.Background(), "SELECT name FROM employee WHERE id = ?", 1)
698698
assert.NotNil(t, row)
@@ -721,7 +721,7 @@ func TestTx_Exec(t *testing.T) {
721721
mock.ExpectExec("INSERT INTO employee VALUES(?, ?)").
722722
WithArgs(2, "doe").WillReturnResult(sqlmock.NewResult(1, 1))
723723
mockMetrics.EXPECT().RecordHistogram(gomock.Any(), "app_sql_stats",
724-
gomock.Any())
724+
gomock.Any(), "type", "INSERT")
725725

726726
res, err = tx.Exec("INSERT INTO employee VALUES(?, ?)", 2, "doe")
727727
assert.Nil(t, err)
@@ -751,7 +751,7 @@ func TestTx_ExecError(t *testing.T) {
751751
mock.ExpectExec("INSERT INTO employee VALUES(?, ?").
752752
WithArgs(2, "doe").WillReturnError(errSyntax)
753753
mockMetrics.EXPECT().RecordHistogram(gomock.Any(), "app_sql_stats",
754-
gomock.Any())
754+
gomock.Any(), "type", "INSERT")
755755

756756
res, err = tx.Exec("INSERT INTO employee VALUES(?, ?", 2, "doe")
757757
assert.Nil(t, res)
@@ -782,7 +782,7 @@ func TestTx_ExecContext(t *testing.T) {
782782
mock.ExpectExec(`INSERT INTO employee VALUES(?, ?)`).
783783
WithArgs(2, "doe").WillReturnResult(sqlmock.NewResult(1, 1))
784784
mockMetrics.EXPECT().RecordHistogram(gomock.Any(), "app_sql_stats",
785-
gomock.Any())
785+
gomock.Any(), "type", "INSERT")
786786

787787
res, err = tx.ExecContext(context.Background(), "INSERT INTO employee VALUES(?, ?)", 2, "doe")
788788
assert.Nil(t, err)
@@ -812,7 +812,7 @@ func TestTx_ExecContextError(t *testing.T) {
812812
mock.ExpectExec(`INSERT INTO employee VALUES(?, ?)`).
813813
WithArgs(2, "doe").WillReturnResult(sqlmock.NewResult(1, 1))
814814
mockMetrics.EXPECT().RecordHistogram(gomock.Any(), "app_sql_stats",
815-
gomock.Any())
815+
gomock.Any(), "type", "INSERT")
816816

817817
res, err = tx.ExecContext(context.Background(), "INSERT INTO employee VALUES(?, ?)", 2, "doe")
818818
assert.Nil(t, err)
@@ -841,7 +841,7 @@ func TestTx_Prepare(t *testing.T) {
841841

842842
mock.ExpectPrepare("SELECT name FROM employee WHERE id = ?")
843843
mockMetrics.EXPECT().RecordHistogram(gomock.Any(), "app_sql_stats",
844-
gomock.Any())
844+
gomock.Any(), "type", "SELECT")
845845

846846
stmt, err = tx.Prepare("SELECT name FROM employee WHERE id = ?")
847847
assert.Nil(t, err)
@@ -870,7 +870,7 @@ func TestTx_PrepareError(t *testing.T) {
870870

871871
mock.ExpectPrepare("SELECT name FROM employee WHERE id = ?")
872872
mockMetrics.EXPECT().RecordHistogram(gomock.Any(), "app_sql_stats",
873-
gomock.Any())
873+
gomock.Any(), "type", "SELECT")
874874

875875
stmt, err = tx.Prepare("SELECT name FROM employee WHERE id = ?")
876876
assert.Nil(t, err)
@@ -894,7 +894,7 @@ func TestTx_Commit(t *testing.T) {
894894
tx := getTransaction(db, mock)
895895

896896
mockMetrics.EXPECT().RecordHistogram(gomock.Any(), "app_sql_stats",
897-
gomock.Any())
897+
gomock.Any(), "type", "COMMIT")
898898
mock.ExpectCommit()
899899

900900
err = tx.Commit()
@@ -918,7 +918,7 @@ func TestTx_CommitError(t *testing.T) {
918918
tx := getTransaction(db, mock)
919919

920920
mockMetrics.EXPECT().RecordHistogram(gomock.Any(), "app_sql_stats",
921-
gomock.Any())
921+
gomock.Any(), "type", "COMMIT")
922922
mock.ExpectCommit().WillReturnError(errDB)
923923

924924
err = tx.Commit()
@@ -943,7 +943,7 @@ func TestTx_RollBack(t *testing.T) {
943943
tx := getTransaction(db, mock)
944944

945945
mockMetrics.EXPECT().RecordHistogram(gomock.Any(), "app_sql_stats",
946-
gomock.Any())
946+
gomock.Any(), "type", "ROLLBACK")
947947
mock.ExpectRollback()
948948

949949
err = tx.Rollback()
@@ -967,7 +967,7 @@ func TestTx_RollbackError(t *testing.T) {
967967
tx := getTransaction(db, mock)
968968

969969
mockMetrics.EXPECT().RecordHistogram(gomock.Any(), "app_sql_stats",
970-
gomock.Any())
970+
gomock.Any(), "type", "ROLLBACK")
971971
mock.ExpectRollback().WillReturnError(errDB)
972972

973973
err = tx.Rollback()

0 commit comments

Comments
 (0)