Skip to content
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

Polish public interface for dbkit (root) and dbrutil packages #11

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dbrutil/composite_event_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"github.com/gocraft/dbr/v2"
)

// CompositeEventReceiver represents a composition of event receivers from dbr package and implements Composite design pattern.
// CompositeEventReceiver represents a composition of event receivers from dbr package
// and implements a Composite design pattern.
type CompositeEventReceiver struct {
Receivers []dbr.EventReceiver
}
Expand Down
14 changes: 10 additions & 4 deletions dbrutil/metrics.go → dbrutil/query_metrics_event_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ package dbrutil
import (
"time"

"github.com/acronis/go-dbkit"
"github.com/gocraft/dbr/v2"
)

// MetricsCollector is an interface for collecting metrics about SQL queries.
type MetricsCollector interface {
ObserveQueryDuration(query string, duration time.Duration)
}

// QueryMetricsEventReceiverOpts consists options for QueryMetricsEventReceiver.
type QueryMetricsEventReceiverOpts struct {
AnnotationPrefix string
Expand All @@ -23,13 +27,15 @@ type QueryMetricsEventReceiverOpts struct {
// To be collected, SQL query should be annotated (comment starting with specified prefix).
type QueryMetricsEventReceiver struct {
*dbr.NullEventReceiver
metricsCollector dbkit.MetricsCollector
metricsCollector MetricsCollector
annotationPrefix string
annotationModifier func(string) string
}

// NewQueryMetricsEventReceiverWithOpts creates a new QueryMetricsEventReceiver with additinal options.
func NewQueryMetricsEventReceiverWithOpts(mc *dbkit.PrometheusMetrics, options QueryMetricsEventReceiverOpts) *QueryMetricsEventReceiver {
func NewQueryMetricsEventReceiverWithOpts(
mc MetricsCollector, options QueryMetricsEventReceiverOpts,
) *QueryMetricsEventReceiver {
return &QueryMetricsEventReceiver{
metricsCollector: mc,
annotationPrefix: options.AnnotationPrefix,
Expand All @@ -38,7 +44,7 @@ func NewQueryMetricsEventReceiverWithOpts(mc *dbkit.PrometheusMetrics, options Q
}

// NewQueryMetricsEventReceiver creates a new QueryMetricsEventReceiver.
func NewQueryMetricsEventReceiver(mc *dbkit.PrometheusMetrics, annotationPrefix string) *QueryMetricsEventReceiver {
func NewQueryMetricsEventReceiver(mc MetricsCollector, annotationPrefix string) *QueryMetricsEventReceiver {
options := QueryMetricsEventReceiverOpts{
AnnotationPrefix: annotationPrefix,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/gocraft/dbr/v2"
)

// SlowQueryLogEventReceiverOpts consists options for SlowQueryLogEventReceiver.
// SlowQueryLogEventReceiverOpts contains options for SlowQueryLogEventReceiver.
type SlowQueryLogEventReceiverOpts struct {
AnnotationPrefix string
AnnotationModifier func(string) string
Expand All @@ -29,9 +29,10 @@ type SlowQueryLogEventReceiver struct {
annotationModifier func(string) string
}

// NewSlowQueryLogEventReceiverWithOpts creates a new SlowQueryLogEventReceiver with additinal options.
func NewSlowQueryLogEventReceiverWithOpts(logger log.FieldLogger, longQueryTime time.Duration,
options SlowQueryLogEventReceiverOpts) *SlowQueryLogEventReceiver {
// NewSlowQueryLogEventReceiverWithOpts creates a new SlowQueryLogEventReceiver with additional options.
func NewSlowQueryLogEventReceiverWithOpts(
logger log.FieldLogger, longQueryTime time.Duration, options SlowQueryLogEventReceiverOpts,
) *SlowQueryLogEventReceiver {
return &SlowQueryLogEventReceiver{
NullEventReceiver: &dbr.NullEventReceiver{},
logger: logger,
Expand Down
12 changes: 4 additions & 8 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

type MetricsCollector interface {
ObserveQueryDuration(query string, duration time.Duration)
}

// PrometheusMetricsLabelQuery is a label name for SQL query in Prometheus metrics.
const PrometheusMetricsLabelQuery = "query"

// DefaultQueryDurationBuckets is default buckets into which observations of executing SQL queries are counted.
var DefaultQueryDurationBuckets = []float64{0.001, 0.01, 0.1, 0.25, 0.5, 1, 2.5, 5, 10}

// MetricsCollectorOpts represents an options for PrometheusMetrics.
type MetricsCollectorOpts struct {
// PrometheusMetricsOpts represents an options for PrometheusMetrics.
type PrometheusMetricsOpts struct {
// Namespace is a namespace for metrics. It will be prepended to all metric names.
Namespace string

Expand All @@ -48,11 +44,11 @@ type PrometheusMetrics struct {

// NewPrometheusMetrics creates a new metrics collector.
func NewPrometheusMetrics() *PrometheusMetrics {
return NewPrometheusMetricsWithOpts(MetricsCollectorOpts{})
return NewPrometheusMetricsWithOpts(PrometheusMetricsOpts{})
}

// NewPrometheusMetricsWithOpts is a more configurable version of creating PrometheusMetrics.
func NewPrometheusMetricsWithOpts(opts MetricsCollectorOpts) *PrometheusMetrics {
func NewPrometheusMetricsWithOpts(opts PrometheusMetricsOpts) *PrometheusMetrics {
queryDurationBuckets := opts.QueryDurationBuckets
if queryDurationBuckets == nil {
queryDurationBuckets = DefaultQueryDurationBuckets
Expand Down