Added Samples and Documentation for metrics

Updated documentation and sample

Updated the test to use original metrics definition
This commit is contained in:
Glenn Renfro
2022-05-25 18:48:47 -04:00
parent 73c1ae693c
commit 38323952ca
14 changed files with 921 additions and 1 deletions

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2022-2022 the original author or authors.
*
* Licensed 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
*
* https://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.spring.taskmetrics;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MetricConfiguration {
@Bean
public SimpleMeterRegistry meterRegistry() {
return new SimpleMeterRegistry();
}
}

View File

@@ -0,0 +1,75 @@
/*
* Copyright 2022-2022 the original author or authors.
*
* Licensed 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
*
* https://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.spring.taskmetrics;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.cloud.task.listener.annotation.AfterTask;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableTask
public class TaskMetricsApplication {
private static final Log logger = LogFactory.getLog(TaskMetricsApplication.class);
@Autowired
public SimpleMeterRegistry simpleMeterRegistry;
public static void main(String[] args) {
SpringApplication.run(TaskMetricsApplication.class, args);
}
@Bean
public ApplicationRunner applicationRunner() {
return new ApplicationRunner() {
@Override
public void run(ApplicationArguments args) throws Exception {
logger.info("Hello ApplicationRunner Metric's World");
}
};
}
@Bean
public CommandLineRunner commandLineRunner() {
return new CommandLineRunner() {
@Override
public void run(String... args) throws Exception {
logger.info("Hello CommandLineRunner Metric's World");
}
};
}
/**
* Prints the metrics as recorded in the simpleMeterRegistry.
*/
@AfterTask
public void afterTask(TaskExecution taskExecution) {
System.out.println(simpleMeterRegistry.getMetersAsString());
}
}

View File

@@ -0,0 +1,6 @@
logging.level.org.springframework.cloud.task=debug
management.metrics.tags.service=task-metrics-application
management.metrics.tags.application=task-metrics-application-58
spring.cloud.task.name=taskmetrics
spring.cloud.task.observation.enabled=true

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2022-2022 the original author or authors.
*
* Licensed 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
*
* https://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.spring.taskmetrics;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
@SpringBootTest
@ExtendWith(OutputCaptureExtension.class)
class TaskMetricsApplicationTests {
@Test
void contextLoads(CapturedOutput output) {
String result = output.getAll();
assertThat(result).contains("spring.cloud.task(TIMER)[application=" +
"'task-metrics-application-58', service='task-metrics-application', " +
"task.exception='none', task.execution.id='");
}
}