This is an automated email from the ASF dual-hosted git repository. liubao pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
commit 5856cec17bc6238d07fe792629944a4350e34a9d Author: zhengyangyong <[email protected]> AuthorDate: Wed Dec 27 16:06:31 2017 +0800 SCB-85 add write file module Signed-off-by: zhengyangyong <[email protected]> --- .../metrics-performance-test/pom.xml | 13 --- .../{ => metrics-write-file-log4j2-config}/pom.xml | 22 +++-- .../log4j2config/Log4j2ConfigInitializer.java | 62 ++++++++++++++ .../{ => metrics-write-file}/pom.xml | 10 +-- .../sample/writefile/MetricsContentConvertor.java | 27 ++++++ .../sample/writefile/MetricsContentFormatter.java | 24 ++++++ .../sample/writefile/MetricsFileOutput.java | 40 +++++++++ .../writefile/MetricsObserverInitializer.java | 71 ++++++++++++++++ .../writefile/SimpleMetricsContentConvertor.java | 37 +++++++++ .../writefile/SimpleMetricsContentFormatter.java | 97 ++++++++++++++++++++++ metrics/metrics-sample/pom.xml | 17 +++- .../src/main/resources/microservice.yaml | 9 +- 12 files changed, 400 insertions(+), 29 deletions(-) diff --git a/metrics/metrics-sample/metrics-performance-test/pom.xml b/metrics/metrics-sample/metrics-performance-test/pom.xml index 7a51ea6..f37e1fe 100644 --- a/metrics/metrics-sample/metrics-performance-test/pom.xml +++ b/metrics/metrics-sample/metrics-performance-test/pom.xml @@ -42,19 +42,6 @@ <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> </dependency> - - <!--add or move metrics-core for enable or disable--> - <dependency> - <groupId>io.servicecomb</groupId> - <artifactId>metrics-core</artifactId> - <exclusions> - <exclusion> - <groupId>io.servicecomb</groupId> - <artifactId>foundation-metrics</artifactId> - </exclusion> - </exclusions> - <version>0.6.0-SNAPSHOT</version> - </dependency> </dependencies> <build> diff --git a/metrics/metrics-sample/pom.xml b/metrics/metrics-sample/metrics-write-file-log4j2-config/pom.xml similarity index 72% copy from metrics/metrics-sample/pom.xml copy to metrics/metrics-sample/metrics-write-file-log4j2-config/pom.xml index b78f37a..4b8251d 100644 --- a/metrics/metrics-sample/pom.xml +++ b/metrics/metrics-sample/metrics-write-file-log4j2-config/pom.xml @@ -20,17 +20,25 @@ 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</artifactId> + <artifactId>metrics-sample</artifactId> <groupId>io.servicecomb</groupId> <version>0.6.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> - <artifactId>metrics-sample</artifactId> - <packaging>pom</packaging> + <artifactId>metrics-write-file-log4j2-config</artifactId> + + <dependencies> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + <version>2.8.2</version> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <version>2.8.2</version> + </dependency> + </dependencies> - <modules> - <module>metrics-performance-test</module> - </modules> - </project> \ No newline at end of file diff --git a/metrics/metrics-sample/metrics-write-file-log4j2-config/src/main/java/io/servicecomb/metrics/sample/writefile/log4j2config/Log4j2ConfigInitializer.java b/metrics/metrics-sample/metrics-write-file-log4j2-config/src/main/java/io/servicecomb/metrics/sample/writefile/log4j2config/Log4j2ConfigInitializer.java new file mode 100644 index 0000000..d0bc961 --- /dev/null +++ b/metrics/metrics-sample/metrics-write-file-log4j2-config/src/main/java/io/servicecomb/metrics/sample/writefile/log4j2config/Log4j2ConfigInitializer.java @@ -0,0 +1,62 @@ +/* + * 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 io.servicecomb.metrics.sample.writefile.log4j2config; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.config.Configurator; +import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder; +import org.apache.logging.log4j.core.config.builder.api.ComponentBuilder; +import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder; +import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory; +import org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder; +import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration; +import org.springframework.stereotype.Component; + +@Component +public class Log4j2ConfigInitializer { + + public Log4j2ConfigInitializer() { + ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder(); + + builder.setStatusLevel(Level.ERROR); + builder.setConfigurationName("MetricsConfig"); +// create a rolling file appender + LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout") + .addAttribute("pattern", "%m%n"); + ComponentBuilder triggeringPolicy = builder.newComponent("Policies") + .addComponent(builder.newComponent("CronTriggeringPolicy").addAttribute("schedule", "0 0 0 * * ?")) + .addComponent(builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size", "10MB")); + AppenderComponentBuilder appenderBuilder = builder.newAppender("rolling", "RollingFile") + .addAttribute("fileName", "target/rolling.log") + .addAttribute("filePattern", "target/archive/rolling-%d{MM-dd-yy}.log.gz") + .add(layoutBuilder) + .addComponent(triggeringPolicy); + builder.add(appenderBuilder); + +// create the new logger + builder.add(builder.newLogger("TestLogger", Level.DEBUG) + .add(builder.newAppenderRef("rolling")) + .addAttribute("additivity", false)); + + builder.add(builder.newRootLogger(Level.DEBUG) + .add(builder.newAppenderRef("rolling"))); + Configurator.initialize(builder.build()); + } + + +} diff --git a/metrics/metrics-sample/pom.xml b/metrics/metrics-sample/metrics-write-file/pom.xml similarity index 86% copy from metrics/metrics-sample/pom.xml copy to metrics/metrics-sample/metrics-write-file/pom.xml index b78f37a..5152f8a 100644 --- a/metrics/metrics-sample/pom.xml +++ b/metrics/metrics-sample/metrics-write-file/pom.xml @@ -20,17 +20,13 @@ 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</artifactId> + <artifactId>metrics-sample</artifactId> <groupId>io.servicecomb</groupId> <version>0.6.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> - <artifactId>metrics-sample</artifactId> - <packaging>pom</packaging> + <artifactId>metrics-write-file</artifactId> + - <modules> - <module>metrics-performance-test</module> - </modules> - </project> \ No newline at end of file diff --git a/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/MetricsContentConvertor.java b/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/MetricsContentConvertor.java new file mode 100644 index 0000000..8496d64 --- /dev/null +++ b/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/MetricsContentConvertor.java @@ -0,0 +1,27 @@ +/* + * 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 io.servicecomb.metrics.sample.writefile; + +import java.util.Map; + +import io.servicecomb.metrics.common.RegistryMetric; + +//convert metrics to output content +public interface MetricsContentConvertor { + Map<String, String> convert(RegistryMetric registryMetric); +} diff --git a/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/MetricsContentFormatter.java b/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/MetricsContentFormatter.java new file mode 100644 index 0000000..db1fb82 --- /dev/null +++ b/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/MetricsContentFormatter.java @@ -0,0 +1,24 @@ +/* + * 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 io.servicecomb.metrics.sample.writefile; + +import java.util.Map; + +public interface MetricsContentFormatter { + Map<String, String> format(Map<String, String> input); +} diff --git a/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/MetricsFileOutput.java b/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/MetricsFileOutput.java new file mode 100644 index 0000000..d0c6cdd --- /dev/null +++ b/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/MetricsFileOutput.java @@ -0,0 +1,40 @@ +/* + * 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 io.servicecomb.metrics.sample.writefile; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +@Component +public class MetricsFileOutput { + + private final Map<String, org.slf4j.Logger> allLoggers = new ConcurrentHashMap<>(); + + public MetricsFileOutput() { + } + + public void output(Map<String, String> metrics) { + for (String metricName : metrics.keySet()) { + org.slf4j.Logger logger = allLoggers.computeIfAbsent(metricName, l -> LoggerFactory.getLogger(metricName)); + logger.error(metrics.get(metricName)); + } + } +} diff --git a/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/MetricsObserverInitializer.java b/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/MetricsObserverInitializer.java new file mode 100644 index 0000000..051f2ee --- /dev/null +++ b/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/MetricsObserverInitializer.java @@ -0,0 +1,71 @@ +/* + * 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 io.servicecomb.metrics.sample.writefile; + +import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import java.util.Map; +import java.util.concurrent.Executors; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.netflix.config.DynamicPropertyFactory; + +import io.servicecomb.metrics.common.RegistryMetric; +import io.servicecomb.metrics.core.publish.DataSource; + +@Component +public class MetricsObserverInitializer { + + private static final String METRICS_WINDOW_TIME = "servicecomb.metrics.window_time"; + + private final int metricPoll; + + private final MetricsFileOutput fileOutput; + + private final MetricsContentConvertor convertor; + + private final MetricsContentFormatter formatter; + + private final DataSource dataSource; + + @Autowired + public MetricsObserverInitializer(MetricsFileOutput fileOutput, MetricsContentConvertor convertor, + MetricsContentFormatter formatter, DataSource dataSource) { + metricPoll = DynamicPropertyFactory.getInstance().getIntProperty(METRICS_WINDOW_TIME, 5000).get(); + this.fileOutput = fileOutput; + this.convertor = convertor; + this.formatter = formatter; + this.dataSource = dataSource; + this.init(); + } + + public void init() { + final Runnable poller = this::run; + Executors.newScheduledThreadPool(1) + .scheduleWithFixedDelay(poller, 0, metricPoll, MILLISECONDS); + } + + private void run() { + RegistryMetric registryMetric = dataSource.getRegistryMetric(0); + Map<String, String> convertedMetrics = convertor.convert(registryMetric); + Map<String, String> formattedMetrics = formatter.format(convertedMetrics); + fileOutput.output(formattedMetrics); + } +} diff --git a/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/SimpleMetricsContentConvertor.java b/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/SimpleMetricsContentConvertor.java new file mode 100644 index 0000000..60bf180 --- /dev/null +++ b/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/SimpleMetricsContentConvertor.java @@ -0,0 +1,37 @@ +/* + * 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 io.servicecomb.metrics.sample.writefile; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.stereotype.Component; + +import io.servicecomb.metrics.common.RegistryMetric; + +@Component +public class SimpleMetricsContentConvertor implements MetricsContentConvertor { + @Override + public Map<String, String> convert(RegistryMetric registryMetric) { + Map<String, String> pickedMetrics = new HashMap<>(); + + //TODO:draw all metrics from RegistryMetric + + return pickedMetrics; + } +} diff --git a/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/SimpleMetricsContentFormatter.java b/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/SimpleMetricsContentFormatter.java new file mode 100644 index 0000000..1cdc203 --- /dev/null +++ b/metrics/metrics-sample/metrics-write-file/src/main/java/io/servicecomb/metrics/sample/writefile/SimpleMetricsContentFormatter.java @@ -0,0 +1,97 @@ +/* + * 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 io.servicecomb.metrics.sample.writefile; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; + +import com.fasterxml.jackson.core.JsonProcessingException; + +import io.servicecomb.foundation.common.net.NetUtils; +import io.servicecomb.foundation.common.utils.JsonUtils; +import io.servicecomb.serviceregistry.RegistryUtils; + +@Component +public class SimpleMetricsContentFormatter implements MetricsContentFormatter { + private static final Logger logger = LoggerFactory.getLogger(SimpleMetricsContentFormatter.class); + + private final String applicationName; + + private String hostName; + + @Autowired + public SimpleMetricsContentFormatter() { + hostName = NetUtils.getHostName(); + if (StringUtils.isEmpty(hostName)) { + hostName = NetUtils.getHostAddress(); + } + + applicationName = + RegistryUtils.getMicroservice().getAppId() + "." + RegistryUtils.getMicroservice().getServiceName(); + } + + @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 convert metrics data to json convert", 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/metrics/metrics-sample/pom.xml b/metrics/metrics-sample/pom.xml index b78f37a..cbf5803 100644 --- a/metrics/metrics-sample/pom.xml +++ b/metrics/metrics-sample/pom.xml @@ -31,6 +31,21 @@ <modules> <module>metrics-performance-test</module> + <module>metrics-write-file</module> + <module>metrics-write-file-log4j2-config</module> </modules> - + + <!--add or move metrics-core for enable or disable--> + <dependencies> + <dependency> + <groupId>io.servicecomb</groupId> + <artifactId>metrics-common</artifactId> + </dependency> + <dependency> + <groupId>io.servicecomb</groupId> + <artifactId>metrics-core</artifactId> + </dependency> + </dependencies> + + </project> \ No newline at end of file diff --git a/samples/bmi/calculator/src/main/resources/microservice.yaml b/samples/bmi/calculator/src/main/resources/microservice.yaml index 8069b46..d27d2a7 100644 --- a/samples/bmi/calculator/src/main/resources/microservice.yaml +++ b/samples/bmi/calculator/src/main/resources/microservice.yaml @@ -42,4 +42,11 @@ servicecomb: file: enabled: true #metrics file name prefix setting,default value is "metrics" - name_prefix: bmi.calculator \ No newline at end of file + name_prefix: bmi.calculator + + #Version 0.5.0+ config + #Support Muti-WindowTime (MILLISECONDS) like 10000,60000 (10s and 60s), Use WindowTimeIndex make selection,start from 0 + #Default value is single WindowTime : 5000 (5 second) + #Will direct set value of "servo.pollers" + #More info can be found here : https://github.com/Netflix/servo/wiki/Getting-Started -> Polling Intervals Chapter + window_time: 5000 \ No newline at end of file -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
