from prometheus_client import start_http_server, Summary
import random
import time
# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
# HELP nginx_connections_accepted Accepted client connections# TYPE nginx_connections_accepted counternginx_connections_accepted20# HELP nginx_connections_active Active client connections# TYPE nginx_connections_active gaugenginx_connections_active1# HELP nginx_connections_handled Handled client connections# TYPE nginx_connections_handled counternginx_connections_handled20# HELP nginx_connections_reading Connections where NGINX is reading the request header# TYPE nginx_connections_reading gaugenginx_connections_reading0# HELP nginx_connections_waiting Idle client connections# TYPE nginx_connections_waiting gaugenginx_connections_waiting0# HELP nginx_connections_writing Connections where NGINX is writing the response back to the client# TYPE nginx_connections_writing gaugenginx_connections_writing1
## Java Metrics
Exporter, Client Library 형태로 메트릭 수집
- https://github.com/prometheus/jmx_exporter
- https://github.com/prometheus/client_java
SLF4J 와 유사한 방법으로 Micrometer 를 사용
- https://micrometer.io/
## Java Dependencies (Maven)
pom.xml
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
...
# HELP jvm_memory_usage_after_gc_percent The percentage of long-lived heap pool used after the last GC event, in the range [0..1]
# TYPE jvm_memory_usage_after_gc_percent gauge
jvm_memory_usage_after_gc_percent{application="hello-observability",area="heap",pool="long-lived",} 0.01688990317100336
# HELP jvm_gc_max_data_size_bytes Max size of long-lived heap memory pool
# TYPE jvm_gc_max_data_size_bytes gauge
jvm_gc_max_data_size_bytes{application="hello-observability",} 2.283798528E9
# HELP executor_pool_max_threads The maximum allowed number of threads in the pool
# TYPE executor_pool_max_threads gauge
executor_pool_max_threads{application="hello-observability",name="applicationTaskExecutor",} 2.147483647E9
# HELP jvm_memory_used_bytes The amount of used memory
# TYPE jvm_memory_used_bytes gauge
jvm_memory_used_bytes{application="hello-observability",area="nonheap",id="Metaspace",} 1.1375316E8
jvm_memory_used_bytes{application="hello-observability",area="heap",id="G1 Old Gen",} 3.8573136E7
jvm_memory_used_bytes{application="hello-observability",area="nonheap",id="CodeHeap 'non-nmethods'",} 1463552.0
jvm_memory_used_bytes{application="hello-observability",area="heap",id="G1 Eden Space",} 6.7108864E7
jvm_memory_used_bytes{application="hello-observability",area="nonheap",id="CodeHeap 'profiled nmethods'",} 3.8239488E7
jvm_memory_used_bytes{application="hello-observability",area="heap",id="G1 Survivor Space",} 1.048576E7
jvm_memory_used_bytes{application="hello-observability",area="nonheap",id="CodeHeap 'non-profiled nmethods'",} 1.2258176E7
jvm_memory_used_bytes{application="hello-observability",area="nonheap",id="Compressed Class Space",} 1.3920448E7
...
Good data can help with the cultural shift to new systems
Improved debugging and troubleshooting
Reducing MTTD and MTTR
Designs validated with data
Reduced defects; more issues caught proactively
Improved feature velocity
Good Observability (Cultural)
Good data can help with the cultural shift to new systems
Builds transparency across teams
Shared understanding of complex components
Decisions not (entirely) driven or explained by gut feelings or guessing
Freedom to experiment
Blameless culture
Context not control
Thank You
쉽게 설명하면 마이크로서비스에서 각각의 서비스를 span이라고 가정하면, span간 연결되는 점선을 trace의 context를 나타냅니다. 각 context에는 프로세스 내부의 function 또는 RPC를 통해 전달되는 여러가지 정보들이 담겨있습니다.
span context 이외에도 뒤에서 설명할 trace와 span의 parent 식별자(id)와 prometheus와 유사하게 custom label로 process 및 request 관련 정보들을 담을수 있습니다.
Nginx Plus -http://nginx/api
## Demo - Nginx Plus Exporter
http://nginx-exporter:9113/metrics 에서 nginx plus metrics 확인
```properties
# HELP nginxplus_connections_accepted Accepted client connections
# TYPE nginxplus_connections_accepted counter
nginxplus_connections_accepted 88
# HELP nginxplus_connections_active Active client connections
# TYPE nginxplus_connections_active gauge
nginxplus_connections_active 1
# HELP nginxplus_http_requests_current Current http requests
# TYPE nginxplus_http_requests_current gauge
nginxplus_http_requests_current 1
# HELP nginxplus_http_requests_total Total http requests
# TYPE nginxplus_http_requests_total counter
nginxplus_http_requests_total 539
NGNIX Plus - https://grafana.com/grafana/dashboards/12930-nginx/