Skip to content

Commit

Permalink
feat:upgrade trace plugin. (#1470)
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyeBeFreeman authored Dec 3, 2024
1 parent b0ff34b commit 8f75787
Show file tree
Hide file tree
Showing 17 changed files with 409 additions and 87 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@
- [feat:add admin http handler.](https://github.com/Tencent/spring-cloud-tencent/pull/1448)
- [feat:support concurrency rate limit.](https://github.com/Tencent/spring-cloud-tencent/pull/1454)
- [feat:support auth.](https://github.com/Tencent/spring-cloud-tencent/pull/1459)
- [feat:upgrade trace plugin.](https://github.com/Tencent/spring-cloud-tencent/pull/1470)
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@

<properties>
<!-- Project revision -->
<revision>2.0.0.0-2022.0.5-RC4</revision>
<revision>2.0.0.0-2022.0.5-SNAPSHOT</revision>

<!-- Spring Framework -->
<spring.framework.version>6.0.22</spring.framework.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

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;
Expand Down Expand Up @@ -111,6 +112,7 @@ public void register(PolarisRegistration registration) {
InstanceRegisterRequest instanceRegisterRequest = new InstanceRegisterRequest();
instanceRegisterRequest.setNamespace(polarisDiscoveryProperties.getNamespace());
instanceRegisterRequest.setService(serviceId);
OtUtils.setOtServiceNameIfNeeded(serviceId);
instanceRegisterRequest.setHost(registration.getHost());
instanceRegisterRequest.setPort(registration.getPort());
instanceRegisterRequest.setWeight(polarisDiscoveryProperties.getWeight());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.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;
}
}
4 changes: 2 additions & 2 deletions spring-cloud-tencent-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@

<properties>
<!-- Project revision -->
<revision>2.0.0.0-2022.0.5-RC4</revision>
<revision>2.0.0.0-2022.0.5-SNAPSHOT</revision>

<!-- Polaris SDK version -->
<polaris.version>2.0.0.0-RC5</polaris.version>
<polaris.version>2.0.0.0-SNAPSHOT</polaris.version>

<!-- Dependencies -->
<guava.version>32.0.1-jre</guava.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,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;
Expand All @@ -32,15 +32,15 @@
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<SpanAttributesProvider> spanAttributesProviderList;

public TraceServerMetadataEnhancedPlugin(PolarisSDKContextManager polarisSDKContextManager, SpanAttributesProvider spanAttributesProvider) {
public TraceServerPreEnhancedPlugin(PolarisSDKContextManager polarisSDKContextManager, List<SpanAttributesProvider> spanAttributesProviderList) {
this.polarisSDKContextManager = polarisSDKContextManager;
this.spanAttributesProvider = spanAttributesProvider;
this.spanAttributesProviderList = spanAttributesProviderList;
}

@Override
Expand All @@ -50,35 +50,26 @@ public EnhancedPluginType getType() {

@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)) {
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());
if (CollectionUtils.isNotEmpty(spanAttributesProviderList)) {
for (SpanAttributesProvider spanAttributesProvider : spanAttributesProviderList) {
Map<String, String> 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;
}
}
Loading

0 comments on commit 8f75787

Please sign in to comment.