From 93d487ad2d2ef8a49ac33a71dc27414f0c90c9e7 Mon Sep 17 00:00:00 2001 From: xzchaoo <70862045@qq.com> Date: Mon, 10 Feb 2020 14:53:43 +0800 Subject: [PATCH] Feature/support custom tracer logging path (#331) * support custom tracer logging path --- .gitignore | 1 + .../SpringMvcSofaTracerFilterTest.java | 2 +- .../core/appender/TracerLogRootDaemon.java | 22 +++++++++------ .../file/TimedRollingFileAppenderTest.java | 2 +- .../ZipkinSofaTracerAutoConfiguration.java | 2 +- .../ZipkinSofaTracerProperties.java | 5 +++- .../SpringMvcFilterJsonOutputTest.java | 27 ++++++++++--------- 7 files changed, 37 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 87f70279..919d43e7 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ nb-configuration.xml coverage-report logs *.log +pom.xml.versionsBackup diff --git a/sofa-tracer-plugins/sofa-tracer-springmvc-plugin/src/test/java/com/alipay/sofa/tracer/plugins/springmvc/SpringMvcSofaTracerFilterTest.java b/sofa-tracer-plugins/sofa-tracer-springmvc-plugin/src/test/java/com/alipay/sofa/tracer/plugins/springmvc/SpringMvcSofaTracerFilterTest.java index 84f904ce..d7b4dffa 100644 --- a/sofa-tracer-plugins/sofa-tracer-springmvc-plugin/src/test/java/com/alipay/sofa/tracer/plugins/springmvc/SpringMvcSofaTracerFilterTest.java +++ b/sofa-tracer-plugins/sofa-tracer-springmvc-plugin/src/test/java/com/alipay/sofa/tracer/plugins/springmvc/SpringMvcSofaTracerFilterTest.java @@ -115,7 +115,7 @@ public void destroy() { //wait for async output List result = FileUtils.readLines(new File(logDirectoryPath + File.separator + "tracer-self.log")); - Assert.assertTrue(result.size() == 1); + Assert.assertTrue(result.size() == 2); } private void checkFile() { diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/appender/TracerLogRootDaemon.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/appender/TracerLogRootDaemon.java index 80be8b77..f0285acd 100644 --- a/tracer-core/src/main/java/com/alipay/common/tracer/core/appender/TracerLogRootDaemon.java +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/appender/TracerLogRootDaemon.java @@ -16,17 +16,17 @@ */ package com.alipay.common.tracer.core.appender; +import java.io.File; +import java.nio.charset.Charset; + import com.alipay.common.tracer.core.appender.self.SelfLog; import com.alipay.common.tracer.core.appender.self.TracerDaemon; import com.alipay.common.tracer.core.utils.StringUtils; import com.alipay.common.tracer.core.utils.TracerUtils; -import java.io.File; -import java.nio.charset.Charset; - /** * TracerLogRootDaemon - * + *

* Not obtained from the configuration project, obtained directly from the system properties * * @author yangguanchao @@ -55,17 +55,22 @@ public class TracerLogRootDaemon { static { String loggingRoot = System.getProperty("loggingRoot"); + if (StringUtils.isBlank(loggingRoot)) { + loggingRoot = System.getProperty("SOFA_TRACER_LOGGING_PATH"); + } + if (StringUtils.isBlank(loggingRoot)) { + loggingRoot = System.getenv("SOFA_TRACER_LOGGING_PATH"); + } if (StringUtils.isBlank(loggingRoot)) { loggingRoot = System.getProperty("logging.path"); } - - String appendPidToLogPathString = System.getProperty(TRACER_APPEND_PID_TO_LOG_PATH_KEY); - boolean appendPidToLogPath = "true".equalsIgnoreCase(appendPidToLogPathString); - if (StringUtils.isBlank(loggingRoot)) { loggingRoot = System.getProperty("user.home") + File.separator + "logs"; } + String appendPidToLogPathString = System.getProperty(TRACER_APPEND_PID_TO_LOG_PATH_KEY); + boolean appendPidToLogPath = "true".equalsIgnoreCase(appendPidToLogPathString); + String tempLogFileDir = loggingRoot + File.separator + "tracelog"; if (appendPidToLogPath) { @@ -76,6 +81,7 @@ public class TracerLogRootDaemon { try { TracerDaemon.start(); + SelfLog.info("LOG_FILE_DIR is " + LOG_FILE_DIR); } catch (Throwable e) { SelfLog.error("Failed to start Tracer Daemon Thread", e); } diff --git a/tracer-core/src/test/java/com/alipay/common/tracer/core/appender/file/TimedRollingFileAppenderTest.java b/tracer-core/src/test/java/com/alipay/common/tracer/core/appender/file/TimedRollingFileAppenderTest.java index ef3c9705..9fb6f8cf 100644 --- a/tracer-core/src/test/java/com/alipay/common/tracer/core/appender/file/TimedRollingFileAppenderTest.java +++ b/tracer-core/src/test/java/com/alipay/common/tracer/core/appender/file/TimedRollingFileAppenderTest.java @@ -50,7 +50,7 @@ public void test() throws IOException, InterruptedException { timedRollingFileAppender.append(content); // wait to rolling - Thread.sleep(1100); + Thread.sleep(1500); timedRollingFileAppender.append(content); timedRollingFileAppender.flush(); diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/configuration/ZipkinSofaTracerAutoConfiguration.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/configuration/ZipkinSofaTracerAutoConfiguration.java index 8f667880..09f801e1 100644 --- a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/configuration/ZipkinSofaTracerAutoConfiguration.java +++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/configuration/ZipkinSofaTracerAutoConfiguration.java @@ -36,7 +36,7 @@ */ @Configuration @EnableConfigurationProperties(ZipkinSofaTracerProperties.class) -@ConditionalOnProperty(value = "com.alipay.sofa.tracer.zipkin.enabled", matchIfMissing = true) +@ConditionalOnProperty(value = "com.alipay.sofa.tracer.zipkin.enabled", matchIfMissing = false) @ConditionalOnClass({ zipkin2.Span.class, zipkin2.reporter.AsyncReporter.class, RestTemplate.class }) public class ZipkinSofaTracerAutoConfiguration { diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/properties/ZipkinSofaTracerProperties.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/properties/ZipkinSofaTracerProperties.java index 1aaaae85..52bf68bc 100644 --- a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/properties/ZipkinSofaTracerProperties.java +++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/properties/ZipkinSofaTracerProperties.java @@ -31,7 +31,10 @@ public class ZipkinSofaTracerProperties { * URL of the zipkin query server instance. */ private String baseUrl = "http://localhost:9411/"; - private boolean enabled = true; + /** + * zipkin reporter is disabled by default + */ + private boolean enabled = false; /** * When enabled, spans are gzipped before sent to the zipkin server */ diff --git a/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/springmvc/SpringMvcFilterJsonOutputTest.java b/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/springmvc/SpringMvcFilterJsonOutputTest.java index 13ffa2cf..6b8aff61 100644 --- a/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/springmvc/SpringMvcFilterJsonOutputTest.java +++ b/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/springmvc/SpringMvcFilterJsonOutputTest.java @@ -16,15 +16,9 @@ */ package com.alipay.sofa.tracer.boot.springmvc; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.TypeReference; -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.SpringBootWebApplication; -import com.alipay.sofa.tracer.boot.base.controller.SampleRestController; -import com.alipay.sofa.tracer.plugins.springmvc.SpringMvcLogEnum; +import java.util.List; +import java.util.Map; + import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Test; @@ -35,8 +29,15 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.util.Assert; -import java.util.List; -import java.util.Map; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.TypeReference; +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.SpringBootWebApplication; +import com.alipay.sofa.tracer.boot.base.controller.SampleRestController; +import com.alipay.sofa.tracer.plugins.springmvc.SpringMvcLogEnum; import static org.junit.Assert.*; @@ -101,7 +102,9 @@ public void testSofaRestGet() throws Exception { //wait for async output List statContents = FileUtils .readLines(customFileLog(SpringMvcLogEnum.SPRING_MVC_STAT.getDefaultLogName())); - assertEquals(2, statContents.size()); + + // 在很慢的机器上此处不能保证一定是2条, 但>=2 + assertTrue(statContents.size() >= 2); } private static Map parseToMap(String json, Class keyType, Class valueType) {