From f785c33b5049690e1821f8f156cf7fe5ecfe0437 Mon Sep 17 00:00:00 2001 From: suxb201 Date: Tue, 3 Dec 2024 16:51:20 +0800 Subject: [PATCH] feat: add filter rules for keys --- docs/src/en/filter/filter.md | 12 +++++++----- docs/src/zh/filter/filter.md | 12 +++++++----- internal/config/config.go | 2 ++ internal/filter/filter.go | 28 +++++++++++++++++++--------- shake.toml | 6 +++++- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/docs/src/en/filter/filter.md b/docs/src/en/filter/filter.md index 532f720b..b73949c3 100644 --- a/docs/src/en/filter/filter.md +++ b/docs/src/en/filter/filter.md @@ -5,12 +5,14 @@ outline: deep RedisShake provides various built-in filter rules that users can choose from according to their needs. ## Filtering Keys -RedisShake supports filtering by key name prefixes and suffixes. You can set the following options in the configuration file, for example: +RedisShake supports filtering by key name, key name prefixes, and suffixes. You can set the following options in the configuration file, for example: ```toml -allow_key_prefix = ["user:", "product:"] -allow_key_suffix = [":active", ":valid"] -block_key_prefix = ["temp:", "cache:"] -block_key_suffix = [":tmp", ":old"] +allow_keys = ["user:1001", "product:2001"] # allowed key names +allow_key_prefix = ["user:", "product:"] # allowed key name prefixes +allow_key_suffix = [":active", ":valid"] # allowed key name suffixes +block_keys = ["temp:1001", "cache:2001"] # blocked key names +block_key_prefix = ["temp:", "cache:"] # blocked key name prefixes +block_key_suffix = [":tmp", ":old"] # blocked key name suffixes ``` If these options are not set, all keys are allowed by default. diff --git a/docs/src/zh/filter/filter.md b/docs/src/zh/filter/filter.md index 454d09a8..41f256b9 100644 --- a/docs/src/zh/filter/filter.md +++ b/docs/src/zh/filter/filter.md @@ -5,12 +5,14 @@ outline: deep RedisShake 提供了多种内置的过滤规则,用户可以根据需要选择合适的规则。 ## 过滤 Key -RedisShake 支持通过键名前缀和后缀进行过滤。您可以在配置文件中设置以下选项,例如: +RedisShake 支持通过键名、键名前缀和后缀进行过滤。您可以在配置文件中设置以下选项,例如: ```toml -allow_key_prefix = ["user:", "product:"] -allow_key_suffix = [":active", ":valid"] -block_key_prefix = ["temp:", "cache:"] -block_key_suffix = [":tmp", ":old"] +allow_keys = ["user:1001", "product:2001"] # 允许的键名 +allow_key_prefix = ["user:", "product:"] # 允许的键名前缀 +allow_key_suffix = [":active", ":valid"] # 允许的键名后缀 +block_keys = ["temp:1001", "cache:2001"] # 阻止的键名 +block_key_prefix = ["temp:", "cache:"] # 阻止的键名前缀 +block_key_suffix = [":tmp", ":old"] # 阻止的键名后缀 ``` 如果不设置这些选项,默认允许所有键。 diff --git a/internal/config/config.go b/internal/config/config.go index e7492c2e..523cfe2c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -15,8 +15,10 @@ import ( ) type FilterOptions struct { + AllowKeys []string `mapstructure:"allow_keys" default:"[]"` AllowKeyPrefix []string `mapstructure:"allow_key_prefix" default:"[]"` AllowKeySuffix []string `mapstructure:"allow_key_suffix" default:"[]"` + BlockKeys []string `mapstructure:"block_keys" default:"[]"` BlockKeyPrefix []string `mapstructure:"block_key_prefix" default:"[]"` BlockKeySuffix []string `mapstructure:"block_key_suffix" default:"[]"` AllowKeyRegex []string `mapstructure:"allow_key_regex" default:"[]"` diff --git a/internal/filter/filter.go b/internal/filter/filter.go index 5a097481..cfca459a 100644 --- a/internal/filter/filter.go +++ b/internal/filter/filter.go @@ -3,7 +3,7 @@ package filter import ( "RedisShake/internal/config" "RedisShake/internal/entry" - "log" + "RedisShake/internal/log" "slices" "strings" "sync" @@ -47,9 +47,9 @@ func Filter(e *entry.Entry) bool { return false } else { // If we reach here, it means some keys are true and some are false - log.Printf("Error: Inconsistent filter results for entry with %d keys", len(e.Keys)) - log.Printf("Passed keys: %v", passedKeys) - log.Printf("Filtered keys: %v", filteredKeys) + log.Infof("Error: Inconsistent filter results for entry with %d keys", len(e.Keys)) + log.Infof("Passed keys: %v", passedKeys) + log.Infof("Filtered keys: %v", filteredKeys) return false } @@ -97,10 +97,15 @@ func Filter(e *entry.Entry) bool { // blockKeyFilter is block key? default false func blockKeyFilter(key string) bool { - if len(config.Opt.Filter.BlockKeyRegex) == 0 && len(config.Opt.Filter.BlockKeyPrefix) == 0 && - len(config.Opt.Filter.BlockKeySuffix) == 0 { + if len(config.Opt.Filter.BlockKeyRegex) == 0 && + len(config.Opt.Filter.BlockKeyPrefix) == 0 && + len(config.Opt.Filter.BlockKeySuffix) == 0 && + len(config.Opt.Filter.BlockKeys) == 0 { return false } + if slices.Contains(config.Opt.Filter.BlockKeys, key) { + return true + } if blockKeyMatch(config.Opt.Filter.BlockKeyRegex, key) { return true } @@ -120,9 +125,14 @@ func blockKeyFilter(key string) bool { // allowKeyFilter is allow key? default true func allowKeyFilter(key string) bool { - // if all allow filter is empty. default is true - if len(config.Opt.Filter.AllowKeyRegex) == 0 && len(config.Opt.Filter.AllowKeyPrefix) == 0 && - len(config.Opt.Filter.AllowKeySuffix) == 0 { + if len(config.Opt.Filter.AllowKeyRegex) == 0 && + len(config.Opt.Filter.AllowKeyPrefix) == 0 && + len(config.Opt.Filter.AllowKeySuffix) == 0 && + len(config.Opt.Filter.AllowKeys) == 0 { + return true + } + + if slices.Contains(config.Opt.Filter.AllowKeys, key) { return true } // If the RE matches, there is no need to iterate over the others diff --git a/shake.toml b/shake.toml index 0f6d76d0..180495d3 100644 --- a/shake.toml +++ b/shake.toml @@ -43,22 +43,26 @@ buff_send = false # buffer send, default false. may be a sync delay whe [filter] # Allow keys with specific prefixes or suffixes # Examples: +# allow_keys = ["user:1001", "product:2001"] # allow_key_prefix = ["user:", "product:"] # allow_key_suffix = [":active", ":valid"] # allow A collection of keys containing 11-digit mobile phone numbers # allow_key_regex = [":\\d{11}:"] # Leave empty to allow all keys +allow_keys = [] allow_key_prefix = [] allow_key_suffix = [] allow_key_regex = [] # Block keys with specific prefixes or suffixes # Examples: +# block_keys = ["temp:1001", "cache:2001"] # block_key_prefix = ["temp:", "cache:"] # block_key_suffix = [":tmp", ":old"] # block test 11-digit mobile phone numbers keys # block_key_regex = [":test:\\d{11}:"] # Leave empty to block nothing +block_keys = [] block_key_prefix = [] block_key_suffix = [] block_key_regex = [] @@ -90,7 +94,7 @@ block_command_group = [] # Function for custom data processing # For best practices and examples, visit: -# https://tair-opensource.github.io/RedisShake/zh/function/best_practices.html +# https://tair-opensource.github.io/RedisShake/zh/filter/function.html function = "" [advanced]