Skip to content
GitLab
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Primary navigation
Search or go to…
Project
W
waf
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Privacy statement
Keyboard shortcuts
?
What's new
3
Snippets
Groups
Projects
Show more breadcrumbs
tozd
waf
Compare revisions
v0.18.0 to v0.18.1
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
tozd/waf
Select target project
No results found
v0.18.1
Select Git revision
Swap
Target
tozd/waf
Select target project
tozd/waf
1 result
v0.18.0
Select Git revision
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (2)
Easier use of nil metrics.
· 663f6022
Mitar
authored
1 year ago
663f6022
Release v0.18.1.
· c58aafcf
Mitar
authored
1 year ago
c58aafcf
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
CHANGELOG.md
+9
-1
9 additions, 1 deletion
CHANGELOG.md
metrics.go
+74
-16
74 additions, 16 deletions
metrics.go
metrics_test.go
+35
-0
35 additions, 0 deletions
metrics_test.go
middleware_test.go
+1
-0
1 addition, 0 deletions
middleware_test.go
with
119 additions
and
17 deletions
CHANGELOG.md
View file @
c58aafcf
...
...
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
## [0.18.1] - 2024-04-08
### Fixed
-
Metrics API works when metrics are nil (e.g., not available in the context).
All calls are noop.
## [0.18.0] - 2024-04-05
### Added
...
...
@@ -197,7 +204,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
-
First public release.
[
unreleased
]:
https://gitlab.com/tozd/waf/-/compare/v0.18.0...main
[
unreleased
]:
https://gitlab.com/tozd/waf/-/compare/v0.18.1...main
[
0.18.1
]:
https://gitlab.com/tozd/waf/-/compare/v0.18.0...v0.18.1
[
0.18.0
]:
https://gitlab.com/tozd/waf/-/compare/v0.17.1...v0.18.0
[
0.17.1
]:
https://gitlab.com/tozd/waf/-/compare/v0.17.0...v0.17.1
[
0.17.0
]:
https://gitlab.com/tozd/waf/-/compare/v0.16.0...v0.17.0
...
...
This diff is collapsed.
Click to expand it.
metrics.go
View file @
c58aafcf
...
...
@@ -30,7 +30,7 @@ type DurationMeasurement struct {
// Start records the start of the duration.
func
(
d
*
DurationMeasurement
)
Start
()
*
DurationMeasurement
{
if
d
.
Discarded
{
if
d
==
nil
||
d
.
Discarded
{
return
d
}
if
!
d
.
startTime
.
IsZero
()
{
...
...
@@ -42,7 +42,7 @@ func (d *DurationMeasurement) Start() *DurationMeasurement {
// Stop computes the duration.
func
(
d
*
DurationMeasurement
)
Stop
()
*
DurationMeasurement
{
if
d
.
Discarded
{
if
d
==
nil
||
d
.
Discarded
{
return
d
}
if
d
.
startTime
.
IsZero
()
{
...
...
@@ -59,6 +59,9 @@ func (d *DurationMeasurement) Stop() *DurationMeasurement {
//
// Any future calls to measurement methods are ignored.
func
(
d
*
DurationMeasurement
)
Discard
()
*
DurationMeasurement
{
if
d
==
nil
{
return
d
}
d
.
Discarded
=
true
return
d
}
...
...
@@ -73,13 +76,16 @@ type DurationMetric struct {
}
func
(
d
*
DurationMetric
)
Name
()
string
{
if
d
==
nil
{
return
""
}
return
d
.
name
}
func
(
d
*
DurationMetric
)
MarshalZerologObject
(
e
*
zerolog
.
Event
)
{
// We use only really measured durations and not just started
// (it is impossible to both start and end the measurement with 0 duration).
if
d
.
Discarded
||
d
.
Duration
==
0
{
if
d
==
nil
||
d
.
Discarded
||
d
.
Duration
==
0
{
return
}
...
...
@@ -89,7 +95,7 @@ func (d *DurationMetric) MarshalZerologObject(e *zerolog.Event) {
func
(
d
*
DurationMetric
)
ServerTimingString
()
string
{
// We use only really measured durations and not just started
// (it is impossible to both start and end the measurement with 0 duration).
if
d
.
Discarded
||
d
.
Duration
==
0
{
if
d
==
nil
||
d
.
Discarded
||
d
.
Duration
==
0
{
return
""
}
...
...
@@ -101,7 +107,7 @@ func (d *DurationMetric) ServerTimingString() string {
//
// Can be called only once per metric.
func
(
d
*
DurationMetric
)
Start
()
*
DurationMetric
{
if
d
.
Discarded
{
if
d
==
nil
||
d
.
Discarded
{
return
d
}
if
!
d
.
startTime
.
IsZero
()
{
...
...
@@ -113,7 +119,7 @@ func (d *DurationMetric) Start() *DurationMetric {
// Stop computes the duration.
func
(
d
*
DurationMetric
)
Stop
()
*
DurationMetric
{
if
d
.
Discarded
{
if
d
==
nil
||
d
.
Discarded
{
return
d
}
if
d
.
startTime
.
IsZero
()
{
...
...
@@ -130,6 +136,9 @@ func (d *DurationMetric) Stop() *DurationMetric {
//
// Any future calls to metric methods are ignored.
func
(
d
*
DurationMetric
)
Discard
()
*
DurationMetric
{
if
d
==
nil
{
return
d
}
d
.
Discarded
=
true
return
d
}
...
...
@@ -143,11 +152,14 @@ type CounterMetric struct {
}
func
(
c
*
CounterMetric
)
Name
()
string
{
if
c
==
nil
{
return
""
}
return
c
.
name
}
func
(
c
*
CounterMetric
)
MarshalZerologObject
(
e
*
zerolog
.
Event
)
{
if
c
.
Discarded
{
if
c
==
nil
||
c
.
Discarded
{
return
}
...
...
@@ -162,7 +174,7 @@ func (c *CounterMetric) ServerTimingString() string {
// Inc increases the counter by one.
func
(
c
*
CounterMetric
)
Inc
()
*
CounterMetric
{
if
c
.
Discarded
{
if
c
==
nil
||
c
.
Discarded
{
return
c
}
c
.
Count
++
...
...
@@ -171,7 +183,7 @@ func (c *CounterMetric) Inc() *CounterMetric {
// Add increases the counter by n.
func
(
c
*
CounterMetric
)
Add
(
n
int64
)
*
CounterMetric
{
if
c
.
Discarded
{
if
c
==
nil
||
c
.
Discarded
{
return
c
}
c
.
Count
+=
n
...
...
@@ -182,6 +194,9 @@ func (c *CounterMetric) Add(n int64) *CounterMetric {
//
// Any future calls to metric methods are ignored.
func
(
c
*
CounterMetric
)
Discard
()
*
CounterMetric
{
if
c
==
nil
{
return
c
}
c
.
Discarded
=
true
return
c
}
...
...
@@ -198,11 +213,14 @@ type DurationsMetric struct {
}
func
(
d
*
DurationsMetric
)
Name
()
string
{
if
d
==
nil
{
return
""
}
return
d
.
name
}
func
(
d
*
DurationsMetric
)
MarshalZerologObject
(
e
*
zerolog
.
Event
)
{
if
d
.
Discarded
{
if
d
==
nil
||
d
.
Discarded
{
return
}
...
...
@@ -252,6 +270,9 @@ func (d *DurationsMetric) ServerTimingString() string {
//
// Can be called multiple times per metric with each call returning a new measurement.
func
(
d
*
DurationsMetric
)
Start
()
*
DurationMeasurement
{
if
d
==
nil
{
return
nil
}
m
:=
new
(
DurationMeasurement
)
if
d
.
Discarded
{
m
.
Discarded
=
true
...
...
@@ -268,6 +289,9 @@ func (d *DurationsMetric) Start() *DurationMeasurement {
//
// Any future calls to Start return an already discarded duration measurement.
func
(
d
*
DurationsMetric
)
Discard
()
*
DurationsMetric
{
if
d
==
nil
{
return
d
}
d
.
Discarded
=
true
return
d
}
...
...
@@ -283,13 +307,16 @@ type DurationCounterMetric struct {
}
func
(
d
*
DurationCounterMetric
)
Name
()
string
{
if
d
==
nil
{
return
""
}
return
d
.
name
}
func
(
d
*
DurationCounterMetric
)
MarshalZerologObject
(
e
*
zerolog
.
Event
)
{
// We use only really measured durations and not just started
// (it is impossible to both start and end the measurement with 0 duration).
if
d
.
Discarded
||
d
.
Duration
==
0
{
if
d
==
nil
||
d
.
Discarded
||
d
.
Duration
==
0
{
return
}
...
...
@@ -307,7 +334,7 @@ func (d *DurationCounterMetric) MarshalZerologObject(e *zerolog.Event) {
func
(
d
*
DurationCounterMetric
)
ServerTimingString
()
string
{
// We use only really measured durations and not just started
// (it is impossible to both start and end the measurement with 0 duration).
if
d
.
Discarded
||
d
.
Duration
==
0
{
if
d
==
nil
||
d
.
Discarded
||
d
.
Duration
==
0
{
return
""
}
...
...
@@ -319,7 +346,7 @@ func (d *DurationCounterMetric) ServerTimingString() string {
//
// Can be called only once per metric.
func
(
d
*
DurationCounterMetric
)
Start
()
*
DurationCounterMetric
{
if
d
.
Discarded
{
if
d
==
nil
||
d
.
Discarded
{
return
d
}
if
!
d
.
startTime
.
IsZero
()
{
...
...
@@ -331,7 +358,7 @@ func (d *DurationCounterMetric) Start() *DurationCounterMetric {
// Stop computes the duration.
func
(
d
*
DurationCounterMetric
)
Stop
()
*
DurationCounterMetric
{
if
d
.
Discarded
{
if
d
==
nil
||
d
.
Discarded
{
return
d
}
if
d
.
startTime
.
IsZero
()
{
...
...
@@ -348,7 +375,7 @@ func (d *DurationCounterMetric) Stop() *DurationCounterMetric {
//
// Only possible after calling Start and before calling Stop.
func
(
d
*
DurationCounterMetric
)
Inc
()
*
DurationCounterMetric
{
if
d
.
Discarded
{
if
d
==
nil
||
d
.
Discarded
{
return
d
}
if
d
.
startTime
.
IsZero
()
{
...
...
@@ -365,7 +392,7 @@ func (d *DurationCounterMetric) Inc() *DurationCounterMetric {
//
// Only possible after calling Start and before calling Stop.
func
(
d
*
DurationCounterMetric
)
Add
(
n
int64
)
*
DurationCounterMetric
{
if
d
.
Discarded
{
if
d
==
nil
||
d
.
Discarded
{
return
d
}
if
d
.
startTime
.
IsZero
()
{
...
...
@@ -382,6 +409,9 @@ func (d *DurationCounterMetric) Add(n int64) *DurationCounterMetric {
//
// Any future calls to metric methods are ignored.
func
(
d
*
DurationCounterMetric
)
Discard
()
*
DurationCounterMetric
{
if
d
==
nil
{
return
d
}
d
.
Discarded
=
true
return
d
}
...
...
@@ -418,6 +448,10 @@ func NewMetrics() *Metrics {
// nothing is added if the existing metric is equal
// to the metric being added. Otherwise Add panics.
func
(
m
*
Metrics
)
Add
(
mt
metric
)
{
if
m
==
nil
{
return
}
m
.
mu
.
Lock
()
defer
m
.
mu
.
Unlock
()
...
...
@@ -438,6 +472,10 @@ func (m *Metrics) Add(mt metric) {
// If called with the name of an existing duration metric,
// that duration metric is returned instead.
func
(
m
*
Metrics
)
Duration
(
name
string
)
*
DurationMetric
{
if
m
==
nil
{
return
nil
}
m
.
mu
.
Lock
()
defer
m
.
mu
.
Unlock
()
...
...
@@ -460,6 +498,10 @@ func (m *Metrics) Duration(name string) *DurationMetric {
// If called with the name of an existing counter metric,
// that counter metric is returned instead.
func
(
m
*
Metrics
)
Counter
(
name
string
)
*
CounterMetric
{
if
m
==
nil
{
return
nil
}
m
.
mu
.
Lock
()
defer
m
.
mu
.
Unlock
()
...
...
@@ -482,6 +524,10 @@ func (m *Metrics) Counter(name string) *CounterMetric {
// If called with the name of an existing durations metric,
// that durations metric is returned instead.
func
(
m
*
Metrics
)
Durations
(
name
string
)
*
DurationsMetric
{
if
m
==
nil
{
return
nil
}
m
.
mu
.
Lock
()
defer
m
.
mu
.
Unlock
()
...
...
@@ -504,6 +550,10 @@ func (m *Metrics) Durations(name string) *DurationsMetric {
// If called with the name of an existing duration counter metric,
// that duration counter metric is returned instead.
func
(
m
*
Metrics
)
DurationCounter
(
name
string
)
*
DurationCounterMetric
{
if
m
==
nil
{
return
nil
}
m
.
mu
.
Lock
()
defer
m
.
mu
.
Unlock
()
...
...
@@ -522,6 +572,10 @@ func (m *Metrics) DurationCounter(name string) *DurationCounterMetric {
}
func
(
m
*
Metrics
)
MarshalZerologObject
(
e
*
zerolog
.
Event
)
{
if
m
==
nil
{
return
}
m
.
mu
.
Lock
()
defer
m
.
mu
.
Unlock
()
...
...
@@ -542,6 +596,10 @@ func (m *Metrics) MarshalZerologObject(e *zerolog.Event) {
}
func
(
m
*
Metrics
)
ServerTimingString
()
string
{
if
m
==
nil
{
return
""
}
m
.
mu
.
Lock
()
defer
m
.
mu
.
Unlock
()
...
...
This diff is collapsed.
Click to expand it.
metrics_test.go
0 → 100644
View file @
c58aafcf
package
waf_test
import
(
"testing"
"gitlab.com/tozd/waf"
)
func
TestMetrics
(
t
*
testing
.
T
)
{
t
.
Parallel
()
metrics
:=
(
*
waf
.
Metrics
)(
nil
)
// There is a similar set of calls in TestMetricsMiddleware.
metrics
.
Counter
(
"counter"
)
.
Add
(
40
)
metrics
.
Counter
(
"discardedCounter"
)
.
Inc
()
.
Discard
()
metrics
.
Duration
(
"duration"
)
.
Start
()
.
Stop
()
metrics
.
Duration
(
"foreverDuration"
)
.
Start
()
metrics
.
Duration
(
"discardedDuration1"
)
.
Start
()
.
Discard
()
.
Stop
()
metrics
.
Duration
(
"discardedDuration2"
)
.
Start
()
.
Stop
()
.
Discard
()
ds
:=
metrics
.
Durations
(
"durations"
)
ds
.
Start
()
.
Stop
()
ds
.
Start
()
.
Stop
()
ds
=
metrics
.
Durations
(
"discardedDurations"
)
ds
.
Start
()
.
Discard
()
.
Stop
()
ds
.
Start
()
.
Stop
()
.
Discard
()
dc
:=
metrics
.
DurationCounter
(
"dc"
)
.
Start
()
.
Add
(
43
)
metrics
.
DurationCounter
(
"discardedDc1"
)
.
Start
()
.
Add
(
32
)
.
Discard
()
.
Stop
()
metrics
.
DurationCounter
(
"discardedDc2"
)
.
Start
()
.
Add
(
33
)
.
Stop
()
.
Discard
()
metrics
.
DurationCounter
(
"foreverDc"
)
.
Start
()
.
Add
(
43
)
metrics
.
Counter
(
"counter"
)
.
Inc
()
metrics
.
DurationCounter
(
"dc"
)
.
Inc
()
metrics
.
Duration
(
"trailer"
)
.
Start
()
.
Stop
()
dc
.
Stop
()
}
This diff is collapsed.
Click to expand it.
middleware_test.go
View file @
c58aafcf
...
...
@@ -635,6 +635,7 @@ func TestMetricsMiddleware(t *testing.T) {
})(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
ctx
:=
r
.
Context
()
metrics
:=
MustGetMetrics
(
ctx
)
// There is a similar set of calls in TestMetrics.
metrics
.
Counter
(
"counter"
)
.
Add
(
40
)
metrics
.
Counter
(
"discardedCounter"
)
.
Inc
()
.
Discard
()
metrics
.
Duration
(
"duration"
)
.
Start
()
.
Stop
()
...
...
This diff is collapsed.
Click to expand it.