Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:upgrade trace plugin. #1480

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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 @@ -110,6 +111,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) 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,15 +31,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 @@ -49,35 +49,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
Loading