feat: add trace report support

pull/1322/head
andrew shan 1 year ago
parent 74f500b60d
commit 5799cbbd44

@ -258,7 +258,7 @@ public class MetadataContext extends com.tencent.polaris.metadata.core.manager.M
case FRAGMENT_RAW_TRANSHEADERS: case FRAGMENT_RAW_TRANSHEADERS:
return getMapMetadataAsMap(MetadataType.CUSTOM, FRAGMENT_RAW_TRANSHEADERS, TransitiveType.NONE, false); return getMapMetadataAsMap(MetadataType.CUSTOM, FRAGMENT_RAW_TRANSHEADERS, TransitiveType.NONE, false);
case FRAGMENT_RAW_TRANSHEADERS_KV: case FRAGMENT_RAW_TRANSHEADERS_KV:
return getMapMetadataAsMap(MetadataType.CUSTOM, FRAGMENT_RAW_TRANSHEADERS_KV, TransitiveType.PASS_THROUGH, false); return getMapMetadataAsMap(MetadataType.CUSTOM, FRAGMENT_RAW_TRANSHEADERS_KV, TransitiveType.NONE, false);
default: default:
return getMapMetadataAsMap(MetadataType.CUSTOM, fragment, TransitiveType.NONE, false); return getMapMetadataAsMap(MetadataType.CUSTOM, fragment, TransitiveType.NONE, false);
} }
@ -293,7 +293,7 @@ public class MetadataContext extends com.tencent.polaris.metadata.core.manager.M
putMapMetadataAsMap(MetadataType.CUSTOM, FRAGMENT_RAW_TRANSHEADERS, TransitiveType.NONE, false, context); putMapMetadataAsMap(MetadataType.CUSTOM, FRAGMENT_RAW_TRANSHEADERS, TransitiveType.NONE, false, context);
break; break;
case FRAGMENT_RAW_TRANSHEADERS_KV: case FRAGMENT_RAW_TRANSHEADERS_KV:
putMapMetadataAsMap(MetadataType.CUSTOM, FRAGMENT_RAW_TRANSHEADERS_KV, TransitiveType.PASS_THROUGH, false, context); putMapMetadataAsMap(MetadataType.CUSTOM, FRAGMENT_RAW_TRANSHEADERS_KV, TransitiveType.NONE, false, context);
break; break;
default: default:
putMapMetadataAsMap(MetadataType.CUSTOM, fragment, TransitiveType.NONE, false, context); putMapMetadataAsMap(MetadataType.CUSTOM, fragment, TransitiveType.NONE, false, context);

@ -74,7 +74,7 @@
<revision>1.14.0-2022.0.5-SNAPSHOT</revision> <revision>1.14.0-2022.0.5-SNAPSHOT</revision>
<!-- Polaris SDK version --> <!-- Polaris SDK version -->
<polaris.version>1.15.7-SNAPSHOT</polaris.version> <polaris.version>1.16.0-SNAPSHOT</polaris.version>
<!-- Dependencies --> <!-- Dependencies -->
<guava.version>32.0.1-jre</guava.version> <guava.version>32.0.1-jre</guava.version>

@ -15,7 +15,7 @@
<dependency> <dependency>
<groupId>com.alibaba.cloud</groupId> <groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2023.0.0.0-RC1</version> <version>2022.0.0.0</version>
</dependency> </dependency>
<dependency> <dependency>

@ -20,6 +20,7 @@
<module>spring-cloud-starter-tencent-discovery-adapter-plugin</module> <module>spring-cloud-starter-tencent-discovery-adapter-plugin</module>
<module>spring-cloud-tencent-lossless-plugin</module> <module>spring-cloud-tencent-lossless-plugin</module>
<module>spring-cloud-starter-tencent-threadlocal-plugin</module> <module>spring-cloud-starter-tencent-threadlocal-plugin</module>
<module>spring-cloud-tencent-trace-plugin</module>
</modules> </modules>
</project> </project>

@ -29,7 +29,7 @@
<dependency> <dependency>
<groupId>com.alibaba.cloud</groupId> <groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2023.0.0.0-RC1</version> <version>2022.0.0.0</version>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>

@ -0,0 +1,24 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>spring-cloud-tencent-plugin-starters</artifactId>
<groupId>com.tencent.cloud</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-tencent-trace-plugin</artifactId>
<name>Spring Cloud Tencent Trace Plugin</name>
<dependencies>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-rpc-enhancement</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>trace-otel</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,28 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 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;
import java.util.Map;
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
public interface SpanAttributesProvider {
Map<String, String> getConsumerSpanAttributes(EnhancedPluginContext context);
}

@ -0,0 +1,81 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 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;
import java.util.Collections;
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.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 TraceMetadataEnhancedPlugin implements EnhancedPlugin {
private final PolarisSDKContextManager polarisSDKContextManager;
private SpanAttributesProvider spanAttributesProvider;
public TraceMetadataEnhancedPlugin(PolarisSDKContextManager polarisSDKContextManager) {
this.polarisSDKContextManager = polarisSDKContextManager;
}
public void setSpanAttributesProvider(SpanAttributesProvider spanAttributesProvider) {
this.spanAttributesProvider = spanAttributesProvider;
}
@Override
public EnhancedPluginType getType() {
return EnhancedPluginType.Client.PRE;
}
@Override
public void run(EnhancedPluginContext context) throws Throwable {
AssemblyAPI assemblyAPI = polarisSDKContextManager.getAssemblyAPI();
Map<String, String> attributes = new HashMap<>();
if (null != spanAttributesProvider) {
Map<String, String> additionalAttributes = spanAttributesProvider.getConsumerSpanAttributes(context);
if (CollectionUtils.isNotEmpty(additionalAttributes)) {
attributes.putAll(additionalAttributes);
}
}
MetadataContext metadataContext = MetadataContextHolder.get();
Map<String, String> transitiveCustomAttributes = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
if (CollectionUtils.isNotEmpty(transitiveCustomAttributes)) {
attributes.putAll(transitiveCustomAttributes);
}
TraceAttributes traceAttributes = new TraceAttributes();
traceAttributes.setAttributes(attributes);
traceAttributes.setAttributeLocation(TraceAttributes.AttributeLocation.SPAN);
assemblyAPI.updateTraceAttributes(traceAttributes);
}
@Override
public int getOrder() {
return PluginOrderConstant.ClientPluginOrder.CONSUMER_TRACE_METADATA_PLUGIN_ORDER;
}
}

@ -58,5 +58,11 @@ public class PluginOrderConstant {
* {@link com.tencent.cloud.metadata.core.EncodeTransferMedataRestTemplateEnhancedPlugin}. * {@link com.tencent.cloud.metadata.core.EncodeTransferMedataRestTemplateEnhancedPlugin}.
*/ */
public static final int CONSUMER_TRANSFER_METADATA_PLUGIN_ORDER = Ordered.HIGHEST_PRECEDENCE + 10; public static final int CONSUMER_TRANSFER_METADATA_PLUGIN_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
/**
* order for
* {@link com.tencent.cloud.plugin.trace.TraceMetadataEnhancedPlugin}
*/
public static final int CONSUMER_TRACE_METADATA_PLUGIN_ORDER = CONSUMER_TRANSFER_METADATA_PLUGIN_ORDER - 1;
} }
} }

Loading…
Cancel
Save