-
Notifications
You must be signed in to change notification settings - Fork 930
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sliding window and rt quantile metrics (#2356)
- Loading branch information
Showing
10 changed files
with
360 additions
and
1 deletion.
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
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
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,42 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package aggregate | ||
|
||
// pane represents a window over a period of time. | ||
// It uses interface{} to store any type of value. | ||
type pane struct { | ||
startInMs int64 | ||
endInMs int64 | ||
intervalInMs int64 | ||
value interface{} | ||
} | ||
|
||
func newPane(intervalInMs, startInMs int64, value interface{}) *pane { | ||
return &pane{ | ||
startInMs: startInMs, | ||
endInMs: startInMs + intervalInMs, | ||
intervalInMs: intervalInMs, | ||
value: value, | ||
} | ||
} | ||
|
||
func (p *pane) resetTo(startInMs int64, value interface{}) { | ||
p.startInMs = startInMs | ||
p.endInMs = startInMs + p.intervalInMs | ||
p.value = value | ||
} |
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,85 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package aggregate | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
import ( | ||
"github.com/influxdata/tdigest" | ||
) | ||
|
||
// TimeWindowQuantile wrappers sliding window around T-Digest. | ||
// | ||
// It uses T-Digest algorithm to calculate quantile. | ||
// The window is divided into several panes, and each pane's value is a TDigest instance. | ||
type TimeWindowQuantile struct { | ||
compression float64 | ||
window *slidingWindow | ||
mux sync.RWMutex | ||
} | ||
|
||
func NewTimeWindowQuantile(compression float64, paneCount int, timeWindowSeconds int64) *TimeWindowQuantile { | ||
return &TimeWindowQuantile{ | ||
compression: compression, | ||
window: newSlidingWindow(paneCount, timeWindowSeconds*1000), | ||
} | ||
} | ||
|
||
// Quantile returns a quantile of the sliding window by merging all panes. | ||
func (t *TimeWindowQuantile) Quantile(q float64) float64 { | ||
return t.mergeTDigests().Quantile(q) | ||
} | ||
|
||
// Quantiles returns quantiles of the sliding window by merging all panes. | ||
func (t *TimeWindowQuantile) Quantiles(qs []float64) []float64 { | ||
td := t.mergeTDigests() | ||
|
||
res := make([]float64, len(qs)) | ||
for i, q := range qs { | ||
res[i] = td.Quantile(q) | ||
} | ||
|
||
return res | ||
} | ||
|
||
// mergeTDigests merges all panes' TDigests into one TDigest. | ||
func (t *TimeWindowQuantile) mergeTDigests() *tdigest.TDigest { | ||
t.mux.RLock() | ||
defer t.mux.RUnlock() | ||
|
||
td := tdigest.NewWithCompression(t.compression) | ||
for _, v := range t.window.values(time.Now().UnixMilli()) { | ||
td.AddCentroidList(v.(*tdigest.TDigest).Centroids()) | ||
} | ||
return td | ||
} | ||
|
||
// Add adds a value to the sliding window's current pane. | ||
func (t *TimeWindowQuantile) Add(value float64) { | ||
t.mux.Lock() | ||
defer t.mux.Unlock() | ||
|
||
t.window.currentPane(time.Now().UnixMilli(), t.newEmptyValue).value.(*tdigest.TDigest).Add(value, 1) | ||
} | ||
|
||
func (t *TimeWindowQuantile) newEmptyValue() interface{} { | ||
return tdigest.NewWithCompression(t.compression) | ||
} |
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,60 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package aggregate | ||
|
||
import "testing" | ||
|
||
func TestAddAndQuantile(t1 *testing.T) { | ||
timeWindowQuantile := NewTimeWindowQuantile(100, 10, 1) | ||
for i := 1; i <= 100; i++ { | ||
timeWindowQuantile.Add(float64(i)) | ||
} | ||
|
||
type args struct { | ||
q float64 | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want float64 | ||
}{ | ||
{ | ||
name: "Quantile: 0.01", | ||
args: args{ | ||
q: 0.01, | ||
}, | ||
want: 1.5, | ||
}, | ||
{ | ||
name: "Quantile: 0.99", | ||
args: args{ | ||
q: 0.99, | ||
}, | ||
want: 99.5, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t1.Run(tt.name, func(t1 *testing.T) { | ||
t := timeWindowQuantile | ||
if got := t.Quantile(tt.args.q); got != tt.want { | ||
t1.Errorf("Quantile() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.