Skip to content

Commit

Permalink
Enhancement sofatracer runnable (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
glmapper authored and QilongZhang committed Feb 22, 2019
1 parent f728346 commit 4179ead
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alipay.common.tracer.core.async;

import com.alipay.common.tracer.core.context.trace.SofaTraceContext;
import com.alipay.common.tracer.core.extensions.SpanExtensionFactory;
import com.alipay.common.tracer.core.holder.SofaTraceContextHolder;
import com.alipay.common.tracer.core.span.SofaTracerSpan;

Expand Down Expand Up @@ -58,6 +59,7 @@ public T call() throws Exception {
if (Thread.currentThread().getId() != tid) {
if (currentSpan != null) {
traceContext.push(currentSpan);
SpanExtensionFactory.logStartedSpan(currentSpan);
}
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alipay.common.tracer.core.async;

import com.alipay.common.tracer.core.context.trace.SofaTraceContext;
import com.alipay.common.tracer.core.extensions.SpanExtensionFactory;
import com.alipay.common.tracer.core.holder.SofaTraceContextHolder;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import java.lang.Runnable;
Expand Down Expand Up @@ -57,6 +58,7 @@ public void run() {
if (Thread.currentThread().getId() != tid) {
if (currentSpan != null) {
traceContext.push(currentSpan);
SpanExtensionFactory.logStartedSpan(currentSpan);
}
}
try {
Expand Down
58 changes: 58 additions & 0 deletions tracer-extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,64 @@
<artifactId>slf4j-api</artifactId>
</dependency>

<!-- test 依赖 -->
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit-coverage</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.extensions.log;

import com.alipay.common.tracer.core.SofaTracer;
import com.alipay.common.tracer.core.async.SofaTracerRunnable;
import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext;
import com.alipay.common.tracer.core.context.trace.SofaTraceContext;
import com.alipay.common.tracer.core.holder.SofaTraceContextHolder;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.common.tracer.extensions.log.constants.MDCKeyConstants;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.MDC;

import static org.mockito.Mockito.mock;

/**
* @author: guolei.sgl (guolei.sgl@antfin.com) 2019/2/21 11:41 AM
* @since:
**/
public class MDCSpanExtensionTest {

@Test
public void testMDC() {
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpanContext sofaTracerSpanContext = new SofaTracerSpanContext("12321", "0");
SofaTracer sofaTracer = mock((SofaTracer.class));
final SofaTracerSpan mockSpan = new SofaTracerSpan(sofaTracer, System.currentTimeMillis(),
"test", sofaTracerSpanContext, null);
sofaTraceContext.push(mockSpan);
final String traceId = sofaTracerSpanContext.getTraceId();
TestRunnable testRunnable = new TestRunnable(traceId, mockSpan);
Thread thread = new Thread(new SofaTracerRunnable(testRunnable));
thread.start();
}

class TestRunnable implements Runnable {

private String traceId;
SofaTracerSpan mockSpan;

public TestRunnable(String traceId, SofaTracerSpan mockSpan) {
this.traceId = traceId;
this.mockSpan = mockSpan;
}

@Override
public void run() {
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan currentSpan = sofaTraceContext.getCurrentSpan();
Assert.assertTrue(currentSpan != mockSpan);
String traceIdMdc = MDC.get(MDCKeyConstants.MDC_TRACEID);
Assert.assertTrue(traceId.equals(traceIdMdc));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.tracer.examples.slf4j.controller;

import com.alipay.common.tracer.core.async.SofaTracerRunnable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -52,7 +53,19 @@ public Map<String, Object> slf4j(@RequestParam(value = "name", defaultValue = "S
resultMap.put("success", true);
resultMap.put("id", counter.incrementAndGet());
resultMap.put("content", String.format(TEMPLATE, name));
logger.info("SOFATracer Print TraceId and SpanId");
long id = Thread.currentThread().getId();
logger.info("SOFATracer Print TraceId and SpanId ");

// Asynchronous thread transparent transmission
final SofaTracerRunnable sofaTracerRunnable = new SofaTracerRunnable(new Runnable() {
@Override
public void run() {
logger.info("SOFATracer Print TraceId and SpanId In Child Thread.");
}
});

Thread thread = new Thread(sofaTracerRunnable);
thread.start();
return resultMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,20 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
import com.alipay.common.tracer.core.reporter.digest.manager.SofaTracerDigestReporterAsyncManager;
import com.alipay.common.tracer.core.listener.SpanReportListenerHolder;
import com.alipay.common.tracer.core.samplers.SofaTracerPercentageBasedSampler;
import com.alipay.common.tracer.core.reporter.stat.manager.SofaTracerStatisticReporterCycleTimesManager;
import com.alipay.common.tracer.core.reporter.stat.manager.SofaTracerStatisticReporterManager;
import com.alipay.sofa.tracer.boot.TestUtil;
import com.alipay.sofa.tracer.boot.base.AbstractTestBase;
import com.alipay.sofa.tracer.boot.base.controller.SampleRestController;
import com.alipay.sofa.tracer.plugins.springmvc.SpringMvcLogEnum;
import com.alipay.sofa.tracer.plugins.springmvc.SpringMvcTracer;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.util.Assert;

import java.io.File;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -61,6 +58,9 @@ public void testSofaRestGet() throws Exception {
SofaTracerConfiguration.setProperty(
SofaTracerConfiguration.SAMPLER_STRATEGY_PERCENTAGE_KEY, "100");

// do not report to zipkin
SpanReportListenerHolder.clear();

assertNotNull(testRestTemplate);
String restUrl = urlHttpPrefix + "/greeting";
int countTimes = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@
package com.alipay.sofa.tracer.boot.springmvc;

import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
import com.alipay.common.tracer.core.reporter.digest.manager.SofaTracerDigestReporterAsyncManager;
import com.alipay.common.tracer.core.listener.SpanReportListenerHolder;
import com.alipay.common.tracer.core.samplers.SofaTracerPercentageBasedSampler;
import com.alipay.sofa.tracer.boot.TestUtil;
import com.alipay.sofa.tracer.boot.base.AbstractTestBase;
import com.alipay.sofa.tracer.boot.base.controller.SampleRestController;
import com.alipay.sofa.tracer.plugins.springmvc.SpringMvcLogEnum;
import com.alipay.sofa.tracer.plugins.springmvc.SpringMvcTracer;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;

import java.io.File;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;

Expand Down Expand Up @@ -61,6 +58,8 @@ public void testSofaRestGet() throws Exception {
SofaTracerPercentageBasedSampler.TYPE);
SofaTracerConfiguration.setProperty(
SofaTracerConfiguration.SAMPLER_STRATEGY_PERCENTAGE_KEY, "100");
// do not report to zipkin
SpanReportListenerHolder.clear();

assertNotNull(testRestTemplate);
String restUrl = urlHttpPrefix + "/greeting";
Expand Down

0 comments on commit 4179ead

Please sign in to comment.