This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Usage metric pushing #38
Merged
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
Usage metric pushing
- Loading branch information
commit 9b2232d267573713f13a30b7c8e9cff02d3e62b4
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 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,41 @@ | ||
package activity | ||
|
||
import ( | ||
"time" | ||
|
||
"cdr.dev/coder-cli/internal/entclient" | ||
"go.coder.com/flog" | ||
"golang.org/x/time/rate" | ||
) | ||
|
||
const pushInterval = time.Minute | ||
|
||
// Pusher pushes activity metrics no more than once per pushInterval. Pushes | ||
// within the same interval are a no-op. | ||
type Pusher struct { | ||
envID string | ||
source string | ||
|
||
client *entclient.Client | ||
rate *rate.Limiter | ||
} | ||
|
||
func NewPusher(c *entclient.Client, envID, source string) *Pusher { | ||
return &Pusher{ | ||
envID: envID, | ||
source: source, | ||
client: c, | ||
rate: rate.NewLimiter(rate.Every(pushInterval), 1), | ||
} | ||
} | ||
|
||
func (p *Pusher) Push() { | ||
if !p.rate.Allow() { | ||
return | ||
} | ||
|
||
err := p.client.PushActivity(p.source, p.envID) | ||
if err != nil { | ||
flog.Error("push activity: %s", err.Error()) | ||
} | ||
} |
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,17 @@ | ||
package activity | ||
|
||
import "io" | ||
|
||
type activityWriter struct { | ||
p *Pusher | ||
wr io.Writer | ||
} | ||
|
||
func (w *activityWriter) Write(p []byte) (n int, err error) { | ||
w.p.Push() | ||
return w.wr.Write(p) | ||
} | ||
|
||
func (p *Pusher) Writer(wr io.Writer) io.Writer { | ||
return &activityWriter{p: p, wr: wr} | ||
} |
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,19 @@ | ||
package entclient | ||
|
||
import "net/http" | ||
|
||
func (c Client) PushActivity(source string, envID string) error { | ||
res, err := c.request("POST", "/api/metrics/usage/push", map[string]string{ | ||
coadler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"source": source, | ||
"environment_id": envID, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if res.StatusCode != http.StatusOK { | ||
return bodyError(res) | ||
} | ||
|
||
return nil | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.