-
Notifications
You must be signed in to change notification settings - Fork 976
chore: move usage types to new package #19103
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
deansheather
merged 1 commit into
main
from
07-30-chore_move_usage_types_to_new_package
Aug 20, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Package usagetypes contains the types for usage events. These are kept in | ||
// their own package to avoid importing any real code from coderd. | ||
// | ||
// Imports in this package should be limited to the standard library and the | ||
// following packages ONLY: | ||
// - github.com/google/uuid | ||
// - golang.org/x/xerrors | ||
// | ||
// This package is imported by the Tallyman codebase. | ||
package usagetypes | ||
|
||
// Please read the package documentation before adding imports. | ||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"strings" | ||
|
||
"golang.org/x/xerrors" | ||
) | ||
|
||
// UsageEventType is an enum of all usage event types. It mirrors the database | ||
// type `usage_event_type`. | ||
type UsageEventType string | ||
|
||
const ( | ||
UsageEventTypeDCManagedAgentsV1 UsageEventType = "dc_managed_agents_v1" | ||
) | ||
|
||
func (e UsageEventType) Valid() bool { | ||
switch e { | ||
case UsageEventTypeDCManagedAgentsV1: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
func (e UsageEventType) IsDiscrete() bool { | ||
return e.Valid() && strings.HasPrefix(string(e), "dc_") | ||
} | ||
|
||
func (e UsageEventType) IsHeartbeat() bool { | ||
return e.Valid() && strings.HasPrefix(string(e), "hb_") | ||
} | ||
|
||
// ParseEvent parses the raw event data into the specified Go type. It fails if | ||
// there is any unknown fields or extra data after the event. The returned event | ||
// is validated. | ||
func ParseEvent[T Event](data json.RawMessage) (T, error) { | ||
dec := json.NewDecoder(bytes.NewReader(data)) | ||
dec.DisallowUnknownFields() | ||
|
||
var event T | ||
err := dec.Decode(&event) | ||
if err != nil { | ||
return event, xerrors.Errorf("unmarshal %T event: %w", event, err) | ||
} | ||
if dec.More() { | ||
return event, xerrors.Errorf("extra data after %T event", event) | ||
} | ||
err = event.Valid() | ||
if err != nil { | ||
return event, xerrors.Errorf("invalid %T event: %w", event, err) | ||
} | ||
|
||
return event, nil | ||
} | ||
|
||
// ParseEventWithType parses the raw event data into the specified Go type. It | ||
// fails if there is any unknown fields or extra data after the event. The | ||
// returned event is validated. | ||
func ParseEventWithType(eventType UsageEventType, data json.RawMessage) (Event, error) { | ||
switch eventType { | ||
case UsageEventTypeDCManagedAgentsV1: | ||
return ParseEvent[DCManagedAgentsV1](data) | ||
default: | ||
return nil, xerrors.Errorf("unknown event type: %s", eventType) | ||
} | ||
} | ||
|
||
// Event is a usage event that can be collected by the usage collector. | ||
// | ||
// Note that the following event types should not be updated once they are | ||
// merged into the product. Please consult Dean before making any changes. | ||
// | ||
// This type cannot be implemented outside of this package as it this package | ||
// is the source of truth for the coder/tallyman repo. | ||
type Event interface { | ||
usageEvent() // to prevent external types from implementing this interface | ||
EventType() UsageEventType | ||
Valid() error | ||
Fields() map[string]any // fields to be marshaled and sent to tallyman/Metronome | ||
} | ||
|
||
// DiscreteEvent is a usage event that is collected as a discrete event. | ||
type DiscreteEvent interface { | ||
Event | ||
discreteUsageEvent() // marker method, also prevents external types from implementing this interface | ||
} | ||
|
||
// DCManagedAgentsV1 is a discrete usage event for the number of managed agents. | ||
// This event is sent in the following situations: | ||
// - Once on first startup after usage tracking is added to the product with | ||
// the count of all existing managed agents (count=N) | ||
// - A new managed agent is created (count=1) | ||
type DCManagedAgentsV1 struct { | ||
Count uint64 `json:"count"` | ||
} | ||
|
||
var _ DiscreteEvent = DCManagedAgentsV1{} | ||
|
||
func (DCManagedAgentsV1) usageEvent() {} | ||
func (DCManagedAgentsV1) discreteUsageEvent() {} | ||
func (DCManagedAgentsV1) EventType() UsageEventType { | ||
return UsageEventTypeDCManagedAgentsV1 | ||
} | ||
|
||
func (e DCManagedAgentsV1) Valid() error { | ||
if e.Count == 0 { | ||
return xerrors.New("count must be greater than 0") | ||
} | ||
return nil | ||
} | ||
|
||
func (e DCManagedAgentsV1) Fields() map[string]any { | ||
return map[string]any{ | ||
"count": e.Count, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package usagetypes_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/v2/coderd/usage/usagetypes" | ||
) | ||
|
||
func TestParseEvent(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("ExtraFields", func(t *testing.T) { | ||
t.Parallel() | ||
_, err := usagetypes.ParseEvent[usagetypes.DCManagedAgentsV1]([]byte(`{"count": 1, "extra": "field"}`)) | ||
require.ErrorContains(t, err, "unmarshal usagetypes.DCManagedAgentsV1 event") | ||
}) | ||
|
||
t.Run("ExtraData", func(t *testing.T) { | ||
t.Parallel() | ||
_, err := usagetypes.ParseEvent[usagetypes.DCManagedAgentsV1]([]byte(`{"count": 1}{"count": 2}`)) | ||
require.ErrorContains(t, err, "extra data after usagetypes.DCManagedAgentsV1 event") | ||
}) | ||
|
||
t.Run("DCManagedAgentsV1", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
event, err := usagetypes.ParseEvent[usagetypes.DCManagedAgentsV1]([]byte(`{"count": 1}`)) | ||
require.NoError(t, err) | ||
require.Equal(t, usagetypes.DCManagedAgentsV1{Count: 1}, event) | ||
require.Equal(t, map[string]any{"count": uint64(1)}, event.Fields()) | ||
|
||
_, err = usagetypes.ParseEvent[usagetypes.DCManagedAgentsV1]([]byte(`{"count": "invalid"}`)) | ||
require.ErrorContains(t, err, "unmarshal usagetypes.DCManagedAgentsV1 event") | ||
|
||
_, err = usagetypes.ParseEvent[usagetypes.DCManagedAgentsV1]([]byte(`{}`)) | ||
require.ErrorContains(t, err, "invalid usagetypes.DCManagedAgentsV1 event: count must be greater than 0") | ||
}) | ||
} | ||
|
||
func TestParseEventWithType(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("UnknownEvent", func(t *testing.T) { | ||
t.Parallel() | ||
_, err := usagetypes.ParseEventWithType(usagetypes.UsageEventType("fake"), []byte(`{}`)) | ||
require.ErrorContains(t, err, "unknown event type: fake") | ||
}) | ||
|
||
t.Run("DCManagedAgentsV1", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
eventType := usagetypes.UsageEventTypeDCManagedAgentsV1 | ||
event, err := usagetypes.ParseEventWithType(eventType, []byte(`{"count": 1}`)) | ||
require.NoError(t, err) | ||
require.Equal(t, usagetypes.DCManagedAgentsV1{Count: 1}, event) | ||
require.Equal(t, eventType, event.EventType()) | ||
require.Equal(t, map[string]any{"count": uint64(1)}, event.Fields()) | ||
}) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍