Skip to content

Commit

Permalink
Rename MetricsCollectorOpts to PrometheusMetricsOpts for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
vasayxtx committed Feb 20, 2025
1 parent 6427575 commit 9954453
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
12 changes: 8 additions & 4 deletions 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,14 +27,14 @@ 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.MetricsCollector, options QueryMetricsEventReceiverOpts,
mc MetricsCollector, options QueryMetricsEventReceiverOpts,
) *QueryMetricsEventReceiver {
return &QueryMetricsEventReceiver{
metricsCollector: mc,
Expand All @@ -40,7 +44,7 @@ func NewQueryMetricsEventReceiverWithOpts(
}

// NewQueryMetricsEventReceiver creates a new QueryMetricsEventReceiver.
func NewQueryMetricsEventReceiver(mc dbkit.MetricsCollector, annotationPrefix string) *QueryMetricsEventReceiver {
func NewQueryMetricsEventReceiver(mc MetricsCollector, annotationPrefix string) *QueryMetricsEventReceiver {
options := QueryMetricsEventReceiverOpts{
AnnotationPrefix: annotationPrefix,
}
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

0 comments on commit 9954453

Please sign in to comment.