Skip to content

Commit 76c0f9f

Browse files
deansheatherethanndickson
authored andcommitted
Progress
1 parent 5dfef33 commit 76c0f9f

File tree

6 files changed

+121
-25
lines changed

6 files changed

+121
-25
lines changed

coderd/agentapi/api.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/coder/coder/v2/coderd/database/pubsub"
2323
"github.com/coder/coder/v2/coderd/externalauth"
2424
"github.com/coder/coder/v2/coderd/prometheusmetrics"
25-
"github.com/coder/coder/v2/coderd/schedule"
2625
"github.com/coder/coder/v2/coderd/telemetry"
2726
"github.com/coder/coder/v2/coderd/tracing"
2827
"github.com/coder/coder/v2/coderd/workspacestats"
@@ -62,7 +61,6 @@ type Options struct {
6261
Pubsub pubsub.Pubsub
6362
DerpMapFn func() *tailcfg.DERPMap
6463
TailnetCoordinator *atomic.Pointer[tailnet.Coordinator]
65-
TemplateScheduleStore *atomic.Pointer[schedule.TemplateScheduleStore]
6664
StatsReporter *workspacestats.Reporter
6765
AppearanceFetcher *atomic.Pointer[appearance.Fetcher]
6866
PublishWorkspaceUpdateFn func(ctx context.Context, workspaceID uuid.UUID)

coderd/agentapi/api_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package agentapi_test
2+
3+
import (
4+
"context"
5+
"net/url"
6+
"sync/atomic"
7+
"testing"
8+
"time"
9+
10+
"github.com/google/uuid"
11+
"github.com/stretchr/testify/require"
12+
"go.uber.org/goleak"
13+
"tailscale.com/tailcfg"
14+
15+
"cdr.dev/slog/sloggers/slogtest"
16+
"github.com/coder/coder/v2/agent/proto"
17+
"github.com/coder/coder/v2/coderd/agentapi"
18+
"github.com/coder/coder/v2/coderd/appearance"
19+
"github.com/coder/coder/v2/coderd/database/dbtestutil"
20+
"github.com/coder/coder/v2/coderd/externalauth"
21+
"github.com/coder/coder/v2/coderd/prometheusmetrics"
22+
"github.com/coder/coder/v2/coderd/schedule"
23+
"github.com/coder/coder/v2/coderd/telemetry"
24+
"github.com/coder/coder/v2/coderd/workspacestats"
25+
"github.com/coder/coder/v2/codersdk"
26+
"github.com/coder/coder/v2/tailnet"
27+
"github.com/coder/coder/v2/tailnet/tailnettest"
28+
"github.com/coder/coder/v2/testutil"
29+
)
30+
31+
func TestMain(m *testing.M) {
32+
goleak.VerifyTestMain(m)
33+
}
34+
35+
func Test_APIClose(t *testing.T) {
36+
t.Parallel()
37+
38+
ctx := testutil.Context(t, testutil.WaitMedium)
39+
log := slogtest.Make(t, nil)
40+
41+
db, pubsub := dbtestutil.NewDB(t)
42+
fCoord := tailnettest.NewFakeCoordinator()
43+
var coord tailnet.Coordinator = fCoord
44+
coordPtr := atomic.Pointer[tailnet.Coordinator]{}
45+
coordPtr.Store(&coord)
46+
47+
mockTemplateScheduleStore := schedule.MockTemplateScheduleStore{}
48+
var templateScheduleStore schedule.TemplateScheduleStore = mockTemplateScheduleStore
49+
templateScheduleStorePtr := atomic.Pointer[schedule.TemplateScheduleStore]{}
50+
templateScheduleStorePtr.Store(&templateScheduleStore)
51+
52+
statsBatcher, closeBatcher, err := workspacestats.NewBatcher(ctx, workspacestats.BatcherWithStore(db))
53+
require.NoError(t, err)
54+
t.Cleanup(closeBatcher)
55+
statsTracker := workspacestats.NewTracker(db)
56+
t.Cleanup(func() {
57+
_ = statsTracker.Close()
58+
})
59+
statsReporter := workspacestats.NewReporter(workspacestats.ReporterOptions{
60+
Database: db,
61+
Logger: log,
62+
Pubsub: pubsub,
63+
TemplateScheduleStore: &templateScheduleStorePtr,
64+
StatsBatcher: statsBatcher,
65+
UsageTracker: statsTracker,
66+
UpdateAgentMetricsFn: func(_ context.Context, _ prometheusmetrics.AgentMetricLabels, _ []*proto.Stats_Metric) {},
67+
AppStatBatchSize: 0,
68+
})
69+
70+
appearanceFetcherPtr := atomic.Pointer[appearance.Fetcher]{}
71+
appearanceFetcherPtr.Store(&appearance.DefaultFetcher)
72+
73+
api := agentapi.New(agentapi.Options{
74+
AgentID: uuid.New(),
75+
Ctx: ctx,
76+
Log: log,
77+
Database: db,
78+
Pubsub: pubsub,
79+
DerpMapFn: func() *tailcfg.DERPMap {
80+
return &tailcfg.DERPMap{Regions: map[int]*tailcfg.DERPRegion{999: {RegionCode: "test"}}}
81+
},
82+
TailnetCoordinator: &coordPtr,
83+
StatsReporter: statsReporter,
84+
AppearanceFetcher: &appearanceFetcherPtr,
85+
PublishWorkspaceUpdateFn: func(_ context.Context, _ uuid.UUID) {},
86+
NetworkTelemetryBatchFn: func(_ []telemetry.NetworkEvent) {},
87+
AccessURL: &url.URL{
88+
Scheme: "http",
89+
Host: "localhost",
90+
},
91+
AppHostname: "",
92+
AgentStatsRefreshInterval: time.Second,
93+
DisableDirectConnections: false,
94+
DerpForceWebSockets: false,
95+
DerpMapUpdateFrequency: time.Second,
96+
NetworkTelemetryBatchFrequency: time.Second,
97+
NetworkTelemetryBatchMaxSize: 1,
98+
ExternalAuthConfigs: []*externalauth.Config{},
99+
Experiments: codersdk.Experiments{},
100+
WorkspaceID: uuid.New(),
101+
UpdateAgentMetricsFn: func(_ context.Context, _ prometheusmetrics.AgentMetricLabels, _ []*proto.Stats_Metric) {},
102+
})
103+
104+
err = api.Close()
105+
require.NoError(t, err)
106+
}

coderd/telemetry/telemetry.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,8 +1240,8 @@ type NetworkEvent struct {
12401240
Status string `json:"status"` // connected, disconnected
12411241
DisconnectionReason string `json:"disconnection_reason"`
12421242
ClientType string `json:"client_type"` // cli, agent, coderd, wsproxy
1243-
NodeIDSelf uuid.UUID `json:"node_id_self"`
1244-
NodeIDRemote uuid.UUID `json:"node_id_remote"`
1243+
NodeIDSelf uint64 `json:"node_id_self"`
1244+
NodeIDRemote uint64 `json:"node_id_remote"`
12451245
P2PEndpoint NetworkEventP2PEndpoint `json:"p2p_endpoint"`
12461246
LogIPHashes map[string]NetworkEventIPFields `json:"log_ip_hashes"`
12471247
HomeDERP string `json:"home_derp"`
@@ -1272,14 +1272,6 @@ func NetworkEventFromProto(proto *tailnetproto.TelemetryEvent) (NetworkEvent, er
12721272
if err != nil {
12731273
return NetworkEvent{}, xerrors.Errorf("parse id %q: %w", proto.Id, err)
12741274
}
1275-
nodeIDSelf, err := uuid.ParseBytes(proto.NodeIdSelf)
1276-
if err != nil {
1277-
return NetworkEvent{}, xerrors.Errorf("parse node_id_self %q: %w", proto.NodeIdSelf, err)
1278-
}
1279-
nodeIDRemote, err := uuid.ParseBytes(proto.NodeIdRemote)
1280-
if err != nil {
1281-
return NetworkEvent{}, xerrors.Errorf("parse node_id_remote %q: %w", proto.NodeIdRemote, err)
1282-
}
12831275

12841276
logIPHashes := make(map[string]NetworkEventIPFields, len(proto.LogIpHashes))
12851277
for k, v := range proto.LogIpHashes {
@@ -1293,8 +1285,8 @@ func NetworkEventFromProto(proto *tailnetproto.TelemetryEvent) (NetworkEvent, er
12931285
Status: strings.ToLower(proto.Status.String()),
12941286
DisconnectionReason: proto.DisconnectionReason,
12951287
ClientType: strings.ToLower(proto.ClientType.String()),
1296-
NodeIDSelf: nodeIDSelf,
1297-
NodeIDRemote: nodeIDRemote,
1288+
NodeIDSelf: proto.NodeIdSelf,
1289+
NodeIDRemote: proto.NodeIdRemote,
12981290
P2PEndpoint: p2pEndpointFromProto(proto.P2PEndpoint),
12991291
LogIPHashes: logIPHashes,
13001292
HomeDERP: proto.HomeDerp,

coderd/workspaceagentsrpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ func (api *API) workspaceAgentRPC(rw http.ResponseWriter, r *http.Request) {
131131
Pubsub: api.Pubsub,
132132
DerpMapFn: api.DERPMap,
133133
TailnetCoordinator: &api.TailnetCoordinator,
134-
TemplateScheduleStore: api.TemplateScheduleStore,
135134
AppearanceFetcher: &api.AppearanceFetcher,
136135
StatsReporter: api.statsReporter,
137136
PublishWorkspaceUpdateFn: api.publishWorkspaceUpdate,
@@ -157,6 +156,7 @@ func (api *API) workspaceAgentRPC(rw http.ResponseWriter, r *http.Request) {
157156
WorkspaceID: build.WorkspaceID, // saves the extra lookup later
158157
UpdateAgentMetricsFn: api.UpdateAgentMetrics,
159158
})
159+
defer agentAPI.Close()
160160

161161
streamID := tailnet.StreamID{
162162
Name: fmt.Sprintf("%s-%s-%s", owner.Username, workspace.Name, workspaceAgent.Name),

tailnet/proto/tailnet.pb.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tailnet/proto/tailnet.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ message TelemetryEvent {
170170
Status status = 4;
171171
string disconnection_reason = 5;
172172
ClientType client_type = 6;
173-
bytes node_id_self = 7;
174-
bytes node_id_remote = 8;
173+
uint64 node_id_self = 7;
174+
uint64 node_id_remote = 8;
175175
P2PEndpoint p2p_endpoint = 9;
176176
map<string, IPFields> log_ip_hashes = 10;
177177
string home_derp = 11;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy