Skip to content

Commit

Permalink
update metrics handling and simplify stats tracking
Browse files Browse the repository at this point in the history
this change makes statistical metrics
per worker without sync and keeps
user-defined metrics shared with sync
  • Loading branch information
umputun committed Feb 8, 2025
1 parent b546864 commit ceb2e75
Show file tree
Hide file tree
Showing 6 changed files with 517 additions and 443 deletions.
19 changes: 4 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,26 +336,16 @@ p, _ := pool.NewStateful[string](2, maker)
```go
// create worker with metrics tracking
worker := pool.WorkerFunc[string](func(ctx context.Context, v string) error {
// track custom metrics only, pool handles standard metrics automatically
m := metrics.Get(ctx)

// track operation timing
operationEnd := m.StartTimer("operation")
defer operationEnd()

// simulate work
time.Sleep(time.Millisecond * 100)

// track custom metrics
if strings.HasPrefix(v, "important") {
m.Inc("important-tasks")
}

// track success/failure
// process the value
if err := process(v); err != nil {
m.Inc(metrics.CountErrors)
return err
}
m.Inc(metrics.CountProcessed)
return nil
})

Expand All @@ -369,17 +359,16 @@ p.Submit("important-task2")
p.Close(context.Background())

// get structured metrics
stats := p.Metrics().Stats()
stats := p.Metrics().GetStats()
fmt.Printf("Processed: %d\n", stats.Processed)
fmt.Printf("Errors: %d\n", stats.Errors)
fmt.Printf("Processing time: %v\n", stats.ProcessingTime)
fmt.Printf("Wait time: %v\n", stats.WaitTime)
fmt.Printf("Total time: %v\n", stats.TotalTime)

// get raw metrics
// get user-defined metrics
m := p.Metrics()
fmt.Printf("Important tasks: %d\n", m.Get("important-tasks"))
fmt.Printf("Operation time: %v\n", m.GetDuration("operation"))
```

## Complete Example: Processing Pipeline
Expand Down
13 changes: 11 additions & 2 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,16 @@
//
// # Metrics
//
// The pool automatically collects various metrics including:
// The pool automatically tracks standard stats metrics (processed counts, errors, timings).
// Workers can also record additional custom metrics:
//
// m := metrics.Get(ctx)
// m.Inc("custom-counter")
//
// Access metrics:
// value := m.Get("custom-counter")
//
// Collected statistical metrics including:
//
// - Number of processed items
// - Number of errors
Expand All @@ -93,7 +102,7 @@
// - Initialization time
// - Total time
//
// Access metrics:
// Access stats:
//
// stats := pool.Metrics().Stats()
// fmt.Printf("processed: %d, errors: %d", stats.Processed, stats.Errors)
Expand Down
Loading

0 comments on commit ceb2e75

Please sign in to comment.