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

OTEL tweaks #428

Merged
merged 4 commits into from
Dec 17, 2023
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
42 changes: 27 additions & 15 deletions rueidisotel/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,9 @@ import (
)

var (
DefaultHistogramBuckets = []float64{
defaultHistogramBuckets = []float64{
.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10,
}
DefaultDialFn = func(dst string, dialer *net.Dialer, cfg *tls.Config) (conn net.Conn, err error) {
if cfg != nil {
return tls.DialWithDialer(dialer, "tcp", dst, cfg)
}
return dialer.Dial("tcp", dst)
}
)

type HistogramOption struct {
Expand All @@ -44,10 +38,13 @@ func WithHistogramOption(histogramOption HistogramOption) Option {
// - rueidis_dial_conns: number of active connections
// - rueidis_dial_latency: dial latency in seconds
func NewClient(clientOption rueidis.ClientOption, opts ...Option) (rueidis.Client, error) {
oclient := newClient(opts...)
oclient, err := newClient(opts...)
if err != nil {
return nil, err
}

if clientOption.DialFn == nil {
clientOption.DialFn = DefaultDialFn
clientOption.DialFn = defaultDialFn
}

attempt, err := oclient.meter.Int64Counter("rueidis_dial_attempt")
Expand Down Expand Up @@ -90,13 +87,13 @@ func NewClient(clientOption rueidis.ClientOption, opts ...Option) (rueidis.Clien
return oclient, nil
}

func newClient(opts ...Option) *otelclient {
func newClient(opts ...Option) (*otelclient, error) {
cli := &otelclient{}
for _, opt := range opts {
opt(cli)
}
if cli.histogramOption.Buckets == nil {
cli.histogramOption.Buckets = DefaultHistogramBuckets
cli.histogramOption.Buckets = defaultHistogramBuckets
}
if cli.meterProvider == nil {
cli.meterProvider = otel.GetMeterProvider() // Default to global MeterProvider
Expand All @@ -109,9 +106,16 @@ func newClient(opts ...Option) *otelclient {
cli.meter = cli.meterProvider.Meter(name)
cli.tracer = cli.tracerProvider.Tracer(name)
// Now create the counters using the meter
cli.cscMiss, _ = cli.meter.Int64Counter("rueidis_do_cache_miss")
cli.cscHits, _ = cli.meter.Int64Counter("rueidis_do_cache_hits")
return cli
var err error
cli.cscMiss, err = cli.meter.Int64Counter("rueidis_do_cache_miss")
if err != nil {
return nil, err
}
cli.cscHits, err = cli.meter.Int64Counter("rueidis_do_cache_hits")
if err != nil {
return nil, err
}
return cli, nil
}

func trackDialing(
Expand All @@ -132,7 +136,8 @@ func trackDialing(
return nil, err
}

dialLatency.Record(ctx, time.Since(start).Seconds())
// Use floating point division for higher precision (instead of Seconds method).
dialLatency.Record(ctx, float64(time.Since(start))/float64(time.Second))
success.Add(ctx, 1)
conns.Add(ctx, 1)

Expand All @@ -157,3 +162,10 @@ func (t *connTracker) Close() error {

return t.Conn.Close()
}

func defaultDialFn(dst string, dialer *net.Dialer, cfg *tls.Config) (conn net.Conn, err error) {
if cfg != nil {
return tls.DialWithDialer(dialer, "tcp", dst, cfg)
}
return dialer.Dial("tcp", dst)
}
7 changes: 6 additions & 1 deletion rueidisotel/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ var (
var _ rueidis.Client = (*otelclient)(nil)

// WithClient creates a new rueidis.Client with OpenTelemetry tracing enabled.
//
// Deprecated: use NewClient() instead.
func WithClient(client rueidis.Client, opts ...Option) rueidis.Client {
cli := newClient(opts...)
cli, err := newClient(opts...)
if err != nil {
panic(err)
}
cli.client = client
return cli
}
Expand Down