(t *testing.T)
| 639 | } |
| 640 | |
| 641 | func TestAgentStats(t *testing.T) { |
| 642 | t.Parallel() |
| 643 | |
| 644 | ctx, cancelFunc := context.WithCancel(context.Background()) |
| 645 | t.Cleanup(cancelFunc) |
| 646 | |
| 647 | db, pubsub := dbtestutil.NewDB(t) |
| 648 | log := testutil.Logger(t) |
| 649 | |
| 650 | batcher, closeBatcher, err := workspacestats.NewBatcher(ctx, |
| 651 | // We had previously set the batch size to 1 here, but that caused |
| 652 | // intermittent test flakes due to a race between the batcher completing |
| 653 | // its flush and the test asserting that the metrics were collected. |
| 654 | // Instead, we close the batcher after all stats have been posted, which |
| 655 | // forces a flush. |
| 656 | workspacestats.BatcherWithStore(db), |
| 657 | workspacestats.BatcherWithLogger(log), |
| 658 | ) |
| 659 | require.NoError(t, err, "create stats batcher failed") |
| 660 | t.Cleanup(closeBatcher) |
| 661 | |
| 662 | tLogger := testutil.Logger(t) |
| 663 | // Build sample workspaces with test agents and fake agent client |
| 664 | client, _, _ := coderdtest.NewWithAPI(t, &coderdtest.Options{ |
| 665 | Database: db, |
| 666 | IncludeProvisionerDaemon: true, |
| 667 | Pubsub: pubsub, |
| 668 | StatsBatcher: batcher, |
| 669 | Logger: &tLogger, |
| 670 | }) |
| 671 | |
| 672 | user := coderdtest.CreateFirstUser(t, client) |
| 673 | |
| 674 | agent1 := prepareWorkspaceAndAgent(ctx, t, client, user, 1) |
| 675 | agent2 := prepareWorkspaceAndAgent(ctx, t, client, user, 2) |
| 676 | agent3 := prepareWorkspaceAndAgent(ctx, t, client, user, 3) |
| 677 | defer agent1.DRPCConn().Close() |
| 678 | defer agent2.DRPCConn().Close() |
| 679 | defer agent3.DRPCConn().Close() |
| 680 | |
| 681 | registry := prometheus.NewRegistry() |
| 682 | |
| 683 | // given |
| 684 | var i int64 |
| 685 | for i = 0; i < 3; i++ { |
| 686 | _, err = agent1.UpdateStats(ctx, &agentproto.UpdateStatsRequest{ |
| 687 | Stats: &agentproto.Stats{ |
| 688 | TxBytes: 1 + i, RxBytes: 2 + i, |
| 689 | SessionCountVscode: 3 + i, SessionCountJetbrains: 4 + i, SessionCountReconnectingPty: 5 + i, SessionCountSsh: 6 + i, |
| 690 | ConnectionCount: 7 + i, ConnectionMedianLatencyMs: 8000, |
| 691 | ConnectionsByProto: map[string]int64{"TCP": 1}, |
| 692 | }, |
| 693 | }) |
| 694 | require.NoError(t, err) |
| 695 | |
| 696 | _, err = agent2.UpdateStats(ctx, &agentproto.UpdateStatsRequest{ |
| 697 | Stats: &agentproto.Stats{ |
| 698 | TxBytes: 2 + i, RxBytes: 4 + i, |
nothing calls this directly
no test coverage detected