Repository: alibaba/sentinel-golang
Pull request #454 (2022-01-21 09:14:59)

feat: add timeout counter metric
<!--  Thanks for submitting a pull request! Here are some tips for you:
1. Please make sure you have read and understood the contributing guidelines: https://github.com/alibaba/blob/master/CONTRIBUTING.md
2. Please make sure the PR has a corresponding issue.
-->

### Describe what this PR does / why we need it
Add a new independent metric for timeout, it records how many requests was failed due to timeout, this data is useful to improve the precision of concurrency amount statistics when a large number of requests timeout

### Does this pull request fix one issue?

<!--If that, add "Fixes #xxxx" below in the next line. For example, Fixes #15. Otherwise, add "NONE" -->

### Describe how you did it


### Describe how to verify it


### Special notes for reviews

codecov-commenter: # [Codecov](https://codecov.io/gh/alibaba/sentinel-golang/pull/454?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba) Report
> :exclamation: No coverage uploaded for pull request base (`mosn-customized@ddef8a7`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba#section-missing-base-commit).
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/alibaba/sentinel-golang/pull/454/graphs/tree.svg?width=650&height=150&src=pr&token=SrpzbhKA3E&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba)](https://codecov.io/gh/alibaba/sentinel-golang/pull/454?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba)

```diff
@@                Coverage Diff                 @@
##             mosn-customized     #454   +/-   ##
==================================================
  Coverage                   ?   52.21%           
==================================================
  Files                      ?       74           
  Lines                      ?     4857           
  Branches                   ?        0           
==================================================
  Hits                       ?     2536           
  Misses                     ?     2038           
  Partials                   ?      283           
```



------

[Continue to review full report at Codecov](https://codecov.io/gh/alibaba/sentinel-golang/pull/454?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/alibaba/sentinel-golang/pull/454?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba). Last update [ddef8a7...52b5b32](https://codecov.io/gh/alibaba/sentinel-golang/pull/454?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba).


Patch:
diff --git a/core/base/metric_item.go b/core/base/metric_item.go
index da471c8..f23180b 100644
--- a/core/base/metric_item.go
+++ b/core/base/metric_item.go
@@ -37,6 +37,7 @@ type MetricItem struct {
 	ErrorQps             uint64
 	AvgRt                uint64
 	OccupiedPassQps      uint64
+	TimeoutCounter       uint64
 	Concurrency          uint32
 	SecondMaxConcurrency uint32
 }
diff --git a/core/base/stat.go b/core/base/stat.go
index 5559ab5..7278bd1 100644
--- a/core/base/stat.go
+++ b/core/base/stat.go
@@ -37,6 +37,8 @@ const (
 	MetricEventRt
 	// Specific for verifying mode
 	MetricEventMonitorBlock
+	// timeout counter
+	MetricEventTimeout
 	// hack for the number of event
 	MetricEventTotal
 )
diff --git a/core/stat/base/sliding_window_metric.go b/core/stat/base/sliding_window_metric.go
index 4d779ac..a2b26e3 100644
--- a/core/stat/base/sliding_window_metric.go
+++ b/core/stat/base/sliding_window_metric.go
@@ -243,6 +243,7 @@ func (m *SlidingWindowMetric) metricItemFromBucket(w *BucketWrap) *base.MetricIt
 		BlockQps:        uint64(mb.Get(base.MetricEventBlock)),
 		MonitorBlockQps: uint64(mb.Get(base.MetricEventMonitorBlock)),
 		ErrorQps:        uint64(mb.Get(base.MetricEventError)),
+		TimeoutCounter:  uint64(mb.Get(base.MetricEventTimeout)),
 		CompleteQps:     uint64(completeQps),
 		Timestamp:       w.BucketStart,
 	}


Test patch:
diff --git a/core/stat/base/metric_bucket_test.go b/core/stat/base/metric_bucket_test.go
index 263a20d..e1b55c8 100644
--- a/core/stat/base/metric_bucket_test.go
+++ b/core/stat/base/metric_bucket_test.go
@@ -27,7 +27,7 @@ func Test_metricBucket_MemSize(t *testing.T) {
 	mb := NewMetricBucket()
 	t.Log("mb:", mb)
 	size := unsafe.Sizeof(*mb)
-	if size != 64 {
+	if size != 72 {
 		t.Error("unexpect memory size of MetricBucket")
 	}
 }