@@ -29,6 +29,7 @@ import (
29
29
"github.com/coreos/go-systemd/daemon"
30
30
embeddedpostgres "github.com/fergusstrange/embedded-postgres"
31
31
"github.com/google/go-github/v43/github"
32
+ "github.com/google/uuid"
32
33
"github.com/pion/turn/v2"
33
34
"github.com/pion/webrtc/v3"
34
35
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -53,6 +54,7 @@ import (
53
54
"github.com/coder/coder/coderd/database/databasefake"
54
55
"github.com/coder/coder/coderd/devtunnel"
55
56
"github.com/coder/coder/coderd/gitsshkey"
57
+ "github.com/coder/coder/coderd/telemetry"
56
58
"github.com/coder/coder/coderd/tracing"
57
59
"github.com/coder/coder/coderd/turnconn"
58
60
"github.com/coder/coder/codersdk"
@@ -81,6 +83,7 @@ func server() *cobra.Command {
81
83
oauth2GithubClientSecret string
82
84
oauth2GithubAllowedOrganizations []string
83
85
oauth2GithubAllowSignups bool
86
+ telemetryURL string
84
87
tlsCertFile string
85
88
tlsClientCAFile string
86
89
tlsClientAuth string
@@ -134,6 +137,7 @@ func server() *cobra.Command {
134
137
}
135
138
136
139
config := createConfig (cmd )
140
+ builtinPostgres := false
137
141
// Only use built-in if PostgreSQL URL isn't specified!
138
142
if ! inMemoryDatabase && postgresURL == "" {
139
143
var closeFunc func () error
@@ -142,6 +146,7 @@ func server() *cobra.Command {
142
146
if err != nil {
143
147
return err
144
148
}
149
+ builtinPostgres = true
145
150
defer func () {
146
151
// Gracefully shut PostgreSQL down!
147
152
_ = closeFunc ()
@@ -253,6 +258,7 @@ func server() *cobra.Command {
253
258
SSHKeygenAlgorithm : sshKeygenAlgorithm ,
254
259
TURNServer : turnServer ,
255
260
TracerProvider : tracerProvider ,
261
+ Telemetry : telemetry .NewNoop (),
256
262
}
257
263
258
264
if oauth2GithubClientSecret != "" {
@@ -285,6 +291,44 @@ func server() *cobra.Command {
285
291
}
286
292
}
287
293
294
+ deploymentID , err := options .Database .GetDeploymentID (cmd .Context ())
295
+ if errors .Is (err , sql .ErrNoRows ) {
296
+ err = nil
297
+ }
298
+ if err != nil {
299
+ return xerrors .Errorf ("get deployment id: %w" , err )
300
+ }
301
+ if deploymentID == "" {
302
+ deploymentID = uuid .NewString ()
303
+ err = options .Database .InsertDeploymentID (cmd .Context (), deploymentID )
304
+ if err != nil {
305
+ return xerrors .Errorf ("set deployment id: %w" , err )
306
+ }
307
+ }
308
+
309
+ // Parse the raw telemetry URL!
310
+ telemetryURL , err := url .Parse (telemetryURL )
311
+ if err != nil {
312
+ return xerrors .Errorf ("parse telemetry url: %w" , err )
313
+ }
314
+ if ! inMemoryDatabase || cmd .Flags ().Changed ("telemetry-url" ) {
315
+ options .Telemetry , err = telemetry .New (telemetry.Options {
316
+ BuiltinPostgres : builtinPostgres ,
317
+ DeploymentID : deploymentID ,
318
+ Database : options .Database ,
319
+ Logger : logger .Named ("telemetry" ),
320
+ URL : telemetryURL ,
321
+ GitHubOAuth : oauth2GithubClientID != "" ,
322
+ Prometheus : promEnabled ,
323
+ STUN : len (stunServers ) != 0 ,
324
+ Tunnel : tunnel ,
325
+ })
326
+ if err != nil {
327
+ return xerrors .Errorf ("create telemetry reporter: %w" , err )
328
+ }
329
+ defer options .Telemetry .Close ()
330
+ }
331
+
288
332
coderAPI := coderd .New (options )
289
333
client := codersdk .New (localURL )
290
334
if tlsEnable {
@@ -438,6 +482,8 @@ func server() *cobra.Command {
438
482
<- devTunnelErrChan
439
483
}
440
484
485
+ // Ensures a last report can be sent before exit!
486
+ options .Telemetry .Close ()
441
487
cmd .Println ("Waiting for WebSocket connections to close..." )
442
488
shutdownConns ()
443
489
coderAPI .Close ()
@@ -485,6 +531,8 @@ func server() *cobra.Command {
485
531
"Specifies organizations the user must be a member of to authenticate with GitHub." )
486
532
cliflag .BoolVarP (root .Flags (), & oauth2GithubAllowSignups , "oauth2-github-allow-signups" , "" , "CODER_OAUTH2_GITHUB_ALLOW_SIGNUPS" , false ,
487
533
"Specifies whether new users can sign up with GitHub." )
534
+ cliflag .StringVarP (root .Flags (), & telemetryURL , "telemetry-url" , "" , "CODER_TELEMETRY_URL" , "https://telemetry.coder.com" , "Specifies a URL to send telemetry to." )
535
+ _ = root .Flags ().MarkHidden ("telemetry-url" )
488
536
cliflag .BoolVarP (root .Flags (), & tlsEnable , "tls-enable" , "" , "CODER_TLS_ENABLE" , false , "Specifies if TLS will be enabled" )
489
537
cliflag .StringVarP (root .Flags (), & tlsCertFile , "tls-cert-file" , "" , "CODER_TLS_CERT_FILE" , "" ,
490
538
"Specifies the path to the certificate for TLS. It requires a PEM-encoded file. " +
0 commit comments