@@ -10,6 +10,8 @@ import (
1010
1111 "github.com/lib/pq"
1212
13+ "github.com/rudderlabs/rudder-go-kit/stats"
14+
1315 "github.com/rudderlabs/rudder-go-kit/bytesize"
1416 "github.com/rudderlabs/rudder-go-kit/config"
1517
@@ -46,15 +48,23 @@ func WithNow(now func() time.Time) Opt {
4648 }
4749}
4850
51+ func WithStats (s stats.Stats ) Opt {
52+ return func (r * repo ) {
53+ r .statsFactory = s
54+ }
55+ }
56+
4957type repo struct {
50- db * sqlmw.DB
51- now func () time.Time
58+ db * sqlmw.DB
59+ now func () time.Time
60+ statsFactory stats.Stats
5261}
5362
5463func newRepo (db * sqlmw.DB , opts ... Opt ) * repo {
5564 r := & repo {
56- db : db ,
57- now : timeutil .Now ,
65+ db : db ,
66+ now : timeutil .Now ,
67+ statsFactory : stats .NOP ,
5868 }
5969 for _ , opt := range opts {
6070 opt (r )
@@ -67,6 +77,10 @@ func (n *repo) resetForWorkspace(
6777 ctx context.Context ,
6878 workspaceIdentifier string ,
6979) error {
80+ defer n .timerStat ("reset_for_workspace" , stats.Tags {
81+ "workspaceIdentifier" : workspaceIdentifier ,
82+ })()
83+
7084 _ , err := n .db .ExecContext (ctx , `
7185 DELETE FROM ` + notifierTableName + `
7286 WHERE workspace = $1;
@@ -86,6 +100,11 @@ func (n *repo) insert(
86100 workspaceIdentifier string ,
87101 batchID string ,
88102) error {
103+ defer n .timerStat ("insert" , stats.Tags {
104+ "workspaceIdentifier" : workspaceIdentifier ,
105+ "jobType" : string (publishRequest .JobType ),
106+ })()
107+
89108 txn , err := n .db .BeginTx (ctx , & sql.TxOptions {})
90109 if err != nil {
91110 return fmt .Errorf ("inserting: begin transaction: %w" , err )
@@ -166,6 +185,8 @@ func (n *repo) pendingByBatchID(
166185 ctx context.Context ,
167186 batchID string ,
168187) (int64 , error ) {
188+ defer n .timerStat ("pending_by_batch_id" , nil )()
189+
169190 var count int64
170191
171192 err := n .db .QueryRowContext (ctx , `
@@ -195,6 +216,8 @@ func (n *repo) getByBatchID(
195216 ctx context.Context ,
196217 batchID string ,
197218) ([]Job , error ) {
219+ defer n .timerStat ("get_by_batch_id" , nil )()
220+
198221 query := `
199222 SELECT
200223 id,
@@ -300,6 +323,8 @@ func (n *repo) deleteByBatchID(
300323 ctx context.Context ,
301324 batchID string ,
302325) error {
326+ defer n .timerStat ("delete_by_batch_id" , nil )()
327+
303328 _ , err := n .db .ExecContext (ctx , `
304329 DELETE FROM ` + notifierTableName + ` WHERE batch_id = $1;
305330 ` ,
@@ -328,6 +353,10 @@ func (n *repo) claim(
328353 ctx context.Context ,
329354 workerID string ,
330355) (* Job , error ) {
356+ defer n .timerStat ("claim" , stats.Tags {
357+ "workerID" : workerID ,
358+ })()
359+
331360 row := n .db .QueryRowContext (ctx , `
332361 UPDATE
333362 ` + notifierTableName + `
@@ -376,6 +405,12 @@ func (n *repo) onClaimFailed(
376405 claimError error ,
377406 maxAttempt int ,
378407) error {
408+ defer n .timerStat ("on_claim_failed" , stats.Tags {
409+ "workspaceIdentifier" : job .WorkspaceIdentifier ,
410+ "jobStatus" : string (job .Status ),
411+ "jobType" : string (job .Type ),
412+ })()
413+
379414 query := fmt .Sprint (`
380415 UPDATE
381416 ` + notifierTableName + `
@@ -415,6 +450,12 @@ func (n *repo) onClaimSuccess(
415450 job * Job ,
416451 payload json.RawMessage ,
417452) error {
453+ defer n .timerStat ("on_claim_success" , stats.Tags {
454+ "workspaceIdentifier" : job .WorkspaceIdentifier ,
455+ "jobStatus" : string (job .Status ),
456+ "jobType" : string (job .Type ),
457+ })()
458+
418459 _ , err := n .db .ExecContext (ctx , `
419460 UPDATE
420461 ` + notifierTableName + `
@@ -442,6 +483,8 @@ func (n *repo) orphanJobIDs(
442483 ctx context.Context ,
443484 intervalInSeconds int ,
444485) ([]int64 , error ) {
486+ defer n .timerStat ("orphan_job_ids" , nil )()
487+
445488 rows , err := n .db .QueryContext (ctx , `
446489 UPDATE
447490 ` + notifierTableName + `
@@ -489,6 +532,8 @@ func (n *repo) orphanJobIDs(
489532}
490533
491534func (n * repo ) refreshClaim (ctx context.Context , jobId int64 ) error {
535+ defer n .timerStat ("refresh_claim" , nil )()
536+
492537 _ , err := n .db .ExecContext (ctx , `
493538 UPDATE
494539 ` + notifierTableName + `
@@ -507,3 +552,13 @@ func (n *repo) refreshClaim(ctx context.Context, jobId int64) error {
507552 }
508553 return nil
509554}
555+
556+ // timerStat returns a function that records the duration of a database action.
557+ func (n * repo ) timerStat (action string , extraTags stats.Tags ) func () {
558+ statName := "notifier_repo_query_duration_seconds"
559+ tags := stats.Tags {"action" : action , "repoType" : notifierTableName }
560+ for k , v := range extraTags {
561+ tags [k ] = v
562+ }
563+ return n .statsFactory .NewTaggedStat (statName , stats .TimerType , tags ).RecordDuration ()
564+ }
0 commit comments