diff --git a/pom.xml b/pom.xml index 0ff2e56ee..d45710f40 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ tracer-all-parent - 3.1.9 + 3.1.10 pom tracer-all-parent Alipay SOFATracer Log Implemented by OpenTracing diff --git a/sofa-tracer-plugins/sofa-tracer-datasource-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-datasource-plugin/pom.xml index f2990452b..627a3d915 100644 --- a/sofa-tracer-plugins/sofa-tracer-datasource-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-datasource-plugin/pom.xml @@ -5,7 +5,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml 4.0.0 diff --git a/sofa-tracer-plugins/sofa-tracer-dubbo-2.6.x-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-dubbo-2.6.x-plugin/pom.xml index 638bd2f96..f116aa827 100644 --- a/sofa-tracer-plugins/sofa-tracer-dubbo-2.6.x-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-dubbo-2.6.x-plugin/pom.xml @@ -5,7 +5,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml 4.0.0 diff --git a/sofa-tracer-plugins/sofa-tracer-dubbo-common-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-dubbo-common-plugin/pom.xml index a78693cfc..b51b23869 100644 --- a/sofa-tracer-plugins/sofa-tracer-dubbo-common-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-dubbo-common-plugin/pom.xml @@ -5,7 +5,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml 4.0.0 diff --git a/sofa-tracer-plugins/sofa-tracer-dubbo-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-dubbo-plugin/pom.xml index 6371df780..f7ec8827f 100644 --- a/sofa-tracer-plugins/sofa-tracer-dubbo-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-dubbo-plugin/pom.xml @@ -5,7 +5,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml 4.0.0 diff --git a/sofa-tracer-plugins/sofa-tracer-flexible-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-flexible-plugin/pom.xml index f909e4f4e..94d3df934 100644 --- a/sofa-tracer-plugins/sofa-tracer-flexible-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-flexible-plugin/pom.xml @@ -5,7 +5,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml 4.0.0 diff --git a/sofa-tracer-plugins/sofa-tracer-httpclient-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-httpclient-plugin/pom.xml index e807bcc70..6d898560c 100644 --- a/sofa-tracer-plugins/sofa-tracer-httpclient-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-httpclient-plugin/pom.xml @@ -5,7 +5,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml 4.0.0 diff --git a/sofa-tracer-plugins/sofa-tracer-kafkamq-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-kafkamq-plugin/pom.xml index 065b9b1e2..e5c613fc6 100644 --- a/sofa-tracer-plugins/sofa-tracer-kafkamq-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-kafkamq-plugin/pom.xml @@ -5,7 +5,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml 4.0.0 diff --git a/sofa-tracer-plugins/sofa-tracer-mongodb-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-mongodb-plugin/pom.xml index 75c00c7ac..99d63b7f5 100644 --- a/sofa-tracer-plugins/sofa-tracer-mongodb-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-mongodb-plugin/pom.xml @@ -5,7 +5,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml 4.0.0 diff --git a/sofa-tracer-plugins/sofa-tracer-okhttp-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-okhttp-plugin/pom.xml index ce2bb6bc6..9c1a00f08 100644 --- a/sofa-tracer-plugins/sofa-tracer-okhttp-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-okhttp-plugin/pom.xml @@ -5,7 +5,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml 4.0.0 diff --git a/sofa-tracer-plugins/sofa-tracer-rabbitmq-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-rabbitmq-plugin/pom.xml index b58aaece9..d970daea0 100644 --- a/sofa-tracer-plugins/sofa-tracer-rabbitmq-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-rabbitmq-plugin/pom.xml @@ -5,7 +5,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml 4.0.0 diff --git a/sofa-tracer-plugins/sofa-tracer-redis-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-redis-plugin/pom.xml index c9fe205dd..d9e9a58f1 100644 --- a/sofa-tracer-plugins/sofa-tracer-redis-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-redis-plugin/pom.xml @@ -5,7 +5,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml 4.0.0 diff --git a/sofa-tracer-plugins/sofa-tracer-resttmplate-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-resttmplate-plugin/pom.xml index fc412eff2..dbe13d901 100644 --- a/sofa-tracer-plugins/sofa-tracer-resttmplate-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-resttmplate-plugin/pom.xml @@ -5,7 +5,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml 4.0.0 diff --git a/sofa-tracer-plugins/sofa-tracer-rocketmq-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-rocketmq-plugin/pom.xml index 69e9ea8f1..06a1ee497 100644 --- a/sofa-tracer-plugins/sofa-tracer-rocketmq-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-rocketmq-plugin/pom.xml @@ -5,7 +5,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml 4.0.0 diff --git a/sofa-tracer-plugins/sofa-tracer-spring-cloud-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-spring-cloud-plugin/pom.xml index f557841d4..eb235716b 100644 --- a/sofa-tracer-plugins/sofa-tracer-spring-cloud-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-spring-cloud-plugin/pom.xml @@ -5,7 +5,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml 4.0.0 diff --git a/sofa-tracer-plugins/sofa-tracer-springmessage-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-springmessage-plugin/pom.xml index 05056a417..b5489911d 100644 --- a/sofa-tracer-plugins/sofa-tracer-springmessage-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-springmessage-plugin/pom.xml @@ -5,7 +5,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml 4.0.0 diff --git a/sofa-tracer-plugins/sofa-tracer-springmvc-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-springmvc-plugin/pom.xml index 0c4613e1a..c1266adbb 100644 --- a/sofa-tracer-plugins/sofa-tracer-springmvc-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-springmvc-plugin/pom.xml @@ -4,7 +4,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml diff --git a/sofa-tracer-plugins/sofa-tracer-zipkin-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-zipkin-plugin/pom.xml index 262403448..7e64a3684 100644 --- a/sofa-tracer-plugins/sofa-tracer-zipkin-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-zipkin-plugin/pom.xml @@ -5,7 +5,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml 4.0.0 diff --git a/tracer-all/pom.xml b/tracer-all/pom.xml index 44c396532..056bf3f25 100644 --- a/tracer-all/pom.xml +++ b/tracer-all/pom.xml @@ -5,12 +5,12 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 4.0.0 tracer-all - 3.1.9 + 3.1.10 jar SOFATracer in one without SOFABoot starter diff --git a/tracer-core/pom.xml b/tracer-core/pom.xml index b157672ee..6f838684d 100644 --- a/tracer-core/pom.xml +++ b/tracer-core/pom.xml @@ -4,7 +4,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../pom.xml diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/SofaTracer.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/SofaTracer.java index 68f8cb331..665c571f2 100644 --- a/tracer-core/src/main/java/com/alipay/common/tracer/core/SofaTracer.java +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/SofaTracer.java @@ -52,7 +52,7 @@ * SofaTracer * * @author yangguanchao - * @since 2017/06/17 + * @since 2017 /06/17 */ public class SofaTracer implements Tracer { @@ -76,6 +76,10 @@ public class SofaTracer implements Tracer { */ private final Reporter serverReporter; + private final Reporter clientEventReporter; + + private final Reporter serverEventReporter; + /** * Cache some information related to the tracer globally */ @@ -86,24 +90,68 @@ public class SofaTracer implements Tracer { */ private final Sampler sampler; + /** + * Instantiates a new Sofa tracer. + * + * @param tracerType the tracer type + * @param clientReporter the client reporter + * @param serverReporter the server reporter + * @param sampler the sampler + * @param tracerTags the tracer tags + */ protected SofaTracer(String tracerType, Reporter clientReporter, Reporter serverReporter, Sampler sampler, Map tracerTags) { this.tracerType = tracerType; this.clientReporter = clientReporter; this.serverReporter = serverReporter; + this.clientEventReporter = null; + this.serverEventReporter = null; this.sampler = sampler; if (tracerTags != null && tracerTags.size() > 0) { this.tracerTags.putAll(tracerTags); } } + /** + * Instantiates a new Sofa tracer. + * + * @param tracerType the tracer type + * @param sampler the sampler + */ protected SofaTracer(String tracerType, Sampler sampler) { this.tracerType = tracerType; this.clientReporter = null; this.serverReporter = null; + this.clientEventReporter = null; + this.serverEventReporter = null; this.sampler = sampler; } + /** + * Instantiates a new Sofa tracer. + * + * @param tracerType the tracer type + * @param clientReporter the client reporter + * @param serverReporter the server reporter + * @param clientEventReporter the client event reporter + * @param serverEventReporter the server event reporter + * @param sampler the sampler + * @param tracerTags the tracer tags + */ + protected SofaTracer(String tracerType, Reporter clientReporter, Reporter serverReporter, + Reporter clientEventReporter, Reporter serverEventReporter, + Sampler sampler, Map tracerTags) { + this.tracerType = tracerType; + this.clientReporter = clientReporter; + this.serverReporter = serverReporter; + this.clientEventReporter = clientEventReporter; + this.serverEventReporter = serverEventReporter; + this.sampler = sampler; + if (tracerTags != null && tracerTags.size() > 0) { + this.tracerTags.putAll(tracerTags); + } + } + @Override public SpanBuilder buildSpan(String operationName) { return new SofaTracerSpanBuilder(operationName); @@ -128,6 +176,11 @@ public SpanContext extract(Format format, C carrier) { return registryExtractor.extract(carrier); } + /** + * Report span. + * + * @param span the span + */ public void reportSpan(SofaTracerSpan span) { if (span == null) { return; @@ -153,6 +206,36 @@ public void reportSpan(SofaTracerSpan span) { } } + /** + * Report event. + * + * @param span the span + */ + public void reportEvent(SofaTracerSpan span) { + if (span == null) { + return; + } + // sampler is support & current span is root span + if (sampler != null && (span.isClient() && span.getParentSofaTracerSpan() == null)) { + span.getSofaTracerSpanContext().setSampled(sampler.sample(span).isSampled()); + } + // invoke listener + this.invokeReportListeners(span); + if (span.isClient() + || this.getTracerType().equalsIgnoreCase(ComponentNameConstants.FLEXIBLE)) { + if (this.clientEventReporter != null) { + this.clientEventReporter.report(span); + } + } else if (span.isServer()) { + if (this.serverEventReporter != null) { + this.serverEventReporter.report(span); + } + } else { + // ignore ,do not statical + SelfLog.warn("Span reported neither client nor server.Ignore!"); + } + } + /** * Shuts down the {@link Reporter} and {@link Sampler} */ @@ -164,27 +247,78 @@ public void close() { this.serverReporter.close(); } + if (this.clientEventReporter != null) { + this.clientEventReporter.close(); + } + + if (this.serverEventReporter != null) { + this.serverEventReporter.close(); + } + if (sampler != null) { this.sampler.close(); } } + /** + * Gets tracer type. + * + * @return the tracer type + */ public String getTracerType() { return tracerType; } + /** + * Gets client reporter. + * + * @return the client reporter + */ public Reporter getClientReporter() { return clientReporter; } + /** + * Gets server reporter. + * + * @return the server reporter + */ public Reporter getServerReporter() { return serverReporter; } + /** + * Gets client event reporter. + * + * @return the client event reporter + */ + public Reporter getClientEventReporter() { + return clientEventReporter; + } + + /** + * Gets server event reporter. + * + * @return the server event reporter + */ + public Reporter getServerEventReporter() { + return serverEventReporter; + } + + /** + * Gets sampler. + * + * @return the sampler + */ public Sampler getSampler() { return sampler; } + /** + * Gets tracer tags. + * + * @return the tracer tags + */ public Map getTracerTags() { return tracerTags; } @@ -194,6 +328,11 @@ public String toString() { return "SofaTracer{" + "tracerType='" + tracerType + '}'; } + /** + * Invoke report listeners. + * + * @param sofaTracerSpan the sofa tracer span + */ protected void invokeReportListeners(SofaTracerSpan sofaTracerSpan) { List listeners = SpanReportListenerHolder .getSpanReportListenersHolder(); @@ -224,6 +363,11 @@ public class SofaTracerSpanBuilder implements io.opentracing.Tracer.SpanBuilder private final Map tags = new HashMap<>(); + /** + * Instantiates a new Sofa tracer span builder. + * + * @param operationName the operation name + */ public SofaTracerSpanBuilder(String operationName) { this.operationName = operationName; } @@ -392,6 +536,9 @@ private SofaTracerSpanContext preferredReference() { } } + /** + * The type Builder. + */ public static final class Builder { private final String tracerType; @@ -400,45 +547,121 @@ public static final class Builder { private Reporter serverReporter; + private Reporter clientEventReporter; + + private Reporter serverEventReporter; + private Map tracerTags = new HashMap(); private Sampler sampler; + /** + * Instantiates a new Builder. + * + * @param tracerType the tracer type + */ public Builder(String tracerType) { AssertUtils.isTrue(StringUtils.isNotBlank(tracerType), "tracerType must be not empty"); this.tracerType = tracerType; } + /** + * With client reporter builder. + * + * @param clientReporter the client reporter + * @return the builder + */ public Builder withClientReporter(Reporter clientReporter) { this.clientReporter = clientReporter; return this; } + /** + * With server reporter builder. + * + * @param serverReporter the server reporter + * @return the builder + */ public Builder withServerReporter(Reporter serverReporter) { this.serverReporter = serverReporter; return this; } + /** + * With client event reporter builder. + * + * @param clientEventReporter the client event reporter + * @return the builder + */ + public Builder withClientEventReporter(Reporter clientEventReporter) { + this.clientEventReporter = clientEventReporter; + return this; + } + + /** + * With server event reporter builder. + * + * @param serverEventReporter the server event reporter + * @return the builder + */ + public Builder withServerEventReporter(Reporter serverEventReporter) { + this.serverEventReporter = serverEventReporter; + return this; + } + + /** + * With sampler builder. + * + * @param sampler the sampler + * @return the builder + */ public Builder withSampler(Sampler sampler) { this.sampler = sampler; return this; } + /** + * With tag builder. + * + * @param key the key + * @param value the value + * @return the builder + */ public Builder withTag(String key, String value) { tracerTags.put(key, value); return this; } + /** + * With tag builder. + * + * @param key the key + * @param value the value + * @return the builder + */ public Builder withTag(String key, Boolean value) { tracerTags.put(key, value); return this; } + /** + * With tag builder. + * + * @param key the key + * @param value the value + * @return the builder + */ public Builder withTag(String key, Number value) { tracerTags.put(key, value); return this; } + /** + * With tags builder. + * + * @param tags the tags + * @return the builder + */ public Builder withTags(Map tags) { if (tags == null || tags.size() <= 0) { return this; @@ -466,6 +689,11 @@ public Builder withTags(Map tags) { return this; } + /** + * Build sofa tracer. + * + * @return the sofa tracer + */ public SofaTracer build() { try { sampler = SamplerFactory.getSampler(); @@ -473,7 +701,7 @@ public SofaTracer build() { SelfLog.error(LogCode2Description.convert(SPACE_ID, "01-00002")); } return new SofaTracer(this.tracerType, this.clientReporter, this.serverReporter, - this.sampler, this.tracerTags); + this.clientEventReporter, this.serverEventReporter, this.sampler, this.tracerTags); } } } diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/reporter/digest/event/SpanEventDiskReporter.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/reporter/digest/event/SpanEventDiskReporter.java new file mode 100644 index 000000000..907d72653 --- /dev/null +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/reporter/digest/event/SpanEventDiskReporter.java @@ -0,0 +1,187 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alipay.common.tracer.core.reporter.digest.event; + +import com.alipay.common.tracer.core.appender.TraceAppender; +import com.alipay.common.tracer.core.appender.encoder.SpanEncoder; +import com.alipay.common.tracer.core.appender.file.LoadTestAwareAppender; +import com.alipay.common.tracer.core.appender.manager.AsyncCommonDigestAppenderManager; +import com.alipay.common.tracer.core.appender.self.SelfLog; +import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration; +import com.alipay.common.tracer.core.reporter.digest.AbstractDiskReporter; +import com.alipay.common.tracer.core.reporter.digest.manager.SofaTracerDigestReporterAsyncManager; +import com.alipay.common.tracer.core.span.SofaTracerSpan; +import com.alipay.common.tracer.core.utils.AssertUtils; +import com.alipay.common.tracer.core.utils.StringUtils; + +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * The type Span event disk reporter. + * + * @author yuqian + * @version : SpanEventDiskReporterImpl.java, v 0.1 2025-03-10 14:53 yuqian Exp $$ + */ +public class SpanEventDiskReporter extends AbstractDiskReporter { + + private final AtomicBoolean isEventFileInited = new AtomicBoolean(false); + + private final String eventLogType; + + private final String eventRollingPolicy; + + private String eventLogReserveConfig; + + private final SpanEncoder contextEncoder; + + private String logNameKey; + + /** + * Instantiates a new Span event disk reporter. + * @param eventLogType the event log type + * @param eventRollingPolicy the event rolling policy + * @param eventLogReserveConfig the event log reserve config + * @param contextEncoder the context encoder + * @param logNameKey the log name key + */ + public SpanEventDiskReporter(String eventLogType, String eventRollingPolicy, + String eventLogReserveConfig, SpanEncoder contextEncoder, + String logNameKey) { + AssertUtils.hasText(eventLogType, "eventLogType can't be empty"); + AssertUtils.notNull(contextEncoder, "contextEncoder can't be null"); + this.eventLogType = eventLogType; + this.eventRollingPolicy = eventRollingPolicy; + this.eventLogReserveConfig = eventLogReserveConfig; + this.contextEncoder = contextEncoder; + this.logNameKey = logNameKey; + } + + /** + * Get digest reporter instance type + * + * @return + */ + @Override + public String getDigestReporterType() { + return this.eventLogType; + } + + /** + * Get stat reporter instance type + * + * @return + */ + @Override + public String getStatReporterType() { + return StringUtils.EMPTY_STRING; + } + + /** + * print digest log + * + * @param span span + */ + @Override + public void digestReport(SofaTracerSpan span) { + //lazy initialization + if (!this.isEventFileInited.get()) { + this.initDigestFile(); + } + AsyncCommonDigestAppenderManager asyncDigestManager = SofaTracerDigestReporterAsyncManager + .getSofaTracerDigestReporterAsyncManager(); + if (asyncDigestManager.isAppenderAndEncoderExist(this.eventLogType)) { + //Print only when appender and encoder are present + asyncDigestManager.append(span); + } else { + SelfLog.warn(span.toString() + " have no logType set, so ignore data persistence."); + } + } + + /** + * print stat log + * + * @param span span + */ + @Override + public void statisticReport(SofaTracerSpan span) { + return; + } + + /** + * Gets event log type. + * + * @return the event log type + */ + public String getEventLogType() { + return eventLogType; + } + + /** + * Gets is event file inited. + * + * @return the is event file inited + */ + public AtomicBoolean getIsEventFileInited() { + return isEventFileInited; + } + + public String getEventRollingPolicy() { + return eventRollingPolicy; + } + + public String getEventLogReserveConfig() { + return eventLogReserveConfig; + } + + public SpanEncoder getContextEncoder() { + return contextEncoder; + } + + public String getLogNameKey() { + return logNameKey; + } + + private synchronized void initDigestFile() { + if (this.isEventFileInited.get()) { + //double check init + return; + } + if (StringUtils.isNotBlank(logNameKey)) { + String currentDigestLogReserveConfig = SofaTracerConfiguration + .getLogReserveConfig(logNameKey); + if (!currentDigestLogReserveConfig.equals(eventLogReserveConfig)) { + SelfLog.info("the lognamekey : " + logNameKey + + " take effect. the old logreserveconfig is " + eventLogReserveConfig + + " and " + "the new logreverseconfig is " + + currentDigestLogReserveConfig); + eventLogReserveConfig = currentDigestLogReserveConfig; + } + } + TraceAppender digestTraceAppender = LoadTestAwareAppender + .createLoadTestAwareTimedRollingFileAppender(this.eventLogType, + this.eventRollingPolicy, this.eventLogReserveConfig); + //registry digest + AsyncCommonDigestAppenderManager asyncDigestManager = SofaTracerDigestReporterAsyncManager + .getSofaTracerDigestReporterAsyncManager(); + if (!asyncDigestManager.isAppenderAndEncoderExist(this.eventLogType)) { + asyncDigestManager.addAppender(this.eventLogType, digestTraceAppender, + this.contextEncoder); + } + //Already exists or created for the first time + this.isEventFileInited.set(true); + } +} \ No newline at end of file diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/span/SofaTracerSpan.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/span/SofaTracerSpan.java index d7cd3bf39..98c5dc8f4 100644 --- a/tracer-core/src/main/java/com/alipay/common/tracer/core/span/SofaTracerSpan.java +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/span/SofaTracerSpan.java @@ -45,24 +45,35 @@ * SofaTracerSpan * * @author yangguanchao - * @since 2017/06/17 + * @since 2017 /06/17 */ public class SofaTracerSpan implements Span { + /** + * The constant ARRAY_SEPARATOR. + */ public static final char ARRAY_SEPARATOR = '|'; private final SofaTracer sofaTracer; private final List spanReferences; - /** tags for String */ + /** + * tags for String + */ private final Map tagsWithStr = new ConcurrentHashMap<>(); - /** tags for Boolean */ + /** + * tags for Boolean + */ private final Map tagsWithBool = new ConcurrentHashMap<>(); - /** tags for Number */ + /** + * tags for Number + */ private final Map tagsWithNumber = new ConcurrentHashMap<>(); private final ConcurrentLinkedQueue logs = new ConcurrentLinkedQueue<>(); + private SpanEventData eventData; + private String operationName = StringUtils.EMPTY_STRING; private final SofaTracerSpanContext sofaTracerSpanContext; @@ -82,6 +93,11 @@ public class SofaTracerSpan implements Span { */ private SofaTracerSpan parentSofaTracerSpan = null; + /** + * Clone instance sofa tracer span. + * + * @return the sofa tracer span + */ public SofaTracerSpan cloneInstance() { SofaTracerSpanContext spanContext = this.sofaTracerSpanContext.cloneInstance(); Map tags = new HashMap<>(); @@ -121,9 +137,9 @@ public SofaTracerSpan(SofaTracer sofaTracer, long startTime, String operationNam /** * Note: - * + *

* 1.As a server-side: After reverting back to {@link SofaTracerSpanContext}, you can directly construct Server Span (traceId, spanId unchanged) - * + *

* 2.As a client: need to be built by {@link SofaTracer.SofaTracerSpanBuilder#start()} * * @param sofaTracer sofaTracer @@ -167,6 +183,28 @@ public void finish(long endTime) { SpanExtensionFactory.logStoppedSpan(this); } + /** + * Add event. + * + * @param eventData the event data + */ + public void addEvent(SpanEventData eventData) { + if (eventData == null) { + SelfLog.error("event is null"); + return; + } + + if (this.endTime > 0 && System.currentTimeMillis() > this.endTime) { + // span is closed, can not report event + SelfLog.error("span is closed, can not add event"); + return; + } + + SofaTracerSpan clonedSpan = this.cloneInstance(); + clonedSpan.setEventData(eventData); + this.sofaTracer.reportEvent(clonedSpan); + } + @Override public void close() { this.finish(); @@ -222,6 +260,12 @@ public Span log(long currentTime, String eventValue) { return this.log(currentTime, fields); } + /** + * Log span. + * + * @param logData the log data + * @return the span + */ public Span log(LogData logData) { if (logData == null) { return this; @@ -258,8 +302,8 @@ public Span log(long currentTime, String eventName, /* @Nullable */Object payloa } /** - * * The default settings are business baggage + * * @param key * @param value * @return @@ -272,6 +316,7 @@ public Span setBaggageItem(String key, String value) { /** * The default read are business baggage + * * @param key * @return */ @@ -289,12 +334,13 @@ public Span setOperationName(String operationName) { //======================= Extended API interface starts /** + * Report error. * - * @param errorType errorType error description:timeout_error/biz_error... - * @param context context - * @param e e - * @param errorSourceApp errorSourceApp trade|rpc - * @param errorSources errorSources + * @param errorType errorType error description:timeout_error/biz_error... + * @param context context + * @param e e + * @param errorSourceApp errorSourceApp trade|rpc + * @param errorSources errorSources */ public void reportError(String errorType, Map context, Throwable e, String errorSourceApp, String... errorSources) { @@ -355,11 +401,11 @@ public void profile(String profileApp, String protocolType, String profileMessag * Return itself as the parent of the next context *

* Use countMatches to count . for higher performance, see performance test data for com.alipay.common.tracer.benchmark.CountBenchmark - * Preventing SofaTracerSpan from nesting too deeply causes a memory leak. * This time recreates a context so that the above context can be released. *

- * @return + * + * @return this as parent when exceed layer */ public SofaTracerSpan getThisAsParentWhenExceedLayer() { final SofaTracerSpan parent; @@ -385,6 +431,11 @@ public SofaTracerSpan getThisAsParentWhenExceedLayer() { //======================= Extended API interface end + /** + * Gets span references. + * + * @return the span references + */ public List getSpanReferences() { if (spanReferences == null) { return Collections.emptyList(); @@ -392,62 +443,155 @@ public List getSpanReferences() { return Collections.unmodifiableList(spanReferences); } + /** + * Gets sofa tracer. + * + * @return the sofa tracer + */ public SofaTracer getSofaTracer() { return sofaTracer; } + /** + * Gets start time. + * + * @return the start time + */ public long getStartTime() { return startTime; } + /** + * Sets start time. + * + * @param startTime the start time + */ public void setStartTime(long startTime) { this.startTime = startTime; } + /** + * Gets end time. + * + * @return the end time + */ public long getEndTime() { return endTime; } + /** + * Sets end time. + * + * @param endTime the end time + */ public void setEndTime(long endTime) { this.endTime = endTime; } + /** + * Gets duration microseconds. + * + * @return the duration microseconds + */ public long getDurationMicroseconds() { return this.endTime - this.startTime; } + /** + * Gets tags with str. + * + * @return the tags with str + */ public Map getTagsWithStr() { return tagsWithStr; } + /** + * Gets tags with bool. + * + * @return the tags with bool + */ public Map getTagsWithBool() { return tagsWithBool; } + /** + * Gets tags with number. + * + * @return the tags with number + */ public Map getTagsWithNumber() { return tagsWithNumber; } + /** + * Gets operation name. + * + * @return the operation name + */ public String getOperationName() { return operationName; } + /** + * Gets sofa tracer span context. + * + * @return the sofa tracer span context + */ public SofaTracerSpanContext getSofaTracerSpanContext() { return sofaTracerSpanContext; } + /** + * Gets logs. + * + * @return the logs + */ public ConcurrentLinkedQueue getLogs() { return logs; } + /** + * Gets log type. + * + * @return the log type + */ public String getLogType() { return logType; } + /** + * Sets log type. + * + * @param logType the log type + */ public void setLogType(String logType) { this.logType = logType; } + /** + * Gets event data. + * + * @return the event data + */ + public SpanEventData getEventData() { + return eventData; + } + + /** + * Sets event data. + * + * @param eventData the event data + */ + public void setEventData(SpanEventData eventData) { + this.eventData = eventData; + } + + /** + * Gets parent sofa tracer span. + * + * @return the parent sofa tracer span + */ public SofaTracerSpan getParentSofaTracerSpan() { if (this.parentSofaTracerSpan != null) { return this.parentSofaTracerSpan.getThisAsParentWhenExceedLayer(); @@ -455,14 +599,29 @@ public SofaTracerSpan getParentSofaTracerSpan() { return null; } + /** + * Sets parent sofa tracer span. + * + * @param parentSofaTracerSpan the parent sofa tracer span + */ public void setParentSofaTracerSpan(SofaTracerSpan parentSofaTracerSpan) { this.parentSofaTracerSpan = parentSofaTracerSpan; } + /** + * Is server boolean. + * + * @return the boolean + */ public boolean isServer() { return Tags.SPAN_KIND_SERVER.equals(tagsWithStr.get(Tags.SPAN_KIND.getKey())); } + /** + * Is client boolean. + * + * @return the boolean + */ public boolean isClient() { return Tags.SPAN_KIND_CLIENT.equals(tagsWithStr.get(Tags.SPAN_KIND.getKey())); } diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/span/SpanEventData.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/span/SpanEventData.java new file mode 100644 index 000000000..76cb962c0 --- /dev/null +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/span/SpanEventData.java @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alipay.common.tracer.core.span; + +import com.alipay.common.tracer.core.utils.StringUtils; + +import java.io.Serializable; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * The type Span event data. + * + * @author yuqian + * @version : SpanEventData.java, v 0.1 2025-04-07 20:17 yuqian Exp $$ + */ +public class SpanEventData implements Serializable { + + private long timestamp; + + private final Map eventTagWithStr = new ConcurrentHashMap<>(); + + private final Map eventTagWithNumber = new ConcurrentHashMap<>(); + + private final Map eventTagWithBool = new ConcurrentHashMap<>(); + + /** + * Gets timestamp. + * + * @return the timestamp + */ + public long getTimestamp() { + return timestamp; + } + + /** + * Sets timestamp. + * + * @param timestamp the timestamp + */ + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } + + /** + * Gets event tag with number. + * + * @return the event tag with number + */ + public Map getEventTagWithNumber() { + return eventTagWithNumber; + } + + /** + * Gets event tag with bool. + * + * @return the event tag with bool + */ + public Map getEventTagWithBool() { + return eventTagWithBool; + } + + /** + * Gets event tag with str. + * + * @return the event tag with str + */ + public Map getEventTagWithStr() { + return eventTagWithStr; + } + + /** + * Adds a string tag to this event. + * + * @param key the tag key + * @param value the tag value + * @return this SpanEventData for chaining + */ + public SpanEventData addTag(String key, String value) { + if (StringUtils.isNotBlank(key) && value != null) { + eventTagWithStr.put(key, value); + } + return this; + } + + /** + * Adds a numeric tag to this event. + * + * @param key the tag key + * @param value the tag value + * @return this SpanEventData for chaining + */ + public SpanEventData addTag(String key, Number value) { + if (StringUtils.isNotBlank(key) && value != null) { + eventTagWithNumber.put(key, value); + } + return this; + } + + /** + * Adds a boolean tag to this event. + * + * @param key the tag key + * @param value the tag value + * @return this SpanEventData for chaining + */ + public SpanEventData addTag(String key, Boolean value) { + if (StringUtils.isNotBlank(key) && value != null) { + eventTagWithBool.put(key, value); + } + return this; + } +} \ No newline at end of file diff --git a/tracer-core/src/test/java/com/alipay/common/tracer/core/reporter/digest/event/SpanEventDiskReporterTest.java b/tracer-core/src/test/java/com/alipay/common/tracer/core/reporter/digest/event/SpanEventDiskReporterTest.java new file mode 100644 index 000000000..3dba5567c --- /dev/null +++ b/tracer-core/src/test/java/com/alipay/common/tracer/core/reporter/digest/event/SpanEventDiskReporterTest.java @@ -0,0 +1,156 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alipay.common.tracer.core.reporter.digest.event; + +import com.alipay.common.tracer.core.SofaTracer; +import com.alipay.common.tracer.core.appender.self.SelfLog; +import com.alipay.common.tracer.core.base.AbstractTestBase; +import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration; +import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; +import com.alipay.common.tracer.core.span.SofaTracerSpan; +import com.alipay.common.tracer.core.tracertest.encoder.ClientSpanEventEncoder; +import com.alipay.common.tracer.core.tracertest.type.TracerTestLogEnum; +import com.alipay.common.tracer.core.utils.StringUtils; +import io.opentracing.tag.Tags; +import org.apache.commons.io.FileUtils; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; +import java.util.concurrent.*; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +/** + * @author yuqian + * @version : SpanEventDiskReporterTest.java, v 0.1 2025-03-10 16:59 yuqian Exp $$ + */ +public class SpanEventDiskReporterTest extends AbstractTestBase { + + private final String eventLogType = TracerTestLogEnum.RPC_CLIENT_EVENT + .getDefaultLogName(); + + private final String expectRollingPolicy = SofaTracerConfiguration + .getRollingPolicy(TracerTestLogEnum.RPC_CLIENT_EVENT + .getRollingKey()); + + private final String expectLogReserveConfig = SofaTracerConfiguration + .getLogReserveConfig(TracerTestLogEnum.RPC_CLIENT_EVENT + .getLogReverseKey()); + + private final ClientSpanEventEncoder clientSpanEventEncoder = new ClientSpanEventEncoder(); + + private SpanEventDiskReporter spanEventDiskReporter; + + private SofaTracerSpan sofaTracerSpan; + + @Before + public void before() { + this.spanEventDiskReporter = new SpanEventDiskReporter(eventLogType, expectRollingPolicy, + expectLogReserveConfig, clientSpanEventEncoder, null); + this.sofaTracerSpan = mock(SofaTracerSpan.class); + } + + @Test + public void testGetDigestReporterType() { + assertEquals(eventLogType, this.spanEventDiskReporter.getEventLogType()); + } + + @Test + public void testDigestReport() throws InterruptedException { + this.spanEventDiskReporter.digestReport(this.sofaTracerSpan); + assertTrue(this.spanEventDiskReporter.getIsEventFileInited().get()); + } + + @Test + public void testGetDigestLogType() { + assertEquals(this.eventLogType, this.spanEventDiskReporter.getEventLogType()); + } + + @Test + public void testGetDigestRollingPolicy() { + String rollingPolicy = this.spanEventDiskReporter.getEventRollingPolicy(); + assertEquals(expectRollingPolicy, rollingPolicy); + } + + @Test + public void testGetDigestLogReserveConfig() { + String logReserveConfig = this.spanEventDiskReporter.getEventLogReserveConfig(); + assertEquals(expectLogReserveConfig, logReserveConfig); + } + + @Test + public void testGetContextEncoder() { + assertEquals(clientSpanEventEncoder, this.spanEventDiskReporter.getContextEncoder()); + String logNameKey = this.spanEventDiskReporter.getLogNameKey(); + assertTrue(StringUtils.isBlank(logNameKey)); + } + + @Test + public void testFixInitDigestFile() throws Exception { + //should be only one item log + SelfLog.warn("SelfLog init success!!!"); + int nThreads = 30; + ExecutorService executor = new ThreadPoolExecutor(nThreads, nThreads, 0L, + TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>()); + CountDownLatch countDownLatch = new CountDownLatch(nThreads); + for (int i = 0; i < nThreads; i++) { + Runnable worker = new WorkerInitThread(this.spanEventDiskReporter, "" + i, + countDownLatch); + executor.execute(worker); + } + //noinspection ResultOfMethodCallIgnored + countDownLatch.await(3, TimeUnit.SECONDS); + // When there is no control for concurrent initialization, report span will get an error; + // when the repair method is initialized,other threads need to wait for initialization to complete. + List contents = FileUtils.readLines(tracerSelfLog()); + assertEquals("Actual concurrent init file size = " + contents.size(), 1, contents.size()); + } + + static class WorkerInitThread implements Runnable { + private final SpanEventDiskReporter reporter; + private final String command; + private final CountDownLatch countDownLatch; + + public WorkerInitThread(SpanEventDiskReporter reporter, String s, + CountDownLatch countDownLatch) { + this.command = s; + this.reporter = reporter; + this.countDownLatch = countDownLatch; + } + + @Override + public void run() { + processCommand(); + countDownLatch.countDown(); + } + + private void processCommand() { + SofaTracerSpan span = new SofaTracerSpan(mock(SofaTracer.class), + System.currentTimeMillis(), "open", SofaTracerSpanContext.rootStart(), null); + span.setTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER); + this.reporter.digestReport(span); + } + + @Override + public String toString() { + return this.command; + } + } +} \ No newline at end of file diff --git a/tracer-core/src/test/java/com/alipay/common/tracer/core/span/SofaTracerSpanTest.java b/tracer-core/src/test/java/com/alipay/common/tracer/core/span/SofaTracerSpanTest.java index 7d67bf03d..3339a28b1 100644 --- a/tracer-core/src/test/java/com/alipay/common/tracer/core/span/SofaTracerSpanTest.java +++ b/tracer-core/src/test/java/com/alipay/common/tracer/core/span/SofaTracerSpanTest.java @@ -22,8 +22,10 @@ import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; import com.alipay.common.tracer.core.generator.TraceIdGenerator; import com.alipay.common.tracer.core.reporter.digest.DiskReporterImpl; +import com.alipay.common.tracer.core.reporter.digest.event.SpanEventDiskReporter; import com.alipay.common.tracer.core.reporter.facade.Reporter; import com.alipay.common.tracer.core.tracertest.encoder.ClientSpanEncoder; +import com.alipay.common.tracer.core.tracertest.encoder.ClientSpanEventEncoder; import com.alipay.common.tracer.core.tracertest.encoder.ServerSpanEncoder; import com.alipay.common.tracer.core.utils.StringUtils; import com.google.common.collect.Lists; @@ -38,12 +40,15 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentLinkedQueue; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; /** * SofaTracerSpan Tester. @@ -54,9 +59,13 @@ */ public class SofaTracerSpanTest extends AbstractTestBase { - private final String clientLogType = "client-log-test.log"; + private final String clientLogType = "client-log-test.log"; - private final String serverLogType = "server-log-test.log"; + private final String clientEventType = "client-event-log-test.log"; + + private final String serverLogType = "server-log-test.log"; + + private final String serverEventType = "server-event-log-test.log"; private SofaTracer sofaTracer; @@ -68,10 +77,16 @@ public void setup() { Reporter serverReporter = new DiskReporterImpl(serverLogType, new ServerSpanEncoder()); + Reporter clientEventReporter = new SpanEventDiskReporter(clientEventType, "", "", + new ClientSpanEventEncoder(), null); + + Reporter serverEventReporter = new SpanEventDiskReporter(serverEventType, "", "", + new ClientSpanEventEncoder(), null); String tracerType = "SofaTracerSpanTest"; sofaTracer = new SofaTracer.Builder(tracerType) .withTag("tracer", "SofaTraceContextHolderTest").withClientReporter(clientReporter) - .withServerReporter(serverReporter).build(); + .withServerReporter(serverReporter).withClientEventReporter(clientEventReporter) + .withServerEventReporter(serverEventReporter).build(); sofaTracerSpan = (SofaTracerSpan) this.sofaTracer.buildSpan("SofaTracerSpanTest").start(); } @@ -216,6 +231,17 @@ public void testFinish() { 111 < span.getDurationMicroseconds() && span.getDurationMicroseconds() < endTime); } + @Test + public void testEvent() { + SofaTracerSpan span = (SofaTracerSpan) this.sofaTracer.buildSpan("testWithTimestamp") + .withStartTimestamp(111).start(); + SpanEventData spanEventData = new SpanEventData(); + spanEventData.setTimestamp(System.currentTimeMillis()); + spanEventData.getEventTagWithStr().put("tag.key", "value"); + span.addEvent(spanEventData); + span.setTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT); + } + /** * Method: close() */ diff --git a/tracer-core/src/test/java/com/alipay/common/tracer/core/tracertest/SofaTracerTest.java b/tracer-core/src/test/java/com/alipay/common/tracer/core/tracertest/SofaTracerTest.java index 4864f4f60..611f612e6 100644 --- a/tracer-core/src/test/java/com/alipay/common/tracer/core/tracertest/SofaTracerTest.java +++ b/tracer-core/src/test/java/com/alipay/common/tracer/core/tracertest/SofaTracerTest.java @@ -22,11 +22,14 @@ import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration; import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; import com.alipay.common.tracer.core.reporter.digest.DiskReporterImpl; +import com.alipay.common.tracer.core.reporter.digest.event.SpanEventDiskReporter; import com.alipay.common.tracer.core.reporter.facade.Reporter; import com.alipay.common.tracer.core.samplers.Sampler; import com.alipay.common.tracer.core.samplers.SofaTracerPercentageBasedSampler; import com.alipay.common.tracer.core.span.SofaTracerSpan; +import com.alipay.common.tracer.core.span.SpanEventData; import com.alipay.common.tracer.core.tracertest.encoder.ClientSpanEncoder; +import com.alipay.common.tracer.core.tracertest.encoder.ClientSpanEventEncoder; import com.alipay.common.tracer.core.tracertest.encoder.ServerSpanEncoder; import com.alipay.common.tracer.core.tracertest.type.TracerTestLogEnum; import com.alipay.common.tracer.core.utils.StringUtils; @@ -74,11 +77,22 @@ public void beforeInstance() { DiskReporterImpl clientReporter = new DiskReporterImpl( TracerTestLogEnum.RPC_CLIENT.getDefaultLogName(), new ClientSpanEncoder()); + SpanEventDiskReporter clientEventReporter = new SpanEventDiskReporter( + TracerTestLogEnum.RPC_CLIENT_EVENT.getDefaultLogName(), "", "", + new ClientSpanEventEncoder(), null); + //server DiskReporterImpl serverReporter = new DiskReporterImpl( TracerTestLogEnum.RPC_SERVER.getDefaultLogName(), new ServerSpanEncoder()); + + SpanEventDiskReporter serverEventReporter = new SpanEventDiskReporter( + TracerTestLogEnum.RPC_SERVER.getDefaultLogName(), "", "", new ClientSpanEventEncoder(), + null); + sofaTracer = new SofaTracer.Builder(tracerType).withTag("tracer", "tracerTest") .withClientReporter(clientReporter).withServerReporter(serverReporter) + .withClientEventReporter(clientEventReporter) + .withServerEventReporter(serverEventReporter) .withTag(tracerGlobalTagKey, tracerGlobalTagValue).build(); } @@ -132,6 +146,16 @@ public void testReportSpan() { SofaTracerSpan span = (SofaTracerSpan) this.sofaTracer.buildSpan("testInjectSpan") .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).start(); //Report Do not prohibit writing + SpanEventData spanEventData = new SpanEventData(); + spanEventData.setTimestamp(System.currentTimeMillis()); + spanEventData.getEventTagWithStr().put("kkk11", "vvv22"); + span.addEvent(spanEventData); + + SpanEventData spanEventData2 = new SpanEventData(); + spanEventData2.setTimestamp(System.currentTimeMillis()); + spanEventData2.getEventTagWithStr().put("kkk222", "vvv33"); + span.addEvent(spanEventData2); + span.finish(); TestUtil.periodicallyAssert(() -> { @@ -159,6 +183,10 @@ public void testIsDisableAllDigestLog() { SofaTracerConfiguration.DISABLE_MIDDLEWARE_DIGEST_LOG_KEY, "true"); SofaTracerSpan span = (SofaTracerSpan) this.sofaTracer.buildSpan("testInjectSpan") .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).start(); + SpanEventData spanEventData = new SpanEventData(); + spanEventData.getEventTagWithNumber().put("tag.num", 999); + spanEventData.getEventTagWithBool().put("tag.key", true); + span.addEvent(spanEventData); //report span.finish(); assertFalse(customFileLog(TracerTestLogEnum.RPC_CLIENT.getDefaultLogName()).exists()); @@ -178,6 +206,9 @@ public void testIsDisableClientDigestLog() { //create SofaTracerSpan span = (SofaTracerSpan) this.sofaTracer.buildSpan("testInjectSpan") .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).start(); + SpanEventData spanEventData = new SpanEventData(); + spanEventData.getEventTagWithStr().put("kkk", "vvv"); + span.addEvent(spanEventData); //report span.finish(); assertFalse(customFileLog(clientLogTypeName).exists()); @@ -195,6 +226,9 @@ public void testClose() { .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).start(); this.sofaTracer.close(); //report + SpanEventData spanEventData = new SpanEventData(); + spanEventData.getEventTagWithStr().put("kkk", "vvv"); + span.addEvent(spanEventData); span.finish(); String clientLogTypeName = TracerTestLogEnum.RPC_CLIENT.getDefaultLogName(); assertFalse(customFileLog(clientLogTypeName).exists()); diff --git a/tracer-core/src/test/java/com/alipay/common/tracer/core/tracertest/encoder/ClientSpanEventEncoder.java b/tracer-core/src/test/java/com/alipay/common/tracer/core/tracertest/encoder/ClientSpanEventEncoder.java new file mode 100644 index 000000000..7e7369fb5 --- /dev/null +++ b/tracer-core/src/test/java/com/alipay/common/tracer/core/tracertest/encoder/ClientSpanEventEncoder.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alipay.common.tracer.core.tracertest.encoder; + +import com.alipay.common.tracer.core.appender.builder.XStringBuilder; +import com.alipay.common.tracer.core.appender.encoder.SpanEncoder; +import com.alipay.common.tracer.core.appender.self.Timestamp; +import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; +import com.alipay.common.tracer.core.span.SofaTracerSpan; +import com.alipay.common.tracer.core.utils.StringUtils; + +import java.io.IOException; +import java.util.Map; + +/** + * The type Client span event encoder. + * + * @author yuqian + * @version : ClientSpanEventEncoder.java, v 0.1 2025-03-10 17:02 yuqian Exp $$ + */ +public class ClientSpanEventEncoder implements SpanEncoder { + + private XStringBuilder xsb = new XStringBuilder(); + + @Override + public String encode(SofaTracerSpan span) throws IOException { + SofaTracerSpanContext spanContext = span.getSofaTracerSpanContext(); + xsb.reset(); + // + xsb.append(Timestamp.format(span.getEventData().getTimestamp())); + //traceId + xsb.append(spanContext.getTraceId()); + //spanId + xsb.append(spanContext.getSpanId()); + //tags string + xsb.append(StringUtils.mapToString(span.getEventData().getEventTagWithStr())); + //tags bool + Map tagsBool = span.getEventData().getEventTagWithBool(); + StringBuilder tagsBoolBuild = new StringBuilder(); + for (Map.Entry entry : tagsBool.entrySet()) { + tagsBoolBuild.append(entry.getKey()).append(StringUtils.EQUAL) + .append(entry.getValue().toString()).append(StringUtils.AND); + } + xsb.append(tagsBoolBuild.toString()); + + //tags number + Map tagsNum = span.getEventData().getEventTagWithNumber(); + StringBuilder tagsNumBuild = new StringBuilder(); + for (Map.Entry entry : tagsNum.entrySet()) { + tagsNumBuild.append(entry.getKey()).append(StringUtils.EQUAL) + .append(entry.getValue().toString()).append(StringUtils.AND); + } + xsb.append(tagsNumBuild.toString()); + + //baggage + Map baggage = spanContext.getBizBaggage(); + xsb.appendEnd(StringUtils.mapToString(baggage)); + return xsb.toString(); + } +} \ No newline at end of file diff --git a/tracer-core/src/test/java/com/alipay/common/tracer/core/tracertest/type/TracerTestLogEnum.java b/tracer-core/src/test/java/com/alipay/common/tracer/core/tracertest/type/TracerTestLogEnum.java index 7a9838d12..b4ef27373 100644 --- a/tracer-core/src/test/java/com/alipay/common/tracer/core/tracertest/type/TracerTestLogEnum.java +++ b/tracer-core/src/test/java/com/alipay/common/tracer/core/tracertest/type/TracerTestLogEnum.java @@ -27,7 +27,10 @@ public enum TracerTestLogEnum { RPC_CLIENT("rpc_client_log_name", "rpc_client.log", "rpc_client_rolling"), RPC_SERVER( "rpc_server_log_name", "rpc_server.log", - "rpc_server_rolling"); + "rpc_server_rolling"), RPC_CLIENT_EVENT( + "rpc_client_event_log_name", + "rpc_client_event.log", + "rpc_client_event_rolling"), ; private String logReverseKey; diff --git a/tracer-extensions/pom.xml b/tracer-extensions/pom.xml index 56bad974f..faabb89f0 100644 --- a/tracer-extensions/pom.xml +++ b/tracer-extensions/pom.xml @@ -4,7 +4,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 tracer-extensions diff --git a/tracer-sofa-boot-starter/pom.xml b/tracer-sofa-boot-starter/pom.xml index 846e6c098..cccb14aaa 100644 --- a/tracer-sofa-boot-starter/pom.xml +++ b/tracer-sofa-boot-starter/pom.xml @@ -5,7 +5,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../pom.xml diff --git a/tracer-test/core-test/pom.xml b/tracer-test/core-test/pom.xml index 8b3c5c630..556b42a28 100644 --- a/tracer-test/core-test/pom.xml +++ b/tracer-test/core-test/pom.xml @@ -4,7 +4,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml diff --git a/tracer-test/log4j-test/pom.xml b/tracer-test/log4j-test/pom.xml index 5b4f57359..6f6437246 100644 --- a/tracer-test/log4j-test/pom.xml +++ b/tracer-test/log4j-test/pom.xml @@ -4,7 +4,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml diff --git a/tracer-test/log4j2-test/pom.xml b/tracer-test/log4j2-test/pom.xml index 7d812dd8a..000731896 100644 --- a/tracer-test/log4j2-test/pom.xml +++ b/tracer-test/log4j2-test/pom.xml @@ -4,7 +4,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml diff --git a/tracer-test/logback-test/pom.xml b/tracer-test/logback-test/pom.xml index 021395303..b3366aa25 100644 --- a/tracer-test/logback-test/pom.xml +++ b/tracer-test/logback-test/pom.xml @@ -4,7 +4,7 @@ tracer-all-parent com.alipay.sofa - 3.1.9 + 3.1.10 ../../pom.xml