- Notifications
You must be signed in to change notification settings - Fork1k
chore: add usage tracking package#19095
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -5666,3 +5666,34 @@ func (s *MethodTestSuite) TestUserSecrets() { | ||
Asserts(userSecret, policy.ActionRead, userSecret, policy.ActionDelete) | ||
})) | ||
} | ||
func (s *MethodTestSuite) TestUsageEvents() { | ||
s.Run("InsertUsageEvent", s.Mocked(func(db *dbmock.MockStore, faker *gofakeit.Faker, check *expects) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Mocked! Nice! Been slowly converting them all 👍 | ||
params := database.InsertUsageEventParams{ | ||
ID: "1", | ||
EventType: "dc_managed_agents_v1", | ||
EventData: []byte("{}"), | ||
CreatedAt: dbtime.Now(), | ||
} | ||
db.EXPECT().InsertUsageEvent(gomock.Any(), params).Return(nil) | ||
check.Args(params).Asserts(rbac.ResourceUsageEvent, policy.ActionCreate) | ||
})) | ||
s.Run("SelectUsageEventsForPublishing", s.Mocked(func(db *dbmock.MockStore, faker *gofakeit.Faker, check *expects) { | ||
now := dbtime.Now() | ||
db.EXPECT().SelectUsageEventsForPublishing(gomock.Any(), now).Return([]database.UsageEvent{}, nil) | ||
check.Args(now).Asserts(rbac.ResourceUsageEvent, policy.ActionUpdate) | ||
})) | ||
s.Run("UpdateUsageEventsPostPublish", s.Mocked(func(db *dbmock.MockStore, faker *gofakeit.Faker, check *expects) { | ||
now := dbtime.Now() | ||
params := database.UpdateUsageEventsPostPublishParams{ | ||
Now: now, | ||
IDs: []string{"1", "2"}, | ||
FailureMessages: []string{"error", "error"}, | ||
SetPublishedAts: []bool{false, false}, | ||
} | ||
db.EXPECT().UpdateUsageEventsPostPublish(gomock.Any(), params).Return(nil) | ||
check.Args(params).Asserts(rbac.ResourceUsageEvent, policy.ActionUpdate) | ||
})) | ||
} |
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE usage_events; |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,25 @@ | ||||||||||
CREATETABLEusage_events ( | ||||||||||
idTEXTPRIMARY KEY, | ||||||||||
-- We use a TEXT column with a CHECK constraint rather than an enum because of | ||||||||||
-- the limitations with adding new values to an enum and using them in the | ||||||||||
-- same transaction. | ||||||||||
event_typeTEXTNOT NULLCONSTRAINT usage_event_type_checkCHECK (event_typeIN ('dc_managed_agents_v1')), | ||||||||||
Comment on lines +3 to +6 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Adding enum values is actually easy now:
Removing them though is a problem:
(Although we never run down migrations so....) Making it an enum generates the proper Golang enum There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Spike asked me to not do this in a prior comment because you can't add a value and then reference it in the same transaction unless you're on pg 17. We run all migrations in a single transaction (for good reason, to avoid partial upgrades), which means we can never reference enum types in future transactions. | ||||||||||
event_data JSONBNOT NULL, | ||||||||||
created_atTIMESTAMP WITH TIME ZONENOT NULL, | ||||||||||
publish_started_atTIMESTAMP WITH TIME ZONE DEFAULTNULL, | ||||||||||
published_atTIMESTAMP WITH TIME ZONE DEFAULTNULL, | ||||||||||
failure_messageTEXT DEFAULTNULL | ||||||||||
); | ||||||||||
COMMENT ON TABLE usage_events IS'usage_events contains usage data that is collected from the product and potentially shipped to the usage collector service.'; | ||||||||||
COMMENT ON COLUMN usage_events.id IS'For "discrete" event types, this is a random UUID. For "heartbeat" event types, this is a combination of the event type and a truncated timestamp.'; | ||||||||||
COMMENT ON COLUMN usage_events.event_type IS'The usage event type with version. "dc" means "discrete" (e.g. a single event, for counters), "hb" means "heartbeat" (e.g. a recurring event that contains a total count of usage generated from the database, for gauges).'; | ||||||||||
COMMENT ON COLUMN usage_events.event_data IS'Event payload. Determined by the matching usage struct for this event type.'; | ||||||||||
COMMENT ON COLUMN usage_events.publish_started_at IS'Set to a timestamp while the event is being published by a Coder replica to the usage collector service. Used to avoid duplicate publishes by multiple replicas. Timestamps older than 1 hour are considered expired.'; | ||||||||||
COMMENT ON COLUMN usage_events.published_at IS'Set to a timestamp when the event is successfully (or permanently unsuccessfully) published to the usage collector service. If set, the event should never be attempted to be published again.'; | ||||||||||
COMMENT ON COLUMN usage_events.failure_message IS'Set to an error message when the event is temporarily or permanently unsuccessfully published to the usage collector service.'; | ||||||||||
-- Create an index with all three fields used by the | ||||||||||
-- SelectUsageEventsForPublishing query. | ||||||||||
CREATEINDEXidx_usage_events_select_for_publishing | ||||||||||
ON usage_events (published_at, publish_started_at, created_at); |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.