@@ -27,6 +27,8 @@ import (
27
27
"github.com/google/uuid"
28
28
"github.com/pion/udp"
29
29
"github.com/pkg/sftp"
30
+ "github.com/prometheus/client_golang/prometheus"
31
+ promgo "github.com/prometheus/client_model/go"
30
32
"github.com/spf13/afero"
31
33
"github.com/stretchr/testify/assert"
32
34
"github.com/stretchr/testify/require"
@@ -1724,7 +1726,7 @@ func (c closeFunc) Close() error {
1724
1726
return c ()
1725
1727
}
1726
1728
1727
- func setupAgent (t * testing.T , metadata agentsdk.Manifest , ptyTimeout time.Duration ) (
1729
+ func setupAgent (t * testing.T , metadata agentsdk.Manifest , ptyTimeout time.Duration , opts ... func (agent. Options ) agent. Options ) (
1728
1730
* codersdk.WorkspaceAgentConn ,
1729
1731
* client ,
1730
1732
<- chan * agentsdk.Stats ,
@@ -1749,12 +1751,19 @@ func setupAgent(t *testing.T, metadata agentsdk.Manifest, ptyTimeout time.Durati
1749
1751
statsChan : statsCh ,
1750
1752
coordinator : coordinator ,
1751
1753
}
1752
- closer := agent .New (agent.Options {
1754
+
1755
+ options := agent.Options {
1753
1756
Client : c ,
1754
1757
Filesystem : fs ,
1755
1758
Logger : logger .Named ("agent" ),
1756
1759
ReconnectingPTYTimeout : ptyTimeout ,
1757
- })
1760
+ }
1761
+
1762
+ for _ , opt := range opts {
1763
+ options = opt (options )
1764
+ }
1765
+
1766
+ closer := agent .New (options )
1758
1767
t .Cleanup (func () {
1759
1768
_ = closer .Close ()
1760
1769
})
@@ -1979,3 +1988,107 @@ func tempDirUnixSocket(t *testing.T) string {
1979
1988
1980
1989
return t .TempDir ()
1981
1990
}
1991
+
1992
+ func TestAgent_Metrics_SSH (t * testing.T ) {
1993
+ t .Parallel ()
1994
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
1995
+ defer cancel ()
1996
+
1997
+ registry := prometheus .NewRegistry ()
1998
+
1999
+ //nolint:dogsled
2000
+ conn , _ , _ , _ , _ := setupAgent (t , agentsdk.Manifest {}, 0 , func (o agent.Options ) agent.Options {
2001
+ o .PrometheusRegistry = registry
2002
+ return o
2003
+ })
2004
+
2005
+ sshClient , err := conn .SSHClient (ctx )
2006
+ require .NoError (t , err )
2007
+ defer sshClient .Close ()
2008
+ session , err := sshClient .NewSession ()
2009
+ require .NoError (t , err )
2010
+ defer session .Close ()
2011
+ stdin , err := session .StdinPipe ()
2012
+ require .NoError (t , err )
2013
+ err = session .Shell ()
2014
+ require .NoError (t , err )
2015
+
2016
+ expected := []agentsdk.AgentMetric {
2017
+ {
2018
+ Name : "agent_reconnecting_pty_connections_total" ,
2019
+ Type : agentsdk .AgentMetricTypeCounter ,
2020
+ Value : 0 ,
2021
+ },
2022
+ {
2023
+ Name : "agent_sessions_total" ,
2024
+ Type : agentsdk .AgentMetricTypeCounter ,
2025
+ Value : 1 ,
2026
+ Labels : []agentsdk.AgentMetricLabel {
2027
+ {
2028
+ Name : "magic_type" ,
2029
+ Value : "ssh" ,
2030
+ },
2031
+ {
2032
+ Name : "pty" ,
2033
+ Value : "no" ,
2034
+ },
2035
+ },
2036
+ },
2037
+ {
2038
+ Name : "agent_ssh_server_failed_connections_total" ,
2039
+ Type : agentsdk .AgentMetricTypeCounter ,
2040
+ Value : 0 ,
2041
+ },
2042
+ {
2043
+ Name : "agent_ssh_server_sftp_connections_total" ,
2044
+ Type : agentsdk .AgentMetricTypeCounter ,
2045
+ Value : 0 ,
2046
+ },
2047
+ {
2048
+ Name : "agent_ssh_server_sftp_server_errors_total" ,
2049
+ Type : agentsdk .AgentMetricTypeCounter ,
2050
+ Value : 0 ,
2051
+ },
2052
+ }
2053
+
2054
+ var actual []* promgo.MetricFamily
2055
+ assert .Eventually (t , func () bool {
2056
+ actual , err = registry .Gather ()
2057
+ if err != nil {
2058
+ return false
2059
+ }
2060
+
2061
+ if len (expected ) != len (actual ) {
2062
+ return false
2063
+ }
2064
+
2065
+ return verifyCollectedMetrics (t , expected , actual )
2066
+ }, testutil .WaitLong , testutil .IntervalFast )
2067
+
2068
+ require .Len (t , actual , len (expected ))
2069
+ verifyCollectedMetrics (t , expected , actual )
2070
+
2071
+ _ = stdin .Close ()
2072
+ err = session .Wait ()
2073
+ require .NoError (t , err )
2074
+ }
2075
+
2076
+ func verifyCollectedMetrics (t * testing.T , expected []agentsdk.AgentMetric , actual []* promgo.MetricFamily ) bool {
2077
+ for i , e := range expected {
2078
+ require .Equal (t , e .Name , actual [i ].GetName ())
2079
+ require .Equal (t , string (e .Type ), strings .ToLower (actual [i ].GetType ().String ()))
2080
+
2081
+ for _ , m := range actual [i ].GetMetric () {
2082
+ require .Equal (t , e .Value , m .Counter .GetValue ())
2083
+
2084
+ if len (m .GetLabel ()) > 0 {
2085
+ for j , lbl := range m .GetLabel () {
2086
+ require .Equal (t , e .Labels [j ].Name , lbl .GetName ())
2087
+ require .Equal (t , e .Labels [j ].Value , lbl .GetValue ())
2088
+ }
2089
+ }
2090
+ m .GetLabel ()
2091
+ }
2092
+ }
2093
+ return true
2094
+ }
0 commit comments