grpc/interceptors/metrics/options.go
Adphi 3a3d77169c
interceptors: migrate to otel and add logging interceptor
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
2024-10-17 17:15:05 +02:00

60 lines
1.2 KiB
Go

package metrics
import (
"context"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
"github.com/prometheus/client_golang/prometheus"
)
type ExemplarFromCtxFunc func(ctx context.Context) prometheus.Labels
type Option func(*options)
func WithCounterOptons(opts ...grpc_prometheus.CounterOption) Option {
return func(o *options) {
o.copts = append(o.copts, opts...)
}
}
func WithHandlingTimeHistogram(opts ...grpc_prometheus.HistogramOption) Option {
return func(o *options) {
o.hopts = append(o.hopts, opts...)
}
}
func WithHistogramOpts(opts ...grpc_prometheus.HistogramOption) Option {
return func(o *options) {
o.hopts = append(o.hopts, opts...)
}
}
func WithExemplarFromContext(fn ExemplarFromCtxFunc) Option {
return func(o *options) {
o.fn = fn
}
}
func WithRegisterer(reg prometheus.Registerer) Option {
return func(o *options) {
o.reg = reg
}
}
type options struct {
copts []grpc_prometheus.CounterOption
hopts []grpc_prometheus.HistogramOption
fn func(ctx context.Context) prometheus.Labels
reg prometheus.Registerer
}
func (o *options) apply(opts ...Option) *options {
for _, v := range opts {
v(o)
}
if o.reg == nil {
o.reg = prometheus.DefaultRegisterer
}
return o
}