-
Notifications
You must be signed in to change notification settings - Fork 894
Daily Active User Metrics #3735
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
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
502aa52
agent: add ConnStats
ammario 3893045
agent: add StatsReporter
ammario c03ad90
Frontend tests pass
ammario 1bd9cec
Split DAUChart into its own file
ammario e46329b
Get FE tests passing with real data!
ammario d472b13
Test frontend
ammario e0295e0
Fix compilation error
ammario 2f1a423
Rename ConnStats to StatsConn
ammario 0a50cc9
continues instead of returns
ammario 7feab5e
Fix some test failures
ammario a4d2cf7
Redo tests
ammario 52c9d10
Address review comments
ammario 3f9901e
REVAMP — backend tests work
ammario 7840509
Black triangle
ammario 39170cf
Consolidate template state machines
ammario eb373d6
Move workspaceagent endpoint
ammario a3d87b8
Address most review comments
ammario 31ba0c6
Improve contrast in chart
ammario 5b906c1
Add more agent tests
ammario 49d9386
Fix JS ci
ammario b14a077
A bunch of minor touch ups
ammario 8da24c4
Stabilize protoc
ammario 00ec953
Merge remote-tracking branch 'origin/main' into metrics
ammario 4940319
Update lockfile
ammario 22b2028
Address comments + attempt to fix protoc
ammario 0157365
fixup! Address comments + attempt to fix protoc
ammario b166cdd
Try to fix protoc...
ammario 4201998
PROTO!
ammario 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
agent: add StatsReporter
- Loading branch information
commit 38930457e318b8993f44a14968e3a8d46dba00fa
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,3 +42,4 @@ site/out/ | |
.vscode/*.log | ||
**/*.swp | ||
.coderv2/* | ||
**/__debug_bin |
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 |
---|---|---|
@@ -1,38 +1,90 @@ | ||
package agent | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net" | ||
"time" | ||
"sync" | ||
"sync/atomic" | ||
|
||
"cdr.dev/slog" | ||
) | ||
|
||
// ConnStats wraps a net.Conn with statistics. | ||
type ConnStats struct { | ||
CreatedAt time.Time `json:"created_at,omitempty"` | ||
Protocol string `json:"protocol,omitempty"` | ||
RxBytes uint64 `json:"rx_bytes,omitempty"` | ||
TxBytes uint64 `json:"tx_bytes,omitempty"` | ||
|
||
*ProtocolStats | ||
net.Conn `json:"-"` | ||
} | ||
|
||
var _ net.Conn = new(ConnStats) | ||
|
||
func (c *ConnStats) Read(b []byte) (n int, err error) { | ||
n, err = c.Conn.Read(b) | ||
c.RxBytes += uint64(n) | ||
atomic.AddInt64(&c.RxBytes, int64(n)) | ||
return n, err | ||
} | ||
|
||
func (c *ConnStats) Write(b []byte) (n int, err error) { | ||
n, err = c.Conn.Write(b) | ||
c.TxBytes += uint64(n) | ||
atomic.AddInt64(&c.TxBytes, int64(n)) | ||
return n, err | ||
} | ||
|
||
type ProtocolStats struct { | ||
NumConns int64 `json:"num_comms"` | ||
|
||
// RxBytes must be read with atomic. | ||
RxBytes int64 `json:"rx_bytes"` | ||
|
||
// TxBytes must be read with atomic. | ||
TxBytes int64 `json:"tx_bytes"` | ||
} | ||
|
||
var _ net.Conn = new(ConnStats) | ||
|
||
// Stats records the Agent's network connection statistics for use in | ||
// user-facing metrics and debugging. | ||
type Stats struct { | ||
Conns []ConnStats `json:"conns,omitempty"` | ||
sync.RWMutex `json:"-"` | ||
ProtocolStats map[string]*ProtocolStats `json:"conn_stats,omitempty"` | ||
ammario marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func (s *Stats) Copy() *Stats { | ||
s.RLock() | ||
ss := Stats{ProtocolStats: make(map[string]*ProtocolStats, len(s.ProtocolStats))} | ||
for k, cs := range s.ProtocolStats { | ||
ss.ProtocolStats[k] = &ProtocolStats{ | ||
NumConns: atomic.LoadInt64(&cs.NumConns), | ||
RxBytes: atomic.LoadInt64(&cs.RxBytes), | ||
TxBytes: atomic.LoadInt64(&cs.TxBytes), | ||
} | ||
} | ||
s.RUnlock() | ||
return &ss | ||
} | ||
|
||
// wrapConn returns a new connection that records statistics. | ||
func (s *Stats) wrapConn(conn net.Conn, protocol string) net.Conn { | ||
s.Lock() | ||
ps, ok := s.ProtocolStats[protocol] | ||
if !ok { | ||
ps = &ProtocolStats{} | ||
s.ProtocolStats[protocol] = ps | ||
} | ||
s.Unlock() | ||
|
||
atomic.AddInt64(&ps.NumConns, 1) | ||
cs := &ConnStats{ | ||
ProtocolStats: ps, | ||
Conn: conn, | ||
} | ||
|
||
return cs | ||
} | ||
|
||
// StatsReporter periodically accept and records agent stats. | ||
type StatsReporter func( | ||
ctx context.Context, | ||
log slog.Logger, | ||
stats func() *Stats, | ||
) (io.Closer, 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
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
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.
Uh oh!
There was an error while loading. Please reload this page.