Prometheus, một dự án của Cloud Native Computing Foundation, là một hệ thống giám sát dịch vụ và hệ thống. Nó thu thập các chỉ số [dữ liệu chuỗi thời gian] từ các mục tiêu được định cấu hình ở các khoảng thời gian nhất định, đánh giá các biểu thức quy tắc, hiển thị kết quả và có thể kích hoạt cảnh báo nếu một số điều kiện được coi là đúng. Dữ liệu chuỗi thời gian thô thu được từ máy chủ Prometheus đôi khi khó diễn giải. Để giúp hiểu rõ hơn về các số liệu này, chúng tôi đã tạo một trình bao bọc Python cho api Prometheus http để xử lý và phân tích số liệu dễ dàng hơn
Thư viện
prom = PrometheusConnect[] my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'} prom.get_current_metric_value[metric_name='up', label_config=my_label_config] # Here, we are fetching the values of a particular metric name prom.custom_query[query="prometheus_http_requests_total"] # Now, lets try to fetch the `sum` of the metrics prom.custom_query[query="sum[prometheus_http_requests_total]"]1 bao gồm nhiều mô-đun hỗ trợ kết nối với máy chủ Prometheus, tìm nạp các số liệu cần thiết và thực hiện các hoạt động tổng hợp khác nhau trên dữ liệu chuỗi thời gian
Kết nối và thu thập số liệu từ máy chủ Prometheus
Mô-đun
prom = PrometheusConnect[] my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'} prom.get_current_metric_value[metric_name='up', label_config=my_label_config] # Here, we are fetching the values of a particular metric name prom.custom_query[query="prometheus_http_requests_total"] # Now, lets try to fetch the `sum` of the metrics prom.custom_query[query="sum[prometheus_http_requests_total]"]2 của thư viện có thể được sử dụng để kết nối với máy chủ Prometheus. Mô-đun này về cơ bản là một lớp được tạo để thu thập các số liệu từ máy chủ Prometheus. Nó lưu trữ các tham số kết nối sau
- url - [str] url cho máy chủ prometheus
- tiêu đề – [dict] Từ điển các tiêu đề http được sử dụng để giao tiếp với máy chủ. Ví dụ. {“Ủy quyền”. “người mang my_oauth_token_to_the_host”}
- disable_ssl – [bool] Nếu được đặt thành True, sẽ tắt xác minh chứng chỉ ssl đối với các yêu cầu http được gửi tới máy chủ prometheus
from prometheus_api_client import PrometheusConnect prom = PrometheusConnect[url ="", disable_ssl=True] # Get the list of all the metrics that the Prometheus host scrapes prom.all_metrics[]
Bạn cũng có thể tìm nạp dữ liệu chuỗi thời gian cho một số liệu cụ thể bằng các truy vấn tùy chỉnh như sau
prom = PrometheusConnect[] my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'} prom.get_current_metric_value[metric_name='up', label_config=my_label_config] # Here, we are fetching the values of a particular metric name prom.custom_query[query="prometheus_http_requests_total"] # Now, lets try to fetch the `sum` of the metrics prom.custom_query[query="sum[prometheus_http_requests_total]"]
Chúng tôi cũng có thể sử dụng các truy vấn tùy chỉnh để tìm nạp dữ liệu số liệu trong một khoảng thời gian cụ thể. Ví dụ: hãy thử tìm nạp dữ liệu trong 2 ngày qua cho một số liệu cụ thể theo khối của 1 ngày
# Import the required datetime functions from prometheus_api_client.utils import parse_datetime from datetime import timedelta start_time = parse_datetime["2d"] end_time = parse_datetime["now"] chunk_size = timedelta[days=1] metric_data = prom.get_metric_range_data[ "up{cluster='my_cluster_id'}", # this is the metric name and label config start_time=start_time, end_time=end_time, chunk_size=chunk_size, ]
Để biết thêm các chức năng có trong mô-đun
prom = PrometheusConnect[] my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'} prom.get_current_metric_value[metric_name='up', label_config=my_label_config] # Here, we are fetching the values of a particular metric name prom.custom_query[query="prometheus_http_requests_total"] # Now, lets try to fetch the `sum` of the metrics prom.custom_query[query="sum[prometheus_http_requests_total]"]2, hãy tham khảo điều này
Hiểu dữ liệu số liệu được tìm nạp
Mô-đun
prom = PrometheusConnect[] my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'} prom.get_current_metric_value[metric_name='up', label_config=my_label_config] # Here, we are fetching the values of a particular metric name prom.custom_query[query="prometheus_http_requests_total"] # Now, lets try to fetch the `sum` of the metrics prom.custom_query[query="sum[prometheus_http_requests_total]"]4 khởi tạo danh sách các đối tượng Số liệu cho các số liệu được tìm nạp từ máy chủ Prometheus do truy vấn promql
# Import the MetricsList and Metric modules from prometheus_api_client import PrometheusConnect, MetricsList, Metric prom = PrometheusConnect[] my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'} metric_data = prom.get_metric_range_data[metric_name='up', label_config=my_label_config] metric_object_list = MetricsList[metric_data] # metric_object_list will be initialized as # a list of Metric objects for all the # metrics downloaded using get_metric query # We can see what each of the metric objects look like for item in metric_object_list: print[item.metric_name, item.label_config, "\n"]
Mỗi mục trong
prom = PrometheusConnect[] my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'} prom.get_current_metric_value[metric_name='up', label_config=my_label_config] # Here, we are fetching the values of a particular metric name prom.custom_query[query="prometheus_http_requests_total"] # Now, lets try to fetch the `sum` of the metrics prom.custom_query[query="sum[prometheus_http_requests_total]"]5 được khởi tạo như một đối tượng lớp
prom = PrometheusConnect[] my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'} prom.get_current_metric_value[metric_name='up', label_config=my_label_config] # Here, we are fetching the values of a particular metric name prom.custom_query[query="prometheus_http_requests_total"] # Now, lets try to fetch the `sum` of the metrics prom.custom_query[query="sum[prometheus_http_requests_total]"]6. Hãy xem xét một trong các chỉ số từ lớp
prom = PrometheusConnect[] my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'} prom.get_current_metric_value[metric_name='up', label_config=my_label_config] # Here, we are fetching the values of a particular metric name prom.custom_query[query="prometheus_http_requests_total"] # Now, lets try to fetch the `sum` of the metrics prom.custom_query[query="sum[prometheus_http_requests_total]"]5 để tìm hiểu thêm về lớp
prom = PrometheusConnect[] my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'} prom.get_current_metric_value[metric_name='up', label_config=my_label_config] # Here, we are fetching the values of a particular metric name prom.custom_query[query="prometheus_http_requests_total"] # Now, lets try to fetch the `sum` of the metrics prom.custom_query[query="sum[prometheus_http_requests_total]"]6
prom = PrometheusConnect[] my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'} prom.get_current_metric_value[metric_name='up', label_config=my_label_config] # Here, we are fetching the values of a particular metric name prom.custom_query[query="prometheus_http_requests_total"] # Now, lets try to fetch the `sum` of the metrics prom.custom_query[query="sum[prometheus_http_requests_total]"]2
Để biết thêm các chức năng có trong mô-đun
prom = PrometheusConnect[] my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'} prom.get_current_metric_value[metric_name='up', label_config=my_label_config] # Here, we are fetching the values of a particular metric name prom.custom_query[query="prometheus_http_requests_total"] # Now, lets try to fetch the `sum` of the metrics prom.custom_query[query="sum[prometheus_http_requests_total]"]4 và
# Import the required datetime functions from prometheus_api_client.utils import parse_datetime from datetime import timedelta start_time = parse_datetime["2d"] end_time = parse_datetime["now"] chunk_size = timedelta[days=1] metric_data = prom.get_metric_range_data[ "up{cluster='my_cluster_id'}", # this is the metric name and label config start_time=start_time, end_time=end_time, chunk_size=chunk_size, ]0, hãy tham khảo điều này
Các hàm số liệu bổ sung
Lớp
prom = PrometheusConnect[] my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'} prom.get_current_metric_value[metric_name='up', label_config=my_label_config] # Here, we are fetching the values of a particular metric name prom.custom_query[query="prometheus_http_requests_total"] # Now, lets try to fetch the `sum` of the metrics prom.custom_query[query="sum[prometheus_http_requests_total]"]6 cũng hỗ trợ nhiều chức năng như cộng, cân bằng và vẽ các đối tượng số liệu khác nhauThêm số liệu
Bạn có thể thêm hai đối tượng số liệu cho cùng một chuỗi thời gian như sau
prom = PrometheusConnect[] my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'} prom.get_current_metric_value[metric_name='up', label_config=my_label_config] # Here, we are fetching the values of a particular metric name prom.custom_query[query="prometheus_http_requests_total"] # Now, lets try to fetch the `sum` of the metrics prom.custom_query[query="sum[prometheus_http_requests_total]"]6Cân bằng số liệu
Quá tải toán tử =, để kiểm tra xem hai số liệu có giống nhau không [là cùng một chuỗi thời gian bất kể dữ liệu của chúng là gì]
prom = PrometheusConnect[] my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'} prom.get_current_metric_value[metric_name='up', label_config=my_label_config] # Here, we are fetching the values of a particular metric name prom.custom_query[query="prometheus_http_requests_total"] # Now, lets try to fetch the `sum` of the metrics prom.custom_query[query="sum[prometheus_http_requests_total]"]7Vẽ đồ thị đối tượng số liệu
Vẽ biểu đồ đường rất đơn giản cho chuỗi thời gian số liệu
prom = PrometheusConnect[] my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'} prom.get_current_metric_value[metric_name='up', label_config=my_label_config] # Here, we are fetching the values of a particular metric name prom.custom_query[query="prometheus_http_requests_total"] # Now, lets try to fetch the `sum` of the metrics prom.custom_query[query="sum[prometheus_http_requests_total]"]8
Nhận dữ liệu số liệu dưới dạng gấu trúc DataFrames
Để thực hiện phân tích và thao tác dữ liệu, thường rất hữu ích nếu dữ liệu được biểu diễn bằng cách sử dụng một. Có hai mô-đun trong thư viện này có thể được sử dụng để xử lý các số liệu thô được tìm nạp vào DataFrame
Mô-đun
# Import the required datetime functions from prometheus_api_client.utils import parse_datetime from datetime import timedelta start_time = parse_datetime["2d"] end_time = parse_datetime["now"] chunk_size = timedelta[days=1] metric_data = prom.get_metric_range_data[ "up{cluster='my_cluster_id'}", # this is the metric name and label config start_time=start_time, end_time=end_time, chunk_size=chunk_size, ]2 chuyển đổi dữ liệu "giá trị chỉ số hiện tại" thành biểu diễn DataFrame và
# Import the required datetime functions from prometheus_api_client.utils import parse_datetime from datetime import timedelta start_time = parse_datetime["2d"] end_time = parse_datetime["now"] chunk_size = timedelta[days=1] metric_data = prom.get_metric_range_data[ "up{cluster='my_cluster_id'}", # this is the metric name and label config start_time=start_time, end_time=end_time, chunk_size=chunk_size, ]3 chuyển đổi dữ liệu "giá trị phạm vi chỉ số" thành biểu diễn DataFrame. Ví dụ sử dụng các lớp này có thể được nhìn thấy dưới đây