Skip to content

Commit 9b5c997

Browse files
committed
Respect the telemetry flag
1 parent df9ecd7 commit 9b5c997

File tree

15 files changed

+86
-45
lines changed

15 files changed

+86
-45
lines changed

coderd/apidoc/docs.go

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

coderd/apidoc/swagger.json

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

coderd/coderd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ func New(options *Options) *API {
447447
WorkspaceProxy: false,
448448
UpgradeMessage: api.DeploymentValues.CLIUpgradeMessage.String(),
449449
DeploymentID: api.DeploymentID,
450+
Telemetry: api.Telemetry.Enabled(),
450451
}
451452
api.SiteHandler = site.New(&site.Options{
452453
BinFS: binFS,

coderd/telemetry/telemetry.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ type Reporter interface {
9393
// database. For example, if a new user is added, a snapshot can
9494
// contain just that user entry.
9595
Report(snapshot *Snapshot)
96+
Enabled() bool
9697
Close()
9798
}
9899

@@ -109,6 +110,10 @@ type remoteReporter struct {
109110
shutdownAt *time.Time
110111
}
111112

113+
func (r *remoteReporter) Enabled() bool {
114+
return true
115+
}
116+
112117
func (r *remoteReporter) Report(snapshot *Snapshot) {
113118
go r.reportSync(snapshot)
114119
}
@@ -948,4 +953,5 @@ type ExternalProvisioner struct {
948953
type noopReporter struct{}
949954

950955
func (*noopReporter) Report(_ *Snapshot) {}
956+
func (*noopReporter) Enabled() bool { return false }
951957
func (*noopReporter) Close() {}

codersdk/deployment.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2173,11 +2173,12 @@ type BuildInfoResponse struct {
21732173
ExternalURL string `json:"external_url"`
21742174
// Version returns the semantic version of the build.
21752175
Version string `json:"version"`
2176-
21772176
// DashboardURL is the URL to hit the deployment's dashboard.
21782177
// For external workspace proxies, this is the coderd they are connected
21792178
// to.
21802179
DashboardURL string `json:"dashboard_url"`
2180+
// Telemetry is a boolean that indicates whether telemetry is enabled.
2181+
Telemetry bool `json:"telemetry"`
21812182

21822183
WorkspaceProxy bool `json:"workspace_proxy"`
21832184

docs/api/general.md

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

docs/api/schemas.md

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

enterprise/wsproxy/wsproxysdk/wsproxysdk_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func TestDialCoordinator(t *testing.T) {
216216
Node: &proto.Node{
217217
Id: 55,
218218
AsOf: timestamppb.New(time.Unix(1689653252, 0)),
219-
Key: peerNodeKey[:],
219+
Key: peerNodeKey,
220220
Disco: string(peerDiscoKey),
221221
PreferredDerp: 0,
222222
DerpLatency: map[string]float64{

site/src/api/typesGenerated.ts

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

site/src/hooks/useEmbeddedMetadata.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const DEFAULT_METADATA_KEY = "property";
2222
* attributes
2323
*/
2424
type AvailableMetadata = Readonly<{
25+
telemetry: boolean;
2526
user: User;
2627
experiments: Experiments;
2728
appearance: AppearanceConfig;
@@ -81,6 +82,7 @@ export class MetadataManager implements MetadataManagerApi {
8182
this.trackedMetadataNodes = new Map();
8283

8384
this.metadata = {
85+
telemetry: this.registerValue("telemetry"),
8486
user: this.registerValue<User>("user"),
8587
appearance: this.registerValue<AppearanceConfig>("appearance"),
8688
entitlements: this.registerValue<Entitlements>("entitlements"),

site/src/pages/LoginPage/LoginPage.tsx

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@ export const LoginPage: FC = () => {
3131
const buildInfoQuery = useQuery(buildInfo(metadata["build-info"]));
3232

3333
if (isSignedIn) {
34-
// This uses `navigator.sendBeacon`, so window.href
35-
// will not stop the request from being sent!
36-
sendDeploymentEvent({
37-
type: "deployment_login",
38-
// This should work most of the time because of embedded
39-
// metadata and the user being logged in.
40-
deployment_id: buildInfoQuery.data?.deployment_id || "",
41-
user_id: user?.id,
42-
})
34+
if (buildInfoQuery.data) {
35+
// This uses `navigator.sendBeacon`, so window.href
36+
// will not stop the request from being sent!
37+
sendDeploymentEvent(buildInfoQuery.data, {
38+
type: "deployment_login",
39+
user_id: user?.id,
40+
});
41+
}
4342

4443
// If the redirect is going to a workspace application, and we
4544
// are missing authentication, then we need to change the href location
@@ -86,13 +85,15 @@ export const LoginPage: FC = () => {
8685
isSigningIn={isSigningIn}
8786
onSignIn={async ({ email, password }) => {
8887
await signIn(email, password);
89-
// This uses `navigator.sendBeacon`, so navigating away
90-
// will not prevent it!
91-
sendDeploymentEvent({
92-
type: "deployment_login",
93-
deployment_id: buildInfoQuery.data?.deployment_id || "",
94-
user_id: user?.id,
95-
})
88+
if (buildInfoQuery.data) {
89+
// This uses `navigator.sendBeacon`, so navigating away
90+
// will not prevent it!
91+
sendDeploymentEvent(buildInfoQuery.data, {
92+
type: "deployment_login",
93+
user_id: user?.id,
94+
});
95+
}
96+
9697
navigate("/");
9798
}}
9899
/>

site/src/pages/SetupPage/SetupPage.test.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,20 @@ describe("Setup Page", () => {
121121
);
122122
await waitForLoaderToBeRemoved();
123123
await waitFor(() => {
124-
expect(navigator.sendBeacon).toBeCalledWith("https://coder.com/api/track-deployment", new Blob([JSON.stringify({
125-
type: "deployment_setup",
126-
deployment_id: MockBuildInfo.deployment_id,
127-
})], {
128-
type: "application/json",
129-
}))
130-
})
131-
})
124+
expect(navigator.sendBeacon).toBeCalledWith(
125+
"https://coder.com/api/track-deployment",
126+
new Blob(
127+
[
128+
JSON.stringify({
129+
type: "deployment_setup",
130+
deployment_id: MockBuildInfo.deployment_id,
131+
}),
132+
],
133+
{
134+
type: "application/json",
135+
},
136+
),
137+
);
138+
});
139+
});
132140
});

site/src/pages/SetupPage/SetupPage.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import { pageTitle } from "utils/page";
1111
import { sendDeploymentEvent } from "utils/telemetry";
1212
import { SetupPageView } from "./SetupPageView";
1313

14-
export const SetupPage: FC<{
15-
telemetryURL?: string;
16-
}> = ({ telemetryURL }) => {
14+
export const SetupPage: FC = () => {
1715
const {
1816
isLoading,
1917
signIn,
@@ -30,14 +28,10 @@ export const SetupPage: FC<{
3028
if (!buildInfoQuery.data) {
3129
return;
3230
}
33-
sendDeploymentEvent(
34-
{
35-
type: "deployment_setup",
36-
deployment_id: buildInfoQuery.data.deployment_id,
37-
},
38-
telemetryURL,
39-
);
40-
}, [buildInfoQuery.data, telemetryURL]);
31+
sendDeploymentEvent(buildInfoQuery.data, {
32+
type: "deployment_setup",
33+
});
34+
}, [buildInfoQuery.data, metadata.telemetry.value]);
4135

4236
if (isLoading) {
4337
return <Loader fullscreen />;

site/src/testHelpers/entities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ export const MockBuildInfo: TypesGen.BuildInfoResponse = {
205205
workspace_proxy: false,
206206
upgrade_message: "My custom upgrade message",
207207
deployment_id: "510d407f-e521-4180-b559-eab4a6d802b8",
208+
telemetry: true,
208209
};
209210

210211
export const MockSupportLinks: TypesGen.LinkConfig[] = [

site/src/utils/telemetry.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1+
import type { BuildInfoResponse } from "api/typesGenerated";
2+
13
// sendDeploymentEvent sends a CORs payload to coder.com
24
// to track a deployment event.
3-
export const sendDeploymentEvent = (payload: {
4-
type: "deployment_setup" | "deployment_login";
5-
deployment_id: string;
6-
user_id?: string;
7-
}) => {
5+
export const sendDeploymentEvent = (
6+
buildInfo: BuildInfoResponse,
7+
payload: {
8+
type: "deployment_setup" | "deployment_login";
9+
user_id?: string;
10+
},
11+
) => {
812
if (typeof navigator === "undefined" || !navigator.sendBeacon) {
913
// It's fine if we don't report this, it's not required!
1014
return;
1115
}
16+
if (!buildInfo.telemetry) {
17+
return;
18+
}
1219
navigator.sendBeacon(
1320
"https://coder.com/api/track-deployment",
14-
new Blob([JSON.stringify(payload)], {
15-
type: "application/json",
16-
}),
21+
new Blob(
22+
[
23+
JSON.stringify({
24+
...payload,
25+
deployment_id: buildInfo.deployment_id,
26+
}),
27+
],
28+
{
29+
type: "application/json",
30+
},
31+
),
1732
);
1833
};

0 commit comments

Comments
 (0)