From edb9ae6ab9a1fb2141ae9d279142de178149eafc Mon Sep 17 00:00:00 2001 From: wujimin Date: Wed, 4 Apr 2018 16:24:23 +0800 Subject: [PATCH] SCB-462 cloud eye publisher switch to new mechanism --- .../pom.xml | 88 ------------- .../samples/mwf/Log4JMetricsFileWriter.java | 78 ------------ .../samples/mwf/ServiceApplication.java | 30 ----- .../samples/mwf/SimpleService.java | 35 ------ .../src/main/resources/microservice.yaml | 46 ------- .../pom.xml | 82 ------------- .../samples/mwf/Log4J2MetricsFileWriter.java | 104 ---------------- .../samples/mwf/SimpleService.java | 35 ------ .../src/main/resources/microservice.yaml | 46 ------- .../metrics-write-file/pom.xml | 32 +++-- .../samples/mwf/CloudEyeFilePublisher.java | 116 ++++++++++++++++++ .../samples/mwf/CloudEyeMetricModel.java | 93 ++++++++++++++ .../samples/mwf/CloudEyeModel.java} | 27 ++-- .../samples/mwf/FileContentConvertor.java | 25 ---- .../samples/mwf/FileContentFormatter.java | 24 ---- .../samples/mwf/MetricsBootListener.java | 40 ------ .../samples/mwf/MetricsFileWriter.java | 22 ---- .../mwf/SimpleFileContentConvertor.java | 86 ------------- .../mwf/SimpleFileContentFormatter.java | 86 ------------- .../samples/mwf/WriteFileInitializer.java | 94 -------------- ...comb.foundation.metrics.MetricsInitializer | 18 +++ .../src/main/resources/sample-log4j2.xml | 48 ++++++++ .../src/main/resources/sample-logback.xml | 52 ++++++++ samples/metrics-write-file-sample/pom.xml | 7 -- 24 files changed, 359 insertions(+), 955 deletions(-) delete mode 100644 samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/pom.xml delete mode 100644 samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/src/main/java/org/apache/servicecomb/samples/mwf/Log4JMetricsFileWriter.java delete mode 100644 samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/src/main/java/org/apache/servicecomb/samples/mwf/ServiceApplication.java delete mode 100644 samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/src/main/java/org/apache/servicecomb/samples/mwf/SimpleService.java delete mode 100644 samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/src/main/resources/microservice.yaml delete mode 100644 samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/pom.xml delete mode 100644 samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/src/main/java/org/apache/servicecomb/samples/mwf/Log4J2MetricsFileWriter.java delete mode 100644 samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/src/main/java/org/apache/servicecomb/samples/mwf/SimpleService.java delete mode 100644 samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/src/main/resources/microservice.yaml create mode 100644 samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/CloudEyeFilePublisher.java create mode 100644 samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/CloudEyeMetricModel.java rename samples/metrics-write-file-sample/{metrics-write-file-log4j2-springboot/src/main/java/org/apache/servicecomb/samples/mwf/ServiceApplication.java => metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/CloudEyeModel.java} (68%) delete mode 100644 samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/FileContentConvertor.java delete mode 100644 samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/FileContentFormatter.java delete mode 100644 samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/MetricsBootListener.java delete mode 100644 samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/MetricsFileWriter.java delete mode 100644 samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/SimpleFileContentConvertor.java delete mode 100644 samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/SimpleFileContentFormatter.java delete mode 100644 samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/WriteFileInitializer.java create mode 100644 samples/metrics-write-file-sample/metrics-write-file/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.metrics.MetricsInitializer create mode 100644 samples/metrics-write-file-sample/metrics-write-file/src/main/resources/sample-log4j2.xml create mode 100644 samples/metrics-write-file-sample/metrics-write-file/src/main/resources/sample-logback.xml diff --git a/samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/pom.xml b/samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/pom.xml deleted file mode 100644 index 806d3b4e12a..00000000000 --- a/samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/pom.xml +++ /dev/null @@ -1,88 +0,0 @@ - - - - - - metrics-write-file-sample - org.apache.servicecomb.samples - 1.0.0-m2-SNAPSHOT - - 4.0.0 - - metrics-write-file-log4j-springboot - - - - - - org.springframework.boot - spring-boot-starter - - - org.slf4j - log4j-over-slf4j - - - - - - org.apache.servicecomb - spring-boot-starter-provider - - - - org.apache.servicecomb - handler-flowcontrol-qps - - - org.apache.servicecomb - handler-bizkeeper - - - org.apache.servicecomb - handler-tracing-zipkin - - - org.hibernate - hibernate-validator - - - - log4j - log4j - - - org.apache.servicecomb.samples - metrics-write-file - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - \ No newline at end of file diff --git a/samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/src/main/java/org/apache/servicecomb/samples/mwf/Log4JMetricsFileWriter.java b/samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/src/main/java/org/apache/servicecomb/samples/mwf/Log4JMetricsFileWriter.java deleted file mode 100644 index cbe417dd44a..00000000000 --- a/samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/src/main/java/org/apache/servicecomb/samples/mwf/Log4JMetricsFileWriter.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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 org.apache.servicecomb.samples.mwf; - -import java.nio.file.Paths; -import java.util.Map; - -import org.apache.log4j.Level; -import org.apache.log4j.Logger; -import org.apache.log4j.PatternLayout; -import org.apache.log4j.RollingFileAppender; -import org.apache.log4j.spi.LoggingEvent; -import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx; -import org.springframework.stereotype.Component; - -import com.netflix.config.DynamicPropertyFactory; - -@Component -public class Log4JMetricsFileWriter implements MetricsFileWriter { - private static final String METRICS_FILE_ROLLING_MAX_FILE_COUNT = "servicecomb.metrics.file.rolling.max_file_count"; - - private static final String METRICS_FILE_ROLLING_MAX_FILE_SIZE = "servicecomb.metrics.file.rolling.max_file_size"; - - private static final String METRICS_FILE_ROOT_PATH = "servicecomb.metrics.file.root_path"; - - - private final Map fileAppenders = new ConcurrentHashMapEx<>(); - - private final int maxFileCount; - - private final String maxFileSize; - - private final String rootPath; - - public Log4JMetricsFileWriter() { - maxFileCount = DynamicPropertyFactory.getInstance().getIntProperty(METRICS_FILE_ROLLING_MAX_FILE_COUNT, 10).get(); - maxFileSize = DynamicPropertyFactory.getInstance() - .getStringProperty(METRICS_FILE_ROLLING_MAX_FILE_SIZE, "10MB") - .get(); - rootPath = DynamicPropertyFactory.getInstance().getStringProperty(METRICS_FILE_ROOT_PATH, "target").get(); - } - - @Override - public void write(String loggerName, String filePrefix, String content) { - RollingFileAppender logger = fileAppenders.computeIfAbsent(loggerName, f -> initLogger(loggerName, filePrefix)); - LoggingEvent event = new LoggingEvent(loggerName, Logger.getLogger(loggerName), Level.ALL, - content, null); - logger.append(event); - } - - private RollingFileAppender initLogger(String loggerName, String filePrefix) { - String fileName = Paths.get(rootPath, filePrefix + "." + loggerName + ".dat").toString(); - RollingFileAppender fileAppender = new RollingFileAppender(); - fileAppender.setName(loggerName); - fileAppender.setFile(fileName); - fileAppender.setLayout(new PatternLayout("%m%n")); - fileAppender.setAppend(true); - fileAppender.setMaxFileSize(maxFileSize); - fileAppender.setMaxBackupIndex(maxFileCount); - fileAppender.activateOptions(); - return fileAppender; - } -} diff --git a/samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/src/main/java/org/apache/servicecomb/samples/mwf/ServiceApplication.java b/samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/src/main/java/org/apache/servicecomb/samples/mwf/ServiceApplication.java deleted file mode 100644 index 6995e381b4c..00000000000 --- a/samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/src/main/java/org/apache/servicecomb/samples/mwf/ServiceApplication.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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 org.apache.servicecomb.samples.mwf; - -import org.apache.servicecomb.springboot.starter.provider.EnableServiceComb; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -@EnableServiceComb -public class ServiceApplication { - public static void main(String[] args) { - SpringApplication.run(ServiceApplication.class, args); - } -} diff --git a/samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/src/main/java/org/apache/servicecomb/samples/mwf/SimpleService.java b/samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/src/main/java/org/apache/servicecomb/samples/mwf/SimpleService.java deleted file mode 100644 index 094df93fe82..00000000000 --- a/samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/src/main/java/org/apache/servicecomb/samples/mwf/SimpleService.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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 org.apache.servicecomb.samples.mwf; - -import java.util.UUID; - -import org.apache.servicecomb.provider.rest.common.RestSchema; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; - -//simple service sim -@RestSchema(schemaId = "demoServiceEndpoint") -@RequestMapping(path = "/") -public class SimpleService { - - @GetMapping(path = "/f") - public String fun() { - return UUID.randomUUID().toString(); - } -} diff --git a/samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/src/main/resources/microservice.yaml b/samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/src/main/resources/microservice.yaml deleted file mode 100644 index 748ef4d851b..00000000000 --- a/samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/src/main/resources/microservice.yaml +++ /dev/null @@ -1,46 +0,0 @@ -## --------------------------------------------------------------------------- -## 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. -## --------------------------------------------------------------------------- - -APPLICATION_ID: mwf -service_description: - name: mwf-log4j2-springboot - version: 0.0.1 -cse: - service: - registry: - address: http://127.0.0.1:30100 - rest: - address: 0.0.0.0:8080 - highway: - address: 0.0.0.0:7070 - handler: - chain: - Provider: - default: bizkeeper-provider - -servicecomb: - metrics: - #metrics double value round places,default value is 1 - round_places: 1 - file: - root_path: ./samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/target/metric/ - rolling: - max_file_count: 10 - max_file_size : 10MB - - #output time,milliseconds - window_time: 5000 diff --git a/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/pom.xml b/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/pom.xml deleted file mode 100644 index 282bbbd4d08..00000000000 --- a/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/pom.xml +++ /dev/null @@ -1,82 +0,0 @@ - - - - - - metrics-write-file-sample - org.apache.servicecomb.samples - 1.0.0-m2-SNAPSHOT - - 4.0.0 - - metrics-write-file-log4j2-springboot - Java Chassis::Metrics::Sample::Metrics::WriteFile::Log4j2 - - - - org.springframework.boot - spring-boot-starter - - - - org.apache.servicecomb - spring-boot-starter-provider - - - - org.apache.servicecomb - handler-flowcontrol-qps - - - org.apache.servicecomb - handler-bizkeeper - - - org.apache.servicecomb - handler-tracing-zipkin - - - org.hibernate - hibernate-validator - - - - org.apache.logging.log4j - log4j-api - - - org.apache.logging.log4j - log4j-core - - - org.apache.servicecomb.samples - metrics-write-file - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - diff --git a/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/src/main/java/org/apache/servicecomb/samples/mwf/Log4J2MetricsFileWriter.java b/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/src/main/java/org/apache/servicecomb/samples/mwf/Log4J2MetricsFileWriter.java deleted file mode 100644 index 737cc4716f3..00000000000 --- a/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/src/main/java/org/apache/servicecomb/samples/mwf/Log4J2MetricsFileWriter.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * 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 org.apache.servicecomb.samples.mwf; - -import java.nio.file.Paths; -import java.util.Map; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.core.LoggerContext; -import org.apache.logging.log4j.core.appender.RollingFileAppender; -import org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy; -import org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy; -import org.apache.logging.log4j.core.config.Configuration; -import org.apache.logging.log4j.core.impl.Log4jLogEvent; -import org.apache.logging.log4j.core.layout.PatternLayout; -import org.apache.logging.log4j.message.SimpleMessage; -import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx; -import org.springframework.stereotype.Component; - -import com.netflix.config.DynamicPropertyFactory; - -@Component -public class Log4J2MetricsFileWriter implements MetricsFileWriter { - private static final String METRICS_FILE_ROLLING_MAX_FILE_COUNT = "servicecomb.metrics.file.rolling.max_file_count"; - - private static final String METRICS_FILE_ROLLING_MAX_FILE_SIZE = "servicecomb.metrics.file.rolling.max_file_size"; - - private static final String METRICS_FILE_ROOT_PATH = "servicecomb.metrics.file.root_path"; - - private final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); - - private final Configuration config = ctx.getConfiguration(); - - private final Map fileAppenders = new ConcurrentHashMapEx<>(); - - private final int maxFileCount; - - private final String maxFileSize; - - private final String rootPath; - - public Log4J2MetricsFileWriter() { - maxFileCount = DynamicPropertyFactory.getInstance().getIntProperty(METRICS_FILE_ROLLING_MAX_FILE_COUNT, 10).get(); - maxFileSize = DynamicPropertyFactory.getInstance() - .getStringProperty(METRICS_FILE_ROLLING_MAX_FILE_SIZE, "10MB") - .get(); - rootPath = DynamicPropertyFactory.getInstance().getStringProperty(METRICS_FILE_ROOT_PATH, "target").get(); - } - - @Override - public void write(String loggerName, String filePrefix, String content) { - RollingFileAppender logger = fileAppenders.computeIfAbsent(loggerName, f -> initLogger(loggerName, filePrefix)); - logger.append(Log4jLogEvent.newBuilder().setMessage(new SimpleMessage(content)).build()); - } - - @SuppressWarnings("deprecation") - private RollingFileAppender initLogger(String loggerName, String filePrefix) { - String fileName = Paths.get(rootPath, filePrefix + "." + loggerName + ".dat").toString(); - String filePattern = Paths.get(rootPath, filePrefix + "." + loggerName + "-%i.dat").toString(); - - PatternLayout layout = PatternLayout.newBuilder().withPattern(PatternLayout.DEFAULT_CONVERSION_PATTERN).build(); - SizeBasedTriggeringPolicy policy = SizeBasedTriggeringPolicy.createPolicy(maxFileSize); - DefaultRolloverStrategy strategy = DefaultRolloverStrategy.createStrategy(String.valueOf(maxFileCount), - null, - null, - null, - null, - false, - config); - - //TODO:use RollingFileAppender.newBuilder throw No such static method exception,will improve later! - return RollingFileAppender - .createAppender(fileName, - filePattern, - "true", - loggerName, - "true", - null, - "true", - policy, - strategy, - layout, - null, - null, - null, - null, - config); - } -} diff --git a/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/src/main/java/org/apache/servicecomb/samples/mwf/SimpleService.java b/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/src/main/java/org/apache/servicecomb/samples/mwf/SimpleService.java deleted file mode 100644 index 094df93fe82..00000000000 --- a/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/src/main/java/org/apache/servicecomb/samples/mwf/SimpleService.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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 org.apache.servicecomb.samples.mwf; - -import java.util.UUID; - -import org.apache.servicecomb.provider.rest.common.RestSchema; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; - -//simple service sim -@RestSchema(schemaId = "demoServiceEndpoint") -@RequestMapping(path = "/") -public class SimpleService { - - @GetMapping(path = "/f") - public String fun() { - return UUID.randomUUID().toString(); - } -} diff --git a/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/src/main/resources/microservice.yaml b/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/src/main/resources/microservice.yaml deleted file mode 100644 index e21b21ad584..00000000000 --- a/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/src/main/resources/microservice.yaml +++ /dev/null @@ -1,46 +0,0 @@ -## --------------------------------------------------------------------------- -## 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. -## --------------------------------------------------------------------------- - -APPLICATION_ID: mwf -service_description: - name: mwf-log4j-springboot - version: 0.0.1 -cse: - service: - registry: - address: http://127.0.0.1:30100 - rest: - address: 0.0.0.0:8080 - highway: - address: 0.0.0.0:7070 - handler: - chain: - Provider: - default: bizkeeper-provider - -servicecomb: - metrics: - #metrics double value round places,default value is 1 - round_places: 1 - file: - root_path: ./samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/target/metric/ - rolling: - max_file_count: 10 - max_file_size : 10MB - - #output time,milliseconds - window_time: 5000 diff --git a/samples/metrics-write-file-sample/metrics-write-file/pom.xml b/samples/metrics-write-file-sample/metrics-write-file/pom.xml index f4951a320a6..54ff6eee853 100644 --- a/samples/metrics-write-file-sample/metrics-write-file/pom.xml +++ b/samples/metrics-write-file-sample/metrics-write-file/pom.xml @@ -16,8 +16,7 @@ ~ limitations under the License. --> - metrics-write-file-sample @@ -35,22 +34,19 @@ metrics-core - - junit - junit - test - - - org.jmockit - jmockit - test - - - org.mockito - mockito-core - test - - + + + + + + + + + + + + + diff --git a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/CloudEyeFilePublisher.java b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/CloudEyeFilePublisher.java new file mode 100644 index 00000000000..acda8a6d775 --- /dev/null +++ b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/CloudEyeFilePublisher.java @@ -0,0 +1,116 @@ +/* + * 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 org.apache.servicecomb.samples.mwf; + +import org.apache.commons.lang3.StringUtils; +import org.apache.servicecomb.foundation.common.net.NetUtils; +import org.apache.servicecomb.foundation.common.utils.JsonUtils; +import org.apache.servicecomb.foundation.metrics.MetricsBootstrapConfig; +import org.apache.servicecomb.foundation.metrics.MetricsInitializer; +import org.apache.servicecomb.foundation.metrics.PolledEvent; +import org.apache.servicecomb.serviceregistry.RegistryUtils; +import org.apache.servicecomb.serviceregistry.api.registry.Microservice; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.google.common.eventbus.EventBus; +import com.google.common.eventbus.Subscribe; +import com.netflix.config.DynamicPropertyFactory; +import com.netflix.spectator.api.CompositeRegistry; +import com.netflix.spectator.api.Measurement; +import com.netflix.spectator.api.Meter; +import com.netflix.spectator.api.Tag; + +/** + * a sample for cloud eye + * + * related product only used logback or log4j2 + * both logback and log4j2 support write to different log file depend on different MDC value + * + * reference sample-logback.xml and sample-log4j2.xml + */ +public class CloudEyeFilePublisher implements MetricsInitializer { + private static final Logger LOGGER = LoggerFactory.getLogger(CloudEyeFilePublisher.class); + + private static final Logger CLOUD_EYE_LOGGER = LoggerFactory.getLogger("cloudEyeLogger"); + + private String filePrefix; + + private String hostName; + + @Override + public void init(CompositeRegistry globalRegistry, EventBus eventBus, MetricsBootstrapConfig config) { + eventBus.register(this); + + Microservice microservice = RegistryUtils.getMicroservice(); + filePrefix = microservice.getAppId() + "." + microservice.getServiceName(); + + hostName = NetUtils.getHostName(); + if (StringUtils.isEmpty(hostName)) { + hostName = NetUtils.getHostAddress(); + } + + System.setProperty("cloudEye.logDir", + DynamicPropertyFactory + .getInstance() + .getStringProperty("cloudEye.logDir", "logs") + .get()); + } + + @Subscribe + public void onPolledEvent(PolledEvent event) { + long now = System.currentTimeMillis(); + for (Meter meter : event.getMeters()) { + for (Measurement measurement : meter.measure()) { + logMeasurement(measurement, now); + } + } + } + + protected void logMeasurement(Measurement measurement, long now) { + String metricKey = generateMetricKey(measurement); + + MDC.put("fileName", filePrefix + "." + metricKey + ".dat"); + + CloudEyeMetricModel metricModel = new CloudEyeMetricModel(); + metricModel.setNode(hostName); + metricModel.setTimestamp(now); + metricModel.getDynamicValue().put(metricKey, String.valueOf(measurement.value())); + + CloudEyeModel model = new CloudEyeModel(); + model.setPlugin_id(filePrefix); + model.setMetric(metricModel); + + try { + CLOUD_EYE_LOGGER.info(JsonUtils.writeValueAsString(model)); + } catch (JsonProcessingException e) { + LOGGER.error("Failed to write cloud eye log.", e); + } + } + + protected String generateMetricKey(Measurement measurement) { + StringBuilder sb = new StringBuilder(); + sb.append(measurement.id().name()); + for (Tag tag : measurement.id().tags()) { + sb.append('.').append(tag.value()); + } + return sb.toString(); + } +} diff --git a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/CloudEyeMetricModel.java b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/CloudEyeMetricModel.java new file mode 100644 index 00000000000..96188c9f8ad --- /dev/null +++ b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/CloudEyeMetricModel.java @@ -0,0 +1,93 @@ +/* + * 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 org.apache.servicecomb.samples.mwf; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.apache.servicecomb.foundation.common.utils.JsonUtils; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; + +public class CloudEyeMetricModel { + private String node; + + private String scope_name; + + private long timestamp; + + private String inface_name; + + @JsonAnySetter + private Map dynamicValue = new HashMap<>(); + + public String getNode() { + return node; + } + + public void setNode(String node) { + this.node = node; + } + + public String getScope_name() { + return scope_name; + } + + public void setScope_name(String scope_name) { + this.scope_name = scope_name; + } + + public long getTimestamp() { + return timestamp; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } + + public String getInface_name() { + return inface_name; + } + + public void setInface_name(String inface_name) { + this.inface_name = inface_name; + } + + @JsonAnyGetter + public Map getDynamicValue() { + return dynamicValue; + } + + public void setDynamicValue(Map dynamicValue) { + this.dynamicValue = dynamicValue; + } + + public static void main(String[] args) throws IOException { + CloudEyeMetricModel model = new CloudEyeMetricModel(); + model.setNode("node"); + model.getDynamicValue().put("a", "v"); + + String s = JsonUtils.writeValueAsString(model); + System.out.println(s); + + CloudEyeMetricModel m1 = JsonUtils.OBJ_MAPPER.readValue(s, CloudEyeMetricModel.class); + + System.out.println(m1.getDynamicValue()); + } +} diff --git a/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/src/main/java/org/apache/servicecomb/samples/mwf/ServiceApplication.java b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/CloudEyeModel.java similarity index 68% rename from samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/src/main/java/org/apache/servicecomb/samples/mwf/ServiceApplication.java rename to samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/CloudEyeModel.java index 6995e381b4c..f91acfcc940 100644 --- a/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/src/main/java/org/apache/servicecomb/samples/mwf/ServiceApplication.java +++ b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/CloudEyeModel.java @@ -14,17 +14,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.servicecomb.samples.mwf; -import org.apache.servicecomb.springboot.starter.provider.EnableServiceComb; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; +public class CloudEyeModel { + private String plugin_id; + + private CloudEyeMetricModel metric; + + public String getPlugin_id() { + return plugin_id; + } + + public void setPlugin_id(String plugin_id) { + this.plugin_id = plugin_id; + } + + public CloudEyeMetricModel getMetric() { + return metric; + } -@SpringBootApplication -@EnableServiceComb -public class ServiceApplication { - public static void main(String[] args) { - SpringApplication.run(ServiceApplication.class, args); + public void setMetric(CloudEyeMetricModel metric) { + this.metric = metric; } } diff --git a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/FileContentConvertor.java b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/FileContentConvertor.java deleted file mode 100644 index dc13f9a0786..00000000000 --- a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/FileContentConvertor.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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 org.apache.servicecomb.samples.mwf; - -import java.util.Map; - -//convert metrics to output content -public interface FileContentConvertor { - Map convert(Map registryMetric); -} diff --git a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/FileContentFormatter.java b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/FileContentFormatter.java deleted file mode 100644 index f93198a9844..00000000000 --- a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/FileContentFormatter.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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 org.apache.servicecomb.samples.mwf; - -import java.util.Map; - -public interface FileContentFormatter { - Map format(Map input); -} diff --git a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/MetricsBootListener.java b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/MetricsBootListener.java deleted file mode 100644 index 3c4d7cc3c62..00000000000 --- a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/MetricsBootListener.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 org.apache.servicecomb.samples.mwf; - -import org.apache.servicecomb.core.BootListener; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class MetricsBootListener implements BootListener { - - private final WriteFileInitializer initializer; - - @Autowired - public MetricsBootListener(WriteFileInitializer initializer) { - this.initializer = initializer; - } - - @Override - public void onBootEvent(BootEvent event) { - if (EventType.BEFORE_REGISTRY.equals(event.getEventType())) { - this.initializer.startOutput(); - } - } -} diff --git a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/MetricsFileWriter.java b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/MetricsFileWriter.java deleted file mode 100644 index 313d9197645..00000000000 --- a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/MetricsFileWriter.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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 org.apache.servicecomb.samples.mwf; - -public interface MetricsFileWriter { - void write(String loggerName, String filePrefix, String content); -} diff --git a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/SimpleFileContentConvertor.java b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/SimpleFileContentConvertor.java deleted file mode 100644 index c3f3d59ce53..00000000000 --- a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/SimpleFileContentConvertor.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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 org.apache.servicecomb.samples.mwf; - -import java.math.BigDecimal; -import java.math.RoundingMode; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; - -import org.apache.servicecomb.foundation.metrics.MetricsConst; - -import com.netflix.config.DynamicPropertyFactory; - -public class SimpleFileContentConvertor implements FileContentConvertor { - - private static final String METRICS_ROUND_PLACES = "servicecomb.metrics.round_places"; - - private final int doubleRoundPlaces; - - private final String doubleStringFormatter; - - public SimpleFileContentConvertor() { - doubleRoundPlaces = DynamicPropertyFactory.getInstance().getIntProperty(METRICS_ROUND_PLACES, 1).get(); - doubleStringFormatter = "%." + String.valueOf(doubleRoundPlaces) + "f"; - } - - @Override - public Map convert(Map registryMetric) { - Map pickedMetrics = new HashMap<>(); - for (Entry metric : registryMetric.entrySet()) { - pickedMetrics.put(convertMetricKey(metric.getKey()), - String.format(doubleStringFormatter, round(metric.getValue(), doubleRoundPlaces))); - } - return pickedMetrics; - } - - private double round(double value, int places) { - if (!Double.isNaN(value)) { - BigDecimal decimal = new BigDecimal(value); - return decimal.setScale(places, RoundingMode.HALF_UP).doubleValue(); - } - return 0; - } - - private String convertMetricKey(String key) { - String[] nameAndTag = key.split("\\("); - Map tags = new HashMap<>(); - String[] tagAnValues = nameAndTag[1].split("[=,)]"); - for (int i = 0; i < tagAnValues.length; i += 2) { - tags.put(tagAnValues[i], tagAnValues[i + 1]); - } - if (nameAndTag[0].startsWith(MetricsConst.JVM)) { - return "jvm." + tags.get(MetricsConst.TAG_NAME); - } else { - StringBuilder builder = new StringBuilder(); - builder.append(tags.get(MetricsConst.TAG_OPERATION)); - builder.append("."); - builder.append(tags.get(MetricsConst.TAG_ROLE).toLowerCase()); - builder.append("."); - builder.append(tags.get(MetricsConst.TAG_STAGE)); - builder.append("."); - builder.append(tags.get(MetricsConst.TAG_STATISTIC)); - if (tags.containsKey(MetricsConst.TAG_STATUS)) { - builder.append("."); - builder.append(tags.get(MetricsConst.TAG_STATUS)); - } - return builder.toString(); - } - } -} diff --git a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/SimpleFileContentFormatter.java b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/SimpleFileContentFormatter.java deleted file mode 100644 index 91a55e879fa..00000000000 --- a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/SimpleFileContentFormatter.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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 org.apache.servicecomb.samples.mwf; - -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.stream.Collectors; - -import org.apache.servicecomb.foundation.common.utils.JsonUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.fasterxml.jackson.core.JsonProcessingException; - -public class SimpleFileContentFormatter implements FileContentFormatter { - private static final Logger logger = LoggerFactory.getLogger(SimpleFileContentFormatter.class); - - private final String applicationName; - - private final String hostName; - - public SimpleFileContentFormatter(String hostName, String applicationName) { - this.hostName = hostName; - this.applicationName = applicationName; - } - - @Override - public Map format(Map input) { - return input.entrySet() - .stream() - .collect(Collectors.toMap(Entry::getKey, entry -> { - try { - return JsonUtils.writeValueAsString( - new OutputJsonObject(applicationName, hostName, entry.getKey(), entry.getValue())); - } catch (JsonProcessingException e) { - logger.error("error format metrics data", e); - return ""; - } - })); - } - - - class OutputJsonObject { - private String plugin_id; - - private Map metric; - - public String getPlugin_id() { - return plugin_id; - } - - public Map getMetric() { - return metric; - } - - public OutputJsonObject() { - } - - public OutputJsonObject(String plugin_id, String hostName, String metricName, String metricValue) { - this(); - this.plugin_id = plugin_id; - this.metric = new HashMap<>(); - this.metric.put("node", hostName); - this.metric.put("scope_name", ""); - this.metric.put("timestamp", System.currentTimeMillis()); - this.metric.put("inface_name", ""); - this.metric.put(metricName, metricValue); - } - } -} diff --git a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/WriteFileInitializer.java b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/WriteFileInitializer.java deleted file mode 100644 index d5486b8d368..00000000000 --- a/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/WriteFileInitializer.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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 org.apache.servicecomb.samples.mwf; - -import static java.util.concurrent.TimeUnit.MILLISECONDS; - -import java.util.Map; -import java.util.concurrent.Executors; - -import org.apache.commons.lang3.StringUtils; -import org.apache.servicecomb.foundation.common.net.NetUtils; -import org.apache.servicecomb.metrics.core.MetricsConfig; -import org.apache.servicecomb.metrics.core.MonitorManager; -import org.apache.servicecomb.serviceregistry.RegistryUtils; -import org.apache.servicecomb.serviceregistry.api.registry.Microservice; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import com.netflix.config.DynamicPropertyFactory; - -@Component -public class WriteFileInitializer { - private final int metricPoll; - - private FileContentConvertor convertor; - - private FileContentFormatter formatter; - - private final MetricsFileWriter fileWriter; - - private String filePrefix; - - private String hostName; - - @Autowired - public WriteFileInitializer(MetricsFileWriter fileWriter) { - metricPoll = DynamicPropertyFactory.getInstance().getIntProperty(MetricsConfig.METRICS_WINDOW_TIME, 5000).get(); - this.fileWriter = fileWriter; - this.convertor = new SimpleFileContentConvertor(); - } - - public WriteFileInitializer(MetricsFileWriter fileWriter, String hostName, String filePrefix) { - metricPoll = DynamicPropertyFactory.getInstance().getIntProperty(MetricsConfig.METRICS_WINDOW_TIME, 5000).get(); - this.fileWriter = fileWriter; - this.hostName = hostName; - this.filePrefix = filePrefix; - this.convertor = new SimpleFileContentConvertor(); - this.formatter = new SimpleFileContentFormatter(hostName, filePrefix); - } - - public void startOutput() { - if (StringUtils.isEmpty(filePrefix)) { - Microservice microservice = RegistryUtils.getMicroservice(); - filePrefix = microservice.getAppId() + "." + microservice.getServiceName(); - } - if (StringUtils.isEmpty(hostName)) { - hostName = NetUtils.getHostName(); - if (StringUtils.isEmpty(hostName)) { - hostName = NetUtils.getHostAddress(); - } - } - - formatter = new SimpleFileContentFormatter(hostName, filePrefix); - - final Runnable poller = this::run; - Executors.newScheduledThreadPool(1) - .scheduleWithFixedDelay(poller, 0, metricPoll, MILLISECONDS); - } - - private void run() { - Map metrics = MonitorManager.getInstance().measure(); - Map convertedMetrics = convertor.convert(metrics); - Map formattedMetrics = formatter.format(convertedMetrics); - - for (String metricName : formattedMetrics.keySet()) { - fileWriter.write(metricName, filePrefix, formattedMetrics.get(metricName)); - } - } -} diff --git a/samples/metrics-write-file-sample/metrics-write-file/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.metrics.MetricsInitializer b/samples/metrics-write-file-sample/metrics-write-file/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.metrics.MetricsInitializer new file mode 100644 index 00000000000..3a6ad61ea7c --- /dev/null +++ b/samples/metrics-write-file-sample/metrics-write-file/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.metrics.MetricsInitializer @@ -0,0 +1,18 @@ +# +# 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. +# + +org.apache.servicecomb.samples.mwf.CloudEyeFilePublisher \ No newline at end of file diff --git a/samples/metrics-write-file-sample/metrics-write-file/src/main/resources/sample-log4j2.xml b/samples/metrics-write-file-sample/metrics-write-file/src/main/resources/sample-log4j2.xml new file mode 100644 index 00000000000..99ed192d4f5 --- /dev/null +++ b/samples/metrics-write-file-sample/metrics-write-file/src/main/resources/sample-log4j2.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + %msg%n + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/metrics-write-file-sample/metrics-write-file/src/main/resources/sample-logback.xml b/samples/metrics-write-file-sample/metrics-write-file/src/main/resources/sample-logback.xml new file mode 100644 index 00000000000..ee574146d26 --- /dev/null +++ b/samples/metrics-write-file-sample/metrics-write-file/src/main/resources/sample-logback.xml @@ -0,0 +1,52 @@ + + + + + + + + %d [%level] [%thread] - %msg (%F:%L\)%n + + + + + + + + + + + fileName + unknown + + + + + ${cloudEye.logDir}/${fileName} + true + + %msg%n + + + + + + + + + \ No newline at end of file diff --git a/samples/metrics-write-file-sample/pom.xml b/samples/metrics-write-file-sample/pom.xml index 207e907b1f6..539a98cd5c1 100644 --- a/samples/metrics-write-file-sample/pom.xml +++ b/samples/metrics-write-file-sample/pom.xml @@ -31,8 +31,6 @@ pom metrics-write-file - metrics-write-file-log4j-springboot - metrics-write-file-log4j2-springboot @@ -42,11 +40,6 @@ metrics-core 1.0.0-m2-SNAPSHOT - - org.apache.servicecomb.samples - metrics-write-file - 1.0.0-m2-SNAPSHOT -