-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add Prometheus metric to track sql stmts per transaction #29
Merged
yvardhineni
merged 6 commits into
infobloxopen:main
from
yvardhineni:add_metric_to_track_exec_querys_per_transaction
Jun 19, 2024
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1bff9ed
feat: Add Prometheus metric to track exec querys per transaction
yvardhineni 612f262
feat: Add Prometheus metric to track exec querys per transaction
yvardhineni 7f60ff2
downgraded prom versions to support go 1.19
yvardhineni e5cc080
fix ut
yvardhineni 5fd9914
naming conventions
yvardhineni 700112e
removed interceptor
yvardhineni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package hotload | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
const ( | ||
GRPCMethodKey = "grpc_method" | ||
GRPCServiceKey = "grpc_service" | ||
) | ||
|
||
// execQuerySummary is a prometheus metric to keep track of the number of times | ||
// exec query is called in a transaction | ||
var execQuerySummary = prometheus.NewSummaryVec(prometheus.SummaryOpts{ | ||
Name: "transaction_exec_query_total", | ||
Help: "The number of times exec query is called in a transaction", | ||
}, []string{GRPCServiceKey, GRPCMethodKey}) | ||
|
||
func init() { | ||
prometheus.MustRegister(execQuerySummary) | ||
} | ||
|
||
// PromUnaryServerInterceptor returns a unary server interceptor that sets the | ||
// prometheus labels for the grpc service and method. This is useful for | ||
// population the prometheus metrics. | ||
func PromUnaryServerInterceptor() func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||
yvardhineni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||
service, method := splitMethod(info.FullMethod) | ||
labels := map[string]string{ | ||
GRPCMethodKey: method, | ||
GRPCServiceKey: service, | ||
} | ||
ctx = ContextWithExecLabels(ctx, labels) | ||
return handler(ctx, req) | ||
} | ||
} | ||
|
||
func splitMethod(fullMethod string) (service, method string) { | ||
// fullMethod is in the form "/service/method" | ||
// We need to split it into service and method | ||
split := strings.Split(fullMethod, "/") | ||
if len(split) != 3 { | ||
return "unknown", "unknown" | ||
} | ||
return split[1], split[2] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package hotload | ||
|
||
import ( | ||
"context" | ||
"database/sql/driver" | ||
) | ||
|
||
// managedTx wraps a sql/driver.Tx so that it can store the context of the | ||
// transaction and clean up the execQueryCounter on Commit or Rollback. | ||
type managedTx struct { | ||
tx driver.Tx | ||
conn *managedConn | ||
ctx context.Context | ||
} | ||
|
||
func (t *managedTx) Commit() error { | ||
err := t.tx.Commit() | ||
t.cleanup() | ||
return err | ||
} | ||
|
||
func (t *managedTx) Rollback() error { | ||
err := t.tx.Rollback() | ||
t.cleanup() | ||
return err | ||
} | ||
|
||
func observeExecQuerySummary(ctx context.Context, counter int) { | ||
labels := GetExecLabelsFromContext(ctx) | ||
execQuerySummary.WithLabelValues(labels[GRPCServiceKey], labels[GRPCMethodKey]).Observe(float64(counter)) | ||
} | ||
|
||
func (t *managedTx) cleanup() { | ||
observeExecQuerySummary(t.ctx, t.conn.execQueryCounter) | ||
t.conn.resetExecQueryCounter() | ||
} | ||
|
||
var promLabelKey = struct{}{} | ||
|
||
func ContextWithExecLabels(ctx context.Context, labels map[string]string) context.Context { | ||
return context.WithValue(ctx, promLabelKey, labels) | ||
} | ||
|
||
func GetExecLabelsFromContext(ctx context.Context) map[string]string { | ||
if ctx == nil { | ||
return nil | ||
} | ||
|
||
if ctx.Value(promLabelKey) == nil { | ||
return nil | ||
} | ||
|
||
return ctx.Value(promLabelKey).(map[string]string) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Exec() call above also needs an inc()