feat:upgrade trace plugin. (#1480)
parent
8e1874b598
commit
01d24732af
@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
* 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.common.util;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utils for otel.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public final class OtUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key of otel resource attributes.
|
||||||
|
*/
|
||||||
|
public static final String OTEL_RESOURCE_ATTRIBUTES = "otel.resource.attributes";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key of lane id.
|
||||||
|
*/
|
||||||
|
public static final String OTEL_LANE_ID_KEY = "lane-id";
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(OtUtils.class);
|
||||||
|
|
||||||
|
private static String otServiceName = null;
|
||||||
|
|
||||||
|
private OtUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the service name is not set, it will be set when the service is registered.
|
||||||
|
*
|
||||||
|
* @param serviceName service name
|
||||||
|
*/
|
||||||
|
public static void setOtServiceNameIfNeeded(String serviceName) {
|
||||||
|
try {
|
||||||
|
String attributes = null;
|
||||||
|
if (null != System.getenv(OTEL_RESOURCE_ATTRIBUTES)) {
|
||||||
|
attributes = System.getenv(OTEL_RESOURCE_ATTRIBUTES);
|
||||||
|
}
|
||||||
|
if (null != System.getProperty(OTEL_RESOURCE_ATTRIBUTES)) {
|
||||||
|
attributes = System.getProperty(OTEL_RESOURCE_ATTRIBUTES);
|
||||||
|
}
|
||||||
|
if (attributes == null || !attributes.contains("service.name")) {
|
||||||
|
otServiceName = serviceName;
|
||||||
|
System.setProperty(OTEL_RESOURCE_ATTRIBUTES, "service.name=" + serviceName);
|
||||||
|
LOGGER.info("update ot service name, old:{}, new:{}",
|
||||||
|
attributes, System.getProperty(OTEL_RESOURCE_ATTRIBUTES));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
for (String attribute : attributes.split(",")) {
|
||||||
|
if (attribute.contains("service.name=")) {
|
||||||
|
otServiceName = attribute.replace("service.name=", "");
|
||||||
|
LOGGER.info("use env ot service name:{}", otServiceName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Throwable throwable) {
|
||||||
|
LOGGER.error("set ot service name failed.", throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getOtServiceName() {
|
||||||
|
return otServiceName;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import com.tencent.cloud.plugin.trace.attribute.SpanAttributesProvider;
|
||||||
|
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.ClassUtils;
|
||||||
|
import io.opentelemetry.context.Scope;
|
||||||
|
|
||||||
|
public class TraceClientFinallyEnhancedPlugin implements EnhancedPlugin {
|
||||||
|
|
||||||
|
public TraceClientFinallyEnhancedPlugin() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnhancedPluginType getType() {
|
||||||
|
return EnhancedPluginType.Client.FINALLY;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(EnhancedPluginContext context) throws Throwable {
|
||||||
|
Object otScope = context.getExtraData().get(SpanAttributesProvider.OT_SCOPE_KEY);
|
||||||
|
if (ClassUtils.isClassPresent("io.opentelemetry.context.Scope") && otScope instanceof Scope) {
|
||||||
|
((Scope) otScope).close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return PluginOrderConstant.ClientPluginOrder.TRACE_CLIENT_PLUGIN_ORDER;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
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 TraceClientPreEnhancedPlugin implements EnhancedPlugin {
|
||||||
|
|
||||||
|
private final PolarisSDKContextManager polarisSDKContextManager;
|
||||||
|
|
||||||
|
private final List<SpanAttributesProvider> spanAttributesProviderList;
|
||||||
|
|
||||||
|
public TraceClientPreEnhancedPlugin(PolarisSDKContextManager polarisSDKContextManager, List<SpanAttributesProvider> spanAttributesProviderList) {
|
||||||
|
this.polarisSDKContextManager = polarisSDKContextManager;
|
||||||
|
this.spanAttributesProviderList = spanAttributesProviderList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnhancedPluginType getType() {
|
||||||
|
return EnhancedPluginType.Client.PRE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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.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;
|
||||||
|
}
|
||||||
|
}
|
41
spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/TraceServerMetadataEnhancedPlugin.java → spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/TraceServerPreEnhancedPlugin.java
41
spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/TraceServerMetadataEnhancedPlugin.java → spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/TraceServerPreEnhancedPlugin.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<String, String> getServerSpanAttributes(EnhancedPluginContext context) {
|
||||||
|
Map<String, String> attributes = new HashMap<>();
|
||||||
|
MetadataContext metadataContext = MetadataContextHolder.get();
|
||||||
|
Map<String, String> transitiveCustomAttributes = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||||
|
if (CollectionUtils.isNotEmpty(transitiveCustomAttributes)) {
|
||||||
|
for (Map.Entry<String, String> entry : transitiveCustomAttributes.entrySet()) {
|
||||||
|
attributes.put("custom." + entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Map<String, String> disposableCustomAttributes = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_DISPOSABLE);
|
||||||
|
if (CollectionUtils.isNotEmpty(disposableCustomAttributes)) {
|
||||||
|
for (Map.Entry<String, String> entry : disposableCustomAttributes.entrySet()) {
|
||||||
|
attributes.put("custom." + entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Map<String, String> upstreamDisposableCustomAttributes = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_UPSTREAM_DISPOSABLE);
|
||||||
|
if (CollectionUtils.isNotEmpty(upstreamDisposableCustomAttributes)) {
|
||||||
|
for (Map.Entry<String, String> 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<String, String> getClientBaggageAttributes(EnhancedPluginContext context) {
|
||||||
|
Map<String, String> attributes = new HashMap<>();
|
||||||
|
MetadataContext metadataContext = MetadataContextHolder.get();
|
||||||
|
Map<String, String> transitiveCustomAttributes = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||||
|
if (CollectionUtils.isNotEmpty(transitiveCustomAttributes)) {
|
||||||
|
for (Map.Entry<String, String> entry : transitiveCustomAttributes.entrySet()) {
|
||||||
|
attributes.put("custom." + entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Map<String, String> disposableCustomAttributes = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_DISPOSABLE);
|
||||||
|
if (CollectionUtils.isNotEmpty(disposableCustomAttributes)) {
|
||||||
|
for (Map.Entry<String, String> 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;
|
||||||
|
}
|
||||||
|
}
|
16
spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/SpanAttributesProvider.java → spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/attribute/SpanAttributesProvider.java
16
spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/SpanAttributesProvider.java → spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/attribute/SpanAttributesProvider.java
7
spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/tsf/TsfSpanAttributesProvider.java → spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/attribute/tsf/TsfSpanAttributesProvider.java
7
spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/tsf/TsfSpanAttributesProvider.java → spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/cloud/plugin/trace/attribute/tsf/TsfSpanAttributesProvider.java
@ -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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,3 +1,2 @@
|
|||||||
com.tencent.cloud.plugin.trace.config.TraceConfigModifierAutoConfiguration
|
com.tencent.cloud.plugin.trace.config.TraceConfigModifierAutoConfiguration
|
||||||
com.tencent.cloud.plugin.trace.config.TraceEnhancedPluginAutoConfiguration
|
com.tencent.cloud.plugin.trace.config.TraceEnhancedPluginAutoConfiguration
|
||||||
com.tencent.cloud.plugin.trace.tsf.TsfTracePropertiesAutoConfiguration
|
|
Loading…
Reference in new issue