From 01d24732afc0d3deeebfc8ea9c92f1770850eb8a Mon Sep 17 00:00:00 2001 From: Haotian Zhang Date: Tue, 10 Dec 2024 12:07:54 +0800 Subject: [PATCH] feat:upgrade trace plugin. (#1480) --- CHANGELOG.md | 1 + .../registry/PolarisServiceRegistry.java | 2 + .../tencent/cloud/common/util/OtUtils.java | 84 ++++++++++++++++ .../TraceClientFinallyEnhancedPlugin.java | 50 ++++++++++ .../trace/TraceClientPreEnhancedPlugin.java | 78 +++++++++++++++ ...java => TraceServerPreEnhancedPlugin.java} | 41 +++----- .../PolarisSpanAttributesProvider.java | 99 +++++++++++++++++++ .../SpanAttributesProvider.java | 16 ++- .../tsf/TsfSpanAttributesProvider.java | 7 +- .../TraceEnhancedPluginAutoConfiguration.java | 39 +++++++- .../TsfTracePropertiesAutoConfiguration.java | 35 ------- ...ot.autoconfigure.AutoConfiguration.imports | 1 - .../tsf/TsfCoreEnvironmentPostProcessor.java | 6 ++ .../plugin/EnhancedPluginContext.java | 14 +-- .../plugin/PluginOrderConstant.java | 12 ++- 15 files changed, 404 insertions(+), 81 deletions(-) create mode 100644 spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/OtUtils.java create mode 100644 spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/TraceClientFinallyEnhancedPlugin.java create mode 100644 spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/TraceClientPreEnhancedPlugin.java rename spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/{TraceServerMetadataEnhancedPlugin.java => TraceServerPreEnhancedPlugin.java} (57%) create mode 100644 spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/attribute/PolarisSpanAttributesProvider.java rename spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/{ => attribute}/SpanAttributesProvider.java (67%) rename spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/{ => attribute}/tsf/TsfSpanAttributesProvider.java (91%) delete mode 100644 spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/tsf/TsfTracePropertiesAutoConfiguration.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 870e963ee..9be8aa0b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,3 +40,4 @@ - [feat:upgrade spring cloud 2023 version.](https://github.com/Tencent/spring-cloud-tencent/pull/1451) - [feat:support concurrency rate limit.](https://github.com/Tencent/spring-cloud-tencent/pull/1455) - [feat:support auth.](https://github.com/Tencent/spring-cloud-tencent/pull/1479) +- [feat:upgrade trace plugin.](https://github.com/Tencent/spring-cloud-tencent/pull/1480) diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java index 44cfe3207..bb07733a0 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java @@ -25,6 +25,7 @@ import java.util.concurrent.ScheduledExecutorService; import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.common.util.OkHttpUtil; +import com.tencent.cloud.common.util.OtUtils; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; @@ -110,6 +111,7 @@ public class PolarisServiceRegistry implements ServiceRegistry spanAttributesProviderList; + + public TraceClientPreEnhancedPlugin(PolarisSDKContextManager polarisSDKContextManager, List spanAttributesProviderList) { + this.polarisSDKContextManager = polarisSDKContextManager; + this.spanAttributesProviderList = spanAttributesProviderList; + } + + @Override + public EnhancedPluginType getType() { + return EnhancedPluginType.Client.PRE; + } + + @Override + public void run(EnhancedPluginContext context) throws Throwable { + Map attributes = new HashMap<>(); + if (CollectionUtils.isNotEmpty(spanAttributesProviderList)) { + for (SpanAttributesProvider spanAttributesProvider : spanAttributesProviderList) { + Map additionalAttributes = spanAttributesProvider.getClientBaggageAttributes(context); + if (CollectionUtils.isNotEmpty(additionalAttributes)) { + attributes.putAll(additionalAttributes); + } + } + } + + TraceAttributes traceAttributes = new TraceAttributes(); + traceAttributes.setAttributes(attributes); + traceAttributes.setAttributeLocation(TraceAttributes.AttributeLocation.BAGGAGE); + + AssemblyAPI assemblyAPI = polarisSDKContextManager.getAssemblyAPI(); + assemblyAPI.updateTraceAttributes(traceAttributes); + Object otScope = traceAttributes.getOtScope(); + if (otScope != null) { + context.getExtraData().put(SpanAttributesProvider.OT_SCOPE_KEY, otScope); + } + } + + @Override + public int getOrder() { + return PluginOrderConstant.ClientPluginOrder.TRACE_CLIENT_PLUGIN_ORDER; + } +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/TraceServerMetadataEnhancedPlugin.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/TraceServerPreEnhancedPlugin.java similarity index 57% rename from spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/TraceServerMetadataEnhancedPlugin.java rename to spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/TraceServerPreEnhancedPlugin.java index e75858d2e..3bdc1c9ed 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/TraceServerMetadataEnhancedPlugin.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/TraceServerPreEnhancedPlugin.java @@ -18,10 +18,10 @@ package com.tencent.cloud.plugin.trace; import java.util.HashMap; +import java.util.List; import java.util.Map; -import com.tencent.cloud.common.metadata.MetadataContext; -import com.tencent.cloud.common.metadata.MetadataContextHolder; +import com.tencent.cloud.plugin.trace.attribute.SpanAttributesProvider; import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin; import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext; @@ -31,15 +31,15 @@ import com.tencent.polaris.api.utils.CollectionUtils; import com.tencent.polaris.assembly.api.AssemblyAPI; import com.tencent.polaris.assembly.api.pojo.TraceAttributes; -public class TraceServerMetadataEnhancedPlugin implements EnhancedPlugin { +public class TraceServerPreEnhancedPlugin implements EnhancedPlugin { private final PolarisSDKContextManager polarisSDKContextManager; - private final SpanAttributesProvider spanAttributesProvider; + private final List spanAttributesProviderList; - public TraceServerMetadataEnhancedPlugin(PolarisSDKContextManager polarisSDKContextManager, SpanAttributesProvider spanAttributesProvider) { + public TraceServerPreEnhancedPlugin(PolarisSDKContextManager polarisSDKContextManager, List spanAttributesProviderList) { this.polarisSDKContextManager = polarisSDKContextManager; - this.spanAttributesProvider = spanAttributesProvider; + this.spanAttributesProviderList = spanAttributesProviderList; } @Override @@ -49,35 +49,26 @@ public class TraceServerMetadataEnhancedPlugin implements EnhancedPlugin { @Override public void run(EnhancedPluginContext context) throws Throwable { - AssemblyAPI assemblyAPI = polarisSDKContextManager.getAssemblyAPI(); Map attributes = new HashMap<>(); - if (null != spanAttributesProvider) { - Map additionalAttributes = spanAttributesProvider.getConsumerSpanAttributes(context); - if (CollectionUtils.isNotEmpty(additionalAttributes)) { - attributes.putAll(additionalAttributes); - } - } - MetadataContext metadataContext = MetadataContextHolder.get(); - Map transitiveCustomAttributes = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); - if (CollectionUtils.isNotEmpty(transitiveCustomAttributes)) { - for (Map.Entry entry : transitiveCustomAttributes.entrySet()) { - attributes.put("custom." + entry.getKey(), entry.getValue()); - } - } - Map disposableCustomAttributes = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_DISPOSABLE); - if (CollectionUtils.isNotEmpty(disposableCustomAttributes)) { - for (Map.Entry entry : disposableCustomAttributes.entrySet()) { - attributes.put("custom." + entry.getKey(), entry.getValue()); + if (CollectionUtils.isNotEmpty(spanAttributesProviderList)) { + for (SpanAttributesProvider spanAttributesProvider : spanAttributesProviderList) { + Map additionalAttributes = spanAttributesProvider.getServerSpanAttributes(context); + if (CollectionUtils.isNotEmpty(additionalAttributes)) { + attributes.putAll(additionalAttributes); + } } } + TraceAttributes traceAttributes = new TraceAttributes(); traceAttributes.setAttributes(attributes); traceAttributes.setAttributeLocation(TraceAttributes.AttributeLocation.SPAN); + + AssemblyAPI assemblyAPI = polarisSDKContextManager.getAssemblyAPI(); assemblyAPI.updateTraceAttributes(traceAttributes); } @Override public int getOrder() { - return PluginOrderConstant.ServerPluginOrder.PROVIDER_TRACE_METADATA_PLUGIN_ORDER; + return PluginOrderConstant.ServerPluginOrder.TRACE_SERVER_PRE_PLUGIN_ORDER; } } diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/attribute/PolarisSpanAttributesProvider.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/attribute/PolarisSpanAttributesProvider.java new file mode 100644 index 000000000..4624c7002 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/attribute/PolarisSpanAttributesProvider.java @@ -0,0 +1,99 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * 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.tencent.cloud.plugin.trace.attribute; + +import java.util.HashMap; +import java.util.Map; + +import com.tencent.cloud.common.metadata.MetadataContext; +import com.tencent.cloud.common.metadata.MetadataContextHolder; +import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext; +import com.tencent.polaris.api.utils.CollectionUtils; +import com.tencent.polaris.api.utils.StringUtils; +import com.tencent.polaris.metadata.core.MessageMetadataContainer; +import com.tencent.polaris.metadata.core.MetadataType; +import com.tencent.polaris.metadata.core.constant.MetadataConstants; +import com.tencent.polaris.metadata.core.manager.CalleeMetadataContainerGroup; + +import static com.tencent.cloud.common.util.OtUtils.OTEL_LANE_ID_KEY; +import static com.tencent.polaris.plugins.router.lane.LaneRouter.TRAFFIC_STAIN_LABEL; + +/** + * Implementation of {@link SpanAttributesProvider} for polaris. + * + * @author Haotian Zhang + */ +public class PolarisSpanAttributesProvider implements SpanAttributesProvider { + @Override + public Map getServerSpanAttributes(EnhancedPluginContext context) { + Map attributes = new HashMap<>(); + MetadataContext metadataContext = MetadataContextHolder.get(); + Map transitiveCustomAttributes = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); + if (CollectionUtils.isNotEmpty(transitiveCustomAttributes)) { + for (Map.Entry entry : transitiveCustomAttributes.entrySet()) { + attributes.put("custom." + entry.getKey(), entry.getValue()); + } + } + Map disposableCustomAttributes = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_DISPOSABLE); + if (CollectionUtils.isNotEmpty(disposableCustomAttributes)) { + for (Map.Entry entry : disposableCustomAttributes.entrySet()) { + attributes.put("custom." + entry.getKey(), entry.getValue()); + } + } + Map upstreamDisposableCustomAttributes = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_UPSTREAM_DISPOSABLE); + if (CollectionUtils.isNotEmpty(upstreamDisposableCustomAttributes)) { + for (Map.Entry entry : upstreamDisposableCustomAttributes.entrySet()) { + attributes.put("custom." + entry.getKey(), entry.getValue()); + } + } + attributes.put("http.port", CalleeMetadataContainerGroup.getStaticApplicationMetadataContainer() + .getRawMetadataStringValue(MetadataConstants.LOCAL_PORT)); + return attributes; + } + + @Override + public Map getClientBaggageAttributes(EnhancedPluginContext context) { + Map attributes = new HashMap<>(); + MetadataContext metadataContext = MetadataContextHolder.get(); + Map transitiveCustomAttributes = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); + if (CollectionUtils.isNotEmpty(transitiveCustomAttributes)) { + for (Map.Entry entry : transitiveCustomAttributes.entrySet()) { + attributes.put("custom." + entry.getKey(), entry.getValue()); + } + } + Map disposableCustomAttributes = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_DISPOSABLE); + if (CollectionUtils.isNotEmpty(disposableCustomAttributes)) { + for (Map.Entry entry : disposableCustomAttributes.entrySet()) { + attributes.put("custom." + entry.getKey(), entry.getValue()); + } + } + attributes.put("http.port", CalleeMetadataContainerGroup.getStaticApplicationMetadataContainer() + .getRawMetadataStringValue(MetadataConstants.LOCAL_PORT)); + attributes.put("net.peer.service", context.getTargetServiceInstance().getServiceId()); + + String serviceLane = metadataContext.getMetadataContainer(MetadataType.MESSAGE, false) + .getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_HEADER, TRAFFIC_STAIN_LABEL); + if (StringUtils.isNotBlank(serviceLane)) { + String[] splits = StringUtils.split(serviceLane, "/"); + if (splits.length >= 2) { + attributes.put(OTEL_LANE_ID_KEY, splits[1]); + } + } + return attributes; + } +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/SpanAttributesProvider.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/attribute/SpanAttributesProvider.java similarity index 67% rename from spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/SpanAttributesProvider.java rename to spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/attribute/SpanAttributesProvider.java index 9f4ce4247..9b46e3156 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/SpanAttributesProvider.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/attribute/SpanAttributesProvider.java @@ -15,13 +15,25 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.plugin.trace; +package com.tencent.cloud.plugin.trace.attribute; +import java.util.HashMap; import java.util.Map; import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext; public interface SpanAttributesProvider { - Map getConsumerSpanAttributes(EnhancedPluginContext context); + /** + * Key of OT scope object to save in EnhancedPluginContext. + */ + String OT_SCOPE_KEY = "OT_SCOPE_KEY"; + + default Map getServerSpanAttributes(EnhancedPluginContext context) { + return new HashMap<>(); + } + + default Map getClientBaggageAttributes(EnhancedPluginContext context) { + return new HashMap<>(); + } } diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/tsf/TsfSpanAttributesProvider.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/attribute/tsf/TsfSpanAttributesProvider.java similarity index 91% rename from spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/tsf/TsfSpanAttributesProvider.java rename to spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/attribute/tsf/TsfSpanAttributesProvider.java index e2e80446f..43d628a1c 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/tsf/TsfSpanAttributesProvider.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/attribute/tsf/TsfSpanAttributesProvider.java @@ -15,12 +15,12 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.plugin.trace.tsf; +package com.tencent.cloud.plugin.trace.attribute.tsf; import java.util.HashMap; import java.util.Map; -import com.tencent.cloud.plugin.trace.SpanAttributesProvider; +import com.tencent.cloud.plugin.trace.attribute.SpanAttributesProvider; import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext; import com.tencent.polaris.api.utils.CollectionUtils; import com.tencent.polaris.api.utils.StringUtils; @@ -31,7 +31,7 @@ import org.springframework.cloud.client.ServiceInstance; public class TsfSpanAttributesProvider implements SpanAttributesProvider { @Override - public Map getConsumerSpanAttributes(EnhancedPluginContext context) { + public Map getClientBaggageAttributes(EnhancedPluginContext context) { Map attributes = new HashMap<>(); if (null != context.getRequest().getUrl()) { attributes.put("remoteInterface", context.getRequest().getUrl().getPath()); @@ -53,5 +53,4 @@ public class TsfSpanAttributesProvider implements SpanAttributesProvider { } return attributes; } - } diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/config/TraceEnhancedPluginAutoConfiguration.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/config/TraceEnhancedPluginAutoConfiguration.java index c4e476042..0d285e14b 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/config/TraceEnhancedPluginAutoConfiguration.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/config/TraceEnhancedPluginAutoConfiguration.java @@ -17,12 +17,19 @@ package com.tencent.cloud.plugin.trace.config; -import com.tencent.cloud.plugin.trace.SpanAttributesProvider; -import com.tencent.cloud.plugin.trace.TraceServerMetadataEnhancedPlugin; +import java.util.List; + +import com.tencent.cloud.plugin.trace.TraceClientFinallyEnhancedPlugin; +import com.tencent.cloud.plugin.trace.TraceClientPreEnhancedPlugin; +import com.tencent.cloud.plugin.trace.TraceServerPreEnhancedPlugin; +import com.tencent.cloud.plugin.trace.attribute.PolarisSpanAttributesProvider; +import com.tencent.cloud.plugin.trace.attribute.SpanAttributesProvider; +import com.tencent.cloud.plugin.trace.attribute.tsf.TsfSpanAttributesProvider; import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -34,9 +41,31 @@ import org.springframework.context.annotation.Configuration; public class TraceEnhancedPluginAutoConfiguration { @Bean - public TraceServerMetadataEnhancedPlugin traceServerMetadataEnhancedPlugin( - PolarisSDKContextManager polarisSDKContextManager, @Autowired(required = false) SpanAttributesProvider spanAttributesProvider) { - return new TraceServerMetadataEnhancedPlugin(polarisSDKContextManager, spanAttributesProvider); + public TraceServerPreEnhancedPlugin traceServerPreEnhancedPlugin( + PolarisSDKContextManager polarisSDKContextManager, @Autowired(required = false) List spanAttributesProviderList) { + return new TraceServerPreEnhancedPlugin(polarisSDKContextManager, spanAttributesProviderList); + } + + @Bean + public TraceClientPreEnhancedPlugin traceClientPreEnhancedPlugin( + PolarisSDKContextManager polarisSDKContextManager, @Autowired(required = false) List spanAttributesProviderList) { + return new TraceClientPreEnhancedPlugin(polarisSDKContextManager, spanAttributesProviderList); + } + + @Bean + public TraceClientFinallyEnhancedPlugin traceClientFinallyEnhancedPlugin() { + return new TraceClientFinallyEnhancedPlugin(); + } + + @Bean + @ConditionalOnMissingBean + public PolarisSpanAttributesProvider polarisSpanAttributesProvider() { + return new PolarisSpanAttributesProvider(); } + @Bean + @ConditionalOnMissingBean + public TsfSpanAttributesProvider tsfClientSpanAttributesProvider() { + return new TsfSpanAttributesProvider(); + } } diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/tsf/TsfTracePropertiesAutoConfiguration.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/tsf/TsfTracePropertiesAutoConfiguration.java deleted file mode 100644 index a00d42fdf..000000000 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/tsf/TsfTracePropertiesAutoConfiguration.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making spring-cloud-tencent available. - * - * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * 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.tencent.cloud.plugin.trace.tsf; - -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration(proxyBeanMethods = false) -@ConditionalOnProperty(value = "spring.cloud.polaris.trace.enabled", matchIfMissing = true) -public class TsfTracePropertiesAutoConfiguration { - - @Bean - @ConditionalOnMissingBean - public TsfSpanAttributesProvider tsfClientSpanAttributesProvider() { - return new TsfSpanAttributesProvider(); - } - -} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index b787a3580..5ca4b3871 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,3 +1,2 @@ com.tencent.cloud.plugin.trace.config.TraceConfigModifierAutoConfiguration com.tencent.cloud.plugin.trace.config.TraceEnhancedPluginAutoConfiguration -com.tencent.cloud.plugin.trace.tsf.TsfTracePropertiesAutoConfiguration \ No newline at end of file diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/extend/tsf/TsfCoreEnvironmentPostProcessor.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/extend/tsf/TsfCoreEnvironmentPostProcessor.java index f35e9d92b..54b70c54c 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/extend/tsf/TsfCoreEnvironmentPostProcessor.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/extend/tsf/TsfCoreEnvironmentPostProcessor.java @@ -61,6 +61,12 @@ public final class TsfCoreEnvironmentPostProcessor implements EnvironmentPostPro if (StringUtils.isNotBlank(tsfAppId)) { Map defaultProperties = new HashMap<>(); + // enabled + String polarisEnabled = environment.getProperty("spring.cloud.polaris.enabled"); + if (StringUtils.isBlank(polarisEnabled)) { + defaultProperties.put("spring.cloud.polaris.enabled", true); + } + // lossless String polarisAdminPort = environment.getProperty("polaris_admin_port"); if (StringUtils.isNotBlank(polarisAdminPort)) { diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/plugin/EnhancedPluginContext.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/plugin/EnhancedPluginContext.java index a2f8df20d..0c51e4e59 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/plugin/EnhancedPluginContext.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/plugin/EnhancedPluginContext.java @@ -19,7 +19,9 @@ package com.tencent.cloud.rpc.enhancement.plugin; import java.net.URI; +import java.util.Map; import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,19 +37,13 @@ import org.springframework.cloud.client.ServiceInstance; public class EnhancedPluginContext { private static final Logger LOGGER = LoggerFactory.getLogger(EnhancedPluginContext.class); - + private final Map extraData = new ConcurrentHashMap<>(); private Object originRequest; - private EnhancedRequestContext request; - private EnhancedResponseContext response; - private Throwable throwable; - private long delay; - private ServiceInstance localServiceInstance; - /** * targetServiceInstance only exist in a client runner type. */ @@ -126,6 +122,10 @@ public class EnhancedPluginContext { } } + public Map getExtraData() { + return extraData; + } + @Override public String toString() { return "EnhancedPluginContext{" + diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/plugin/PluginOrderConstant.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/plugin/PluginOrderConstant.java index 563c97887..647e90feb 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/plugin/PluginOrderConstant.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/plugin/PluginOrderConstant.java @@ -63,15 +63,23 @@ public class PluginOrderConstant { * {@link com.tencent.cloud.plugin.trace.TraceMetadataEnhancedPlugin}. */ public static final int CONSUMER_TRACE_METADATA_PLUGIN_ORDER = CONSUMER_TRANSFER_METADATA_PLUGIN_ORDER - 1; + + /** + * order for + * {@link com.tencent.cloud.plugin.trace.TraceClientPreEnhancedPlugin} + * and + * {@link com.tencent.cloud.plugin.trace.TraceClientFinallyEnhancedPlugin}. + */ + public static final int TRACE_CLIENT_PLUGIN_ORDER = Ordered.HIGHEST_PRECEDENCE + 3; } public static class ServerPluginOrder { /** * order for - * {@link com.tencent.cloud.plugin.trace.TraceServerMetadataEnhancedPlugin}. + * {@link com.tencent.cloud.plugin.trace.TraceServerPreEnhancedPlugin}. */ - public static final int PROVIDER_TRACE_METADATA_PLUGIN_ORDER = Ordered.HIGHEST_PRECEDENCE + 1; + public static final int TRACE_SERVER_PRE_PLUGIN_ORDER = Ordered.HIGHEST_PRECEDENCE + 1; } }