diff options
author | Igor Drozdov <idrozdov@gitlab.com> | 2022-05-16 01:36:40 +0400 |
---|---|---|
committer | Igor Drozdov <idrozdov@gitlab.com> | 2022-05-17 08:17:47 +0400 |
commit | 509e04b63c9bee521b6c6536224f07fa458362d8 (patch) | |
tree | 180d82d6ae834e178174440d75d08b686d57702e /internal/sshd | |
parent | c05ed91ea824ce015f51417196021b7e8a5837d0 (diff) | |
download | gitlab-shell-509e04b63c9bee521b6c6536224f07fa458362d8.tar.gz |
Log canceled requests into separate metrics
When a request get canceled we don't want to consider it an error
Diffstat (limited to 'internal/sshd')
-rw-r--r-- | internal/sshd/connection.go | 8 | ||||
-rw-r--r-- | internal/sshd/connection_test.go | 34 |
2 files changed, 41 insertions, 1 deletions
diff --git a/internal/sshd/connection.go b/internal/sshd/connection.go index ebb4fe0..0295d8f 100644 --- a/internal/sshd/connection.go +++ b/internal/sshd/connection.go @@ -6,6 +6,8 @@ import ( "golang.org/x/crypto/ssh" "golang.org/x/sync/semaphore" + grpccodes "google.golang.org/grpc/codes" + grpcstatus "google.golang.org/grpc/status" "gitlab.com/gitlab-org/gitlab-shell/internal/config" "gitlab.com/gitlab-org/gitlab-shell/internal/metrics" @@ -85,7 +87,11 @@ func (c *connection) handle(ctx context.Context, chans <-chan ssh.NewChannel, ha metrics.SliSshdSessionsTotal.Inc() err := handler(ctx, channel, requests) if err != nil { - metrics.SliSshdSessionsErrorsTotal.Inc() + if grpcstatus.Convert(err).Code() == grpccodes.Canceled { + metrics.SshdCanceledSessions.Inc() + } else { + metrics.SliSshdSessionsErrorsTotal.Inc() + } } ctxlog.Info("connection: handle: done") diff --git a/internal/sshd/connection_test.go b/internal/sshd/connection_test.go index d3a2287..f792300 100644 --- a/internal/sshd/connection_test.go +++ b/internal/sshd/connection_test.go @@ -7,10 +7,14 @@ import ( "testing" "time" + "github.com/prometheus/client_golang/prometheus/testutil" "github.com/stretchr/testify/require" "golang.org/x/crypto/ssh" + grpccodes "google.golang.org/grpc/codes" + grpcstatus "google.golang.org/grpc/status" "gitlab.com/gitlab-org/gitlab-shell/internal/config" + "gitlab.com/gitlab-org/gitlab-shell/internal/metrics" ) type rejectCall struct { @@ -189,3 +193,33 @@ func TestClientAliveInterval(t *testing.T) { require.Eventually(t, func() bool { return KeepAliveMsg == f.SentRequestName() }, time.Second, time.Millisecond) } + +func TestSessionsMetrics(t *testing.T) { + // Unfortunately, there is no working way to reset Counter (not CounterVec) + // https://pkg.go.dev/github.com/prometheus/client_golang/prometheus#pkg-index + initialSessionsTotal := testutil.ToFloat64(metrics.SliSshdSessionsTotal) + initialSessionsErrorTotal := testutil.ToFloat64(metrics.SliSshdSessionsErrorsTotal) + initialCanceledSessions := testutil.ToFloat64(metrics.SshdCanceledSessions) + + newChannel := &fakeNewChannel{channelType: "session"} + + conn, chans := setup(1, newChannel) + conn.handle(context.Background(), chans, func(context.Context, ssh.Channel, <-chan *ssh.Request) error { + close(chans) + return errors.New("custom error") + }) + + require.InDelta(t, initialSessionsTotal+1, testutil.ToFloat64(metrics.SliSshdSessionsTotal), 0.1) + require.InDelta(t, initialSessionsErrorTotal+1, testutil.ToFloat64(metrics.SliSshdSessionsErrorsTotal), 0.1) + require.InDelta(t, initialCanceledSessions, testutil.ToFloat64(metrics.SshdCanceledSessions), 0.1) + + conn, chans = setup(1, newChannel) + conn.handle(context.Background(), chans, func(context.Context, ssh.Channel, <-chan *ssh.Request) error { + close(chans) + return grpcstatus.Error(grpccodes.Canceled, "error") + }) + + require.InDelta(t, initialSessionsTotal+2, testutil.ToFloat64(metrics.SliSshdSessionsTotal), 0.1) + require.InDelta(t, initialSessionsErrorTotal+1, testutil.ToFloat64(metrics.SliSshdSessionsErrorsTotal), 0.1) + require.InDelta(t, initialCanceledSessions+1, testutil.ToFloat64(metrics.SshdCanceledSessions), 0.1) +} |