From 7917028a196cd00f85263547506dde954353e8c3 Mon Sep 17 00:00:00 2001 From: lilai Date: Thu, 7 Sep 2023 17:01:43 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B5=81=E9=87=8F=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E9=80=8F=E4=BC=A0=E7=89=B9=E6=80=A7=EF=BC=9A=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E9=94=AE=E7=9A=84=E5=89=8D=E7=BC=80=E5=90=8E?= =?UTF-8?q?=E7=BC=80=E5=8C=B9=E9=85=8D=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/config.yaml | 10 ++- .../config/TagTransmissionConfig.java | 29 ++++---- .../config/strategy/EqualsMatchStrategy.java | 32 +++++++++ .../config/strategy/MatchStrategy.java | 36 ++++++++++ .../config/strategy/PrefixMatchStrategy.java | 32 +++++++++ .../config/strategy/SuffixMatchStrategy.java | 32 +++++++++ .../config/strategy/TagKeyMatcher.java | 67 +++++++++++++++++++ .../httpclient/HttpClient3xInterceptor.java | 6 +- .../httpclient/HttpClient4xInterceptor.java | 6 +- .../client/jdk/JdkHttpClientInterceptor.java | 6 +- .../client/okhttp/OkHttp2xInterceptor.java | 6 +- .../http/server/HttpServletInterceptor.java | 8 ++- .../kafka/KafkaConsumerRecordInterceptor.java | 7 +- .../mq/kafka/KafkaProducerInterceptor.java | 6 +- .../rocketmq/RocketmqConsumerInterceptor.java | 11 ++- .../rocketmq/RocketmqProducerInterceptor.java | 22 +++--- .../AlibabaDubboConsumerInterceptor.java | 12 ++-- .../AlibabaDubboProviderInterceptor.java | 8 ++- .../dubbo/ApacheDubboConsumerInterceptor.java | 12 ++-- .../dubbo/ApacheDubboProviderInterceptor.java | 8 ++- .../rpc/grpc/ClientCallImplInterceptor.java | 6 +- .../rpc/grpc/ServerHeaderInterceptor.java | 8 ++- .../ServiceCombRpcConsumerInterceptor.java | 6 +- .../ServiceCombRpcProviderInterceptor.java | 8 ++- .../rpc/sofarpc/SofaRpcClientInterceptor.java | 6 +- .../rpc/sofarpc/SofaRpcServerInterceptor.java | 8 ++- .../interceptors/BaseInterceptorTest.java | 6 +- .../listener/TagConfigListener.java | 2 +- 28 files changed, 353 insertions(+), 53 deletions(-) create mode 100644 sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/EqualsMatchStrategy.java create mode 100644 sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/MatchStrategy.java create mode 100644 sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/PrefixMatchStrategy.java create mode 100644 sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/SuffixMatchStrategy.java create mode 100644 sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/TagKeyMatcher.java diff --git a/sermant-plugins/sermant-tag-transmission/config/config.yaml b/sermant-plugins/sermant-tag-transmission/config/config.yaml index 0f79ff6d79..513af0e9da 100644 --- a/sermant-plugins/sermant-tag-transmission/config/config.yaml +++ b/sermant-plugins/sermant-tag-transmission/config/config.yaml @@ -2,8 +2,14 @@ tag.transmission.config: # 是否开启流量标签透传 enabled: true - # 需要透传的流量标签的key - tagKeys: [id,name] + # 需要透传的流量标签的key的匹配规则, 支持等于、前缀、后缀匹配 + matchRule: + # 等于匹配 + equals: ["id", "name"] + # 前缀匹配 + prefix: [] + # 后缀匹配 + suffix: [] # 跨线程传递标签的配置,该能力可单独使用 crossthread.config: diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/TagTransmissionConfig.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/TagTransmissionConfig.java index 11237e942b..625f528147 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/TagTransmissionConfig.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/TagTransmissionConfig.java @@ -18,10 +18,11 @@ import com.huaweicloud.sermant.core.config.common.ConfigTypeKey; import com.huaweicloud.sermant.core.plugin.config.PluginConfig; -import com.huaweicloud.sermant.core.utils.CollectionUtils; +import com.huaweicloud.sermant.core.utils.MapUtils; -import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * 流量标签透传配置 @@ -37,9 +38,9 @@ public class TagTransmissionConfig implements PluginConfig { private boolean enabled; /** - * 需要透传的标签的key + * 需要透传的标签的key的规则 */ - private List tagKeys = new ArrayList<>(); + private Map> matchRule = new HashMap<>(); public boolean isEnabled() { return enabled; @@ -49,23 +50,23 @@ public void setEnabled(boolean enabled) { this.enabled = enabled; } - public List getTagKeys() { - return tagKeys; - } - - public void setTagKeys(List tagKeys) { - this.tagKeys = tagKeys; - } - public boolean isEffect() { - return enabled && !CollectionUtils.isEmpty(tagKeys); + return enabled && !MapUtils.isEmpty(matchRule); } @Override public String toString() { return "TagTransmissionConfig{" + "enabled=" + enabled - + ", tagKeys=" + tagKeys + + ", tagKeys=" + matchRule + '}'; } + + public Map> getMatchRule() { + return matchRule; + } + + public void setMatchRule(Map> matchRule) { + this.matchRule = matchRule; + } } diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/EqualsMatchStrategy.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/EqualsMatchStrategy.java new file mode 100644 index 0000000000..9be05d4e52 --- /dev/null +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/EqualsMatchStrategy.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.sermant.tag.transmission.config.strategy; + +import java.util.List; + +/** + * 等于匹配策略 + * + * @author lilai + * @since 2023-09-07 + */ +public class EqualsMatchStrategy implements MatchStrategy { + @Override + public boolean isMatch(String key, List keyConfigs) { + return keyConfigs.stream().anyMatch(configKey -> configKey.equals(key)); + } +} diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/MatchStrategy.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/MatchStrategy.java new file mode 100644 index 0000000000..a7c5594ab7 --- /dev/null +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/MatchStrategy.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.sermant.tag.transmission.config.strategy; + +import java.util.List; + +/** + * 需要透传的key的匹配策略接口 + * + * @author lilai + * @since 2023-09-07 + */ +public interface MatchStrategy { + /** + * 请求中或线程变量中的key是否匹配配置中要透传的规则 + * + * @param key 被匹配的键 + * @param keyConfigs key的匹配配置 + * @return 匹配结果 + */ + boolean isMatch(String key, List keyConfigs); +} diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/PrefixMatchStrategy.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/PrefixMatchStrategy.java new file mode 100644 index 0000000000..44d8d7dc09 --- /dev/null +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/PrefixMatchStrategy.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.sermant.tag.transmission.config.strategy; + +import java.util.List; + +/** + * 前缀匹配策略 + * + * @author lilai + * @since 2023-09-07 + */ +public class PrefixMatchStrategy implements MatchStrategy { + @Override + public boolean isMatch(String key, List keyConfigs) { + return keyConfigs.stream().anyMatch(key::startsWith); + } +} diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/SuffixMatchStrategy.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/SuffixMatchStrategy.java new file mode 100644 index 0000000000..2170823114 --- /dev/null +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/SuffixMatchStrategy.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.sermant.tag.transmission.config.strategy; + +import java.util.List; + +/** + * 后缀匹配策略 + * + * @author lilai + * @since 2023-09-07 + */ +public class SuffixMatchStrategy implements MatchStrategy { + @Override + public boolean isMatch(String key, List keyConfigs) { + return keyConfigs.stream().anyMatch(key::endsWith); + } +} diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/TagKeyMatcher.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/TagKeyMatcher.java new file mode 100644 index 0000000000..d6edda6727 --- /dev/null +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/TagKeyMatcher.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.sermant.tag.transmission.config.strategy; + +import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; +import com.huaweicloud.sermant.tag.transmission.config.TagTransmissionConfig; + +import java.util.HashMap; +import java.util.Map; + +/** + * 流量标签是否需要透传的匹配器 + * + * @author lilai + * @since 2023-09-07 + */ +public class TagKeyMatcher { + private static final Map STRATEGY_MAP = new HashMap<>(); + + private static final TagTransmissionConfig CONFIG = PluginConfigManager.getPluginConfig( + TagTransmissionConfig.class); + + private static final String EQUALS_RULE_KEY = "equals"; + + private static final String PREFIX_RULE_KEY = "prefix"; + + private static final String SUFFIX_RULE_KEY = "suffix"; + + static { + STRATEGY_MAP.put(EQUALS_RULE_KEY, new EqualsMatchStrategy()); + STRATEGY_MAP.put(PREFIX_RULE_KEY, new PrefixMatchStrategy()); + STRATEGY_MAP.put(SUFFIX_RULE_KEY, new SuffixMatchStrategy()); + } + + private TagKeyMatcher() { + } + + /** + * 是否匹配配置中需要透传的key的规则 + * + * @param key 被匹配的key + * @return 匹配结果 + */ + public static boolean isMatch(String key) { + for (String rule : CONFIG.getMatchRule().keySet()) { + MatchStrategy matchStrategy = STRATEGY_MAP.get(rule); + if (matchStrategy.isMatch(key, CONFIG.getMatchRule().get(rule))) { + return true; + } + } + return false; + } +} diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient3xInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient3xInterceptor.java index d708fc3131..c5034c6a50 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient3xInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient3xInterceptor.java @@ -19,6 +19,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.CollectionUtils; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractClientInterceptor; import org.apache.commons.httpclient.HttpMethod; @@ -54,7 +55,10 @@ public ExecuteContext doAfter(ExecuteContext context) { */ @Override protected void injectTrafficTag2Carrier(HttpMethod httpMethod) { - for (String key : tagTransmissionConfig.getTagKeys()) { + for (String key : TrafficUtils.getTrafficTag().getTag().keySet()) { + if (!TagKeyMatcher.isMatch(key)) { + continue; + } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { continue; diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient4xInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient4xInterceptor.java index 5a5fa1aa75..2fb9af0c98 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient4xInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient4xInterceptor.java @@ -19,6 +19,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.CollectionUtils; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractClientInterceptor; import org.apache.http.HttpRequest; @@ -54,7 +55,10 @@ public ExecuteContext doAfter(ExecuteContext context) { */ @Override protected void injectTrafficTag2Carrier(HttpRequest httpRequest) { - for (String key : tagTransmissionConfig.getTagKeys()) { + for (String key : TrafficUtils.getTrafficTag().getTag().keySet()) { + if (!TagKeyMatcher.isMatch(key)) { + continue; + } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { continue; diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/jdk/JdkHttpClientInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/jdk/JdkHttpClientInterceptor.java index 87a840c4e9..5afb3e8aa8 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/jdk/JdkHttpClientInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/jdk/JdkHttpClientInterceptor.java @@ -19,6 +19,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.CollectionUtils; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractClientInterceptor; import sun.net.www.MessageHeader; @@ -72,7 +73,10 @@ public ExecuteContext onThrow(ExecuteContext context) { */ @Override protected void injectTrafficTag2Carrier(MessageHeader messageHeader) { - for (String key : tagTransmissionConfig.getTagKeys()) { + for (String key : TrafficUtils.getTrafficTag().getTag().keySet()) { + if (!TagKeyMatcher.isMatch(key)) { + continue; + } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { continue; diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/okhttp/OkHttp2xInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/okhttp/OkHttp2xInterceptor.java index 090a262768..4d7e0a3184 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/okhttp/OkHttp2xInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/okhttp/OkHttp2xInterceptor.java @@ -19,6 +19,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.CollectionUtils; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractClientInterceptor; import com.squareup.okhttp.Request.Builder; @@ -63,7 +64,10 @@ public ExecuteContext doAfter(ExecuteContext context) { */ @Override protected void injectTrafficTag2Carrier(Builder builder) { - for (String key : tagTransmissionConfig.getTagKeys()) { + for (String key : TrafficUtils.getTrafficTag().getTag().keySet()) { + if (!TagKeyMatcher.isMatch(key)) { + continue; + } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { continue; diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/server/HttpServletInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/server/HttpServletInterceptor.java index c73f665f9e..799868c7d5 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/server/HttpServletInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/server/HttpServletInterceptor.java @@ -18,6 +18,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractServerInterceptor; import java.util.Collections; @@ -80,7 +81,12 @@ public ExecuteContext onThrow(ExecuteContext context) { @Override protected Map> extractTrafficTagFromCarrier(HttpServletRequest httpServletRequest) { Map> tagMap = new HashMap<>(); - for (String key : tagTransmissionConfig.getTagKeys()) { + Enumeration keyEnumeration = httpServletRequest.getHeaderNames(); + while (keyEnumeration.hasMoreElements()) { + String key = keyEnumeration.nextElement(); + if (!TagKeyMatcher.isMatch(key)) { + continue; + } Enumeration valuesEnumeration = httpServletRequest.getHeaders(key); if (valuesEnumeration == null) { continue; diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaConsumerRecordInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaConsumerRecordInterceptor.java index 08f47761d9..fab097a952 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaConsumerRecordInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaConsumerRecordInterceptor.java @@ -19,6 +19,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.tag.TrafficTag; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractServerInterceptor; import org.apache.kafka.clients.consumer.ConsumerRecord; @@ -66,9 +67,11 @@ public ExecuteContext doAfter(ExecuteContext context) { @Override protected Map> extractTrafficTagFromCarrier(ConsumerRecord consumerRecord) { Map> headerMap = convertHeaders(consumerRecord); - List tagKeys = tagTransmissionConfig.getTagKeys(); Map> tagMap = new HashMap<>(); - for (String key : tagKeys) { + for (String key : headerMap.keySet()) { + if (!TagKeyMatcher.isMatch(key)) { + continue; + } tagMap.put(key, headerMap.get(key)); } return tagMap; diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaProducerInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaProducerInterceptor.java index 0f595fe3af..10deb5ebf6 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaProducerInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaProducerInterceptor.java @@ -19,6 +19,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.CollectionUtils; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractClientInterceptor; import org.apache.kafka.clients.producer.ProducerRecord; @@ -57,7 +58,10 @@ public ExecuteContext doAfter(ExecuteContext context) { @Override protected void injectTrafficTag2Carrier(ProducerRecord producerRecord) { Headers headers = producerRecord.headers(); - for (String key : tagTransmissionConfig.getTagKeys()) { + for (String key : TrafficUtils.getTrafficTag().getTag().keySet()) { + if (!TagKeyMatcher.isMatch(key)) { + continue; + } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { continue; diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqConsumerInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqConsumerInterceptor.java index 91c0ff0007..88550efb40 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqConsumerInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqConsumerInterceptor.java @@ -19,6 +19,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.tag.TrafficTag; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractServerInterceptor; import org.apache.rocketmq.common.message.Message; @@ -27,6 +28,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; /** * RocketMQ流量标签透传的消费者拦截器,支持RocketMQ4.8+ @@ -76,7 +78,14 @@ public ExecuteContext doAfter(ExecuteContext context) { @Override protected Map> extractTrafficTagFromCarrier(Message message) { Map> tagMap = new HashMap<>(); - for (String key : tagTransmissionConfig.getTagKeys()) { + if (message.getProperties() == null) { + return tagMap; + } + Set keySet = message.getProperties().keySet(); + for (String key : keySet) { + if (!TagKeyMatcher.isMatch(key)) { + continue; + } String value = message.getProperty(key); if (value != null) { tagMap.put(key, Collections.singletonList(value)); diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqProducerInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqProducerInterceptor.java index 946b5535f0..6c3014cbc1 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqProducerInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqProducerInterceptor.java @@ -17,14 +17,14 @@ package com.huaweicloud.sermant.tag.transmission.interceptors.mq.rocketmq; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; -import com.huaweicloud.sermant.core.utils.tag.TrafficTag; +import com.huaweicloud.sermant.core.utils.CollectionUtils; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractClientInterceptor; import org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader; import java.util.List; -import java.util.Map; /** * RocketMQ流量标签透传的生产者拦截器,支持RocketMQ4.8+ @@ -64,7 +64,7 @@ public ExecuteContext doBefore(ExecuteContext context) { @Override protected void injectTrafficTag2Carrier(SendMessageRequestHeader header) { String oldProperties = header.getProperties(); - String newProperties = this.insertTags2Properties(oldProperties, TrafficUtils.getTrafficTag()); + String newProperties = this.insertTags2Properties(oldProperties); header.setProperties(newProperties); } @@ -72,19 +72,21 @@ protected void injectTrafficTag2Carrier(SendMessageRequestHeader header) { * 将流量标签插入到properties字符串中 原始properties的格式形如:key1[SOH]value1[STX]key2[SOH]value2,其中[SOH]为ASCII=1的符号,[STX]为ASCII=2的符号 * * @param oldProperties 原始properties - * @param trafficTag 流量标签 * @return String */ - private String insertTags2Properties(String oldProperties, TrafficTag trafficTag) { - Map> tags = trafficTag.getTag(); + private String insertTags2Properties(String oldProperties) { StringBuilder newProperties = new StringBuilder(); - for (Map.Entry> entry : tags.entrySet()) { - if (entry.getValue() == null) { + for (String key : TrafficUtils.getTrafficTag().getTag().keySet()) { + if (!TagKeyMatcher.isMatch(key)) { continue; } - newProperties.append(entry.getKey()); + List values = TrafficUtils.getTrafficTag().getTag().get(key); + if (CollectionUtils.isEmpty(values)) { + continue; + } + newProperties.append(key); newProperties.append(LINK_MARK); - newProperties.append(entry.getValue().get(0)); + newProperties.append(values.get(0)); newProperties.append(SPLIT_MARK); } if (newProperties.length() == 0) { diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/AlibabaDubboConsumerInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/AlibabaDubboConsumerInterceptor.java index 17075cc979..01ce2becdb 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/AlibabaDubboConsumerInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/AlibabaDubboConsumerInterceptor.java @@ -19,12 +19,12 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.CollectionUtils; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractClientInterceptor; import com.alibaba.dubbo.rpc.RpcInvocation; import java.util.List; -import java.util.Map; /** * dubbo流量标签透传consumer端的拦截器,支持alibaba dubbo2.6.x版本 @@ -60,11 +60,15 @@ protected ExecuteContext doAfter(ExecuteContext context) { */ @Override protected void injectTrafficTag2Carrier(RpcInvocation invocation) { - for (Map.Entry> entry : TrafficUtils.getTrafficTag().getTag().entrySet()) { - if (entry.getKey() == null || CollectionUtils.isEmpty(entry.getValue())) { + for (String key : TrafficUtils.getTrafficTag().getTag().keySet()) { + if (!TagKeyMatcher.isMatch(key)) { continue; } - invocation.setAttachment(entry.getKey(), entry.getValue().get(0)); + List values = TrafficUtils.getTrafficTag().getTag().get(key); + if (CollectionUtils.isEmpty(values)) { + continue; + } + invocation.setAttachment(key, values.get(0)); } } } diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/AlibabaDubboProviderInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/AlibabaDubboProviderInterceptor.java index bf0ad03d70..92a9088c52 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/AlibabaDubboProviderInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/AlibabaDubboProviderInterceptor.java @@ -18,6 +18,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractServerInterceptor; import com.alibaba.dubbo.rpc.Invoker; @@ -27,6 +28,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; /** * dubbo流量标签透传的provider端拦截器,支持alibaba dubbo2.6.x版本 @@ -90,7 +92,11 @@ protected ExecuteContext doAfter(ExecuteContext context) { @Override protected Map> extractTrafficTagFromCarrier(RpcInvocation invocation) { Map> tag = new HashMap<>(); - for (String key : tagTransmissionConfig.getTagKeys()) { + Set keySet = invocation.getAttachments().keySet(); + for (String key : keySet) { + if (!TagKeyMatcher.isMatch(key)) { + continue; + } String value = invocation.getAttachment(key); // 流量标签的value为null时,也需存入本地变量,覆盖原来的value,以防误用旧流量标签 diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/ApacheDubboConsumerInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/ApacheDubboConsumerInterceptor.java index e1ed2cbf88..9abb6a2b56 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/ApacheDubboConsumerInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/ApacheDubboConsumerInterceptor.java @@ -19,12 +19,12 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.CollectionUtils; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractClientInterceptor; import org.apache.dubbo.rpc.RpcInvocation; import java.util.List; -import java.util.Map; /** * dubbo流量标签透传consumer端的拦截器,支持dubbo2.7.x, 3.x版本 @@ -55,11 +55,15 @@ protected ExecuteContext doBefore(ExecuteContext context) { */ @Override protected void injectTrafficTag2Carrier(RpcInvocation invocation) { - for (Map.Entry> entry : TrafficUtils.getTrafficTag().getTag().entrySet()) { - if (entry.getKey() == null || CollectionUtils.isEmpty(entry.getValue())) { + for (String key : TrafficUtils.getTrafficTag().getTag().keySet()) { + if (!TagKeyMatcher.isMatch(key)) { continue; } - invocation.setAttachment(entry.getKey(), entry.getValue().get(0)); + List values = TrafficUtils.getTrafficTag().getTag().get(key); + if (CollectionUtils.isEmpty(values)) { + continue; + } + invocation.setAttachment(key, values.get(0)); } } diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/ApacheDubboProviderInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/ApacheDubboProviderInterceptor.java index bfcdf99906..dd371f9361 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/ApacheDubboProviderInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/ApacheDubboProviderInterceptor.java @@ -18,6 +18,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractServerInterceptor; import com.huaweicloud.sermant.tag.transmission.utils.DubboUtils; @@ -28,6 +29,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; /** * dubbo流量标签透传的provider端拦截器,支持dubbo2.7.x, 3.x @@ -104,7 +106,11 @@ protected Map> extractTrafficTagFromCarrier(RpcInvocation i .map(obj -> (Map) obj) .orElse(new HashMap<>()); - for (String key : tagTransmissionConfig.getTagKeys()) { + Set keySet = attachments.keySet(); + for (String key : keySet) { + if (!TagKeyMatcher.isMatch(key)) { + continue; + } Object value = attachments.get(key); if (value instanceof String) { tag.put(key, Collections.singletonList((String) value)); diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ClientCallImplInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ClientCallImplInterceptor.java index ab9e4c2eec..604cf1e996 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ClientCallImplInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ClientCallImplInterceptor.java @@ -19,6 +19,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.CollectionUtils; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractClientInterceptor; import io.grpc.Metadata; @@ -54,7 +55,10 @@ protected ExecuteContext doAfter(ExecuteContext context) { @Override protected void injectTrafficTag2Carrier(Metadata header) { - for (String key : tagTransmissionConfig.getTagKeys()) { + for (String key : TrafficUtils.getTrafficTag().getTag().keySet()) { + if (!TagKeyMatcher.isMatch(key)) { + continue; + } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { continue; diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ServerHeaderInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ServerHeaderInterceptor.java index 7d13d190ab..37ac94b695 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ServerHeaderInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ServerHeaderInterceptor.java @@ -19,6 +19,7 @@ import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; import com.huaweicloud.sermant.tag.transmission.config.TagTransmissionConfig; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import io.grpc.ForwardingServerCall.SimpleForwardingServerCall; import io.grpc.Metadata; @@ -30,6 +31,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; /** * grpc内部的server interceptor,从grpc的header中提取流量标签 @@ -65,7 +67,11 @@ public ServerCall.Listener interceptCall( private void extractTrafficTagFromCarrier(Metadata requestHeaders) { Map> tag = new HashMap<>(); - for (String key : tagTransmissionConfig.getTagKeys()) { + Set keySet = requestHeaders.keys(); + for (String key : keySet) { + if (!TagKeyMatcher.isMatch(key)) { + continue; + } String value = requestHeaders.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER)); // 流量标签的value为null时,也需存入本地变量,覆盖原来的value,以防误用旧流量标签 diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/servicecomb/ServiceCombRpcConsumerInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/servicecomb/ServiceCombRpcConsumerInterceptor.java index a4a0cd5422..5dd5f88187 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/servicecomb/ServiceCombRpcConsumerInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/servicecomb/ServiceCombRpcConsumerInterceptor.java @@ -19,6 +19,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.CollectionUtils; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractClientInterceptor; import org.apache.servicecomb.core.Invocation; @@ -57,7 +58,10 @@ protected ExecuteContext doAfter(ExecuteContext context) { */ @Override protected void injectTrafficTag2Carrier(Invocation invocation) { - for (String key : tagTransmissionConfig.getTagKeys()) { + for (String key : TrafficUtils.getTrafficTag().getTag().keySet()) { + if (!TagKeyMatcher.isMatch(key)) { + continue; + } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { continue; diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/servicecomb/ServiceCombRpcProviderInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/servicecomb/ServiceCombRpcProviderInterceptor.java index 3eb18686c9..7d06402083 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/servicecomb/ServiceCombRpcProviderInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/servicecomb/ServiceCombRpcProviderInterceptor.java @@ -18,6 +18,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractServerInterceptor; import org.apache.servicecomb.core.Invocation; @@ -26,6 +27,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; /** * servicecombRPC provider端interceptor,支持servicecomb2.x版本 @@ -68,7 +70,11 @@ public ExecuteContext onThrow(ExecuteContext context) { @Override protected Map> extractTrafficTagFromCarrier(Invocation invocation) { Map> tag = new HashMap<>(); - for (String key : tagTransmissionConfig.getTagKeys()) { + Set keySet = invocation.getContext().keySet(); + for (String key : keySet) { + if (!TagKeyMatcher.isMatch(key)) { + continue; + } String value = invocation.getContext().get(key); if (value != null) { // consumer端使用servicecombrpc方式调用provider端 diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/sofarpc/SofaRpcClientInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/sofarpc/SofaRpcClientInterceptor.java index 823c64aadd..8c759c1af1 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/sofarpc/SofaRpcClientInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/sofarpc/SofaRpcClientInterceptor.java @@ -19,6 +19,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.CollectionUtils; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractClientInterceptor; import com.alipay.sofa.rpc.core.request.SofaRequest; @@ -57,7 +58,10 @@ protected ExecuteContext doAfter(ExecuteContext context) { */ @Override protected void injectTrafficTag2Carrier(SofaRequest sofaRequest) { - for (String key : tagTransmissionConfig.getTagKeys()) { + for (String key : TrafficUtils.getTrafficTag().getTag().keySet()) { + if (!TagKeyMatcher.isMatch(key)) { + continue; + } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { continue; diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/sofarpc/SofaRpcServerInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/sofarpc/SofaRpcServerInterceptor.java index 9352197772..0303a3bc98 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/sofarpc/SofaRpcServerInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/sofarpc/SofaRpcServerInterceptor.java @@ -18,6 +18,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.strategy.TagKeyMatcher; import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractServerInterceptor; import com.alipay.sofa.rpc.core.request.SofaRequest; @@ -26,6 +27,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; /** * sofarpc server端interceptor,支持5.0+版本 @@ -68,7 +70,11 @@ public ExecuteContext onThrow(ExecuteContext context) { @Override protected Map> extractTrafficTagFromCarrier(SofaRequest sofaRequest) { Map> tag = new HashMap<>(); - for (String key : tagTransmissionConfig.getTagKeys()) { + Set keySet = sofaRequest.getRequestProps().keySet(); + for (String key : keySet) { + if (!TagKeyMatcher.isMatch(key)) { + continue; + } Object value = sofaRequest.getRequestProp(key); if (value instanceof String) { tag.put(key, Collections.singletonList((String) value)); diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/BaseInterceptorTest.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/BaseInterceptorTest.java index a64de42ffc..174df70b82 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/BaseInterceptorTest.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/BaseInterceptorTest.java @@ -26,7 +26,9 @@ import org.mockito.Mockito; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * 流量标签透传UT基础测试类 @@ -51,7 +53,9 @@ public void before() { List tagKeys = new ArrayList<>(); tagKeys.add("id"); tagKeys.add("name"); - tagTransmissionConfig.setTagKeys(tagKeys); + Map> matchRule = new HashMap<>(); + matchRule.put("equals", tagKeys); + tagTransmissionConfig.setMatchRule(matchRule); TrafficUtils.removeTrafficTag(); } diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-service/src/main/java/com/huaweicloud/sermant/tag/transmission/listener/TagConfigListener.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-service/src/main/java/com/huaweicloud/sermant/tag/transmission/listener/TagConfigListener.java index 9cf107a1b1..0a715c4117 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-service/src/main/java/com/huaweicloud/sermant/tag/transmission/listener/TagConfigListener.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-service/src/main/java/com/huaweicloud/sermant/tag/transmission/listener/TagConfigListener.java @@ -72,7 +72,7 @@ private void updateConfig(DynamicConfigEvent event) { return; } tagTransmissionConfig.setEnabled(dynamicConfig.isEnabled()); - tagTransmissionConfig.setTagKeys(dynamicConfig.getTagKeys()); + tagTransmissionConfig.setMatchRule(dynamicConfig.getMatchRule()); LOGGER.info(String.format(Locale.ROOT, "Update tagTransmissionConfig, %s", tagTransmissionConfig.toString())); } From 1e6f6484ac2511a97bbf757b004e55b591c77206 Mon Sep 17 00:00:00 2001 From: lilai Date: Thu, 7 Sep 2023 20:43:51 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B5=81=E9=87=8F=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E9=80=8F=E4=BC=A0=E7=89=B9=E6=80=A7=EF=BC=9A=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E9=94=AE=E7=9A=84=E5=89=8D=E7=BC=80=E5=90=8E?= =?UTF-8?q?=E7=BC=80=E5=8C=B9=E9=85=8D=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/config.yaml | 4 +-- .../config/TagTransmissionConfig.java | 2 +- ...hStrategy.java => ExactMatchStrategy.java} | 6 ++-- .../config/strategy/TagKeyMatcher.java | 4 +-- .../httpclient/HttpClient3xInterceptor.java | 1 + .../httpclient/HttpClient4xInterceptor.java | 1 + .../client/jdk/JdkHttpClientInterceptor.java | 1 + .../client/okhttp/OkHttp2xInterceptor.java | 1 + .../http/server/HttpServletInterceptor.java | 9 ++--- .../kafka/KafkaConsumerRecordInterceptor.java | 4 +++ .../mq/kafka/KafkaProducerInterceptor.java | 1 + .../rocketmq/RocketmqConsumerInterceptor.java | 6 ++-- .../rocketmq/RocketmqProducerInterceptor.java | 4 +++ .../AlibabaDubboConsumerInterceptor.java | 1 + .../AlibabaDubboProviderInterceptor.java | 3 ++ .../dubbo/ApacheDubboConsumerInterceptor.java | 1 + .../rpc/grpc/ClientCallImplInterceptor.java | 2 ++ .../rpc/grpc/ServerHeaderInterceptor.java | 2 +- .../ServiceCombRpcConsumerInterceptor.java | 1 + .../ServiceCombRpcProviderInterceptor.java | 34 ++++++++++++++++--- .../rpc/sofarpc/SofaRpcClientInterceptor.java | 2 ++ .../rpc/sofarpc/SofaRpcServerInterceptor.java | 5 ++- .../interceptors/BaseInterceptorTest.java | 2 +- 23 files changed, 75 insertions(+), 22 deletions(-) rename sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/{EqualsMatchStrategy.java => ExactMatchStrategy.java} (84%) diff --git a/sermant-plugins/sermant-tag-transmission/config/config.yaml b/sermant-plugins/sermant-tag-transmission/config/config.yaml index 513af0e9da..8cb0224cc1 100644 --- a/sermant-plugins/sermant-tag-transmission/config/config.yaml +++ b/sermant-plugins/sermant-tag-transmission/config/config.yaml @@ -4,8 +4,8 @@ tag.transmission.config: enabled: true # 需要透传的流量标签的key的匹配规则, 支持等于、前缀、后缀匹配 matchRule: - # 等于匹配 - equals: ["id", "name"] + # 精确匹配 + exact: ["id", "name"] # 前缀匹配 prefix: [] # 后缀匹配 diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/TagTransmissionConfig.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/TagTransmissionConfig.java index 625f528147..05dc6487cc 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/TagTransmissionConfig.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/TagTransmissionConfig.java @@ -58,7 +58,7 @@ public boolean isEffect() { public String toString() { return "TagTransmissionConfig{" + "enabled=" + enabled - + ", tagKeys=" + matchRule + + ", matchRule=" + matchRule + '}'; } diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/EqualsMatchStrategy.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/ExactMatchStrategy.java similarity index 84% rename from sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/EqualsMatchStrategy.java rename to sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/ExactMatchStrategy.java index 9be05d4e52..14001c00ba 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/EqualsMatchStrategy.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/ExactMatchStrategy.java @@ -19,14 +19,14 @@ import java.util.List; /** - * 等于匹配策略 + * 精确匹配策略 * * @author lilai * @since 2023-09-07 */ -public class EqualsMatchStrategy implements MatchStrategy { +public class ExactMatchStrategy implements MatchStrategy { @Override public boolean isMatch(String key, List keyConfigs) { - return keyConfigs.stream().anyMatch(configKey -> configKey.equals(key)); + return keyConfigs.stream().anyMatch(key::equals); } } diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/TagKeyMatcher.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/TagKeyMatcher.java index d6edda6727..f4dfa8c34e 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/TagKeyMatcher.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/strategy/TagKeyMatcher.java @@ -34,14 +34,14 @@ public class TagKeyMatcher { private static final TagTransmissionConfig CONFIG = PluginConfigManager.getPluginConfig( TagTransmissionConfig.class); - private static final String EQUALS_RULE_KEY = "equals"; + private static final String EXACT_RULE_KEY = "exact"; private static final String PREFIX_RULE_KEY = "prefix"; private static final String SUFFIX_RULE_KEY = "suffix"; static { - STRATEGY_MAP.put(EQUALS_RULE_KEY, new EqualsMatchStrategy()); + STRATEGY_MAP.put(EXACT_RULE_KEY, new ExactMatchStrategy()); STRATEGY_MAP.put(PREFIX_RULE_KEY, new PrefixMatchStrategy()); STRATEGY_MAP.put(SUFFIX_RULE_KEY, new SuffixMatchStrategy()); } diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient3xInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient3xInterceptor.java index c5034c6a50..d477288be8 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient3xInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient3xInterceptor.java @@ -61,6 +61,7 @@ protected void injectTrafficTag2Carrier(HttpMethod httpMethod) { } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { + httpMethod.setRequestHeader(key, null); continue; } diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient4xInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient4xInterceptor.java index 2fb9af0c98..51e327f29e 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient4xInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient4xInterceptor.java @@ -61,6 +61,7 @@ protected void injectTrafficTag2Carrier(HttpRequest httpRequest) { } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { + httpRequest.addHeader(key, null); continue; } for (String value : values) { diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/jdk/JdkHttpClientInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/jdk/JdkHttpClientInterceptor.java index 5afb3e8aa8..7d011b95d9 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/jdk/JdkHttpClientInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/jdk/JdkHttpClientInterceptor.java @@ -79,6 +79,7 @@ protected void injectTrafficTag2Carrier(MessageHeader messageHeader) { } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { + messageHeader.add(key, null); continue; } for (String value : values) { diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/okhttp/OkHttp2xInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/okhttp/OkHttp2xInterceptor.java index 4d7e0a3184..25a86b3ce8 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/okhttp/OkHttp2xInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/okhttp/OkHttp2xInterceptor.java @@ -70,6 +70,7 @@ protected void injectTrafficTag2Carrier(Builder builder) { } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { + builder.addHeader(key, null); continue; } for (String value : values) { diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/server/HttpServletInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/server/HttpServletInterceptor.java index 799868c7d5..43bb1f6da8 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/server/HttpServletInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/server/HttpServletInterceptor.java @@ -88,13 +88,14 @@ protected Map> extractTrafficTagFromCarrier(HttpServletRequ continue; } Enumeration valuesEnumeration = httpServletRequest.getHeaders(key); - if (valuesEnumeration == null) { - continue; - } - if (valuesEnumeration.hasMoreElements()) { + if (valuesEnumeration != null && valuesEnumeration.hasMoreElements()) { List values = Collections.list(valuesEnumeration); tagMap.put(key, values); + continue; } + + // 流量标签的value为null时,也需存入本地变量,覆盖原来的value,以防误用旧流量标签 + tagMap.put(key, null); } return tagMap; } diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaConsumerRecordInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaConsumerRecordInterceptor.java index fab097a952..5fd6d332c5 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaConsumerRecordInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaConsumerRecordInterceptor.java @@ -81,6 +81,10 @@ private Map> convertHeaders(ConsumerRecord consumerRe Map> headerMap = new HashMap<>(); for (Header header : consumerRecord.headers()) { String key = header.key(); + if (header.value() == null) { + headerMap.computeIfAbsent(key, k -> null); + continue; + } String value = new String(header.value(), StandardCharsets.UTF_8); headerMap.computeIfAbsent(key, k -> new ArrayList<>()).add(value); } diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaProducerInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaProducerInterceptor.java index 10deb5ebf6..f275bbb256 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaProducerInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaProducerInterceptor.java @@ -64,6 +64,7 @@ protected void injectTrafficTag2Carrier(ProducerRecord producerRecord) { } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { + headers.add(key, null); continue; } for (String value : values) { diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqConsumerInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqConsumerInterceptor.java index 88550efb40..ae2cdb057b 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqConsumerInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqConsumerInterceptor.java @@ -87,11 +87,11 @@ protected Map> extractTrafficTagFromCarrier(Message message continue; } String value = message.getProperty(key); - if (value != null) { - tagMap.put(key, Collections.singletonList(value)); - } else { + if (value == null || "null".equals(value)) { tagMap.put(key, null); + continue; } + tagMap.put(key, Collections.singletonList(value)); } return tagMap; } diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqProducerInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqProducerInterceptor.java index 6c3014cbc1..158ee56335 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqProducerInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqProducerInterceptor.java @@ -82,6 +82,10 @@ private String insertTags2Properties(String oldProperties) { } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { + newProperties.append(key); + newProperties.append(LINK_MARK); + newProperties.append((String) null); + newProperties.append(SPLIT_MARK); continue; } newProperties.append(key); diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/AlibabaDubboConsumerInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/AlibabaDubboConsumerInterceptor.java index 01ce2becdb..64ba5af428 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/AlibabaDubboConsumerInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/AlibabaDubboConsumerInterceptor.java @@ -66,6 +66,7 @@ protected void injectTrafficTag2Carrier(RpcInvocation invocation) { } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { + invocation.setAttachment(key, null); continue; } invocation.setAttachment(key, values.get(0)); diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/AlibabaDubboProviderInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/AlibabaDubboProviderInterceptor.java index 92a9088c52..a0d71e889b 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/AlibabaDubboProviderInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/AlibabaDubboProviderInterceptor.java @@ -92,6 +92,9 @@ protected ExecuteContext doAfter(ExecuteContext context) { @Override protected Map> extractTrafficTagFromCarrier(RpcInvocation invocation) { Map> tag = new HashMap<>(); + if (invocation.getAttachments() == null) { + return tag; + } Set keySet = invocation.getAttachments().keySet(); for (String key : keySet) { if (!TagKeyMatcher.isMatch(key)) { diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/ApacheDubboConsumerInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/ApacheDubboConsumerInterceptor.java index 9abb6a2b56..1fce350b4f 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/ApacheDubboConsumerInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/dubbo/ApacheDubboConsumerInterceptor.java @@ -61,6 +61,7 @@ protected void injectTrafficTag2Carrier(RpcInvocation invocation) { } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { + invocation.setAttachment(key, null); continue; } invocation.setAttachment(key, values.get(0)); diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ClientCallImplInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ClientCallImplInterceptor.java index 604cf1e996..d4d60b83a4 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ClientCallImplInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ClientCallImplInterceptor.java @@ -61,6 +61,8 @@ protected void injectTrafficTag2Carrier(Metadata header) { } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { + // grpc 会对null校验,此处传递"null" 字符串 + header.put(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER), "null"); continue; } header.put(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER), values.get(0)); diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ServerHeaderInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ServerHeaderInterceptor.java index 37ac94b695..259eed5e88 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ServerHeaderInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ServerHeaderInterceptor.java @@ -75,7 +75,7 @@ private void extractTrafficTagFromCarrier(Metadata requestHeaders) { String value = requestHeaders.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER)); // 流量标签的value为null时,也需存入本地变量,覆盖原来的value,以防误用旧流量标签 - if (value == null) { + if (value == null || "null".equals(value)) { tag.put(key, null); continue; } diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/servicecomb/ServiceCombRpcConsumerInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/servicecomb/ServiceCombRpcConsumerInterceptor.java index 5dd5f88187..5a32c5d0d4 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/servicecomb/ServiceCombRpcConsumerInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/servicecomb/ServiceCombRpcConsumerInterceptor.java @@ -64,6 +64,7 @@ protected void injectTrafficTag2Carrier(Invocation invocation) { } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { + invocation.getContext().put(key, null); continue; } invocation.getContext().put(key, values.get(0)); diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/servicecomb/ServiceCombRpcProviderInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/servicecomb/ServiceCombRpcProviderInterceptor.java index 7d06402083..b002761916 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/servicecomb/ServiceCombRpcProviderInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/servicecomb/ServiceCombRpcProviderInterceptor.java @@ -24,6 +24,7 @@ import org.apache.servicecomb.core.Invocation; import java.util.Collections; +import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -70,6 +71,15 @@ public ExecuteContext onThrow(ExecuteContext context) { @Override protected Map> extractTrafficTagFromCarrier(Invocation invocation) { Map> tag = new HashMap<>(); + extractFromContext(invocation, tag); + extractFromRequestEx(invocation, tag); + return tag; + } + + private void extractFromContext(Invocation invocation, Map> tag) { + if (invocation.getContext() == null) { + return; + } Set keySet = invocation.getContext().keySet(); for (String key : keySet) { if (!TagKeyMatcher.isMatch(key)) { @@ -82,16 +92,32 @@ protected Map> extractTrafficTagFromCarrier(Invocation invo continue; } + // 流量标签的value为null时,也需存入本地变量,覆盖原来的value,以防误用旧流量标签 + tag.put(key, null); + } + } + + private void extractFromRequestEx(Invocation invocation, Map> tag) { + if (invocation.getRequestEx() == null) { + return; + } + Enumeration headerNames = invocation.getRequestEx().getHeaderNames(); + while (headerNames.hasMoreElements()) { + String key = headerNames.nextElement(); + if (!TagKeyMatcher.isMatch(key)) { + continue; + } + // consumer端使用非servicecombrpc方式调用provider端,比如httpclient,okhttp等 - value = invocation.getRequestEx().getHeader(key); - if (value != null) { - tag.put(key, Collections.singletonList(value)); + Enumeration valuesEnumeration = invocation.getRequestEx().getHeaders(key); + if (valuesEnumeration != null && valuesEnumeration.hasMoreElements()) { + List values = Collections.list(valuesEnumeration); + tag.put(key, values); continue; } // 流量标签的value为null时,也需存入本地变量,覆盖原来的value,以防误用旧流量标签 tag.put(key, null); } - return tag; } } diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/sofarpc/SofaRpcClientInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/sofarpc/SofaRpcClientInterceptor.java index 8c759c1af1..bdb37d376a 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/sofarpc/SofaRpcClientInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/sofarpc/SofaRpcClientInterceptor.java @@ -64,6 +64,8 @@ protected void injectTrafficTag2Carrier(SofaRequest sofaRequest) { } List values = TrafficUtils.getTrafficTag().getTag().get(key); if (CollectionUtils.isEmpty(values)) { + // sofa 无法添加value为null的键值对 + sofaRequest.addRequestProp(key, "null"); continue; } sofaRequest.addRequestProp(key, values.get(0)); diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/sofarpc/SofaRpcServerInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/sofarpc/SofaRpcServerInterceptor.java index 0303a3bc98..3015be87cd 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/sofarpc/SofaRpcServerInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/rpc/sofarpc/SofaRpcServerInterceptor.java @@ -70,13 +70,16 @@ public ExecuteContext onThrow(ExecuteContext context) { @Override protected Map> extractTrafficTagFromCarrier(SofaRequest sofaRequest) { Map> tag = new HashMap<>(); + if (sofaRequest.getRequestProps() == null) { + return tag; + } Set keySet = sofaRequest.getRequestProps().keySet(); for (String key : keySet) { if (!TagKeyMatcher.isMatch(key)) { continue; } Object value = sofaRequest.getRequestProp(key); - if (value instanceof String) { + if (value instanceof String && !"null".equals(value)) { tag.put(key, Collections.singletonList((String) value)); continue; } diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/BaseInterceptorTest.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/BaseInterceptorTest.java index 174df70b82..f76dd44e8a 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/BaseInterceptorTest.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/BaseInterceptorTest.java @@ -54,7 +54,7 @@ public void before() { tagKeys.add("id"); tagKeys.add("name"); Map> matchRule = new HashMap<>(); - matchRule.put("equals", tagKeys); + matchRule.put("exact", tagKeys); tagTransmissionConfig.setMatchRule(matchRule); TrafficUtils.removeTrafficTag(); }