Skip to content

Commit

Permalink
fix: limit the bytes of redis args (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfyiamcool authored Dec 1, 2023
1 parent 275ec01 commit 99c2c38
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 4 deletions.
5 changes: 3 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Release Notes.

#### Plugins
* Support setting a discard type of reporter.
* Add `redis.max_args_bytes` parameter for redis plugin.

#### Documentation

Expand Down Expand Up @@ -65,7 +66,7 @@ Release Notes.

#### Documentation
* Combine `Supported Libraries` and `Performance Test` into `Plugins` section.
* Add `Tracing, Metrics and Logging` document into `Plugins` section.
* Add `Tracing, Metrics and Logging` document into `Plugins` section.

#### Bug Fixes
* Fix throw panic when log the tracing context before agent core initialized.
Expand Down Expand Up @@ -98,4 +99,4 @@ Release Notes.

#### Issues and PR
- All issues are [here](https://github.com/apache/skywalking/milestone/176?closed=1)
- All and pull requests are [here](https://github.com/apache/skywalking-go/milestone/1?closed=1)
- All and pull requests are [here](https://github.com/apache/skywalking-go/milestone/1?closed=1)
1 change: 1 addition & 0 deletions docs/en/agent/plugin-configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
| http.server_collect_parameters | SW_AGENT_PLUGIN_CONFIG_HTTP_SERVER_COLLECT_PARAMETERS | false | Collect the parameters of the HTTP request on the server side. |
| mongo.collect_statement | SW_AGENT_PLUGIN_CONFIG_MONGO_COLLECT_STATEMENT | false | Collect the statement of the MongoDB request. |
| sql.collect_parameter | SW_AGENT_PLUGIN_CONFIG_SQL_COLLECT_PARAMETER | false | Collect the parameter of the SQL request. |
| redis.max_args_bytes | SW_AGENT_PLUGIN_CONFIG_REDIS_MAX_ARGS_BYTES | 1024 | Limit the bytes size of redis args request. |
| reporter.discard | SW_AGENT_REPORTER_DISCARD | false | Discard the reporter. |
23 changes: 23 additions & 0 deletions plugins/go-redisv9/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to 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. Apache Software Foundation (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 goredisv9

//skywalking:config redis
var config struct {
MaxArgsBytes int `config:"max_args_bytes"`
}
14 changes: 13 additions & 1 deletion plugins/go-redisv9/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (r *redisHook) ProcessHook(next redis.ProcessHook) redis.ProcessHook {
tracing.WithTag(tracing.TagCacheOp, getCacheOp(cmd.FullName())),
tracing.WithTag(tracing.TagCacheCmd, cmd.FullName()),
tracing.WithTag(tracing.TagCacheKey, getKey(cmd.Args())),
tracing.WithTag(tracing.TagCacheArgs, cmd.String()),
tracing.WithTag(tracing.TagCacheArgs, maxString(cmd.String(), config.MaxArgsBytes)),
)

if err != nil {
Expand Down Expand Up @@ -187,3 +187,15 @@ func getKey(args []interface{}) string {
}
return key
}

// maxString limit the bytes length of the redis args.
func maxString(s string, length int) string {
if length <= 0 { // no define or no limit
return s
}

if len(s) > length {
return s[:length]
}
return s
}
2 changes: 2 additions & 0 deletions test/plugins/scenarios/go-redisv9/bin/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@
home="$(cd "$(dirname $0)"; pwd)"
go build ${GO_BUILD_OPTS} -o goredisv9

export SW_AGENT_PLUGIN_CONFIG_REDIS_MAX_ARGS_BYTES=1024

./goredisv9
3 changes: 3 additions & 0 deletions tools/go-agent/config/agent.default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,6 @@ plugin:
sql:
# Collect the parameter of the SQL request
collect_parameter: ${SW_AGENT_PLUGIN_CONFIG_SQL_COLLECT_PARAMETER:false}
redis:
# Limit the bytes size of redis args request
max_args_bytes: ${SW_AGENT_PLUGIN_CONFIG_REDIS_MAX_ARGS_BYTES:1024}
2 changes: 1 addition & 1 deletion tools/go-agent/instrument/plugins/enhance_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (f *ConfigField) GenerateAssignFieldValue(varName string, field, path []str
getFromEnvStr = fmt.Sprintf("if v := tools.GetEnvValue(%q); v != \"\" { result = v };", pluginConfig.EnvKey)
}
parseResStr := ""
parseErrorMessage := "cannot parse the config " + fieldKeyPathStr + ": err.Error()"
parseErrorMessage := fmt.Sprintf(`"cannot parse the config %s: " + err.Error()`, fieldKeyPathStr)
switch f.Type {
case "string":
parseResStr = "return result"
Expand Down

0 comments on commit 99c2c38

Please sign in to comment.