Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 15, 2025

Conversation

deansheather
Copy link
Member

@deansheather deansheather commented Jul 30, 2025

Not used in coderd yet, see stack.

Adds two new packages:

  • coderd/usage: provides an interface for the "Collector" as well as a stub implementation for AGPL
  • enterprise/coderd/usage: provides an interface for the "Publisher" as well as a Tallyman implementation

Relates to coder/internal#814

Copy link
Member Author

deansheather commented Jul 30, 2025

@deansheather deansheather marked this pull request as ready for review July 30, 2025 06:05
Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

I have some non-blocking comments below but might need to take another pass.

const (
CoderLicenseJWTHeader = "Coder-License-JWT"

tallymanURL = "https://tallyman-ingress.coder.com"
Copy link
Member

Choose a reason for hiding this comment

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

I can see an argument for making this a var so it's configurable at build time.

Copy link
Member Author

Choose a reason for hiding this comment

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

If we do need that we can add it very easily in the future.

Copy link
Member

Choose a reason for hiding this comment

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

Obligatory reminder to check migration number before merging!

@github-actions github-actions bot added the stale This issue is like stale bread. label Aug 7, 2025
@github-actions github-actions bot closed this Aug 10, 2025
@deansheather deansheather reopened this Aug 12, 2025
@deansheather deansheather removed the stale This issue is like stale bread. label Aug 12, 2025
@deansheather deansheather force-pushed the 07-30-chore_add_usage_tracking_package branch from e376311 to 82ea8e1 Compare August 15, 2025 09:28
@deansheather deansheather requested a review from Emyrk as a code owner August 15, 2025 09:28
Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

I don't have any further blocking comments and the dbauthz stuff looks OK to me. I'd like a +1 from Spike before merging though. You probably also need to run make gen again.

Copy link
Contributor

@spikecurtis spikecurtis left a comment

Choose a reason for hiding this comment

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

LGTM modulo gen failures

Copy link
Member

@Emyrk Emyrk left a comment

Choose a reason for hiding this comment

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

No blockers from me! 👍

@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

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

Mocked! Nice! Been slowly converting them all 👍

Comment on lines +3 to +6
-- 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_type TEXT NOT NULL CONSTRAINT usage_event_type_check CHECK (event_type IN ('dc_managed_agents_v1')),
Copy link
Member

Choose a reason for hiding this comment

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

Adding enum values is actually easy now:

ALTER TYPE notification_method ADD VALUE IF NOT EXISTS 'inbox';

Removing them though is a problem:

-- The migration is about an enum value change
-- As we can not remove a value from an enum, we can let the down migration empty
-- In order to avoid any failure, we use ADD VALUE IF NOT EXISTS to add the value

(Although we never run down migrations so....)


Making it an enum generates the proper Golang enum

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Comment on lines +59 to +61
-- Note that this selects from the CTE, not the original table. The CTE is named
-- the same as the original table to trick sqlc into reusing the existing struct
-- for the table.
Copy link
Member

Choose a reason for hiding this comment

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

👍 Good trick

Comment on lines +74 to +86
SELECT
UNNEST(@ids::text[]) AS id,
UNNEST(@failure_messages::text[]) AS failure_message,
UNNEST(@set_published_ats::boolean[]) AS set_published_at
) input
WHERE
input.id = usage_events.id
-- If the number of ids, failure messages, and set published ats are not the
-- same, do not do anything. Unfortunately you can't really throw from a
-- query without writing a function or doing some jank like dividing by
-- zero, so this is the best we can do.
AND cardinality(@ids::text[]) = cardinality(@failure_messages::text[])
AND cardinality(@ids::text[]) = cardinality(@set_published_ats::boolean[]);
Copy link
Member

Choose a reason for hiding this comment

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

Yea, if we ever switch to PGX, it has batch operations: https://docs.sqlc.dev/en/latest/reference/changelog.html#batch-operation-improvements

Then we get type safety and the inputs are []struct{ /* ... Fields ... */}

@@ -584,6 +584,7 @@ type Claims struct {
Version uint64 `json:"version"`
Features Features `json:"features"`
RequireTelemetry bool `json:"require_telemetry,omitempty"`
PublishUsageData bool `json:"publish_usage_data,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

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

Interesting this is not an entitlement?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not really a feature, rather a flag to send usage data (which will always get collected in enterprise builds) to a server. So it makes sense that it goes where RequireTelemetry goes since they're very similar.

@deansheather deansheather force-pushed the 07-30-chore_add_usage_tracking_package branch from dca0890 to 2529c80 Compare August 15, 2025 15:19
@deansheather deansheather merged commit a25d856 into main Aug 15, 2025
55 of 58 checks passed
Copy link
Member Author

Merge activity

@deansheather deansheather deleted the 07-30-chore_add_usage_tracking_package branch August 15, 2025 15:31
@github-actions github-actions bot locked and limited conversation to collaborators Aug 15, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants