Skip to content

Commit

Permalink
changed the interface of StartTimeCall to previous interface, and use…
Browse files Browse the repository at this point in the history
… reporter to utilise the timer call function of open metrics

Signed-off-by: Yash Sharma <[email protected]>
  • Loading branch information
yashrsharma44 committed Jan 18, 2021
1 parent 7f546d6 commit aff09f4
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 77 deletions.
8 changes: 4 additions & 4 deletions interceptors/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ type monitoredClientStream struct {
}

func (s *monitoredClientStream) SendMsg(m interface{}) error {
timer := s.reporter.StartTimeCall(time.Now(), string(Send))
start := time.Now()
err := s.ClientStream.SendMsg(m)
s.reporter.PostMsgSend(m, err, timer.ObserveDuration())
s.reporter.PostMsgSend(m, err, time.Since(start))
return err
}

func (s *monitoredClientStream) RecvMsg(m interface{}) error {
timer := s.reporter.StartTimeCall(time.Now(), string(Receive))
start := time.Now()
err := s.ClientStream.RecvMsg(m)
s.reporter.PostMsgReceive(m, err, timer.ObserveDuration())
s.reporter.PostMsgReceive(m, err, time.Since(start))

if err == nil {
return nil
Expand Down
4 changes: 0 additions & 4 deletions interceptors/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ type mockedReporter struct {
postMsgReceives []error
}

func (m *mockedReporter) StartTimeCall(startTime time.Time, _ string) Timer {
return EmptyTimer
}

func (m *mockedReporter) PostCall(err error, _ time.Duration) {
m.m.Lock()
defer m.m.Unlock()
Expand Down
4 changes: 0 additions & 4 deletions interceptors/logging/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ func (c *reporter) logMessage(logger Logger, err error, msg string, duration tim
logger.With(c.opts.durationFieldFunc(duration)...).Log(c.opts.levelFunc(code), msg)
}

func (c *reporter) StartTimeCall(startTime time.Time, callType string) interceptors.Timer {
return interceptors.EmptyTimer
}

func (c *reporter) PostCall(err error, duration time.Duration) {
switch c.opts.shouldLog(interceptors.FullMethod(c.service, c.method), err) {
case LogFinishCall, LogStartAndFinishCall:
Expand Down
8 changes: 0 additions & 8 deletions interceptors/logging/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ type serverPayloadReporter struct {
logger Logger
}

func (c *serverPayloadReporter) StartTimeCall(_ time.Time, _ string) interceptors.Timer {
return interceptors.EmptyTimer
}

func (c *serverPayloadReporter) PostCall(error, time.Duration) {}

func (c *serverPayloadReporter) PostMsgSend(req interface{}, err error, duration time.Duration) {
Expand All @@ -54,10 +50,6 @@ type clientPayloadReporter struct {
logger Logger
}

func (c *clientPayloadReporter) StartTimeCall(_ time.Time, _ string) interceptors.Timer {
return interceptors.EmptyTimer
}

func (c *clientPayloadReporter) PostCall(error, time.Duration) {}

func (c *clientPayloadReporter) PostMsgSend(req interface{}, err error, duration time.Duration) {
Expand Down
19 changes: 0 additions & 19 deletions interceptors/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,8 @@ func (noOpTimer) ObserveDuration() time.Duration {
return 0
}

// Default Timer
type DefaultTimer struct {
startTime time.Time
}

func (tm *DefaultTimer) ObserveDuration() time.Duration {
return time.Since(tm.startTime)
}

var EmptyTimer = &noOpTimer{}

type RPCMethod string

const (
Send RPCMethod = "send"
Receive RPCMethod = "recv"
)

const (
Unary GRPCType = "unary"
ClientStream GRPCType = "client_stream"
Expand Down Expand Up @@ -84,9 +68,7 @@ type ServerReportable interface {
}

type Reporter interface {
StartTimeCall(time.Time, string) Timer
PostCall(err error, rpcDuration time.Duration)

PostMsgSend(reqProto interface{}, err error, sendDuration time.Duration)
PostMsgReceive(replyProto interface{}, err error, recvDuration time.Duration)
}
Expand All @@ -95,7 +77,6 @@ var _ Reporter = NoopReporter{}

type NoopReporter struct{}

func (NoopReporter) StartTimeCall(time.Time, string) Timer { return EmptyTimer }
func (NoopReporter) PostCall(error, time.Duration) {}
func (NoopReporter) PostMsgSend(interface{}, error, time.Duration) {}
func (NoopReporter) PostMsgReceive(interface{}, error, time.Duration) {}
Expand Down
4 changes: 0 additions & 4 deletions interceptors/tags/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ type reporter struct {
initial bool
}

func (c *reporter) StartTimeCall(_ time.Time, _ string) interceptors.Timer {
return interceptors.EmptyTimer
}

func (c *reporter) PostCall(error, time.Duration) {}

func (c *reporter) PostMsgSend(interface{}, error, time.Duration) {}
Expand Down
4 changes: 0 additions & 4 deletions interceptors/tracing/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ type opentracingClientReporter struct {
clientSpan opentracing.Span
}

func (o *opentracingClientReporter) StartTimeCall(_ time.Time, _ string) interceptors.Timer {
return interceptors.EmptyTimer
}

func (o *opentracingClientReporter) PostCall(err error, _ time.Duration) {
// Finish span.
if err != nil && err != io.EOF {
Expand Down
4 changes: 0 additions & 4 deletions interceptors/tracing/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ type opentracingServerReporter struct {
serverSpan opentracing.Span
}

func (o *opentracingServerReporter) StartTimeCall(_ time.Time, _ string) interceptors.Timer {
return interceptors.EmptyTimer
}

func (o *opentracingServerReporter) PostCall(err error, _ time.Duration) {
// Finish span and log context information.
tags := tags.Extract(o.ctx)
Expand Down
49 changes: 23 additions & 26 deletions providers/openmetrics/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,13 @@ import (
)

type reporter struct {
clientMetrics *ClientMetrics
serverMetrics *ServerMetrics
typ interceptors.GRPCType
service, method string
startTime time.Time
kind Kind
}

func (r *reporter) StartTimeCall(startTime time.Time, callType string) interceptors.Timer {
switch r.kind {
case KindClient:
switch callType {
case string(interceptors.Send):
if r.clientMetrics.clientStreamSendHistogramEnabled {
hist := r.clientMetrics.clientStreamSendHistogram.WithLabelValues(string(r.typ), r.service, r.method)
return prometheus.NewTimer(hist)
}
case string(interceptors.Receive):
if r.clientMetrics.clientStreamRecvHistogramEnabled {
hist := r.clientMetrics.clientStreamRecvHistogram.WithLabelValues(string(r.typ), r.service, r.method)
return prometheus.NewTimer(hist)
}
}
}
return interceptors.EmptyTimer
clientMetrics *ClientMetrics
serverMetrics *ServerMetrics
typ interceptors.GRPCType
service, method string
startTime time.Time
kind Kind
sendTimer, receiveTimer interceptors.Timer
}

func (r *reporter) PostCall(err error, duration time.Duration) {
Expand All @@ -50,6 +32,7 @@ func (r *reporter) PostCall(err error, duration time.Duration) {
if r.serverMetrics.serverHandledHistogramEnabled {
r.serverMetrics.serverHandledHistogram.WithLabelValues(string(r.typ), r.service, r.method).Observe(time.Since(r.startTime).Seconds())
}

case KindClient:
r.clientMetrics.clientHandledCounter.WithLabelValues(string(r.typ), r.service, r.method, code.String()).Inc()
if r.clientMetrics.clientHandledHistogramEnabled {
Expand All @@ -64,6 +47,7 @@ func (r *reporter) PostMsgSend(_ interface{}, _ error, _ time.Duration) {
r.serverMetrics.serverStreamMsgSent.WithLabelValues(string(r.typ), r.service, r.method).Inc()
case KindClient:
r.clientMetrics.clientStreamMsgSent.WithLabelValues(string(r.typ), r.service, r.method).Inc()
r.sendTimer.ObserveDuration()
}
}

Expand All @@ -73,6 +57,7 @@ func (r *reporter) PostMsgReceive(_ interface{}, _ error, _ time.Duration) {
r.serverMetrics.serverStreamMsgReceived.WithLabelValues(string(r.typ), r.service, r.method).Inc()
case KindClient:
r.clientMetrics.clientStreamMsgReceived.WithLabelValues(string(r.typ), r.service, r.method).Inc()
r.receiveTimer.ObserveDuration()
}
}

Expand All @@ -97,6 +82,8 @@ func (rep *reportable) reporter(sm *ServerMetrics, cm *ClientMetrics, rpcType in
service: service,
method: method,
kind: kind,
sendTimer: interceptors.EmptyTimer,
receiveTimer: interceptors.EmptyTimer,
}

switch kind {
Expand All @@ -105,14 +92,24 @@ func (rep *reportable) reporter(sm *ServerMetrics, cm *ClientMetrics, rpcType in
r.startTime = time.Now()
}
r.clientMetrics.clientStartedCounter.WithLabelValues(string(r.typ), r.service, r.method).Inc()

if r.clientMetrics.clientStreamSendHistogramEnabled {
hist := r.clientMetrics.clientStreamSendHistogram.WithLabelValues(string(r.typ), r.service, r.method)
r.sendTimer = prometheus.NewTimer(hist)
}

if r.clientMetrics.clientStreamRecvHistogramEnabled {
hist := r.clientMetrics.clientStreamRecvHistogram.WithLabelValues(string(r.typ), r.service, r.method)
r.receiveTimer = prometheus.NewTimer(hist)
}
case KindServer:
if r.serverMetrics.serverHandledHistogramEnabled {
r.startTime = time.Now()
}
r.serverMetrics.serverStartedCounter.WithLabelValues(string(r.typ), r.service, r.method).Inc()
}

// TODO: @yashrsharma - What should we instead of the context interface?
// TODO: @yashrsharma44 - What should we use instead of the context.Background()?
return r, context.Background()
}

Expand Down

0 comments on commit aff09f4

Please sign in to comment.