From 6555e9d1a9735c85afbe571f8305f11f9d99a687 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Fri, 29 Nov 2024 15:55:17 +0800 Subject: [PATCH] feat:support baggage close. --- .../assembly/api/pojo/TraceAttributes.java | 51 +++++---- .../client/flow/DefaultAssemblyFlow.java | 19 +-- .../tencent/polaris/api/utils/ClassUtils.java | 49 ++++++++ .../polaris/api/utils/StringUtils.java | 108 +++++++++++++++++- .../api/plugin/stat/TraceReporter.java | 31 +++-- .../plugins/stat/otel/OtelTraceReporter.java | 68 ++++++----- pom.xml | 2 +- 7 files changed, 253 insertions(+), 75 deletions(-) create mode 100644 polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/utils/ClassUtils.java diff --git a/polaris-assembly/polaris-assembly-api/src/main/java/com/tencent/polaris/assembly/api/pojo/TraceAttributes.java b/polaris-assembly/polaris-assembly-api/src/main/java/com/tencent/polaris/assembly/api/pojo/TraceAttributes.java index d87cdccc0..08a173f5c 100644 --- a/polaris-assembly/polaris-assembly-api/src/main/java/com/tencent/polaris/assembly/api/pojo/TraceAttributes.java +++ b/polaris-assembly/polaris-assembly-api/src/main/java/com/tencent/polaris/assembly/api/pojo/TraceAttributes.java @@ -21,32 +21,41 @@ public class TraceAttributes { - /** - * The location to put attributes - */ - public enum AttributeLocation { - SPAN, - BAGGAGE - } + /** + * The location to put attributes + */ + public enum AttributeLocation { + SPAN, + BAGGAGE + } - private Map attributes; + private Map attributes; + private AttributeLocation attributeLocation; - private AttributeLocation attributeLocation; + private Object otScope; - public Map getAttributes() { - return attributes; - } + public Map getAttributes() { + return attributes; + } - public void setAttributes(Map attributes) { - this.attributes = attributes; - } + public void setAttributes(Map attributes) { + this.attributes = attributes; + } - public AttributeLocation getAttributeLocation() { - return attributeLocation; - } + public AttributeLocation getAttributeLocation() { + return attributeLocation; + } - public void setAttributeLocation(AttributeLocation attributeLocation) { - this.attributeLocation = attributeLocation; - } + public void setAttributeLocation(AttributeLocation attributeLocation) { + this.attributeLocation = attributeLocation; + } + + public Object getOtScope() { + return otScope; + } + + public void setOtScope(Object otScope) { + this.otScope = otScope; + } } diff --git a/polaris-assembly/polaris-assembly-client/src/main/java/com/tencent/polaris/assembly/client/flow/DefaultAssemblyFlow.java b/polaris-assembly/polaris-assembly-client/src/main/java/com/tencent/polaris/assembly/client/flow/DefaultAssemblyFlow.java index 26470da06..2fc6a559e 100644 --- a/polaris-assembly/polaris-assembly-client/src/main/java/com/tencent/polaris/assembly/client/flow/DefaultAssemblyFlow.java +++ b/polaris-assembly/polaris-assembly-client/src/main/java/com/tencent/polaris/assembly/client/flow/DefaultAssemblyFlow.java @@ -191,18 +191,19 @@ public void updateTraceAttributes(TraceAttributes traceAttributes) { return; } TraceReporter traceReporter = extensions.getTraceReporter(); - if (null == traceReporter) { + if (null == traceReporter || !traceReporter.isEnabled()) { return; } switch (traceAttributes.getAttributeLocation()) { - case SPAN: - traceReporter.setSpanAttributes(traceAttributes.getAttributes()); - break; - case BAGGAGE: - traceReporter.setBaggageAttributes(traceAttributes.getAttributes()); - break; - default: - break; + case SPAN: + traceReporter.setSpanAttributes(traceAttributes.getAttributes()); + break; + case BAGGAGE: + Object otScope = traceReporter.setBaggageAttributes(traceAttributes.getAttributes()); + traceAttributes.setOtScope(otScope); + break; + default: + break; } } diff --git a/polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/utils/ClassUtils.java b/polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/utils/ClassUtils.java new file mode 100644 index 000000000..05e7f5735 --- /dev/null +++ b/polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/utils/ClassUtils.java @@ -0,0 +1,49 @@ +/* + * Tencent is pleased to support the open source community by making Polaris 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.polaris.api.utils; + +import com.tencent.polaris.logging.LoggerFactory; +import org.slf4j.Logger; + +/** + * Utils for class. + * + * @author Haotian Zhang + */ +public class ClassUtils { + + private static final Logger LOG = LoggerFactory.getLogger(RuleUtils.class); + + /** + * Check if class is present. + * + * @param className class name + * @return true if present, false otherwise + */ + public static boolean isClassPresent(String className) { + try { + Class.forName(className); + return true; + } catch (ClassNotFoundException e) { + return false; + } catch (Throwable throwable) { + LOG.warn("Failed to check class present", throwable); + return false; + } + } +} diff --git a/polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/utils/StringUtils.java b/polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/utils/StringUtils.java index 8f96f1cbf..905a00d1f 100644 --- a/polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/utils/StringUtils.java +++ b/polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/utils/StringUtils.java @@ -21,6 +21,8 @@ public class StringUtils { + public static final String EMPTY = ""; + private static final String[] EMPTY_STRING_ARRAY = new String[0]; public static boolean isBlank(String str) { @@ -271,7 +273,111 @@ public static String nullSafeToString(Object obj) { return nullSafeToString((short[]) ((short[]) obj)); } else { String str = obj.toString(); - return str != null ? str : ""; + return str != null ? str : EMPTY; + } + } + + public static String substring(String str, int start) { + if (str == null) { + return null; + } + + // handle negatives, which means last n characters + if (start < 0) { + start = str.length() + start; // remember start is negative + } + + if (start < 0) { + start = 0; + } + if (start > str.length()) { + return EMPTY; + } + + return str.substring(start); + } + + public static String substring(String str, int start, int end) { + if (str == null) { + return null; + } + + // handle negatives + if (end < 0) { + end = str.length() + end; // remember end is negative + } + if (start < 0) { + start = str.length() + start; // remember start is negative + } + + // check length next + if (end > str.length()) { + end = str.length(); + } + + // if start is greater than end, return "" + if (start > end) { + return EMPTY; + } + + if (start < 0) { + start = 0; + } + if (end < 0) { + end = 0; + } + + return str.substring(start, end); + } + + public static boolean startsWith(String str, String prefix) { + return startsWith(str, prefix, false); + } + + public static boolean startsWithIgnoreCase(String str, String prefix) { + return startsWith(str, prefix, true); + } + + private static boolean startsWith(String str, String prefix, boolean ignoreCase) { + if (str == null || prefix == null) { + return (str == null && prefix == null); + } + if (prefix.length() > str.length()) { + return false; + } + return str.regionMatches(ignoreCase, 0, prefix, 0, prefix.length()); + } + + public static boolean endsWith(String str, String suffix) { + return endsWith(str, suffix, false); + } + + public static boolean endsWithIgnoreCase(String str, String suffix) { + return endsWith(str, suffix, true); + } + + private static boolean endsWith(String str, String suffix, boolean ignoreCase) { + if (str == null || suffix == null) { + return (str == null && suffix == null); + } + if (suffix.length() > str.length()) { + return false; + } + int strOffset = str.length() - suffix.length(); + return str.regionMatches(ignoreCase, strOffset, suffix, 0, suffix.length()); + } + + public static String upperCase(String str) { + if (str == null) { + return null; + } + return str.toUpperCase(); + } + + public static String[] split(String original, String separator) { + if (original == null) { + return null; } + return original.split(separator); } } diff --git a/polaris-plugins/polaris-plugin-api/src/main/java/com/tencent/polaris/api/plugin/stat/TraceReporter.java b/polaris-plugins/polaris-plugin-api/src/main/java/com/tencent/polaris/api/plugin/stat/TraceReporter.java index 60a04ea36..a5ed0550a 100644 --- a/polaris-plugins/polaris-plugin-api/src/main/java/com/tencent/polaris/api/plugin/stat/TraceReporter.java +++ b/polaris-plugins/polaris-plugin-api/src/main/java/com/tencent/polaris/api/plugin/stat/TraceReporter.java @@ -17,10 +17,10 @@ package com.tencent.polaris.api.plugin.stat; -import java.util.Map; - import com.tencent.polaris.api.plugin.Plugin; +import java.util.Map; + /** * 【扩展点接口】上报调用链 * @@ -29,15 +29,22 @@ */ public interface TraceReporter extends Plugin { - /** - * set the attributes in trace span - * @param attributes span attributes - */ - void setSpanAttributes(Map attributes); + /** + * if the reporter is enabled + */ + boolean isEnabled(); + + /** + * set the attributes in trace span + * + * @param attributes span attributes + */ + void setSpanAttributes(Map attributes); - /** - * set the attributes in baggage span - * @param attributes baggage attributes - */ - void setBaggageAttributes(Map attributes); + /** + * set the attributes in baggage span + * + * @param attributes baggage attributes + */ + Object setBaggageAttributes(Map attributes); } diff --git a/polaris-plugins/polaris-plugins-observability/trace-otel/src/main/java/com/tencent/polaris/plugins/stat/otel/OtelTraceReporter.java b/polaris-plugins/polaris-plugins-observability/trace-otel/src/main/java/com/tencent/polaris/plugins/stat/otel/OtelTraceReporter.java index 8f67d8436..41488f955 100644 --- a/polaris-plugins/polaris-plugins-observability/trace-otel/src/main/java/com/tencent/polaris/plugins/stat/otel/OtelTraceReporter.java +++ b/polaris-plugins/polaris-plugins-observability/trace-otel/src/main/java/com/tencent/polaris/plugins/stat/otel/OtelTraceReporter.java @@ -26,6 +26,7 @@ import com.tencent.polaris.api.plugin.common.PluginTypes; import com.tencent.polaris.api.plugin.compose.Extensions; import com.tencent.polaris.api.plugin.stat.TraceReporter; +import com.tencent.polaris.api.utils.ClassUtils; import com.tencent.polaris.logging.LoggerFactory; import com.tencent.polaris.logging.PolarisLogging; import io.opentelemetry.api.baggage.Baggage; @@ -35,45 +36,50 @@ public class OtelTraceReporter implements TraceReporter { - private static final Logger LOGGER = LoggerFactory.getLogger(PolarisLogging.class); + private static final Logger LOGGER = LoggerFactory.getLogger(PolarisLogging.class); - @Override - public String getName() { - return TraceReporterConfig.DEFAULT_REPORTER_OTEL; - } + @Override + public String getName() { + return TraceReporterConfig.DEFAULT_REPORTER_OTEL; + } - @Override - public PluginType getType() { - return PluginTypes.TRACE_REPORTER.getBaseType(); - } + @Override + public PluginType getType() { + return PluginTypes.TRACE_REPORTER.getBaseType(); + } - @Override - public void init(InitContext ctx) throws PolarisException { + @Override + public void init(InitContext ctx) throws PolarisException { - } + } - @Override - public void postContextInit(Extensions ctx) throws PolarisException { + @Override + public void postContextInit(Extensions ctx) throws PolarisException { - } + } - @Override - public void destroy() { + @Override + public void destroy() { - } + } - @Override - public void setSpanAttributes(Map attributes) { - LOGGER.debug("OtelTraceReporter: setSpanAttributes: {}", attributes); - Span span = Span.current(); - attributes.forEach(span::setAttribute); - } + @Override + public boolean isEnabled() { + return ClassUtils.isClassPresent("io.opentelemetry.api.trace.Span"); + } - @Override - public void setBaggageAttributes(Map attributes) { - LOGGER.debug("OtelTraceReporter: setBaggageAttributes: {}", attributes); - BaggageBuilder builder = Baggage.current().toBuilder(); - attributes.forEach(builder::put); - builder.build().makeCurrent(); - } + @Override + public void setSpanAttributes(Map attributes) { + LOGGER.debug("OtelTraceReporter: setSpanAttributes: {}", attributes); + Span span = Span.current(); + attributes.forEach(span::setAttribute); + } + + @Override + public Object setBaggageAttributes(Map attributes) { + LOGGER.debug("OtelTraceReporter: setBaggageAttributes: {}", attributes); + BaggageBuilder builder = Baggage.current().toBuilder(); + attributes.forEach(builder::put); + return builder.build().makeCurrent(); + } } diff --git a/pom.xml b/pom.xml index 573052e6d..6245838f9 100644 --- a/pom.xml +++ b/pom.xml @@ -66,7 +66,7 @@ - 2.0.0.0-RC5 + 2.0.0.0-SNAPSHOT ${maven.build.timestamp} false