Skip to content

Commit

Permalink
refactor: use point receivers
Browse files Browse the repository at this point in the history
  • Loading branch information
Reasno committed Aug 31, 2021
1 parent 4a807d3 commit 4baa8bc
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 78 deletions.
42 changes: 22 additions & 20 deletions otgorm/gorm_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,34 @@ type Gauges struct {
}

// DBName sets the dbname label of metrics.
func (g Gauges) DBName(dbName string) Gauges {
g.dbName = dbName
return g
func (g *Gauges) DBName(dbName string) *Gauges {
withValues := []string{"dbname", g.dbName}
return &Gauges{
Idle: g.Idle.With(withValues...),
InUse: g.InUse.With(withValues...),
Open: g.Open.With(withValues...),
dbName: dbName,
driver: g.driver,
}
}

// Driver sets the driver label of metrics.
func (g Gauges) Driver(driver string) Gauges {
g.driver = driver
return g
func (g *Gauges) Driver(driver string) *Gauges {
withValues := []string{"driver", driver}
return &Gauges{
Idle: g.Idle.With(withValues...),
InUse: g.InUse.With(withValues...),
Open: g.Open.With(withValues...),
dbName: g.dbName,
driver: driver,
}
}

// Observe records the DBStats collected. It should be called periodically.
func (g Gauges) Observe(stats sql.DBStats) Gauges {
withValues := []string{"dbname", g.dbName, "driver", g.driver}
g.Idle.
With(withValues...).
Set(float64(stats.Idle))

g.InUse.
With(withValues...).
Set(float64(stats.InUse))

g.Open.
With(withValues...).
Set(float64(stats.OpenConnections))
return g
func (g *Gauges) Observe(stats sql.DBStats) {
g.Idle.Set(float64(stats.Idle))
g.InUse.Set(float64(stats.InUse))
g.Open.Set(float64(stats.OpenConnections))
}

// newCollector creates a new database wrapper containing the name of the database,
Expand Down
51 changes: 46 additions & 5 deletions otkafka/reader_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,55 @@ type ReaderStats struct {
}

// Reader sets the writer label in WriterStats.
func (r ReaderStats) Reader(reader string) ReaderStats {
r.reader = reader
return r
func (r *ReaderStats) Reader(reader string) *ReaderStats {
withValues := []string{"reader", reader}
return &ReaderStats{
Dials: r.Dials.With(withValues...),
Fetches: r.Fetches.With(withValues...),
Messages: r.Messages.With(withValues...),
Bytes: r.Bytes.With(withValues...),
Rebalances: r.Rebalances.With(withValues...),
Timeouts: r.Timeouts.With(withValues...),
Errors: r.Errors.With(withValues...),
Offset: r.Offset.With(withValues...),
Lag: r.Lag.With(withValues...),
MinBytes: r.MinBytes.With(withValues...),
MaxBytes: r.MaxBytes.With(withValues...),
MaxWait: r.MaxWait.With(withValues...),
QueueLength: r.QueueLength.With(withValues...),
QueueCapacity: r.QueueCapacity.With(withValues...),
DialTime: AggStats{
Min: r.DialTime.Min.With(withValues...),
Max: r.DialTime.Max.With(withValues...),
Avg: r.DialTime.Avg.With(withValues...),
},
ReadTime: AggStats{
Min: r.ReadTime.Min.With(withValues...),
Max: r.ReadTime.Max.With(withValues...),
Avg: r.ReadTime.Avg.With(withValues...),
},
WaitTime: AggStats{
Min: r.WaitTime.Min.With(withValues...),
Max: r.WaitTime.Max.With(withValues...),
Avg: r.WaitTime.Avg.With(withValues...),
},
FetchSize: AggStats{
Min: r.FetchSize.Min.With(withValues...),
Max: r.FetchSize.Max.With(withValues...),
Avg: r.FetchSize.Avg.With(withValues...),
},
FetchBytes: AggStats{
Min: r.FetchBytes.Min.With(withValues...),
Max: r.FetchBytes.Max.With(withValues...),
Avg: r.FetchBytes.Avg.With(withValues...),
},
reader: reader,
}
}

// Observe records the reader stats. It should be called periodically.
func (r ReaderStats) Observe(stats kafka.ReaderStats) {
withValues := []string{"reader", r.reader, "client_id", stats.ClientID, "topic", stats.Topic, "partition", stats.Partition}
func (r *ReaderStats) Observe(stats kafka.ReaderStats) {
withValues := []string{"client_id", stats.ClientID, "topic", stats.Topic, "partition", stats.Partition}
r.Dials.With(withValues...).Add(float64(stats.Dials))
r.Fetches.With(withValues...).Add(float64(stats.Fetches))
r.Messages.With(withValues...).Add(float64(stats.Messages))
Expand Down
43 changes: 38 additions & 5 deletions otkafka/writer_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,47 @@ type WriterStats struct {
}

// Writer sets the writer label in WriterStats.
func (w WriterStats) Writer(writer string) WriterStats {
w.writer = writer
return w
func (w *WriterStats) Writer(writer string) *WriterStats {
withValues := []string{"writer", writer}
return &WriterStats{
Writes: w.Writes.With(withValues...),
Messages: w.Messages.With(withValues...),
Bytes: w.Bytes.With(withValues...),
Errors: w.Errors.With(withValues...),
MaxAttempts: w.MaxAttempts.With(withValues...),
MaxBatchSize: w.MaxBatchSize.With(withValues...),
BatchTimeout: w.BatchTimeout.With(withValues...),
ReadTimeout: w.ReadTimeout.With(withValues...),
WriteTimeout: w.WriteTimeout.With(withValues...),
RequiredAcks: w.RequiredAcks.With(withValues...),
BatchTime: AggStats{
Min: w.BatchTime.Min.With(withValues...),
Max: w.BatchTime.Max.With(withValues...),
Avg: w.BatchTime.Avg.With(withValues...),
},
WriteTime: AggStats{
Min: w.WriteTime.Min.With(withValues...),
Max: w.WriteTime.Max.With(withValues...),
Avg: w.WriteTime.Avg.With(withValues...),
},
WaitTime: AggStats{
Min: w.WaitTime.Min.With(withValues...),
Max: w.WaitTime.Max.With(withValues...),
Avg: w.WaitTime.Avg.With(withValues...),
},
BatchBytes: AggStats{
Min: w.BatchBytes.Min.With(withValues...),
Max: w.BatchBytes.Max.With(withValues...),
Avg: w.BatchBytes.Avg.With(withValues...),
},
Async: w.Async.With(withValues...),
writer: writer,
}
}

// Observe records the writer stats. It should called periodically.
func (w WriterStats) Observe(stats kafka.WriterStats) WriterStats {
withValues := []string{"writer", w.writer, "topic", stats.Topic}
func (w *WriterStats) Observe(stats kafka.WriterStats) *WriterStats {
withValues := []string{"topic", stats.Topic}

w.Writes.With(withValues...).Add(float64(stats.Writes))
w.Messages.With(withValues...).Add(float64(stats.Messages))
Expand Down
41 changes: 18 additions & 23 deletions otredis/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,33 @@ type Gauges struct {
}

// DBName sets the dbname label of redis metrics.
func (r Gauges) DBName(dbName string) Gauges {
r.dbName = dbName
return r
func (g *Gauges) DBName(dbName string) *Gauges {
withValues := []string{"dbname", dbName}
return &Gauges{
Hits: g.Hits.With(withValues...),
Misses: g.Misses.With(withValues...),
Timeouts: g.Timeouts.With(withValues...),
TotalConns: g.TotalConns.With(withValues...),
IdleConns: g.IdleConns.With(withValues...),
StaleConns: g.StaleConns.With(withValues...),
dbName: dbName,
}
}

// Observe records the redis pool stats. It should be called periodically.
func (r Gauges) Observe(stats *redis.PoolStats) {
withValues := []string{"dbname", r.dbName}
func (g *Gauges) Observe(stats *redis.PoolStats) {

r.Hits.
With(withValues...).
Set(float64(stats.Hits))
g.Hits.Set(float64(stats.Hits))

r.Misses.
With(withValues...).
Set(float64(stats.Misses))
g.Misses.Set(float64(stats.Misses))

r.Timeouts.
With(withValues...).
Set(float64(stats.Timeouts))
g.Timeouts.Set(float64(stats.Timeouts))

r.TotalConns.
With(withValues...).
Set(float64(stats.TotalConns))
g.TotalConns.Set(float64(stats.TotalConns))

r.IdleConns.
With(withValues...).
Set(float64(stats.IdleConns))
g.IdleConns.Set(float64(stats.IdleConns))

r.StaleConns.
With(withValues...).
Set(float64(stats.StaleConns))
g.StaleConns.Set(float64(stats.StaleConns))
}

// newCollector creates a new redis wrapper containing the name of the redis.
Expand Down
32 changes: 22 additions & 10 deletions srvgrpc/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,36 @@ type RequestDurationSeconds struct {
}

// Module specifies the module label for RequestDurationSeconds.
func (r RequestDurationSeconds) Module(module string) RequestDurationSeconds {
r.module = module
return r
func (r *RequestDurationSeconds) Module(module string) *RequestDurationSeconds {
return &RequestDurationSeconds{
Histogram: r.Histogram.With("module", module),
module: module,
service: r.service,
route: r.route,
}
}

// Service specifies the service label for RequestDurationSeconds.
func (r RequestDurationSeconds) Service(service string) RequestDurationSeconds {
r.service = service
return r
func (r *RequestDurationSeconds) Service(service string) *RequestDurationSeconds {
return &RequestDurationSeconds{
Histogram: r.Histogram.With("service", service),
module: r.module,
service: service,
route: r.route,
}
}

// Route specifies the method label for RequestDurationSeconds.
func (r RequestDurationSeconds) Route(route string) RequestDurationSeconds {
r.route = route
return r
func (r *RequestDurationSeconds) Route(route string) *RequestDurationSeconds {
return &RequestDurationSeconds{
Histogram: r.Histogram.With("route", route),
module: r.module,
service: r.service,
route: route,
}
}

// Observe records the time taken to process the request.
func (r RequestDurationSeconds) Observe(seconds float64) {
r.Histogram.With("module", r.module, "service", r.service, "route", r.route).Observe(seconds)
r.Histogram.Observe(seconds)
}
5 changes: 3 additions & 2 deletions srvgrpc/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ import (
)

func TestRequestDurationSeconds(t *testing.T) {
rds := RequestDurationSeconds{
rds := &RequestDurationSeconds{
Histogram: generic.NewHistogram("foo", 2),
}
rds = rds.Module("m").Service("s").Route("r")
rds.Observe(5)

assert.Equal(t, 5.0, rds.Histogram.(*generic.Histogram).Quantile(0.5))
assert.ElementsMatch(t, []string{"module", "m", "service", "s", "route", "r"}, rds.Histogram.(*generic.Histogram).LabelValues())

f := grpc.UnaryHandler(func(ctx context.Context, req interface{}) (interface{}, error) {
time.Sleep(time.Millisecond)
return nil, nil
})
_, _ = Metrics(&rds)(context.Background(), nil, &grpc.UnaryServerInfo{FullMethod: "/"}, f)
_, _ = Metrics(rds)(context.Background(), nil, &grpc.UnaryServerInfo{FullMethod: "/"}, f)
assert.GreaterOrEqual(t, 1.0, rds.Histogram.(*generic.Histogram).Quantile(0.5))
}
34 changes: 23 additions & 11 deletions srvhttp/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,36 @@ type RequestDurationSeconds struct {
}

// Module specifies the module label for RequestDurationSeconds.
func (r RequestDurationSeconds) Module(module string) RequestDurationSeconds {
r.module = module
return r
func (r *RequestDurationSeconds) Module(module string) *RequestDurationSeconds {
return &RequestDurationSeconds{
Histogram: r.Histogram.With("module", module),
module: module,
service: r.service,
route: r.route,
}
}

// Service specifies the service label for RequestDurationSeconds.
func (r RequestDurationSeconds) Service(service string) RequestDurationSeconds {
r.service = service
return r
func (r *RequestDurationSeconds) Service(service string) *RequestDurationSeconds {
return &RequestDurationSeconds{
Histogram: r.Histogram.With("service", service),
module: r.module,
service: service,
route: r.route,
}
}

// Route specifies the method label for RequestDurationSeconds.
func (r RequestDurationSeconds) Route(route string) RequestDurationSeconds {
r.route = route
return r
func (r *RequestDurationSeconds) Route(route string) *RequestDurationSeconds {
return &RequestDurationSeconds{
Histogram: r.Histogram.With("route", route),
module: r.module,
service: r.service,
route: route,
}
}

// Observe records the time taken to process the request.
func (r RequestDurationSeconds) Observe(seconds float64) {
r.Histogram.With("module", r.module, "service", r.service, "route", r.route).Observe(seconds)
func (r *RequestDurationSeconds) Observe(seconds float64) {
r.Histogram.Observe(seconds)
}
5 changes: 3 additions & 2 deletions srvhttp/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ import (
)

func TestRequestDurationSeconds(t *testing.T) {
rds := RequestDurationSeconds{
rds := &RequestDurationSeconds{
Histogram: generic.NewHistogram("foo", 2),
}
rds = rds.Module("m").Service("s").Route("r")
rds.Observe(5)

assert.Equal(t, 5.0, rds.Histogram.(*generic.Histogram).Quantile(0.5))
assert.ElementsMatch(t, []string{"module", "m", "service", "s", "route", "r"}, rds.Histogram.(*generic.Histogram).LabelValues())

f := http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
time.Sleep(time.Millisecond)
})
h := Metrics(&rds)(f)
h := Metrics(rds)(f)
h.ServeHTTP(nil, httptest.NewRequest(http.MethodGet, "/", nil))
assert.GreaterOrEqual(t, 1.0, rds.Histogram.(*generic.Histogram).Quantile(0.5))
}

0 comments on commit 4baa8bc

Please sign in to comment.