wujimin closed pull request #635: [SCB-462] cloud eye publisher switch to new mechanism URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/635
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): 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 806d3b4e1..000000000 --- a/samples/metrics-write-file-sample/metrics-write-file-log4j-springboot/pom.xml +++ /dev/null @@ -1,88 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - ~ 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. - --> - -<project xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <parent> - <artifactId>metrics-write-file-sample</artifactId> - <groupId>org.apache.servicecomb.samples</groupId> - <version>1.0.0-m2-SNAPSHOT</version> - </parent> - <modelVersion>4.0.0</modelVersion> - - <artifactId>metrics-write-file-log4j-springboot</artifactId> - - <!--need exclusion log4j-over-slf4j--> - <!--spring boot starter included log4j-over-slf4j,this log bridge not implement the full interface for log4j, - excluded the bridge jar let all slf4j calls go directly to log4j instead of the bridge. - more info can be found here : https://www.slf4j.org/legacy.html#log4j-over-slf4j --> - <dependencies> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter</artifactId> - <exclusions> - <exclusion> - <groupId>org.slf4j</groupId> - <artifactId>log4j-over-slf4j</artifactId> - </exclusion> - </exclusions> - </dependency> - - <dependency> - <groupId>org.apache.servicecomb</groupId> - <artifactId>spring-boot-starter-provider</artifactId> - </dependency> - - <dependency> - <groupId>org.apache.servicecomb</groupId> - <artifactId>handler-flowcontrol-qps</artifactId> - </dependency> - <dependency> - <groupId>org.apache.servicecomb</groupId> - <artifactId>handler-bizkeeper</artifactId> - </dependency> - <dependency> - <groupId>org.apache.servicecomb</groupId> - <artifactId>handler-tracing-zipkin</artifactId> - </dependency> - <dependency> - <groupId>org.hibernate</groupId> - <artifactId>hibernate-validator</artifactId> - </dependency> - - <dependency> - <groupId>log4j</groupId> - <artifactId>log4j</artifactId> - </dependency> - <dependency> - <groupId>org.apache.servicecomb.samples</groupId> - <artifactId>metrics-write-file</artifactId> - </dependency> - </dependencies> - - <build> - <plugins> - <plugin> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-maven-plugin</artifactId> - </plugin> - </plugins> - </build> - -</project> \ 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 cbe417dd4..000000000 --- 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<String, RollingFileAppender> 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 6995e381b..000000000 --- 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 094df93fe..000000000 --- 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 748ef4d85..000000000 --- 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 282bbbd4d..000000000 --- a/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/pom.xml +++ /dev/null @@ -1,82 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - ~ 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. - --> - -<project xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <parent> - <artifactId>metrics-write-file-sample</artifactId> - <groupId>org.apache.servicecomb.samples</groupId> - <version>1.0.0-m2-SNAPSHOT</version> - </parent> - <modelVersion>4.0.0</modelVersion> - - <artifactId>metrics-write-file-log4j2-springboot</artifactId> - <name>Java Chassis::Metrics::Sample::Metrics::WriteFile::Log4j2</name> - - <dependencies> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter</artifactId> - </dependency> - - <dependency> - <groupId>org.apache.servicecomb</groupId> - <artifactId>spring-boot-starter-provider</artifactId> - </dependency> - - <dependency> - <groupId>org.apache.servicecomb</groupId> - <artifactId>handler-flowcontrol-qps</artifactId> - </dependency> - <dependency> - <groupId>org.apache.servicecomb</groupId> - <artifactId>handler-bizkeeper</artifactId> - </dependency> - <dependency> - <groupId>org.apache.servicecomb</groupId> - <artifactId>handler-tracing-zipkin</artifactId> - </dependency> - <dependency> - <groupId>org.hibernate</groupId> - <artifactId>hibernate-validator</artifactId> - </dependency> - - <dependency> - <groupId>org.apache.logging.log4j</groupId> - <artifactId>log4j-api</artifactId> - </dependency> - <dependency> - <groupId>org.apache.logging.log4j</groupId> - <artifactId>log4j-core</artifactId> - </dependency> - <dependency> - <groupId>org.apache.servicecomb.samples</groupId> - <artifactId>metrics-write-file</artifactId> - </dependency> - </dependencies> - - <build> - <plugins> - <plugin> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-maven-plugin</artifactId> - </plugin> - </plugins> - </build> -</project> 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 737cc4716..000000000 --- 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<String, RollingFileAppender> 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 094df93fe..000000000 --- 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 e21b21ad5..000000000 --- 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 f4951a320..54ff6eee8 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. --> -<project xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>metrics-write-file-sample</artifactId> @@ -35,22 +34,19 @@ <artifactId>metrics-core</artifactId> </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <scope>test</scope> - </dependency> - <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> - </dependencies> +<!-- <dependency> --> +<!-- <groupId>org.apache.logging.log4j</groupId> --> +<!-- <artifactId>log4j-core</artifactId> --> +<!-- </dependency> --> +<!-- <dependency> --> +<!-- <groupId>org.apache.logging.log4j</groupId> --> +<!-- <artifactId>log4j-slf4j-impl</artifactId> --> +<!-- </dependency> --> +<!-- <dependency> --> +<!-- <groupId>ch.qos.logback</groupId> --> +<!-- <artifactId>logback-classic</artifactId> --> +<!-- </dependency> --> + </dependencies> </project> 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 000000000..acda8a6d7 --- /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 000000000..9bf1dab26 --- /dev/null +++ b/samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf/CloudEyeMetricModel.java @@ -0,0 +1,77 @@ +/* + * 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 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<String, String> 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<String, String> getDynamicValue() { + return dynamicValue; + } + + public void setDynamicValue(Map<String, String> dynamicValue) { + this.dynamicValue = dynamicValue; + } +} 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 6995e381b..f91acfcc9 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 dc13f9a07..000000000 --- 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<String, String> convert(Map<String, Double> 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 f93198a98..000000000 --- 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<String, String> format(Map<String, String> 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 3c4d7cc3c..000000000 --- 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 313d91976..000000000 --- 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 c3f3d59ce..000000000 --- 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<String, String> convert(Map<String, Double> registryMetric) { - Map<String, String> pickedMetrics = new HashMap<>(); - for (Entry<String, Double> 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<String, String> 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 91a55e879..000000000 --- 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<String, String> format(Map<String, String> 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<String, Object> metric; - - public String getPlugin_id() { - return plugin_id; - } - - public Map<String, Object> 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 d5486b8d3..000000000 --- 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<String, Double> metrics = MonitorManager.getInstance().measure(); - Map<String, String> convertedMetrics = convertor.convert(metrics); - Map<String, String> 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 000000000..3a6ad61ea --- /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 000000000..99ed192d4 --- /dev/null +++ b/samples/metrics-write-file-sample/metrics-write-file/src/main/resources/sample-log4j2.xml @@ -0,0 +1,48 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> + +<Configuration status="INFO"> + <Appenders> + <Console name="stdout" target="SYSTEM_OUT"> + <PatternLayout pattern="%d [%level] [%thread] - %msg (%F:%L\)%n" /> + </Console> + + <Routing name="cloudEyeRouting"> + <Routes pattern="$${ctx:fileName}"> + <Route> + <File name="cloudEyeAppender-${ctx:fileName}" append="true" + fileName="${sys:cloudEye.logDir}/${ctx:fileName}"> + <PatternLayout> + <Pattern>%msg%n</Pattern> + </PatternLayout> + </File> + </Route> + </Routes> + </Routing> + </Appenders> + + <Loggers> + <Root level="info"> + <AppenderRef ref="stdout" /> + </Root> + + <Logger name="cloudEyeLogger" level="info" additivity="false"> + <Appender-ref ref="cloudEyeRouting" /> + </Logger> + </Loggers> +</Configuration> \ 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 000000000..ee574146d --- /dev/null +++ b/samples/metrics-write-file-sample/metrics-write-file/src/main/resources/sample-logback.xml @@ -0,0 +1,52 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> + +<configuration scan="true"> + <jmxConfigurator /> + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>%d [%level] [%thread] - %msg (%F:%L\)%n</pattern> + </encoder> + </appender> + + <root level="INFO"> + <appender-ref ref="STDOUT" /> + </root> + + <appender name="cloudEyeAppender" class="ch.qos.logback.classic.sift.SiftingAppender"> + <!-- This is MDC value --> + <discriminator> + <key>fileName</key> + <defaultValue>unknown</defaultValue> + </discriminator> + + <sift> + <appender name="cloudEyeAppender-${fileName}" class="ch.qos.logback.core.FileAppender"> + <file>${cloudEye.logDir}/${fileName}</file> + <append>true</append> + <layout class="ch.qos.logback.classic.PatternLayout"> + <pattern>%msg%n</pattern> + </layout> + </appender> + </sift> + </appender> + + <logger name="cloudEyeLogger" level="info" additivity="false"> + <appender-ref ref="cloudEyeAppender" /> + </logger> +</configuration> \ 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 207e907b1..539a98cd5 100644 --- a/samples/metrics-write-file-sample/pom.xml +++ b/samples/metrics-write-file-sample/pom.xml @@ -31,8 +31,6 @@ <packaging>pom</packaging> <modules> <module>metrics-write-file</module> - <module>metrics-write-file-log4j-springboot</module> - <module>metrics-write-file-log4j2-springboot</module> </modules> <dependencyManagement> @@ -42,11 +40,6 @@ <artifactId>metrics-core</artifactId> <version>1.0.0-m2-SNAPSHOT</version> </dependency> - <dependency> - <groupId>org.apache.servicecomb.samples</groupId> - <artifactId>metrics-write-file</artifactId> - <version>1.0.0-m2-SNAPSHOT</version> - </dependency> </dependencies> </dependencyManagement> ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services