From e6f12b5a7a04c0fab47ddcdccd2360de4ee18f2f Mon Sep 17 00:00:00 2001 From: elmer Date: Fri, 15 Sep 2023 11:48:36 +0800 Subject: [PATCH] add max length configuration for service_name and instance_name (#609) --- CHANGES.md | 2 ++ .../skywalking/apm/util/ConfigInitializer.java | 17 ++++++++++++++--- apm-sniffer/config/agent.config | 4 +++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 87c8ea24b..182457972 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,6 +9,8 @@ Release Notes. * Fix the issue of createSpan failure caused by invalid request URL in HttpClient 4.x/5.x plugin * Optimize ElasticSearch 6.x 7.x plugin compatibility * Fix an issue with the httpasyncclient component where the isError state is incorrect. +* Support customization for the length limitation of string configurations +* Add max length configurations in `agent.config` file for service_name and instance_name #### Documentation diff --git a/apm-commons/apm-util/src/main/java/org/apache/skywalking/apm/util/ConfigInitializer.java b/apm-commons/apm-util/src/main/java/org/apache/skywalking/apm/util/ConfigInitializer.java index c350b803e..53976898a 100644 --- a/apm-commons/apm-util/src/main/java/org/apache/skywalking/apm/util/ConfigInitializer.java +++ b/apm-commons/apm-util/src/main/java/org/apache/skywalking/apm/util/ConfigInitializer.java @@ -85,9 +85,20 @@ public class ConfigInitializer { } else { // Convert the value into real type final Length lengthDefine = field.getAnnotation(Length.class); - if (lengthDefine != null && propertyValue.length() > lengthDefine.value()) { - StringUtil.cut(propertyValue, lengthDefine.value()); - System.err.printf("The config value will be truncated , because the length max than %d : %s -> %s%n", lengthDefine.value(), configKey, propertyValue); + if (lengthDefine != null) { + int lengthLimited = lengthDefine.value(); + String lengthKey = String.format("%s#length", configKey); + if (properties.containsKey(lengthKey)) { + try { + lengthLimited = Integer.valueOf(properties.getProperty(lengthKey)); + } catch (NumberFormatException ex) { + System.err.printf("The length config (%s=%s) is invalid. The value can not be cast to number.", lengthKey, properties.getProperty(lengthKey)); + } + } + if (propertyValue.length() > lengthLimited) { + StringUtil.cut(propertyValue, lengthLimited); + System.err.printf("The config value will be truncated , because the length max than %d : %s -> %s%n", lengthDefine.value(), configKey, propertyValue); + } } Object convertedValue = convertToTypicalType(type, propertyValue); if (convertedValue != null) { diff --git a/apm-sniffer/config/agent.config b/apm-sniffer/config/agent.config index dea03afe8..1874a77be 100755 --- a/apm-sniffer/config/agent.config +++ b/apm-sniffer/config/agent.config @@ -18,6 +18,7 @@ # ${service name} = [${group name}::]${logic name} # The group name is optional only. agent.service_name=${SW_AGENT_NAME:Your_ApplicationName} +agent.service_name#length=${SW_AGENT_NAME_MAX_LENGTH:50} # The agent namespace agent.namespace=${SW_AGENT_NAMESPACE:} @@ -49,6 +50,7 @@ agent.is_open_debugging_class=${SW_AGENT_OPEN_DEBUG:false} # Instance name is the identity of an instance, should be unique in the service. If empty, SkyWalking agent will # generate an 32-bit uuid. BY Default, SkyWalking uses UUID@hostname as the instance name. Max length is 50(UTF-8 char) agent.instance_name=${SW_AGENT_INSTANCE_NAME:} +agent.instance_name#length=${SW_AGENT_INSTANCE_NAME_MAX_LENGTH:50} # service instance properties in json format. e.g. agent.instance_properties_json = {"org": "apache-skywalking"} agent.instance_properties_json=${SW_INSTANCE_PROPERTIES_JSON:} @@ -307,4 +309,4 @@ plugin.redisson.redis_parameter_max_length=${SW_PLUGIN_REDISSON_REDIS_PARAMETER_ # Specify which command should be converted to write operation plugin.redisson.operation_mapping_write=${SW_PLUGIN_REDISSON_OPERATION_MAPPING_WRITE:getset,set,setbit,setex,setnx,setrange,strlen,mset,msetnx,psetex,incr,incrby,incrbyfloat,decr,decrby,append,hmset,hset,hsetnx,hincrby,hincrbyfloat,hdel,rpoplpush,rpush,rpushx,lpush,lpushx,lrem,ltrim,lset,brpoplpush,linsert,sadd,sdiff,sdiffstore,sinterstore,sismember,srem,sunion,sunionstore,sinter,zadd,zincrby,zinterstore,zrange,zrangebylex,zrangebyscore,zrank,zrem,zremrangebylex,zremrangebyrank,zremrangebyscore,zrevrange,zrevrangebyscore,zrevrank,zunionstore,xadd,xdel,del,xtrim} # Specify which command should be converted to read operation -plugin.redisson.operation_mapping_read=${SW_PLUGIN_REDISSON_OPERATION_MAPPING_READ:getrange,getbit,mget,hvals,hkeys,hlen,hexists,hget,hgetall,hmget,blpop,brpop,lindex,llen,lpop,lrange,rpop,scard,srandmember,spop,sscan,smove,zlexcount,zscore,zscan,zcard,zcount,xget,get,xread,xlen,xrange,xrevrange} \ No newline at end of file +plugin.redisson.operation_mapping_read=${SW_PLUGIN_REDISSON_OPERATION_MAPPING_READ:getrange,getbit,mget,hvals,hkeys,hlen,hexists,hget,hgetall,hmget,blpop,brpop,lindex,llen,lpop,lrange,rpop,scard,srandmember,spop,sscan,smove,zlexcount,zscore,zscan,zcard,zcount,xget,get,xread,xlen,xrange,xrevrange}