-
Notifications
You must be signed in to change notification settings - Fork 498
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0ff34b
commit 8f75787
Showing
17 changed files
with
409 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/OtUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...plugin/src/main/java/com/tencent/cloud/plugin/trace/TraceClientFinallyEnhancedPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...ace-plugin/src/main/java/com/tencent/cloud/plugin/trace/TraceClientPreEnhancedPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.