@@ -781,6 +781,47 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
781
781
// This should be output before the logs start streaming.
782
782
cliui .Infof (inv .Stdout , "\n ==> Logs will stream in below (press ctrl+c to gracefully exit):" )
783
783
784
+ // We set this to a valid function pointer later
785
+ orgSyncEnabledFn := new (func () bool )
786
+ if vals .Telemetry .Enable {
787
+ vals , err := vals .WithoutSecrets ()
788
+ if err != nil {
789
+ return xerrors .Errorf ("remove secrets from deployment values: %w" , err )
790
+ }
791
+ options .Telemetry , err = telemetry .New (telemetry.Options {
792
+ BuiltinPostgres : builtinPostgres ,
793
+ DeploymentID : deploymentID ,
794
+ Database : options .Database ,
795
+ Logger : logger .Named ("telemetry" ),
796
+ URL : vals .Telemetry .URL .Value (),
797
+ Tunnel : tunnel != nil ,
798
+ DeploymentConfig : vals ,
799
+ ParseLicenseJWT : func (lic * telemetry.License ) error {
800
+ // This will be nil when running in AGPL-only mode.
801
+ if options .ParseLicenseClaims == nil {
802
+ return nil
803
+ }
804
+
805
+ email , trial , err := options .ParseLicenseClaims (lic .JWT )
806
+ if err != nil {
807
+ return err
808
+ }
809
+ if email != "" {
810
+ lic .Email = & email
811
+ }
812
+ lic .Trial = & trial
813
+ return nil
814
+ },
815
+ OrganizationSyncEnabled : orgSyncEnabledFn ,
816
+ })
817
+ if err != nil {
818
+ return xerrors .Errorf ("create telemetry reporter: %w" , err )
819
+ }
820
+ defer options .Telemetry .Close ()
821
+ } else {
822
+ logger .Warn (ctx , fmt .Sprintf (`telemetry disabled, unable to notify of security issues. Read more: %s/admin/setup/telemetry` , vals .DocsURL .String ()))
823
+ }
824
+
784
825
// This prevents the pprof import from being accidentally deleted.
785
826
_ = pprof .Handler
786
827
if vals .Pprof .Enable {
@@ -822,50 +863,22 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
822
863
return xerrors .Errorf ("create coder API: %w" , err )
823
864
}
824
865
825
- if vals .Telemetry .Enable {
826
- vals , err := vals .WithoutSecrets ()
827
- if err != nil {
828
- return xerrors .Errorf ("remove secrets from deployment values: %w" , err )
866
+ if vals .Telemetry .Enable && options .Telemetry != nil {
867
+ // We can initialize this pointer only now because the function
868
+ // depends on the coderAPI. Read the docstring on
869
+ // Options.OrganizationSyncEnabled in coderd/telemetry/telemetry.go
870
+ // for more context.
871
+ * orgSyncEnabledFn = func () bool {
872
+ // Sanity check just in case.
873
+ if coderAPI == nil || coderAPI .IDPSync == nil {
874
+ return false
875
+ }
876
+ // nolint:gocritic // AsSystemRestricted is fine here because it's a read-only operation
877
+ // used for telemetry reporting.
878
+ return coderAPI .IDPSync .OrganizationSyncEnabled (dbauthz .AsSystemRestricted (ctx ), options .Database )
829
879
}
830
- options .Telemetry , err = telemetry .New (telemetry.Options {
831
- BuiltinPostgres : builtinPostgres ,
832
- DeploymentID : deploymentID ,
833
- Database : options .Database ,
834
- Logger : logger .Named ("telemetry" ),
835
- URL : vals .Telemetry .URL .Value (),
836
- Tunnel : tunnel != nil ,
837
- DeploymentConfig : vals ,
838
- ParseLicenseJWT : func (lic * telemetry.License ) error {
839
- // This will be nil when running in AGPL-only mode.
840
- if options .ParseLicenseClaims == nil {
841
- return nil
842
- }
843
880
844
- email , trial , err := options .ParseLicenseClaims (lic .JWT )
845
- if err != nil {
846
- return err
847
- }
848
- if email != "" {
849
- lic .Email = & email
850
- }
851
- lic .Trial = & trial
852
- return nil
853
- },
854
- OrganizationSyncEnabled : func () bool {
855
- if coderAPI == nil || coderAPI .IDPSync == nil {
856
- return false
857
- }
858
- // nolint:gocritic // AsSystemRestricted is fine here because it's a read-only operation
859
- // used for telemetry reporting.
860
- return coderAPI .IDPSync .OrganizationSyncEnabled (dbauthz .AsSystemRestricted (ctx ), options .Database )
861
- },
862
- })
863
- if err != nil {
864
- return xerrors .Errorf ("create telemetry reporter: %w" , err )
865
- }
866
- defer options .Telemetry .Close ()
867
- } else {
868
- logger .Warn (ctx , fmt .Sprintf (`telemetry disabled, unable to notify of security issues. Read more: %s/admin/setup/telemetry` , vals .DocsURL .String ()))
881
+ options .Telemetry .Start ()
869
882
}
870
883
871
884
if vals .Prometheus .Enable {
0 commit comments