Skip to content

Commit

Permalink
grpc-ecosystemgh-60 NewClientMetrics is using collectorOptions direct…
Browse files Browse the repository at this point in the history
…ly so it can access buckets
  • Loading branch information
piotrkowalczuk committed Oct 21, 2018
1 parent 85d9d85 commit d784bec
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
26 changes: 20 additions & 6 deletions client_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,28 @@ type ClientMetrics struct {
// ClientMetrics when not using the default Prometheus monitor registry, for
// example when wanting to control which monitor are added to a registry as
// opposed to automatically adding monitor via init functions.
func NewClientMetrics(counterOpts ...CollectorOption) *ClientMetrics {
opts := counterOptions(counterOpts).apply(prom.Opts{
Namespace: namespace,
Subsystem: "client",
})
func NewClientMetrics(opts ...CollectorOption) *ClientMetrics {
var collectorOptions collectorOptions
for _, fn := range opts {
fn(&collectorOptions)
}

if collectorOptions.namespace == "" {
collectorOptions.namespace = namespace
}
if collectorOptions.subsystem == "" {
collectorOptions.subsystem = "client"
}

return &ClientMetrics{
clientMonitor: initClientMonitor(opts),
clientMonitor: initClientMonitor(
prom.Opts{
Namespace: collectorOptions.namespace,
Subsystem: collectorOptions.subsystem,
ConstLabels: collectorOptions.constLabels,
},
collectorOptions.buckets,
),
}
}

Expand Down
21 changes: 11 additions & 10 deletions metric_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import (
prom "github.com/prometheus/client_golang/prometheus"
)

type options struct {
// CollectorOption lets you add options to monitor using With* funcs.
type CollectorOption func(*collectorOptions)

type collectorOptions struct {
namespace, subsystem string
constLabels prom.Labels
buckets []float64
}

// A CollectorOption lets you add options to monitor using With* funcs.
type CollectorOption func(*options)

type counterOptions []CollectorOption

// TODO: remove once ServerMetrics is changed.
func (co counterOptions) apply(base prom.Opts) prom.Opts {
var opts options
var opts collectorOptions
for _, f := range co {
f(&opts)
}
Expand All @@ -34,28 +35,28 @@ func (co counterOptions) apply(base prom.Opts) prom.Opts {

// WithConstLabels allows you to add ConstLabels to Counter monitor.
func WithConstLabels(labels prom.Labels) CollectorOption {
return func(o *options) {
return func(o *collectorOptions) {
o.constLabels = labels
}
}

// WithSubsystem allows to change default subsystem.
// WithNamespace allows to change default subsystem.
func WithNamespace(namespace string) CollectorOption {
return func(o *options) {
return func(o *collectorOptions) {
o.namespace = namespace
}
}

// WithSubsystem allows to change default subsystem.
func WithSubsystem(subsystem string) CollectorOption {
return func(o *options) {
return func(o *collectorOptions) {
o.subsystem = subsystem
}
}

// WithBuckets allows you to specify custom bucket ranges for histograms.
func WithBuckets(buckets []float64) CollectorOption {
return func(o *options) {
return func(o *collectorOptions) {
o.buckets = buckets
}
}
4 changes: 2 additions & 2 deletions monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type clientMonitor struct {

var _ prometheus.Collector = &clientMonitor{}

func initClientMonitor(opts prometheus.Opts) *clientMonitor {
func initClientMonitor(opts prometheus.Opts, buckets []float64) *clientMonitor {
dialer := prometheus.NewCounterVec(
counterOpts(opts, "reconnects_total", "A total number of reconnects made by the client."),
[]string{"address"},
Expand Down Expand Up @@ -112,7 +112,7 @@ func initClientMonitor(opts prometheus.Opts) *clientMonitor {

// Histograms
requestDuration := prometheus.NewHistogramVec(
histogramOpts(opts, "request_duration_histogram_seconds", "The RPC request latencies in seconds on the client side.", nil),
histogramOpts(opts, "request_duration_histogram_seconds", "The RPC request latencies in seconds on the client side.", buckets),
[]string{labelService, labelMethod, labelCode, labelType},
)

Expand Down
4 changes: 2 additions & 2 deletions server_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func NewServerMetrics(counterOpts ...CollectorOption) *ServerMetrics {

//// EnableHandlingTimeHistogram enables histograms being registered when
//// registering the ServerMetrics on a Prometheus registry. Histograms can be
//// expensive on Prometheus servers. It takes options to configure histogram
//// options such as the defined buckets.
//// expensive on Prometheus servers. It takes collectorOptions to configure histogram
//// collectorOptions such as the defined buckets.
//func (m *ServerMetrics) EnableHandlingTimeHistogram(opts ...HistogramCollectorOption) {
// for _, o := range opts {
// o(&m.serverHandledHistogramOpts)
Expand Down

0 comments on commit d784bec

Please sign in to comment.