Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore: track disabled telemetry#16347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
hugodutka merged 22 commits intomainfromhugodutka/track-disabled-telemetry
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from9 commits
Commits
Show all changes
22 commits
Select commitHold shift + click to select a range
b44071a
ReportDisabledIfNeeded
hugodutkaJan 30, 2025
bb08f9d
add a test for TestReportDisabledIfNeeded
hugodutkaJan 30, 2025
db99f01
remove unused values
hugodutkaJan 30, 2025
7293db5
fix data race
hugodutkaJan 30, 2025
2ceaf0d
disable report on close when reporting telemetry disabled
hugodutkaJan 30, 2025
9cccf4d
fmt
hugodutkaJan 30, 2025
ce2b563
simplify the telemetry disabled reporting logic
hugodutkaJan 31, 2025
7085aa0
more accurate comments
hugodutkaJan 31, 2025
16ba740
clearer comment
hugodutkaJan 31, 2025
3b62bf9
fix test
hugodutkaJan 31, 2025
43b7e6b
clarify comment
hugodutkaJan 31, 2025
aa56a6e
nit
hugodutkaJan 31, 2025
a29b7f2
add another comment
hugodutkaJan 31, 2025
f3cb1b8
shorten comment
hugodutkaJan 31, 2025
75d231f
fix log message
hugodutkaJan 31, 2025
a1c12cf
add an extra integration test
hugodutkaJan 31, 2025
a0fce8d
use only a single telemetry item to track telemetry enabled status
hugodutkaJan 31, 2025
9a86ec9
rename RecordTelemetryStatusChange to RecordTelemetryStatus
hugodutkaJan 31, 2025
e032cce
update comment
hugodutkaJan 31, 2025
a7fe670
lint
hugodutkaJan 31, 2025
b4768fa
fix test
hugodutkaJan 31, 2025
a2846d5
replace "1" and "0" with "true" and "false"
hugodutkaFeb 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletionscli/server.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -814,11 +814,32 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
if err != nil {
return xerrors.Errorf("create telemetry reporter: %w", err)
}
go options.Telemetry.RunSnapshotter()
defer options.Telemetry.Close()
} else {
logger.Warn(ctx, fmt.Sprintf(`telemetry disabled, unable to notify of security issues. Read more: %s/admin/setup/telemetry`, vals.DocsURL.String()))
}

if !vals.Telemetry.Enable.Value() {
reporter, err := telemetry.New(telemetry.Options{
DeploymentID: deploymentID,
Database: options.Database,
Logger: logger.Named("telemetry"),
URL: vals.Telemetry.URL.Value(),
DisableReportOnClose: true,
})
if err != nil {
logger.Debug(ctx, "create telemetry reporter (disabled)", slog.Error(err))
} else {
go func() {
defer reporter.Close()
if err := reporter.ReportDisabledIfNeeded(); err != nil {
logger.Debug(ctx, "failed to report disabled telemetry", slog.Error(err))
}
}()
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Should this not just be in the else block?

Suggested change
}else {
logger.Warn(ctx,fmt.Sprintf(`telemetry disabled, unable to notify of security issues. Read more: %s/admin/setup/telemetry`,vals.DocsURL.String()))
}
if!vals.Telemetry.Enable.Value() {
reporter,err:= telemetry.New(telemetry.Options{
DeploymentID:deploymentID,
Database:options.Database,
Logger:logger.Named("telemetry"),
URL:vals.Telemetry.URL.Value(),
DisableReportOnClose:true,
})
iferr!=nil {
logger.Debug(ctx,"create telemetry reporter (disabled)",slog.Error(err))
}else {
gofunc() {
deferreporter.Close()
iferr:=reporter.ReportDisabledIfNeeded();err!=nil {
logger.Debug(ctx,"failed to report disabled telemetry",slog.Error(err))
}
}()
}
}
}else {
logger.Warn(ctx,fmt.Sprintf(`telemetry disabled, unable to notify of security issues. Read more: %s/admin/setup/telemetry`,vals.DocsURL.String()))
reporter,err:= telemetry.New(telemetry.Options{
DeploymentID:deploymentID,
Database:options.Database,
Logger:logger.Named("telemetry"),
URL:vals.Telemetry.URL.Value(),
DisableReportOnClose:true,
})
iferr!=nil {
logger.Debug(ctx,"create telemetry reporter (disabled)",slog.Error(err))
}else {
gofunc() {
deferreporter.Close()
iferr:=reporter.ReportDisabledIfNeeded();err!=nil {
logger.Debug(ctx,"failed to report disabled telemetry",slog.Error(err))
}
}()
}
}

Is there a way we can just configuretelemetry.New and pass in theenabled flag into the telemetry struct? I ask because this init code is now ~60 lines long, wondering we can push theelse condition just into theRunSnapshotter.

Just a thought.

hugodutka reacted with thumbs up emoji
// This prevents the pprof import from being accidentally deleted.
_ = pprof.Handler
if vals.Pprof.Enable {
Expand Down
101 changes: 88 additions & 13 deletionscoderd/telemetry/telemetry.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,10 +47,11 @@ type Options struct {
// URL is an endpoint to direct telemetry towards!
URL *url.URL

DeploymentID string
DeploymentConfig *codersdk.DeploymentValues
BuiltinPostgres bool
Tunnel bool
DeploymentID string
DeploymentConfig *codersdk.DeploymentValues
BuiltinPostgres bool
Tunnel bool
DisableReportOnClose bool

SnapshotFrequency time.Duration
ParseLicenseJWT func(lic *License) error
Expand All@@ -59,6 +60,9 @@ type Options struct {
// New constructs a reporter for telemetry data.
// Duplicate data will be sent, it's on the server-side to index by UUID.
// Data is anonymized prior to being sent!
//
// The returned Reporter should be started with RunSnapshotter() to begin
// reporting.
func New(options Options) (Reporter, error) {
if options.SnapshotFrequency == 0 {
// Report once every 30mins by default!
Expand All@@ -83,7 +87,6 @@ func New(options Options) (Reporter, error) {
snapshotURL: snapshotURL,
startedAt: dbtime.Now(),
}
go reporter.runSnapshotter()
return reporter, nil
}

Expand All@@ -101,6 +104,14 @@ type Reporter interface {
Report(snapshot *Snapshot)
Enabled() bool
Close()
// RunSnapshotter runs reporting in a loop. It should be called in a
// goroutine to avoid blocking the caller.
RunSnapshotter()
// ReportDisabledIfNeeded reports that the telemetry was disabled in the following scenarios:
// 1. The telemetry was enabled at some point, and we haven't reported the disabled telemetry yet.
// 2. The telemetry was enabled at some point, we reported the disabled telemetry, the telemetry
// was enabled again, then disabled again, and we haven't reported it yet.
ReportDisabledIfNeeded() error
}

type remoteReporter struct {
Expand DownExpand Up@@ -161,10 +172,12 @@ func (r *remoteReporter) Close() {
close(r.closed)
now := dbtime.Now()
r.shutdownAt = &now
// Report a final collection of telemetry prior to close!
// This could indicate final actions a user has taken, and
// the time the deployment was shutdown.
r.reportWithDeployment()
if !r.options.DisableReportOnClose {
// Report a final collection of telemetry prior to close!
// This could indicate final actions a user has taken, and
// the time the deployment was shutdown.
r.reportWithDeployment()
}
r.closeFunc()
}

Expand All@@ -177,7 +190,18 @@ func (r *remoteReporter) isClosed() bool {
}
}

func (r *remoteReporter) runSnapshotter() {
func (r *remoteReporter) recordTelemetryEnabled() {
if err := r.options.Database.UpsertTelemetryItem(r.ctx, database.UpsertTelemetryItemParams{
Key: string(TelemetryItemKeyTelemetryEnabled),
Value: "",
}); err != nil {
r.options.Logger.Debug(r.ctx, "upsert last telemetry report", slog.Error(err))
}
}

func (r *remoteReporter) RunSnapshotter() {
r.recordTelemetryEnabled()

first := true
ticker := time.NewTicker(r.options.SnapshotFrequency)
defer ticker.Stop()
Expand DownExpand Up@@ -330,6 +354,53 @@ func checkIDPOrgSync(ctx context.Context, db database.Store, values *codersdk.De
return syncConfig.Field != "", nil
}

func (r *remoteReporter) ReportDisabledIfNeeded() error {
db := r.options.Database
telemetryEnabled, telemetryEnabledErr := db.GetTelemetryItem(r.ctx, string(TelemetryItemKeyTelemetryEnabled))
if telemetryEnabledErr != nil && !errors.Is(telemetryEnabledErr, sql.ErrNoRows) {
r.options.Logger.Debug(r.ctx, "get telemetry enabled", slog.Error(telemetryEnabledErr))
}
telemetryDisabled, telemetryDisabledErr := db.GetTelemetryItem(r.ctx, string(TelemetryItemKeyTelemetryDisabled))
if telemetryDisabledErr != nil && !errors.Is(telemetryDisabledErr, sql.ErrNoRows) {
r.options.Logger.Debug(r.ctx, "get telemetry disabled", slog.Error(telemetryDisabledErr))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Is there a benefit to making this 2 rows?

If it was just 1 row, with the last state being recorded, would that be sufficient? As in, just storetelemetryEnabled = true/false

Then you'd have to mergerecordTelemetryEnabled and this function that sends teh final packet.

| Description                            | telemetryEnabled (db) | telemetryEnabled (is) | Report Telemetry Disabled ||----------------------------------------|-----------------------|-----------------------|---------------------------|| New deployment                         | <null>                | true                  | No                        || New deployment, but disabled           | <null>                | false                 | No                        || Telemetry enabled, and still is        | true                  | true                  | No                        || Telemetry enabled, but disabled        | true                  | false                 | Yes                       || Telemetry was disabled, now is enabled | false                 | true                  | Yes                       || Disabled, still disabled               | false                 | false                 | No                        |

If there is some reason the logic that saves theenabled prop and the logic that sends the packet cannot be combined, then this suggestion would not work.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

You're right, I think it would work, and it would simplify the logic too. We'd only need to report telemetry disabled when the telemetryEnabled in db switches from true to false. I'll refactor this.

Emyrk reacted with thumbs up emoji
// There are 2 scenarios in which we want to report the disabled telemetry:
// 1. The telemetry was enabled at some point, and we haven't reported the disabled telemetry yet.
// 2. The telemetry was enabled at some point, we reported the disabled telemetry, the telemetry
// was enabled again, then disabled again, and we haven't reported it yet.
//
// - In both cases, the TelemetryEnabled item will be present.
// - In case 1. the TelemetryDisabled item will not be present.
// - In case 2. the TelemetryDisabled item will be present, and the TelemetryEnabled item will
// be more recent than the TelemetryDisabled item.
shouldReportDisabledTelemetry := telemetryEnabledErr == nil &&
(errors.Is(telemetryDisabledErr, sql.ErrNoRows) ||
(telemetryDisabledErr == nil && telemetryEnabled.UpdatedAt.After(telemetryDisabled.UpdatedAt)))
if !shouldReportDisabledTelemetry {
return nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Can we extract this to it's own function, maybe throw some quick unit tests around just this logic? I know you have a test that tests the full flow, but this PR comes down to this conditional which is a bit hard to read.

We can define the full space of input values and test it easily if we extract it to a function. If I am reading the comments correctly, this is the truth table:

| Description                | telemetryEnabled  | telemetryDisabled | Report Telemetry Disabled ||----------------------------|-------------------|-------------------|---------------------------|| New deployment             | <null>            | <null>            | No                        || Enabled                    | exists @ time = 0 | <null>            | Yes                       || Disabled,but never enabled | <null>            | exists @ time = 0 | No                        || Enabled after disabled     | exists @ time = 1 | exists @ time = 0 | No                        || Disabled after enabled     | exists @ time = 0 | exists @ time = 1 | Yes                       |

If my understanding is correct, then this code is correct as well.

Just a suggestion if we extract this to a function, we can implement the logic in easier to read conditionals.

funcshouldReportDisabledTelemetry(/* ... */) {iferrors.Is(telemetryEnabled,sql.ErrNoRows) {returnfalse// Telemetry was never enabled, so do not report  }iferrors.Is(telemetryDisabledErr,sql.ErrNoRows) {returntrue// Never reported teletry was disabled, so send the report  }returntelemetryEnabled.UpdatedAt.After(telemetryDisabled.UpdatedAt)}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think I'd prefer it being 1 row as mentioned above it possible

hugodutka reacted with thumbs up emoji

if err := db.UpsertTelemetryItem(r.ctx, database.UpsertTelemetryItemParams{
Key: string(TelemetryItemKeyTelemetryDisabled),
Value: "",
}); err != nil {
return xerrors.Errorf("upsert telemetry disabled: %w", err)
}
item, err := db.GetTelemetryItem(r.ctx, string(TelemetryItemKeyTelemetryDisabled))
if err != nil {
return xerrors.Errorf("get telemetry disabled: %w", err)
}

r.reportSync(
&Snapshot{
TelemetryItems: []TelemetryItem{
ConvertTelemetryItem(item),
},
},
)
return nil
}

// createSnapshot collects a full snapshot from the database.
func (r *remoteReporter) createSnapshot() (*Snapshot, error) {
var (
Expand DownExpand Up@@ -1567,6 +1638,8 @@ type telemetryItemKey string
//revive:disable:exported
const (
TelemetryItemKeyHTMLFirstServedAt telemetryItemKey = "html_first_served_at"
TelemetryItemKeyTelemetryEnabled telemetryItemKey = "telemetry_enabled"
TelemetryItemKeyTelemetryDisabled telemetryItemKey = "telemetry_disabled"
)

type TelemetryItem struct {
Expand All@@ -1578,6 +1651,8 @@ type TelemetryItem struct {

type noopReporter struct{}

func (*noopReporter) Report(_ *Snapshot) {}
func (*noopReporter) Enabled() bool { return false }
func (*noopReporter) Close() {}
func (*noopReporter) Report(_ *Snapshot) {}
func (*noopReporter) Enabled() bool { return false }
func (*noopReporter) Close() {}
func (*noopReporter) RunSnapshotter() {}
func (*noopReporter) ReportDisabledIfNeeded() error { return nil }
78 changes: 75 additions & 3 deletionscoderd/telemetry/telemetry_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -361,31 +361,102 @@ func TestTelemetryItem(t *testing.T) {
require.Equal(t, item.Value, "new_value")
}

func collectSnapshot(t *testing.T, db database.Store, addOptionsFn func(opts telemetry.Options) telemetry.Options) (*telemetry.Deployment, *telemetry.Snapshot) {
func TestReportDisabledIfNeeded(t *testing.T) {
t.Parallel()
db, _ := dbtestutil.NewDB(t)
ctx := testutil.Context(t, testutil.WaitMedium)
serverURL, _, snapshotChan := mockTelemetryServer(t)

options := telemetry.Options{
Database: db,
Logger: testutil.Logger(t),
URL: serverURL,
DeploymentID: uuid.NewString(),
DisableReportOnClose: true,
}

reporter, err := telemetry.New(options)
require.NoError(t, err)
t.Cleanup(reporter.Close)

// No telemetry enabled item, so no report should be sent
require.NoError(t, reporter.ReportDisabledIfNeeded())
require.Empty(t, snapshotChan)

// Telemetry enabled item present, and a telemetry disabled item not present
// Report should be sent
_ = dbgen.TelemetryItem(t, db, database.TelemetryItem{
Key: string(telemetry.TelemetryItemKeyTelemetryEnabled),
Value: "",
})
require.NoError(t, reporter.ReportDisabledIfNeeded())
select {
case snapshot := <-snapshotChan:
require.Len(t, snapshot.TelemetryItems, 1)
require.Equal(t, snapshot.TelemetryItems[0].Key, string(telemetry.TelemetryItemKeyTelemetryDisabled))
case <-time.After(testutil.WaitShort / 2):
t.Fatal("timeout waiting for snapshot")
}

// Telemetry enabled item present, and a more recent telemetry disabled item present
// Report should not be sent
require.NoError(t, reporter.ReportDisabledIfNeeded())
require.Empty(t, snapshotChan)

// Telemetry enabled item present, and a less recent telemetry disabled item present
// Report should be sent
// Wait a bit to ensure UpdatedAt is bigger when we upsert the telemetry enabled item
time.Sleep(100 * time.Millisecond)
require.NoError(t, db.UpsertTelemetryItem(ctx, database.UpsertTelemetryItemParams{
Key: string(telemetry.TelemetryItemKeyTelemetryEnabled),
Value: "",
}))
require.NoError(t, reporter.ReportDisabledIfNeeded())
select {
case snapshot := <-snapshotChan:
require.Len(t, snapshot.TelemetryItems, 1)
require.Equal(t, snapshot.TelemetryItems[0].Key, string(telemetry.TelemetryItemKeyTelemetryDisabled))
case <-time.After(testutil.WaitShort / 2):
t.Fatal("timeout waiting for snapshot")
}
}

func mockTelemetryServer(t *testing.T) (*url.URL, chan *telemetry.Deployment, chan *telemetry.Snapshot) {
t.Helper()
deployment := make(chan *telemetry.Deployment, 64)
snapshot := make(chan *telemetry.Snapshot, 64)
r := chi.NewRouter()
r.Post("/deployment", func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, buildinfo.Version(), r.Header.Get(telemetry.VersionHeader))
w.WriteHeader(http.StatusAccepted)
dd := &telemetry.Deployment{}
err := json.NewDecoder(r.Body).Decode(dd)
require.NoError(t, err)
deployment <- dd
// Ensure the header is sent only after deployment is sent
w.WriteHeader(http.StatusAccepted)
})
r.Post("/snapshot", func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, buildinfo.Version(), r.Header.Get(telemetry.VersionHeader))
w.WriteHeader(http.StatusAccepted)
ss := &telemetry.Snapshot{}
err := json.NewDecoder(r.Body).Decode(ss)
require.NoError(t, err)
snapshot <- ss
// Ensure the header is sent only after snapshot is sent
w.WriteHeader(http.StatusAccepted)
})
server := httptest.NewServer(r)
t.Cleanup(server.Close)
serverURL, err := url.Parse(server.URL)
require.NoError(t, err)

return serverURL, deployment, snapshot
}

func collectSnapshot(t *testing.T, db database.Store, addOptionsFn func(opts telemetry.Options) telemetry.Options) (*telemetry.Deployment, *telemetry.Snapshot) {
t.Helper()

serverURL, deployment, snapshot := mockTelemetryServer(t)

options := telemetry.Options{
Database: db,
Logger: testutil.Logger(t),
Expand All@@ -398,6 +469,7 @@ func collectSnapshot(t *testing.T, db database.Store, addOptionsFn func(opts tel

reporter, err := telemetry.New(options)
require.NoError(t, err)
go reporter.RunSnapshotter()
t.Cleanup(reporter.Close)
return <-deployment, <-snapshot
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp