Skip to content

Commit

Permalink
feat(redis): 裁剪部分twemproxy上报日志 #8198
Browse files Browse the repository at this point in the history
  • Loading branch information
xiepaup authored and iSecloud committed Nov 27, 2024
1 parent 44b9724 commit 7e270f3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dbm-services/redis/db-tools/dbmon/pkg/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package consts

// version
const (
BkDbmonVersion = "v0.15"
BkDbmonVersion = "v0.16"
)

const (
Expand Down
23 changes: 23 additions & 0 deletions dbm-services/redis/db-tools/dbmon/pkg/redistaillog/tail_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
Expand All @@ -21,6 +22,20 @@ import (
"dbm-services/redis/db-tools/dbmon/util"
)

const (
MaxAllowedLogCountPerDuration = 100
)

var (
twemproxyLogStart *regexp.Regexp
logLimiter *util.FixedWindowLimiter
)

func init() {
twemproxyLogStart = regexp.MustCompile(`^\[\d{4}-\d{2}-\d{2}`)
logLimiter = util.NewFixedWindowLimiter(MaxAllowedLogCountPerDuration, time.Minute)
}

// BaseSchema schema
type BaseSchema struct {
BkBizID string `json:"bk_biz_id"`
Expand Down Expand Up @@ -257,6 +272,14 @@ func (task *TailTask) BackgroundTailLog() {
if !task.filterLogLine(line.Text) {
continue
}
// twemproxy resp 格式 多行,,,[2024-11-26 16:45:25.582]
if task.Role == consts.MetaRoleTwemproxy && !twemproxyLogStart.MatchString(line.Text) {
continue
}
// 限流,每分钟 最多允许上报N条日志
if !logLimiter.TryAcquire() {
continue
}
recordItem.CreateTime = line.Time.Local().Format(time.RFC3339)
recordItem.Data = line.Text
tmpBytes, _ := json.Marshal(recordItem)
Expand Down
42 changes: 42 additions & 0 deletions dbm-services/redis/db-tools/dbmon/util/limiter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package util

import (
"sync"
"time"
)

// FixedWindowLimiter 固定窗口限流器
type FixedWindowLimiter struct {
limit int // 窗口请求上限
window time.Duration // 窗口时间大小
counter int // 计数器
lastTime time.Time // 上一次请求的时间
mutex sync.Mutex // 避免并发问题
}

func NewFixedWindowLimiter(limit int, window time.Duration) *FixedWindowLimiter {
return &FixedWindowLimiter{
limit: limit,
window: window,
lastTime: time.Now(),
}
}

func (l *FixedWindowLimiter) TryAcquire() bool {
l.mutex.Lock()
defer l.mutex.Unlock()
// 获取当前时间
now := time.Now()
// 如果当前窗口失效,计数器清0,开启新的窗口
if now.Sub(l.lastTime) > l.window {
l.counter = 0
l.lastTime = now
}
// 若到达窗口请求上限,请求失败
if l.counter >= l.limit {
return false
}
// 若没到窗口请求上限,计数器+1,请求成功
l.counter++
return true
}

0 comments on commit 7e270f3

Please sign in to comment.