package hello;

import io.prometheus.client.CollectorRegistry;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

	private org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(HelloController.class);

	
    @RequestMapping("/")
    public String index() {

    	
    	Random rn = new Random();
    	int answer = rn.nextInt(100) + 1;
    	if(answer>97) {
        	Metrics.requestTotal.labels("index","ERROR").inc();
    		logger.error("main page http 404");
    		throw new ResourceNotFoundException();
    	}
    	
    	//memory usage...
    	List<String> list  = new ArrayList<String>();
    	for(int i=0; i<200000; i++)
    		list.add("eeeeeeeeeeeeeeeeeee"+i);
    	
    	logger.info("main page OK");
       	Metrics.requestTotal.labels("index","OK").inc();
        return "Greetings from Spring Boot!";
    }
    
    @RequestMapping("/endpointA")
    public String handlerA() throws InterruptedException {
    	Metrics.requestTotal.labels("endpointA","OK").inc();
    	return "Greetings from Spring Boot! page A";
    }

    @RequestMapping("/endpointB")
    public String handlerB() throws InterruptedException {
    	Metrics.requestTotal.labels("endpointB","OK").inc();
       	return "Greetings from Spring Boot! page B";
    }
    
    @RequestMapping(value="/metrics" ,produces={"text/plain"})
    public String metrics() {
    	StringWriter writer = new StringWriter();
        try {
            io.prometheus.client.exporter.common.TextFormat.write004(
                    writer, CollectorRegistry.defaultRegistry.metricFamilySamples());
        } catch (Exception e) {
            return e.getMessage();
        }
        return writer.toString();
    }

}

@@ -336,16 +336,65 @@
 roller %7B
+%0A%09%0A%09%0A%09private static final int HTTP_OK_RATE = 97;
 %0A%0A%09priva
@@ -481,16 +481,129 @@
 lass);%0A%0A
+%09%0A%09private int randomInt(int maxValue) %7B%0A    %09Random rn = new Random();%0A    %09return rn.nextInt(maxValue) + 1;%0A%09%7D%0A
 %09%0A    @R
@@ -654,45 +654,8 @@
  %7B%0A%0A
-    %09%0A    %09Random rn = new Random();%0A
     
@@ -673,22 +673,21 @@
  = r
-n.next
+andom
 Int(100)
  + 1
@@ -682,20 +682,16 @@
 Int(100)
- + 1
 ;%0A    %09i
@@ -699,18 +699,28 @@
 (answer%3E
-97
+HTTP_OK_RATE
 ) %7B%0A    
@@ -1265,32 +1265,494 @@
 edException %7B%0A  
+%0A    %09int answer = randomInt(100);%0A    %09if(answer%3E50)%0A    %09%09Metrics.nbTemporaryFile.labels(%22handlerA%22).inc(randomInt(5));%0A    %09else %7B%0A    %09%09int prevValue = (int)Metrics.nbTemporaryFile.labels(%22handlerA%22).get();%0A    %09%09if(prevValue%3E0) %7B%0A    %09%09%09if(prevValue%3C5) %0A    %09%09%09%09Metrics.nbTemporaryFile.labels(%22handlerA%22).dec(randomInt(prevValue));%0A        %09%09else%0A        %09%09%09Metrics.nbTemporaryFile.labels(%22handlerA%22).dec(randomInt(5));%0A        %09%09%0A    %09%09%7D%0A    %09%7D%0A%0A    %09%0A    
   %09Metrics.reque

complete gauge example