@@ -2531,17 +2531,17 @@ func TestAgent_Metrics_SSH(t *testing.T) {
2531
2531
err = session .Shell ()
2532
2532
require .NoError (t , err )
2533
2533
2534
- expected := []agentsdk. AgentMetric {
2534
+ expected := []* proto. Stats_Metric {
2535
2535
{
2536
2536
Name : "agent_reconnecting_pty_connections_total" ,
2537
- Type : agentsdk . AgentMetricTypeCounter ,
2537
+ Type : proto . Stats_Metric_COUNTER ,
2538
2538
Value : 0 ,
2539
2539
},
2540
2540
{
2541
2541
Name : "agent_sessions_total" ,
2542
- Type : agentsdk . AgentMetricTypeCounter ,
2542
+ Type : proto . Stats_Metric_COUNTER ,
2543
2543
Value : 1 ,
2544
- Labels : []agentsdk. AgentMetricLabel {
2544
+ Labels : []* proto. Stats_Metric_Label {
2545
2545
{
2546
2546
Name : "magic_type" ,
2547
2547
Value : "ssh" ,
@@ -2554,30 +2554,46 @@ func TestAgent_Metrics_SSH(t *testing.T) {
2554
2554
},
2555
2555
{
2556
2556
Name : "agent_ssh_server_failed_connections_total" ,
2557
- Type : agentsdk . AgentMetricTypeCounter ,
2557
+ Type : proto . Stats_Metric_COUNTER ,
2558
2558
Value : 0 ,
2559
2559
},
2560
2560
{
2561
2561
Name : "agent_ssh_server_sftp_connections_total" ,
2562
- Type : agentsdk . AgentMetricTypeCounter ,
2562
+ Type : proto . Stats_Metric_COUNTER ,
2563
2563
Value : 0 ,
2564
2564
},
2565
2565
{
2566
2566
Name : "agent_ssh_server_sftp_server_errors_total" ,
2567
- Type : agentsdk . AgentMetricTypeCounter ,
2567
+ Type : proto . Stats_Metric_COUNTER ,
2568
2568
Value : 0 ,
2569
2569
},
2570
2570
{
2571
- Name : "coderd_agentstats_startup_script_seconds " ,
2572
- Type : agentsdk . AgentMetricTypeGauge ,
2571
+ Name : "coderd_agentstats_currently_reachable_peers " ,
2572
+ Type : proto . Stats_Metric_GAUGE ,
2573
2573
Value : 0 ,
2574
- Labels : []agentsdk.AgentMetricLabel {
2574
+ Labels : []* proto.Stats_Metric_Label {
2575
+ {
2576
+ Name : "connection_type" ,
2577
+ Value : "derp" ,
2578
+ },
2579
+ },
2580
+ },
2581
+ {
2582
+ Name : "coderd_agentstats_currently_reachable_peers" ,
2583
+ Type : proto .Stats_Metric_GAUGE ,
2584
+ Value : 1 ,
2585
+ Labels : []* proto.Stats_Metric_Label {
2575
2586
{
2576
- Name : "success " ,
2577
- Value : "true " ,
2587
+ Name : "connection_type " ,
2588
+ Value : "p2p " ,
2578
2589
},
2579
2590
},
2580
2591
},
2592
+ {
2593
+ Name : "coderd_agentstats_startup_script_seconds" ,
2594
+ Type : proto .Stats_Metric_GAUGE ,
2595
+ Value : 1 ,
2596
+ },
2581
2597
}
2582
2598
2583
2599
var actual []* promgo.MetricFamily
@@ -2586,17 +2602,33 @@ func TestAgent_Metrics_SSH(t *testing.T) {
2586
2602
if err != nil {
2587
2603
return false
2588
2604
}
2589
-
2590
- if len ( expected ) != len ( actual ) {
2591
- return false
2605
+ count := 0
2606
+ for _ , m := range actual {
2607
+ count += len ( m . GetMetric ())
2592
2608
}
2593
-
2594
- return verifyCollectedMetrics (t , expected , actual )
2609
+ return count == len (expected )
2595
2610
}, testutil .WaitLong , testutil .IntervalFast )
2596
2611
2597
- require .Len (t , actual , len (expected ))
2598
- collected := verifyCollectedMetrics (t , expected , actual )
2599
- require .True (t , collected , "expected metrics were not collected" )
2612
+ i := 0
2613
+ for _ , mf := range actual {
2614
+ for _ , m := range mf .GetMetric () {
2615
+ assert .Equal (t , expected [i ].Name , mf .GetName ())
2616
+ assert .Equal (t , expected [i ].Type .String (), mf .GetType ().String ())
2617
+ // Value is max expected
2618
+ if expected [i ].Type == proto .Stats_Metric_GAUGE {
2619
+ assert .GreaterOrEqualf (t , expected [i ].Value , m .GetGauge ().GetValue (), "expected %s to be greater than or equal to %f, got %f" , expected [i ].Name , expected [i ].Value , m .GetGauge ().GetValue ())
2620
+ } else if expected [i ].Type == proto .Stats_Metric_COUNTER {
2621
+ assert .GreaterOrEqualf (t , expected [i ].Value , m .GetCounter ().GetValue (), "expected %s to be greater than or equal to %f, got %f" , expected [i ].Name , expected [i ].Value , m .GetCounter ().GetValue ())
2622
+ }
2623
+ for j , lbl := range expected [i ].Labels {
2624
+ assert .Equal (t , m .GetLabel ()[j ], & promgo.LabelPair {
2625
+ Name : & lbl .Name ,
2626
+ Value : & lbl .Value ,
2627
+ })
2628
+ }
2629
+ i ++
2630
+ }
2631
+ }
2600
2632
2601
2633
_ = stdin .Close ()
2602
2634
err = session .Wait ()
@@ -2828,28 +2860,6 @@ func TestAgent_ManageProcessPriority(t *testing.T) {
2828
2860
})
2829
2861
}
2830
2862
2831
- func verifyCollectedMetrics (t * testing.T , expected []agentsdk.AgentMetric , actual []* promgo.MetricFamily ) bool {
2832
- t .Helper ()
2833
-
2834
- for i , e := range expected {
2835
- assert .Equal (t , e .Name , actual [i ].GetName ())
2836
- assert .Equal (t , string (e .Type ), strings .ToLower (actual [i ].GetType ().String ()))
2837
-
2838
- for _ , m := range actual [i ].GetMetric () {
2839
- assert .Equal (t , e .Value , m .Counter .GetValue ())
2840
-
2841
- if len (m .GetLabel ()) > 0 {
2842
- for j , lbl := range m .GetLabel () {
2843
- assert .Equal (t , e .Labels [j ].Name , lbl .GetName ())
2844
- assert .Equal (t , e .Labels [j ].Value , lbl .GetValue ())
2845
- }
2846
- }
2847
- m .GetLabel ()
2848
- }
2849
- }
2850
- return true
2851
- }
2852
-
2853
2863
type syncWriter struct {
2854
2864
mu sync.Mutex
2855
2865
w io.Writer
0 commit comments