Skip to content

Commit

Permalink
add implementation example
Browse files Browse the repository at this point in the history
  • Loading branch information
ketsiambaku committed Aug 28, 2024
1 parent d7b4266 commit cc6c739
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
14 changes: 14 additions & 0 deletions debug/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,18 @@ type (
// ActivityInfo ...
// Deprecated: in development and very likely to change
ActivityInfo = internal.ActivityInfo

// Activities ...
// Deprecated: in development and very likely to change
Activities = internal.Activities
)

func (a Activities) GroupByActivityType() Activities {
// implement me... maybe here or in monorepo ??
return nil
}

func (a Activities) GroupByWorkflowID() Activities {
// implement me... maybe ??
return nil
}
47 changes: 47 additions & 0 deletions internal/common/debug/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package debug

import (
"fmt"
"sync"

"go.uber.org/atomic"
)
Expand All @@ -36,8 +37,54 @@ type (
stopperImpl struct {
pollerTracker *pollerTrackerImpl
}

// activityTrackerImpl implements the ActivityTracker interface
activityTrackerImpl struct {
sync.RWMutex
m map[ActivityInfo]int64
}

// activityStopperImpl implements the Stopper interface
activityStopperImpl struct {
sync.Once
info ActivityInfo
tracker *activityTrackerImpl
}
)

var _ ActivityTracker = &activityTrackerImpl{}
var _ Stopper = &activityStopperImpl{}

func (ati *activityTrackerImpl) Start(info ActivityInfo) Stopper {
ati.Lock()
ati.m[info]++
ati.Unlock()
return &activityStopperImpl{info: info, tracker: ati}
}

func (ati *activityTrackerImpl) Stats() Activities {
var activities Activities
ati.RLock()
defer ati.RUnlock()
for a, count := range ati.m {
if count > 0 {
activities = append(activities, struct {
Info ActivityInfo
Count int64
}{Info: a, Count: count})
}
}
return activities
}

func (asi *activityStopperImpl) Stop() {
asi.Do(func() {
asi.tracker.Lock()
defer asi.tracker.Unlock()
asi.tracker.m[asi.info]--
})
}

func (p *pollerTrackerImpl) Start() Stopper {
p.pollerCount.Inc()
return &stopperImpl{
Expand Down
8 changes: 7 additions & 1 deletion internal/common/debug/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ type (
// Start ...
Start(info ActivityInfo) Stopper
// Stats ...
Stats()
Stats() Activities
}

// Activities ...
Activities []struct {
Info ActivityInfo
Count int64
}
)
2 changes: 1 addition & 1 deletion internal/common/debug/workerstats_noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (r *stopperNoopImpl) Stop() {}
func NewNoopPollerTracker() PollerTracker { return &pollerTrackerNoopImpl{} }

func (at *activityTrackerNoopImpl) Start(info ActivityInfo) Stopper { return &stopperNoopImpl{} }
func (at *activityTrackerNoopImpl) Stats() {}
func (at *activityTrackerNoopImpl) Stats() Activities { return nil }

// NewNoopActivityTracker creates a new PollerTracker instance
func NewNoopActivityTracker() ActivityTracker { return &activityTrackerNoopImpl{} }
Expand Down

0 comments on commit cc6c739

Please sign in to comment.