support server final span

pull/1664/head
shedfreewu 2 months ago
parent b158125b2e
commit d7b1d41717

@ -0,0 +1,74 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 Tencent. 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;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
import com.tencent.cloud.rpc.enhancement.plugin.PluginOrderConstant;
import com.tencent.polaris.api.utils.CollectionUtils;
import com.tencent.polaris.assembly.api.AssemblyAPI;
import com.tencent.polaris.assembly.api.pojo.TraceAttributes;
public class TraceServerFinallyEnhancedPlugin implements EnhancedPlugin {
private final PolarisSDKContextManager polarisSDKContextManager;
private final List<SpanAttributesProvider> spanAttributesProviderList;
public TraceServerFinallyEnhancedPlugin(PolarisSDKContextManager polarisSDKContextManager, List<SpanAttributesProvider> spanAttributesProviderList) {
this.polarisSDKContextManager = polarisSDKContextManager;
this.spanAttributesProviderList = spanAttributesProviderList;
}
@Override
public EnhancedPluginType getType() {
return EnhancedPluginType.Server.FINALLY;
}
@Override
public void run(EnhancedPluginContext context) throws Throwable {
Map<String, String> attributes = new HashMap<>();
if (CollectionUtils.isNotEmpty(spanAttributesProviderList)) {
for (SpanAttributesProvider spanAttributesProvider : spanAttributesProviderList) {
Map<String, String> additionalAttributes = spanAttributesProvider.getServerFinallySpanAttributes(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.TRACE_SERVER_PRE_PLUGIN_ORDER;
}
}

@ -52,7 +52,7 @@ public class TraceServerPreEnhancedPlugin implements EnhancedPlugin {
Map<String, String> attributes = new HashMap<>(); Map<String, String> attributes = new HashMap<>();
if (CollectionUtils.isNotEmpty(spanAttributesProviderList)) { if (CollectionUtils.isNotEmpty(spanAttributesProviderList)) {
for (SpanAttributesProvider spanAttributesProvider : spanAttributesProviderList) { for (SpanAttributesProvider spanAttributesProvider : spanAttributesProviderList) {
Map<String, String> additionalAttributes = spanAttributesProvider.getServerSpanAttributes(context); Map<String, String> additionalAttributes = spanAttributesProvider.getServerPreSpanAttributes(context);
if (CollectionUtils.isNotEmpty(additionalAttributes)) { if (CollectionUtils.isNotEmpty(additionalAttributes)) {
attributes.putAll(additionalAttributes); attributes.putAll(additionalAttributes);
} }

@ -41,7 +41,7 @@ import static com.tencent.polaris.plugins.router.lane.LaneRouter.TRAFFIC_STAIN_L
*/ */
public class PolarisSpanAttributesProvider implements SpanAttributesProvider { public class PolarisSpanAttributesProvider implements SpanAttributesProvider {
@Override @Override
public Map<String, String> getServerSpanAttributes(EnhancedPluginContext context) { public Map<String, String> getServerPreSpanAttributes(EnhancedPluginContext context) {
Map<String, String> attributes = new HashMap<>(); Map<String, String> attributes = new HashMap<>();
MetadataContext metadataContext = MetadataContextHolder.get(); MetadataContext metadataContext = MetadataContextHolder.get();
Map<String, String> transitiveCustomAttributes = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); Map<String, String> transitiveCustomAttributes = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);

@ -29,7 +29,11 @@ public interface SpanAttributesProvider {
*/ */
String OT_SCOPE_KEY = "OT_SCOPE_KEY"; String OT_SCOPE_KEY = "OT_SCOPE_KEY";
default Map<String, String> getServerSpanAttributes(EnhancedPluginContext context) { default Map<String, String> getServerPreSpanAttributes(EnhancedPluginContext context) {
return new HashMap<>();
}
default Map<String, String> getServerFinallySpanAttributes(EnhancedPluginContext context) {
return new HashMap<>(); return new HashMap<>();
} }

@ -69,4 +69,17 @@ public class TsfSpanAttributesProvider implements SpanAttributesProvider {
} }
return attributes; return attributes;
} }
@Override
public Map<String, String> getServerFinallySpanAttributes(EnhancedPluginContext context) {
Map<String, String> attributes = new HashMap<>();
MetadataObjectValue<Map<String, String>> extraTraceAttributeObject = MetadataContextHolder.get().
getMetadataContainer(MetadataType.APPLICATION, true).
getMetadataValue(ContextConstant.Trace.EXTRA_TRACE_ATTRIBUTES);
if (MetadataContextUtils.existMetadataValue(extraTraceAttributeObject)) {
Map<String, String> extraTraceAttributes = extraTraceAttributeObject.getObjectValue().get();
attributes.putAll(extraTraceAttributes);
}
return attributes;
}
} }

@ -21,6 +21,7 @@ import java.util.List;
import com.tencent.cloud.plugin.trace.TraceClientFinallyEnhancedPlugin; import com.tencent.cloud.plugin.trace.TraceClientFinallyEnhancedPlugin;
import com.tencent.cloud.plugin.trace.TraceClientPreEnhancedPlugin; import com.tencent.cloud.plugin.trace.TraceClientPreEnhancedPlugin;
import com.tencent.cloud.plugin.trace.TraceServerFinallyEnhancedPlugin;
import com.tencent.cloud.plugin.trace.TraceServerPreEnhancedPlugin; import com.tencent.cloud.plugin.trace.TraceServerPreEnhancedPlugin;
import com.tencent.cloud.plugin.trace.attribute.PolarisSpanAttributesProvider; import com.tencent.cloud.plugin.trace.attribute.PolarisSpanAttributesProvider;
import com.tencent.cloud.plugin.trace.attribute.SpanAttributesProvider; import com.tencent.cloud.plugin.trace.attribute.SpanAttributesProvider;
@ -46,6 +47,12 @@ public class TraceEnhancedPluginAutoConfiguration {
return new TraceServerPreEnhancedPlugin(polarisSDKContextManager, spanAttributesProviderList); return new TraceServerPreEnhancedPlugin(polarisSDKContextManager, spanAttributesProviderList);
} }
@Bean
public TraceServerFinallyEnhancedPlugin traceServerFinallyEnhancedPlugin(
PolarisSDKContextManager polarisSDKContextManager, @Autowired(required = false) List<SpanAttributesProvider> spanAttributesProviderList) {
return new TraceServerFinallyEnhancedPlugin(polarisSDKContextManager, spanAttributesProviderList);
}
@Bean @Bean
public TraceClientPreEnhancedPlugin traceClientPreEnhancedPlugin( public TraceClientPreEnhancedPlugin traceClientPreEnhancedPlugin(
PolarisSDKContextManager polarisSDKContextManager, @Autowired(required = false) List<SpanAttributesProvider> spanAttributesProviderList) { PolarisSDKContextManager polarisSDKContextManager, @Autowired(required = false) List<SpanAttributesProvider> spanAttributesProviderList) {

@ -17,7 +17,10 @@
package com.tencent.cloud.rpc.enhancement.instrument.filter; package com.tencent.cloud.rpc.enhancement.instrument.filter;
import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.constant.OrderConstant; import com.tencent.cloud.common.constant.OrderConstant;
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.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginRunner; import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginRunner;
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType; import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
@ -80,6 +83,10 @@ public class EnhancedReactiveFilter implements WebFilter, Ordered {
pluginRunner.run(EnhancedPluginType.Server.EXCEPTION, enhancedPluginContext); pluginRunner.run(EnhancedPluginType.Server.EXCEPTION, enhancedPluginContext);
}) })
.doFinally(v -> { .doFinally(v -> {
if (exchange.getAttributes().containsKey(MetadataConstant.HeaderName.METADATA_CONTEXT)) {
MetadataContextHolder.set((MetadataContext) exchange.getAttributes().get(
MetadataConstant.HeaderName.METADATA_CONTEXT));
}
// Run finally enhanced plugins. // Run finally enhanced plugins.
pluginRunner.run(EnhancedPluginType.Server.FINALLY, enhancedPluginContext); pluginRunner.run(EnhancedPluginType.Server.FINALLY, enhancedPluginContext);
}); });

Loading…
Cancel
Save