From 8e1d69b17502ffd98ec8222abb5482ebee62ccbf Mon Sep 17 00:00:00 2001 From: Igor Drozdov Date: Tue, 5 Apr 2022 13:02:45 +0400 Subject: Fix connections duration metrics We need to pass time.Now as a param, otherwise it's calculated on call --- internal/sshd/connection.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'internal') diff --git a/internal/sshd/connection.go b/internal/sshd/connection.go index 7ba9bed..25b082a 100644 --- a/internal/sshd/connection.go +++ b/internal/sshd/connection.go @@ -13,7 +13,6 @@ import ( ) type connection struct { - begin time.Time concurrentSessions *semaphore.Weighted remoteAddr string } @@ -22,7 +21,6 @@ type channelHandler func(context.Context, ssh.Channel, <-chan *ssh.Request) func newConnection(maxSessions int64, remoteAddr string) *connection { return &connection{ - begin: time.Now(), concurrentSessions: semaphore.NewWeighted(maxSessions), remoteAddr: remoteAddr, } @@ -32,9 +30,11 @@ func (c *connection) handle(ctx context.Context, chans <-chan ssh.NewChannel, ha ctxlog := log.WithContextFields(ctx, log.Fields{"remote_addr": c.remoteAddr}) metrics.SshdConnectionsInFlight.Inc() - defer metrics.SshdConnectionsInFlight.Dec() - defer metrics.SshdConnectionDuration.Observe(time.Since(c.begin).Seconds()) + defer func(started time.Time) { + metrics.SshdConnectionsInFlight.Dec() + metrics.SshdConnectionDuration.Observe(time.Since(started).Seconds()) + }(time.Now()) for newChannel := range chans { ctxlog.WithField("channel_type", newChannel.ChannelType()).Info("connection: handle: new channel requested") -- cgit v1.2.1