-
-
Notifications
You must be signed in to change notification settings - Fork 517
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3273 from telepresenceio/knlambert/add-consumption
Add consumption state to the traffic-manager
- Loading branch information
Showing
20 changed files
with
453 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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 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 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,93 @@ | ||
package state | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/telepresenceio/telepresence/v2/pkg/tunnel" | ||
) | ||
|
||
// SessionConsumptionMetricsStaleTTL is the duration after which we consider the metrics to be staled, meaning | ||
// that they should not be updated anymore since the user doesn't really use Telepresence at the moment. | ||
const SessionConsumptionMetricsStaleTTL = 60 * time.Minute | ||
|
||
func NewSessionConsumptionMetrics() *SessionConsumptionMetrics { | ||
return &SessionConsumptionMetrics{ | ||
ConnectDuration: 0, | ||
FromClientBytes: tunnel.NewCounterProbe("FromClientBytes"), | ||
ToClientBytes: tunnel.NewCounterProbe("ToClientBytes"), | ||
|
||
LastUpdate: time.Now(), | ||
} | ||
} | ||
|
||
type SessionConsumptionMetrics struct { | ||
ConnectDuration uint32 | ||
LastUpdate time.Time | ||
|
||
// data from client to the traffic manager. | ||
FromClientBytes *tunnel.CounterProbe | ||
// data from the traffic manager to the client. | ||
ToClientBytes *tunnel.CounterProbe | ||
} | ||
|
||
func (s *SessionConsumptionMetrics) RunCollect(ctx context.Context) { | ||
go s.FromClientBytes.RunCollect(ctx) | ||
go s.ToClientBytes.RunCollect(ctx) | ||
} | ||
|
||
func (s *SessionConsumptionMetrics) Close() { | ||
s.FromClientBytes.Close() | ||
s.ToClientBytes.Close() | ||
} | ||
|
||
func (s *state) GetSessionConsumptionMetrics(sessionID string) *SessionConsumptionMetrics { | ||
s.mu.RLock() | ||
defer s.mu.RUnlock() | ||
for i := range s.sessions { | ||
if css, ok := s.sessions[i].(*clientSessionState); i == sessionID && ok { | ||
return css.ConsumptionMetrics() | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (s *state) GetAllSessionConsumptionMetrics() map[string]*SessionConsumptionMetrics { | ||
allSCM := make(map[string]*SessionConsumptionMetrics) | ||
s.mu.RLock() | ||
defer s.mu.RUnlock() | ||
for sessionID := range s.sessions { | ||
if css, ok := s.sessions[sessionID].(*clientSessionState); ok { | ||
allSCM[sessionID] = css.ConsumptionMetrics() | ||
} | ||
} | ||
return allSCM | ||
} | ||
|
||
// RefreshSessionConsumptionMetrics refreshes the metrics associated to a specific session. | ||
func (s *state) RefreshSessionConsumptionMetrics(sessionID string) { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
session := s.sessions[sessionID] | ||
if _, isClientSession := session.(*clientSessionState); !isClientSession { | ||
return | ||
} | ||
|
||
lastMarked := session.LastMarked() | ||
var scm *SessionConsumptionMetrics | ||
if css, ok := s.sessions[sessionID].(*clientSessionState); ok { | ||
scm = css.ConsumptionMetrics() | ||
} else { | ||
return | ||
} | ||
|
||
// If the last mark is older than the SessionConsumptionMetricsStaleTTL, it indicates that the duration | ||
// metric should no longer be updated, as the user's machine may be in standby. | ||
isStale := time.Now().After(lastMarked.Add(SessionConsumptionMetricsStaleTTL)) | ||
if !isStale { | ||
scm.ConnectDuration += uint32(time.Since(scm.LastUpdate).Seconds()) | ||
} | ||
|
||
scm.LastUpdate = time.Now() | ||
} |
This file contains 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 state | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func (s *suiteState) TestRefreshSessionConsumptionMetrics() { | ||
// given | ||
now := time.Now() | ||
session1 := &clientSessionState{} | ||
session1.SetLastMarked(now) | ||
session3 := &clientSessionState{} | ||
session3.SetLastMarked(now.Add(-24 * time.Hour * 30)) | ||
s.state.sessions["session-1"] = session1 | ||
s.state.sessions["session-2"] = &agentSessionState{} | ||
s.state.sessions["session-3"] = session3 | ||
session1.consumptionMetrics = &SessionConsumptionMetrics{ | ||
ConnectDuration: 42, | ||
LastUpdate: now.Add(-time.Minute), | ||
} | ||
// staled metric | ||
session3.consumptionMetrics = &SessionConsumptionMetrics{ | ||
ConnectDuration: 36, | ||
LastUpdate: session3.lastMarked, | ||
} | ||
|
||
// when | ||
s.state.RefreshSessionConsumptionMetrics("session-1") | ||
s.state.RefreshSessionConsumptionMetrics("session-2") // should not fail. | ||
s.state.RefreshSessionConsumptionMetrics("session-3") // should not refresh a stale metric. | ||
|
||
// then | ||
ccs1 := s.state.sessions["session-1"].(*clientSessionState) | ||
ccs3 := s.state.sessions["session-3"].(*clientSessionState) | ||
|
||
assert.Len(s.T(), s.state.GetAllSessionConsumptionMetrics(), 2) | ||
assert.True(s.T(), (ccs1.ConsumptionMetrics().ConnectDuration) > 42) | ||
assert.Equal(s.T(), 36, int(ccs3.ConsumptionMetrics().ConnectDuration)) | ||
} |
This file contains 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 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.