Amazon Managed Service for Prometheusãå¤ä¸ããããã¨ãã è¨äº ãè¦ããã¦ããããããMicrometerã®ã¢ãã¿ãªã³ã°ã·ã¹ãã ãCloud Watchããä¹ãæããããã£ã¦æ°æã¡ã«ãªã£ãã®ã§Prometheusããµã¯ãã¨è©¦ãã¦ã¿ã¾ããã
Prometheusã¨ã¯
Prometheusã¯ãSoundCloudã2012å¹´ã«ä½ã£ãOSSã®ã·ã¹ãã ã¢ãã¿ãªã³ã°ã¨ã¢ã©ã¼ãã®ãã¼ã«ã§ãã
æ©è½
- ã¡ããªã¯ã¹åã¨key/valueãã¢ã«ãã£ã¦èå¥ãããæç³»åãã¼ã¿ãæã¤å¤æ¬¡å ã®ãã¼ã¿ã¢ãã«
- ãã®æ¬¡å ãå©ç¨ããããã®æè»æ§ã®ããã¯ã¨ãªè¨èªã®PromQLãæä¾
- ä¾åããªãåæ£åã¹ãã¬ã¼ã¸ãã·ã³ã°ã«ãµã¼ããã¼ããç¬ç«ãã¦ãã
- ã¡ããªã¯ã¹ã®åéæ¹æ³ã¯ãã«å
- ä¸éã²ã¼ãã¦ã§ã¤çµç±ã§ããã·ã¥åããµãã¼ããã¦ãã
- ãµã¼ãã¹ãã£ã¹ã«ããªãéçãªè¨å®çµç±ã§ãã¿ã¼ã²ãããæ¤åºããã
- ã°ã©ãåãããã·ã¥ãã¼ãåã®ããã¤ãã®ã¢ã¼ãããµãã¼ããã¦ãã
ã³ã³ãã¼ãã³ã
Prometheusã®ã¨ã³ã·ã¹ãã ã¯è¤æ°ã®ã³ã³ãã¼ãã³ãããæãç«ã¤
- ã¡ã¤ã³ã®Prometheusãµã¼ãã¯æç³»åãã¼ã¿ãåå¾ãã¦ä¿åãã
- è¨æ¸¬ããã¢ããªã±ã¼ã·ã§ã³ã³ã¼ãã®ããã®ã¯ã©ã¤ã¢ã³ãã©ã¤ãã©ãª
- ä¸æçãªã¸ã§ãããµãã¼ãããããã®ããã·ã¥ã²ã¼ãã¦ã§ã¤
- HAProxy, StatsD, Graphite, ãªã©ã®ãããªãµã¼ãã¹ã®ããã®ç¹å¥ãªç®çã®ã¨ã¯ã¹ãã¼ã¿ã¼ãº
- ã¢ã©ã¼ããæ±ãããã®ã¢ã©ã¼ãããã¼ã¸ã£
- å¤ãã®Prometheusã³ã³ãã¼ãã³ãã¯golangã§æ¸ããã¦ãã
Micrometerã¨ã¯
以åæ¸ãã Micrometerã§ã¡ããªã¯ã¹ãåéãã¦Amazon Cloud Watchã§å¯è¦åãã ã«ã¾ã¨ãã¦ããã®ã§çç¥ã
ã¢ããªã±ã¼ã·ã§ã³ä½æ
ã¡ããªã¯ã¹åé対象ã®ã¢ããªã±ã¼ã·ã§ã³ãä½ãã¾ããä»åã¯Spring Bootã§ä½ãã¾ãã
ã¾ããbuild.gradleã®ä¾åé¢ä¿ã§ããPrometheusãActuatorã®ã¨ã³ããã¤ã³ããå©ãã¦ã¡ããªã¯ã¹æ å ±ãåéãã¾ãã
dependencies { implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-web' runtimeOnly 'io.micrometer:micrometer-registry-prometheus' }
次ã«ãã¢ããªã±ã¼ã·ã§ã³ã®å®è£ ã§ãã
@RestController @RequestMapping("/") public class IndexController { @GetMapping public IndexResponse index() { return new IndexResponse("Hello World!!"); } }
ã¢ããªã±ã¼ã·ã§ã³ãèµ·åãã¦ãGET http://localhost:8080
ãå©ãã¨æ¬¡ã®ãããªã¬ã¹ãã³ã¹ãè¿ã£ã¦ãã¾ãã
{ "message": "Hello World!!" }
次ã«ãActuatorã® GET /actuator/prometheus
ã¨ã³ããã¤ã³ããæå¹ã«ãã¾ããPrometheusããã®ã¨ã³ããã¤ã³ããå©ãã¦ã¡ããªã¯ã¹æ
å ±ãåéãã¾ãã
management: endpoints: web: exposure: include: prometheus
GET http://localhost/actuator/prometheus
ãå©ãã¨ãPrometheusã®ãã©ã¼ãããã§ã¡ããªã¯ã¹æ
å ±ãè¿ã£ã¦ãã¾ãã
Dockerã³ã³ããã§èµ·å
docker-compose.ymlãç¨æãã¦ãå ã«å®è£ ããã¢ããªã±ã¼ã·ã§ã³ã¨PrometheusãDockerã³ã³ããä¸ã§èµ·åããããã«ãã¾ãã
version: '3'
services:
prometheus:
image: prom/prometheus
container_name: prometheus
volumes:
- ./prometheus:/etc/prometheus
command: "--config.file=/etc/prometheus/prometheus.yml"
ports:
- 9090:9090
restart: always
micrometer-prometheus-api:
image: micrometer-prometheus-api
container_name: micrometer-prometheus-api
ports:
- 18080:8080
次ã«ãPrometheusã®è¨å®ãã¡ã¤ã«ãç¨æãã¾ãã ã»ã¼ãSpring Bootã®ããã¥ã¡ã³ãã«ãããµã³ãã«éãã§ãã
global: scrape_interval: 10s scrape_timeout: 10s evaluation_interval: 10s scrape_configs: - job_name: 'micrometer-prometheus' metrics_path: '/actuator/prometheus' static_configs: - targets: - micrometer-prometheus-api:8080
次ã«ãjibã®Gradleãã©ã°ã¤ã³ã使ã£ã¦ã¢ããªã±ã¼ã·ã§ã³ã®Dockerã¤ã¡ã¼ã¸ãä½æãã¾ãã8080çªãã¼ãã§èµ·åããããã«ãã¦ãã¾ãã
plugins { id 'com.google.cloud.tools.jib' version '3.0.0' } jib { container { ports ['8080'] jvmFlags = ['--enable-preview'] } from { image = 'openjdk:15-alpine' } to { image = 'micrometer-prometheus-api' tags = ['latest'] } }
./gradlew jibDockerBuild
ã§Dockerã¤ã¡ã¼ã¸ãä½æãã¾ãã
ããã§ã¯ãã³ã³ãããèµ·åãã¦ã¿ã¾ãããã
$ docker-compose up prometheus | level=info ts=2021-05-14T04:11:02.765Z caller=main.go:388 msg="No time or size retention was set so using the default time retention" duration=15d prometheus | level=info ts=2021-05-14T04:11:02.765Z caller=main.go:426 msg="Starting Prometheus" version="(version=2.27.0, branch=HEAD, revision=24c9b61221f7006e87cd62b9fe2901d43e19ed53)" prometheus | level=info ts=2021-05-14T04:11:02.765Z caller=main.go:431 build_context="(go=go1.16.4, user=root@f27daa3b3fec, date=20210512-18:04:51)" prometheus | level=info ts=2021-05-14T04:11:02.765Z caller=main.go:432 host_details="(Linux 5.10.25-linuxkit #1 SMP Tue Mar 23 09:27:39 UTC 2021 x86_64 29b273b8d0c7 (none))" prometheus | level=info ts=2021-05-14T04:11:02.765Z caller=main.go:433 fd_limits="(soft=1048576, hard=1048576)" prometheus | level=info ts=2021-05-14T04:11:02.765Z caller=main.go:434 vm_limits="(soft=unlimited, hard=unlimited)" prometheus | level=info ts=2021-05-14T04:11:02.778Z caller=web.go:540 component=web msg="Start listening for connections" address=0.0.0.0:9090 prometheus | level=info ts=2021-05-14T04:11:02.780Z caller=main.go:803 msg="Starting TSDB ..." prometheus | level=info ts=2021-05-14T04:11:02.788Z caller=tls_config.go:191 component=web msg="TLS is disabled." http2=false prometheus | level=info ts=2021-05-14T04:11:02.790Z caller=head.go:741 component=tsdb msg="Replaying on-disk memory mappable chunks if any" prometheus | level=info ts=2021-05-14T04:11:02.794Z caller=head.go:755 component=tsdb msg="On-disk memory mappable chunks replay completed" duration=6.319µs prometheus | level=info ts=2021-05-14T04:11:02.794Z caller=head.go:761 component=tsdb msg="Replaying WAL, this may take a while" prometheus | level=info ts=2021-05-14T04:11:02.795Z caller=head.go:813 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0 prometheus | level=info ts=2021-05-14T04:11:02.795Z caller=head.go:818 component=tsdb msg="WAL replay completed" checkpoint_replay_duration=42.188µs wal_replay_duration=1.023386ms total_replay_duration=1.236444ms prometheus | level=info ts=2021-05-14T04:11:02.799Z caller=main.go:828 fs_type=EXT4_SUPER_MAGIC prometheus | level=info ts=2021-05-14T04:11:02.799Z caller=main.go:831 msg="TSDB started" prometheus | level=info ts=2021-05-14T04:11:02.799Z caller=main.go:957 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml prometheus | level=info ts=2021-05-14T04:11:02.805Z caller=main.go:988 msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDuration=6.051413ms remote_storage=2.506µs web_handler=635ns query_engine=1.369µs scrape=2.644871ms scrape_sd=85.53µs notify=1.254µs notify_sd=2.059µs rules=1.15µs prometheus | level=info ts=2021-05-14T04:11:02.805Z caller=main.go:775 msg="Server is ready to receive web requests." micrometer-prometheus-api | micrometer-prometheus-api | . ____ _ __ _ _ micrometer-prometheus-api | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ micrometer-prometheus-api | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ micrometer-prometheus-api | \\/ ___)| |_)| | | | | || (_| | ) ) ) ) micrometer-prometheus-api | ' |____| .__|_| |_|_| |_\__, | / / / / micrometer-prometheus-api | =========|_|==============|___/=/_/_/_/ micrometer-prometheus-api | :: Spring Boot :: (v2.4.5) micrometer-prometheus-api | micrometer-prometheus-api | 2021-05-14 05:25:22.680 INFO 1 --- [ main] c.b.micrometerprometheus.Application : Starting Application using Java 15-ea on aad30f1b5aec with PID 1 (/app/classes started by root in /) micrometer-prometheus-api | 2021-05-14 05:25:22.684 INFO 1 --- [ main] c.b.micrometerprometheus.Application : No active profile set, falling back to default profiles: default micrometer-prometheus-api | 2021-05-14 05:25:23.854 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) micrometer-prometheus-api | 2021-05-14 05:25:23.866 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] micrometer-prometheus-api | 2021-05-14 05:25:23.867 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.45] micrometer-prometheus-api | 2021-05-14 05:25:23.928 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext micrometer-prometheus-api | 2021-05-14 05:25:23.929 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1182 ms micrometer-prometheus-api | 2021-05-14 05:25:24.458 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' micrometer-prometheus-api | 2021-05-14 05:25:24.640 INFO 1 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator' micrometer-prometheus-api | 2021-05-14 05:25:24.688 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' micrometer-prometheus-api | 2021-05-14 05:25:24.705 INFO 1 --- [ main] c.b.micrometerprometheus.Application : Started Application in 2.464 seconds (JVM running for 2.963) micrometer-prometheus-api | 2021-05-14 05:25:26.655 INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' micrometer-prometheus-api | 2021-05-14 05:25:26.655 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' micrometer-prometheus-api | 2021-05-14 05:25:26.657 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms
ãªã¯ã¨ã¹ããã°ãã¿ã¦ã¿ãã¨ã10ç§ããã«ã¨ã³ããã¤ã³ããå©ããã¦ãã®ããããã¾ãã
2021-05-14 06:05:39.806 DEBUG 1 --- [nio-8080-exec-7] o.s.w.f.CommonsRequestLoggingFilter : Before request [GET /actuator/prometheus] 2021-05-14 06:05:39.816 DEBUG 1 --- [nio-8080-exec-7] o.s.w.f.CommonsRequestLoggingFilter : Request data : GET /actuator/prometheus] 2021-05-14 06:05:49.784 DEBUG 1 --- [nio-8080-exec-9] o.s.w.f.CommonsRequestLoggingFilter : Before request [GET /actuator/prometheus] 2021-05-14 06:05:49.789 DEBUG 1 --- [nio-8080-exec-9] o.s.w.f.CommonsRequestLoggingFilter : Request data : GET /actuator/prometheus] 2021-05-14 06:05:59.785 DEBUG 1 --- [nio-8080-exec-8] o.s.w.f.CommonsRequestLoggingFilter : Before request [GET /actuator/prometheus] 2021-05-14 06:05:59.792 DEBUG 1 --- [nio-8080-exec-8] o.s.w.f.CommonsRequestLoggingFilter : Request data : GET /actuator/prometheus] 2021-05-14 06:06:09.788 DEBUG 1 --- [io-8080-exec-10] o.s.w.f.CommonsRequestLoggingFilter : Before request [GET /actuator/prometheus] 2021-05-14 06:06:09.796 DEBUG 1 --- [io-8080-exec-10] o.s.w.f.CommonsRequestLoggingFilter : Request data : GET /actuator/prometheus]
Prometheusã®UIã確èª
ãã©ã¦ã¶ã§ãPrometheusã«ã¢ã¯ã»ã¹ããã¨ãUIãè¦ããã¨ã§ãã¾ãã ãExpressionãã«PromQLãå ¥åãã¦ãExecuteããã¿ã³ãæ¼ä¸ããã¨è¡¨ç¤ºããã¾ãã
â Table
â Graph
ãã£ã¦ã¿ãè¨äºãªã®ã§ãä¸æ¦ããã¾ã§ã