diff --git a/internal/export/doc/en/inputs/redis.md b/internal/export/doc/en/inputs/redis.md index 8d8c623946..58d45e4881 100755 --- a/internal/export/doc/en/inputs/redis.md +++ b/internal/export/doc/en/inputs/redis.md @@ -42,7 +42,7 @@ Already tested version: - Redis version v5.0+ -When collecting data under the master-slave architecture, please configure the host information of the slave node for data collection, and you can get the metric information related to the master-slave. +When collecting data under the master-slave architecture, please configure the host information of the slave node or master node for data collection, and you can get the different metric information related to the master-slave. Create Monitor User (**optional**) diff --git a/internal/export/doc/zh/inputs/redis.md b/internal/export/doc/zh/inputs/redis.md index 4f5db9727e..4d80405943 100644 --- a/internal/export/doc/zh/inputs/redis.md +++ b/internal/export/doc/zh/inputs/redis.md @@ -40,7 +40,7 @@ Redis 指标采集器,采集以下数据: ### 前置条件 {#reqirement} -- 在采集主从架构下数据时,请配置从节点的主机信息进行数据采集,可以得到主从相关的指标信息。 +- 在采集主从架构下数据时,配置从节点或主节点的主机信息进行数据采集,可以得到不同的主从相关的指标信息。 - 创建监控用户(**可选**):redis 6.0+ 进入 `redis-cli` 命令行,创建用户并且授权: ```sql diff --git a/internal/plugins/inputs/redis/input.go b/internal/plugins/inputs/redis/input.go index be59472e36..676cc8de32 100644 --- a/internal/plugins/inputs/redis/input.go +++ b/internal/plugins/inputs/redis/input.go @@ -539,6 +539,7 @@ func (*Input) SampleMeasurement() []inputs.Measurement { &commandMeasurement{}, &dbMeasurement{}, &infoMeasurement{}, + &replicaMeasurement{}, &latencyMeasurement{}, &slowlogMeasurement{}, } @@ -569,6 +570,7 @@ func (ipt *Input) Resume() error { func defaultInput() *Input { getClientFieldMap() getInfoFieldMap() + getReplicaFieldMap() getClusterFieldMap() return &Input{ diff --git a/internal/plugins/inputs/redis/metric_redis_info.go b/internal/plugins/inputs/redis/metric_redis_info.go index ea3165834f..d71acd45fb 100644 --- a/internal/plugins/inputs/redis/metric_redis_info.go +++ b/internal/plugins/inputs/redis/metric_redis_info.go @@ -358,15 +358,27 @@ func (m *infoMeasurement) parseInfoData(info string, latencyMs float64, nextTS t val = strings.TrimSuffix(val, "%") + tagsToAdd := map[string]string{ + "redis_version": "unknown", + "role": "unknown", + "redis_build_id": "unknown", + "redis_mode": "unknown", + "os": "unknown", + "maxmemory_policy": "unknown", + "run_id": "unknown", + "process_id": "unknown", + } + + if defaultValue, exists := tagsToAdd[key]; exists { + if val == "" { + val = defaultValue + } + kvs = kvs.AddTag(key, val) + m.tags[key] = val + } + float, err := strconv.ParseFloat(val, 64) if err != nil { - if key == "redis_version" { - if val == "" { - val = "unknown" - } - kvs = kvs.AddTag("redis_version", val) - m.tags["redis_version"] = val - } continue } diff --git a/internal/plugins/inputs/redis/metric_redis_info_test.go b/internal/plugins/inputs/redis/metric_redis_info_test.go index af3dc132a8..6d9669236e 100644 --- a/internal/plugins/inputs/redis/metric_redis_info_test.go +++ b/internal/plugins/inputs/redis/metric_redis_info_test.go @@ -318,8 +318,8 @@ func Test_infoMeasurement_parseInfoData(t *testing.T) { info02: mockInfo, }, want: []string{ - "redis_indo,error_type=WRONGPASS,foo=bar,redis_version=7.0.13 errorstat=8i", - "redis_indo,foo=bar,redis_version=7.0.13 active_defrag_hits=0,active_defrag_key_hits=0,active_defrag_key_misses=0,active_defrag_misses=0,active_defrag_running=0,allocator_active=2080768,allocator_allocated=1738944,allocator_frag_bytes=341824,allocator_frag_ratio=1.2,allocator_resident=4939776,allocator_rss_bytes=2859008,allocator_rss_ratio=2.37,aof_base_size=89,aof_buffer_length=0,aof_current_rewrite_time_sec=-1,aof_current_size=89,aof_delayed_fsync=0,aof_enabled=1,aof_last_cow_size=0,aof_last_rewrite_time_sec=-1,aof_pending_bio_fsync=0,aof_pending_rewrite=0,aof_rewrite_in_progress=0,aof_rewrite_scheduled=0,aof_rewrites=0,arch_bits=64,async_loading=0,blocked_clients=0,client_recent_max_input_buffer=8,client_recent_max_output_buffer=0,clients_in_timeout_table=0,cluster_connections=0,cluster_enabled=0,configured_hz=10,connected_clients=1,connected_slaves=0,current_active_defrag_time=0,current_cow_peak=0,current_cow_size=0,current_cow_size_age=0,current_eviction_exceeded_time=0,current_fork_perc=0,current_save_keys_processed=0,current_save_keys_total=0,dump_payload_sanitizations=0,evicted_clients=0,evicted_keys=0,expire_cycle_cpu_milliseconds=29,expired_keys=0,expired_stale_perc=0,expired_time_cap_reached_count=0,hz=10,info_latency_ms=3.73,instantaneous_input_kbps=0,instantaneous_input_repl_kbps=0,instantaneous_ops_per_sec=0,instantaneous_output_kbps=0,instantaneous_output_repl_kbps=0,io_threaded_reads_processed=0,io_threaded_writes_processed=0,io_threads_active=0,keyspace_hits=0,keyspace_misses=0,latest_fork_usec=0,lazyfree_pending_objects=0,lazyfreed_objects=0,loading=0,lru_clock=4153786,master_repl_offset=0,maxclients=10000,maxmemory=0,mem_aof_buffer=8,mem_clients_normal=1800,mem_clients_slaves=0,mem_cluster_links=0,mem_fragmentation_bytes=5813192,mem_fragmentation_ratio=6.46,mem_not_counted_for_evict=8,mem_replication_backlog=0,mem_total_replication_buffers=0,migrate_cached_sockets=0,module_fork_in_progress=0,module_fork_last_cow_size=0,pubsub_channels=0,pubsub_patterns=0,pubsubshard_channels=0,rdb_bgsave_in_progress=0,rdb_changes_since_last_save=0,rdb_current_bgsave_time_sec=-1,rdb_last_bgsave_time_sec=-1,rdb_last_cow_size=0,rdb_last_load_keys_expired=0,rdb_last_load_keys_loaded=0,rdb_last_save_time=1698651554,rdb_saves=0,rejected_connections=0,repl_backlog_active=0,repl_backlog_first_byte_offset=0,repl_backlog_histlen=0,repl_backlog_size=1048576,rss_overhead_bytes=1937408,rss_overhead_ratio=1.39,second_repl_offset=-1,server_time_usec=1698652602514848,slave_expires_tracked_keys=0,sync_full=0,sync_partial_err=0,sync_partial_ok=0,tcp_port=6379,total_active_defrag_time=0,total_commands_processed=211,total_connections_received=10,total_error_replies=8,total_eviction_exceeded_time=0,total_forks=0,total_net_input_bytes=6445,total_net_output_bytes=407193,total_net_repl_input_bytes=0,total_net_repl_output_bytes=0,total_reads_processed=221,total_system_memory=16826019840,total_writes_processed=213,tracking_clients=0,tracking_total_items=0,tracking_total_keys=0,tracking_total_prefixes=0,unexpected_error_replies=0,uptime_in_days=0,uptime_in_seconds=1048,used_cpu_sys=1.300455,used_cpu_sys_children=0.004007,used_cpu_sys_main_thread=1.296392,used_cpu_sys_percent=0,used_cpu_user=1.717573,used_cpu_user_children=0.007766,used_cpu_user_main_thread=1.714453,used_cpu_user_percent=0,used_memory=1086408,used_memory_dataset=222144,used_memory_dataset_perc=99.11,used_memory_lua=31744,used_memory_overhead=864264,used_memory_peak=1115248,used_memory_peak_perc=97.41,used_memory_rss=6877184,used_memory_scripts=184,used_memory_startup=862272", + "redis_indo,error_type=WRONGPASS,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 errorstat=8i", + "redis_indo,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 active_defrag_hits=0,active_defrag_key_hits=0,active_defrag_key_misses=0,active_defrag_misses=0,active_defrag_running=0,allocator_active=2080768,allocator_allocated=1738944,allocator_frag_bytes=341824,allocator_frag_ratio=1.2,allocator_resident=4939776,allocator_rss_bytes=2859008,allocator_rss_ratio=2.37,aof_base_size=89,aof_buffer_length=0,aof_current_rewrite_time_sec=-1,aof_current_size=89,aof_delayed_fsync=0,aof_enabled=1,aof_last_cow_size=0,aof_last_rewrite_time_sec=-1,aof_pending_bio_fsync=0,aof_pending_rewrite=0,aof_rewrite_in_progress=0,aof_rewrite_scheduled=0,aof_rewrites=0,arch_bits=64,async_loading=0,blocked_clients=0,client_recent_max_input_buffer=8,client_recent_max_output_buffer=0,clients_in_timeout_table=0,cluster_connections=0,cluster_enabled=0,configured_hz=10,connected_clients=1,connected_slaves=0,current_active_defrag_time=0,current_cow_peak=0,current_cow_size=0,current_cow_size_age=0,current_eviction_exceeded_time=0,current_fork_perc=0,current_save_keys_processed=0,current_save_keys_total=0,dump_payload_sanitizations=0,evicted_clients=0,evicted_keys=0,expire_cycle_cpu_milliseconds=29,expired_keys=0,expired_stale_perc=0,expired_time_cap_reached_count=0,hz=10,info_latency_ms=3.73,instantaneous_input_kbps=0,instantaneous_input_repl_kbps=0,instantaneous_ops_per_sec=0,instantaneous_output_kbps=0,instantaneous_output_repl_kbps=0,io_threaded_reads_processed=0,io_threaded_writes_processed=0,io_threads_active=0,keyspace_hits=0,keyspace_misses=0,latest_fork_usec=0,lazyfree_pending_objects=0,lazyfreed_objects=0,loading=0,lru_clock=4153786,master_repl_offset=0,maxclients=10000,maxmemory=0,mem_aof_buffer=8,mem_clients_normal=1800,mem_clients_slaves=0,mem_cluster_links=0,mem_fragmentation_bytes=5813192,mem_fragmentation_ratio=6.46,mem_not_counted_for_evict=8,mem_replication_backlog=0,mem_total_replication_buffers=0,migrate_cached_sockets=0,module_fork_in_progress=0,module_fork_last_cow_size=0,pubsub_channels=0,pubsub_patterns=0,pubsubshard_channels=0,rdb_bgsave_in_progress=0,rdb_changes_since_last_save=0,rdb_current_bgsave_time_sec=-1,rdb_last_bgsave_time_sec=-1,rdb_last_cow_size=0,rdb_last_load_keys_expired=0,rdb_last_load_keys_loaded=0,rdb_last_save_time=1698651554,rdb_saves=0,rejected_connections=0,repl_backlog_active=0,repl_backlog_first_byte_offset=0,repl_backlog_histlen=0,repl_backlog_size=1048576,rss_overhead_bytes=1937408,rss_overhead_ratio=1.39,second_repl_offset=-1,server_time_usec=1698652602514848,slave_expires_tracked_keys=0,sync_full=0,sync_partial_err=0,sync_partial_ok=0,tcp_port=6379,total_active_defrag_time=0,total_commands_processed=211,total_connections_received=10,total_error_replies=8,total_eviction_exceeded_time=0,total_forks=0,total_net_input_bytes=6445,total_net_output_bytes=407193,total_net_repl_input_bytes=0,total_net_repl_output_bytes=0,total_reads_processed=221,total_system_memory=16826019840,total_writes_processed=213,tracking_clients=0,tracking_total_items=0,tracking_total_keys=0,tracking_total_prefixes=0,unexpected_error_replies=0,uptime_in_days=0,uptime_in_seconds=1048,used_cpu_sys=1.300455,used_cpu_sys_children=0.004007,used_cpu_sys_main_thread=1.296392,used_cpu_sys_percent=0,used_cpu_user=1.717573,used_cpu_user_children=0.007766,used_cpu_user_main_thread=1.714453,used_cpu_user_percent=0,used_memory=1086408,used_memory_dataset=222144,used_memory_dataset_perc=99.11,used_memory_lua=31744,used_memory_overhead=864264,used_memory_peak=1115248,used_memory_peak_perc=97.41,used_memory_rss=6877184,used_memory_scripts=184,used_memory_startup=862272", }, wantErr: false, }, @@ -339,32 +339,32 @@ func Test_infoMeasurement_parseInfoData(t *testing.T) { info02: mockInfo, }, want: []string{ - "redis_indo,command_type=acl|setuser,foo=bar,quantile=0.5,redis_version=7.0.13 latency_percentiles_usec=78.335", - "redis_indo,command_type=acl|setuser,foo=bar,quantile=0.99,redis_version=7.0.13 latency_percentiles_usec=103.423", - "redis_indo,command_type=acl|setuser,foo=bar,quantile=0.999,redis_version=7.0.13 latency_percentiles_usec=103.423", - "redis_indo,command_type=auth,foo=bar,quantile=0.5,redis_version=7.0.13 latency_percentiles_usec=51.199", - "redis_indo,command_type=auth,foo=bar,quantile=0.99,redis_version=7.0.13 latency_percentiles_usec=114.175", - "redis_indo,command_type=auth,foo=bar,quantile=0.999,redis_version=7.0.13 latency_percentiles_usec=114.175", - "redis_indo,command_type=client|list,foo=bar,quantile=0.5,redis_version=7.0.13 latency_percentiles_usec=35.071", - "redis_indo,command_type=client|list,foo=bar,quantile=0.99,redis_version=7.0.13 latency_percentiles_usec=88.063", - "redis_indo,command_type=client|list,foo=bar,quantile=0.999,redis_version=7.0.13 latency_percentiles_usec=88.063", - "redis_indo,command_type=command|docs,foo=bar,quantile=0.5,redis_version=7.0.13 latency_percentiles_usec=1835.007", - "redis_indo,command_type=command|docs,foo=bar,quantile=0.99,redis_version=7.0.13 latency_percentiles_usec=1835.007", - "redis_indo,command_type=command|docs,foo=bar,quantile=0.999,redis_version=7.0.13 latency_percentiles_usec=1835.007", - "redis_indo,command_type=info,foo=bar,quantile=0.5,redis_version=7.0.13 latency_percentiles_usec=97.279", - "redis_indo,command_type=info,foo=bar,quantile=0.99,redis_version=7.0.13 latency_percentiles_usec=581.631", - "redis_indo,command_type=info,foo=bar,quantile=0.999,redis_version=7.0.13 latency_percentiles_usec=708.607", - "redis_indo,command_type=latency|latest,foo=bar,quantile=0.5,redis_version=7.0.13 latency_percentiles_usec=4.015", - "redis_indo,command_type=latency|latest,foo=bar,quantile=0.99,redis_version=7.0.13 latency_percentiles_usec=30.079", - "redis_indo,command_type=latency|latest,foo=bar,quantile=0.999,redis_version=7.0.13 latency_percentiles_usec=30.079", - "redis_indo,command_type=ping,foo=bar,quantile=0.5,redis_version=7.0.13 latency_percentiles_usec=2.007", - "redis_indo,command_type=ping,foo=bar,quantile=0.99,redis_version=7.0.13 latency_percentiles_usec=2.007", - "redis_indo,command_type=ping,foo=bar,quantile=0.999,redis_version=7.0.13 latency_percentiles_usec=2.007", - "redis_indo,command_type=slowlog|get,foo=bar,quantile=0.5,redis_version=7.0.13 latency_percentiles_usec=5.023", - "redis_indo,command_type=slowlog|get,foo=bar,quantile=0.99,redis_version=7.0.13 latency_percentiles_usec=24.063", - "redis_indo,command_type=slowlog|get,foo=bar,quantile=0.999,redis_version=7.0.13 latency_percentiles_usec=24.063", - "redis_indo,error_type=WRONGPASS,foo=bar,redis_version=7.0.13 errorstat=8i", - "redis_indo,foo=bar,redis_version=7.0.13 active_defrag_hits=0,active_defrag_key_hits=0,active_defrag_key_misses=0,active_defrag_misses=0,active_defrag_running=0,allocator_active=2080768,allocator_allocated=1738944,allocator_frag_bytes=341824,allocator_frag_ratio=1.2,allocator_resident=4939776,allocator_rss_bytes=2859008,allocator_rss_ratio=2.37,aof_base_size=89,aof_buffer_length=0,aof_current_rewrite_time_sec=-1,aof_current_size=89,aof_delayed_fsync=0,aof_enabled=1,aof_last_cow_size=0,aof_last_rewrite_time_sec=-1,aof_pending_bio_fsync=0,aof_pending_rewrite=0,aof_rewrite_in_progress=0,aof_rewrite_scheduled=0,aof_rewrites=0,arch_bits=64,async_loading=0,blocked_clients=0,client_recent_max_input_buffer=8,client_recent_max_output_buffer=0,clients_in_timeout_table=0,cluster_connections=0,cluster_enabled=0,configured_hz=10,connected_clients=1,connected_slaves=0,current_active_defrag_time=0,current_cow_peak=0,current_cow_size=0,current_cow_size_age=0,current_eviction_exceeded_time=0,current_fork_perc=0,current_save_keys_processed=0,current_save_keys_total=0,dump_payload_sanitizations=0,evicted_clients=0,evicted_keys=0,expire_cycle_cpu_milliseconds=29,expired_keys=0,expired_stale_perc=0,expired_time_cap_reached_count=0,hz=10,info_latency_ms=3.73,instantaneous_input_kbps=0,instantaneous_input_repl_kbps=0,instantaneous_ops_per_sec=0,instantaneous_output_kbps=0,instantaneous_output_repl_kbps=0,io_threaded_reads_processed=0,io_threaded_writes_processed=0,io_threads_active=0,keyspace_hits=0,keyspace_misses=0,latest_fork_usec=0,lazyfree_pending_objects=0,lazyfreed_objects=0,loading=0,lru_clock=4153786,master_repl_offset=0,maxclients=10000,maxmemory=0,mem_aof_buffer=8,mem_clients_normal=1800,mem_clients_slaves=0,mem_cluster_links=0,mem_fragmentation_bytes=5813192,mem_fragmentation_ratio=6.46,mem_not_counted_for_evict=8,mem_replication_backlog=0,mem_total_replication_buffers=0,migrate_cached_sockets=0,module_fork_in_progress=0,module_fork_last_cow_size=0,pubsub_channels=0,pubsub_patterns=0,pubsubshard_channels=0,rdb_bgsave_in_progress=0,rdb_changes_since_last_save=0,rdb_current_bgsave_time_sec=-1,rdb_last_bgsave_time_sec=-1,rdb_last_cow_size=0,rdb_last_load_keys_expired=0,rdb_last_load_keys_loaded=0,rdb_last_save_time=1698651554,rdb_saves=0,rejected_connections=0,repl_backlog_active=0,repl_backlog_first_byte_offset=0,repl_backlog_histlen=0,repl_backlog_size=1048576,rss_overhead_bytes=1937408,rss_overhead_ratio=1.39,second_repl_offset=-1,server_time_usec=1698652602514848,slave_expires_tracked_keys=0,sync_full=0,sync_partial_err=0,sync_partial_ok=0,tcp_port=6379,total_active_defrag_time=0,total_commands_processed=211,total_connections_received=10,total_error_replies=8,total_eviction_exceeded_time=0,total_forks=0,total_net_input_bytes=6445,total_net_output_bytes=407193,total_net_repl_input_bytes=0,total_net_repl_output_bytes=0,total_reads_processed=221,total_system_memory=16826019840,total_writes_processed=213,tracking_clients=0,tracking_total_items=0,tracking_total_keys=0,tracking_total_prefixes=0,unexpected_error_replies=0,uptime_in_days=0,uptime_in_seconds=1048,used_cpu_sys=1.300455,used_cpu_sys_children=0.004007,used_cpu_sys_main_thread=1.296392,used_cpu_sys_percent=0,used_cpu_user=1.717573,used_cpu_user_children=0.007766,used_cpu_user_main_thread=1.714453,used_cpu_user_percent=0,used_memory=1086408,used_memory_dataset=222144,used_memory_dataset_perc=99.11,used_memory_lua=31744,used_memory_overhead=864264,used_memory_peak=1115248,used_memory_peak_perc=97.41,used_memory_rss=6877184,used_memory_scripts=184,used_memory_startup=862272", + "redis_indo,command_type=acl|setuser,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.5,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=78.335", + "redis_indo,command_type=acl|setuser,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.99,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=103.423", + "redis_indo,command_type=acl|setuser,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.999,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=103.423", + "redis_indo,command_type=auth,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.5,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=51.199", + "redis_indo,command_type=auth,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.99,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=114.175", + "redis_indo,command_type=auth,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.999,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=114.175", + "redis_indo,command_type=client|list,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.5,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=35.071", + "redis_indo,command_type=client|list,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.99,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=88.063", + "redis_indo,command_type=client|list,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.999,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=88.063", + "redis_indo,command_type=command|docs,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.5,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=1835.007", + "redis_indo,command_type=command|docs,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.99,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=1835.007", + "redis_indo,command_type=command|docs,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.999,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=1835.007", + "redis_indo,command_type=info,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.5,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=97.279", + "redis_indo,command_type=info,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.99,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=581.631", + "redis_indo,command_type=info,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.999,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=708.607", + "redis_indo,command_type=latency|latest,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.5,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=4.015", + "redis_indo,command_type=latency|latest,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.99,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=30.079", + "redis_indo,command_type=latency|latest,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.999,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=30.079", + "redis_indo,command_type=ping,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.5,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=2.007", + "redis_indo,command_type=ping,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.99,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=2.007", + "redis_indo,command_type=ping,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.999,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=2.007", + "redis_indo,command_type=slowlog|get,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.5,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=5.023", + "redis_indo,command_type=slowlog|get,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.99,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=24.063", + "redis_indo,command_type=slowlog|get,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,quantile=0.999,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 latency_percentiles_usec=24.063", + "redis_indo,error_type=WRONGPASS,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 errorstat=8i", + "redis_indo,foo=bar,maxmemory_policy=noeviction,os=Linux\\ 3.10.0-957.el7.x86_64\\ x86_64,process_id=1,redis_build_id=e9f5dfc882060196,redis_mode=standalone,redis_version=7.0.13,role=master,run_id=85339d49fa59b92c47a3da041dd85b104af97f27 active_defrag_hits=0,active_defrag_key_hits=0,active_defrag_key_misses=0,active_defrag_misses=0,active_defrag_running=0,allocator_active=2080768,allocator_allocated=1738944,allocator_frag_bytes=341824,allocator_frag_ratio=1.2,allocator_resident=4939776,allocator_rss_bytes=2859008,allocator_rss_ratio=2.37,aof_base_size=89,aof_buffer_length=0,aof_current_rewrite_time_sec=-1,aof_current_size=89,aof_delayed_fsync=0,aof_enabled=1,aof_last_cow_size=0,aof_last_rewrite_time_sec=-1,aof_pending_bio_fsync=0,aof_pending_rewrite=0,aof_rewrite_in_progress=0,aof_rewrite_scheduled=0,aof_rewrites=0,arch_bits=64,async_loading=0,blocked_clients=0,client_recent_max_input_buffer=8,client_recent_max_output_buffer=0,clients_in_timeout_table=0,cluster_connections=0,cluster_enabled=0,configured_hz=10,connected_clients=1,connected_slaves=0,current_active_defrag_time=0,current_cow_peak=0,current_cow_size=0,current_cow_size_age=0,current_eviction_exceeded_time=0,current_fork_perc=0,current_save_keys_processed=0,current_save_keys_total=0,dump_payload_sanitizations=0,evicted_clients=0,evicted_keys=0,expire_cycle_cpu_milliseconds=29,expired_keys=0,expired_stale_perc=0,expired_time_cap_reached_count=0,hz=10,info_latency_ms=3.73,instantaneous_input_kbps=0,instantaneous_input_repl_kbps=0,instantaneous_ops_per_sec=0,instantaneous_output_kbps=0,instantaneous_output_repl_kbps=0,io_threaded_reads_processed=0,io_threaded_writes_processed=0,io_threads_active=0,keyspace_hits=0,keyspace_misses=0,latest_fork_usec=0,lazyfree_pending_objects=0,lazyfreed_objects=0,loading=0,lru_clock=4153786,master_repl_offset=0,maxclients=10000,maxmemory=0,mem_aof_buffer=8,mem_clients_normal=1800,mem_clients_slaves=0,mem_cluster_links=0,mem_fragmentation_bytes=5813192,mem_fragmentation_ratio=6.46,mem_not_counted_for_evict=8,mem_replication_backlog=0,mem_total_replication_buffers=0,migrate_cached_sockets=0,module_fork_in_progress=0,module_fork_last_cow_size=0,pubsub_channels=0,pubsub_patterns=0,pubsubshard_channels=0,rdb_bgsave_in_progress=0,rdb_changes_since_last_save=0,rdb_current_bgsave_time_sec=-1,rdb_last_bgsave_time_sec=-1,rdb_last_cow_size=0,rdb_last_load_keys_expired=0,rdb_last_load_keys_loaded=0,rdb_last_save_time=1698651554,rdb_saves=0,rejected_connections=0,repl_backlog_active=0,repl_backlog_first_byte_offset=0,repl_backlog_histlen=0,repl_backlog_size=1048576,rss_overhead_bytes=1937408,rss_overhead_ratio=1.39,second_repl_offset=-1,server_time_usec=1698652602514848,slave_expires_tracked_keys=0,sync_full=0,sync_partial_err=0,sync_partial_ok=0,tcp_port=6379,total_active_defrag_time=0,total_commands_processed=211,total_connections_received=10,total_error_replies=8,total_eviction_exceeded_time=0,total_forks=0,total_net_input_bytes=6445,total_net_output_bytes=407193,total_net_repl_input_bytes=0,total_net_repl_output_bytes=0,total_reads_processed=221,total_system_memory=16826019840,total_writes_processed=213,tracking_clients=0,tracking_total_items=0,tracking_total_keys=0,tracking_total_prefixes=0,unexpected_error_replies=0,uptime_in_days=0,uptime_in_seconds=1048,used_cpu_sys=1.300455,used_cpu_sys_children=0.004007,used_cpu_sys_main_thread=1.296392,used_cpu_sys_percent=0,used_cpu_user=1.717573,used_cpu_user_children=0.007766,used_cpu_user_main_thread=1.714453,used_cpu_user_percent=0,used_memory=1086408,used_memory_dataset=222144,used_memory_dataset_perc=99.11,used_memory_lua=31744,used_memory_overhead=864264,used_memory_peak=1115248,used_memory_peak_perc=97.41,used_memory_rss=6877184,used_memory_scripts=184,used_memory_startup=862272", }, wantErr: false, }, diff --git a/internal/plugins/inputs/redis/metric_replica.go b/internal/plugins/inputs/redis/metric_replica.go index b382752b90..978dfd98ed 100644 --- a/internal/plugins/inputs/redis/metric_replica.go +++ b/internal/plugins/inputs/redis/metric_replica.go @@ -28,14 +28,21 @@ func (m *replicaMeasurement) Info() *inputs.MeasurementInfo { Name: redisReplica, Type: "metric", Fields: map[string]interface{}{ - "repl_delay": &inputs.FieldInfo{DataType: inputs.Int, Type: inputs.Gauge, Desc: "Replica delay"}, - "master_link_down_since_seconds": &inputs.FieldInfo{DataType: inputs.Int, Type: inputs.Gauge, Desc: "Number of seconds since the link is down"}, + "master_repl_offset": &inputs.FieldInfo{DataType: inputs.Int, Type: inputs.Gauge, Desc: "The server's current replication offset."}, + // "repl_delay": &inputs.FieldInfo{DataType: inputs.Int, Type: inputs.Gauge, Desc: "Replica delay"}, + "master_link_down_since_seconds": &inputs.FieldInfo{DataType: inputs.Int, Type: inputs.Count, Desc: "Number of seconds since the link is down when the link between master and replica is down, only collected for slave redis."}, + "master_link_status": &inputs.FieldInfo{DataType: inputs.Int, Type: inputs.Gauge, Desc: "Status of the link (up/down), `1` for up, `0` for down, only collected for slave redis."}, + "slave_offset": &inputs.FieldInfo{DataType: inputs.Int, Type: inputs.Gauge, Desc: "Slave offset, only collected for master redis."}, + "slave_lag": &inputs.FieldInfo{DataType: inputs.Int, Type: inputs.Gauge, Desc: "Slave lag, only collected for master redis."}, }, Tags: map[string]interface{}{ - "host": &inputs.TagInfo{Desc: "Hostname"}, - "server": &inputs.TagInfo{Desc: "Server addr"}, - "service_name": &inputs.TagInfo{Desc: "Service name"}, - "slave_id": &inputs.TagInfo{Desc: "Slave ID"}, + "host": &inputs.TagInfo{Desc: "Hostname."}, + "server": &inputs.TagInfo{Desc: "Server addr."}, + "service_name": &inputs.TagInfo{Desc: "Service name."}, + "slave_id": &inputs.TagInfo{Desc: "Slave ID, only collected for master redis."}, + "slave_addr": &inputs.TagInfo{Desc: "Slave addr, only collected for master redis."}, + "slave_state": &inputs.TagInfo{Desc: "Slave state, only collected for master redis."}, + "master_addr": &inputs.TagInfo{Desc: "Master addr, only collected for slave redis."}, }, } } @@ -69,19 +76,39 @@ var slaveMatch = regexp.MustCompile(`^slave\d+`) // repl_backlog_size:1048576 // repl_backlog_first_byte_offset:1 // repl_backlog_histlen:966. +// +// role:slave +// master_host:127.0.0.1 +// master_port:6380 +// master_link_status:down +// master_last_io_seconds_ago:-1 +// master_sync_in_progress:0 +// slave_repl_offset:1 +// master_link_down_since_seconds:1724739099 +// slave_priority:100 +// slave_read_only:1 +// connected_slaves:0 +// master_replid:45a37268f2359e5367c5767ce93ff303ad3f1918 +// master_replid2:0000000000000000000000000000000000000000 +// master_repl_offset:0 +// second_repl_offset:-1 +// repl_backlog_active:0 +// repl_backlog_size:1048576 +// repl_backlog_first_byte_offset:0 +// repl_backlog_histlen:0 + func (ipt *Input) parseReplicaData(list string) ([]*point.Point, error) { collectCache := []*point.Point{} opts := point.DefaultMetricOptions() opts = append(opts, point.WithTime(time.Now())) + var masterIP, masterPort string - masterDownSeconds := map[string]float64{} - masterData := map[string]float64{} - - // master rdr := strings.NewReader(list) scanner := bufio.NewScanner(rdr) for scanner.Scan() { + var kvs point.KVs line := scanner.Text() + if len(line) == 0 || line[0] == '#' { continue } @@ -93,93 +120,48 @@ func (ipt *Input) parseReplicaData(list string) ([]*point.Point, error) { key, value := record[0], record[1] - if key == "master_repl_offset" { - masterData["master_repl_offset"], _ = strconv.ParseFloat(value, 64) - } - - if key == "master_link_down_since_seconds" { - masterDownSeconds["master_link_down_since_seconds"], _ = strconv.ParseFloat(value, 64) - } - } - - // slaves - rdr = strings.NewReader(list) - scanner = bufio.NewScanner(rdr) - for scanner.Scan() { - var kvs point.KVs - slaveData := map[string]float64{} - var slaveID, ip, port string - - line := scanner.Text() - if len(line) == 0 || line[0] == '#' { - continue + if key == "master_host" { + masterIP = value } - - record := strings.Split(line, ":") - if len(record) != 2 { - continue + if key == "master_port" { + masterPort = value } - key, value := record[0], record[1] - - if slaveMatch.MatchString(key) { - slaveID = strings.TrimPrefix(key, "slave") - kv := strings.SplitN(value, ",", 5) - if len(kv) != 5 { - continue - } - - split := strings.Split(kv[0], "=") - if len(split) != 2 { - l.Warnf("Failed to parse slave ip, got %s", kv[0]) - continue + // key in the replicaMeasurement + if _, has := replicaFieldMap[key]; has { + // slave redis, collect master data + if key == "master_link_status" { + switch value { + case "up": + kvs = kvs.Add(key, 1, false, false) + case "down": + kvs = kvs.Add(key, 0, false, false) + default: + l.Warnf("parseReplicaData: unexpected value for master_link_status, got %s", value) + continue + } + } else { + float, err := strconv.ParseFloat(value, 64) + if err != nil { + l.Warnf("parseMasterData: %s, expect to be int, got %s", err, value) + continue + } + kvs = kvs.Add(key, float, false, false) } - ip = split[1] - - split = strings.Split(kv[1], "=") - if len(split) != 2 { - l.Warnf("Failed to parse slave port, got %s", kv[1]) - continue - } - port = split[1] - - split = strings.Split(kv[3], "=") - if len(split) != 2 { - l.Warnf("Failed to parse slave offset, got %s", kv[3]) - continue - } - - temp, err := strconv.ParseFloat(split[1], 64) - if err != nil { - l.Warnf("ParseFloat: %s, slaveOffset expect to be int, got %s", err, split[1]) - continue - } - slaveData["slave_offset"] = temp - } - - var masterOffset, slaveOffset float64 - var ok bool - if masterOffset, ok = masterData["master_repl_offset"]; !ok { - continue - } - if slaveOffset, ok = slaveData["slave_offset"]; !ok { - continue } - - delay := masterOffset - slaveOffset - addr := fmt.Sprintf("%s:%s", ip, port) - if addr != ":" { + // special key for slave data + if slaveMatch.MatchString(key) { + // master redis, collect slave data + slaveID, ip, port, state, offset, lag := parseConnectedSlaveString(key, value) + kvs = kvs.AddTag("slave_id", slaveID) kvs = kvs.AddTag("slave_addr", fmt.Sprintf("%s:%s", ip, port)) + kvs = kvs.AddTag("slave_state", state) + kvs = kvs.Add("slave_offset", offset, false, false) + kvs = kvs.Add("slave_lag", lag, false, false) } - kvs = kvs.AddTag("slave_id", slaveID) - - if delay >= 0 { - kvs = kvs.Add("repl_delay", delay, false, false) - } - - for k, v := range masterDownSeconds { - kvs = kvs.Add(k, v, false, false) + if masterIP != "" && masterPort != "" { + kvs = kvs.AddTag("master_addr", fmt.Sprintf("%s:%s", masterIP, masterPort)) } if kvs.FieldCount() > 0 { @@ -189,6 +171,45 @@ func (ipt *Input) parseReplicaData(list string) ([]*point.Point, error) { collectCache = append(collectCache, point.NewPointV2(redisReplica, kvs, opts...)) } } - return collectCache, nil } + +var replicaFieldMap = map[string]struct{}{} + +func getReplicaFieldMap() { + m := replicaMeasurement{} + for k := range m.Info().Fields { + replicaFieldMap[k] = struct{}{} + } +} + +/* +slave0:ip=10.254.11.1,port=6379,state=online,offset=1751844676,lag=0 +slave1:ip=10.254.11.2,port=6379,state=online,offset=1751844222,lag=0 +*/ +func parseConnectedSlaveString(slaveName string, keyValues string) (id string, ip string, port string, state string, offset float64, lag float64) { + slaveID := strings.TrimPrefix(slaveName, "slave") + kv := strings.SplitN(keyValues, ",", 5) + if len(kv) != 5 { + return + } + for _, v := range kv { + k := strings.Split(v, "=") + if len(k) != 2 { + continue + } + switch k[0] { + case "ip": + ip = k[1] + case "port": + port = k[1] + case "state": + state = k[1] + case "offset": + offset, _ = strconv.ParseFloat(k[1], 64) + case "lag": + lag, _ = strconv.ParseFloat(k[1], 64) + } + } + return slaveID, ip, port, state, offset, lag +} diff --git a/internal/plugins/inputs/redis/metric_replica_test.go b/internal/plugins/inputs/redis/metric_replica_test.go index 3386641c2e..958ad26d62 100644 --- a/internal/plugins/inputs/redis/metric_replica_test.go +++ b/internal/plugins/inputs/redis/metric_replica_test.go @@ -45,8 +45,9 @@ func TestInput_parseReplicData(t *testing.T) { list: mockReplicaData01, }, want: []string{ - "redis_replica,foo=bar,host=HOST,slave_addr=127.0.0.1:6379,slave_id=1 repl_delay=10", - "redis_replica,foo=bar,host=HOST,slave_addr=127.0.0.1:6380,slave_id=0 repl_delay=10", + "redis_replica,foo=bar,host=HOST master_repl_offset=4056", + "redis_replica,foo=bar,host=HOST,slave_addr=127.0.0.1:6379,slave_id=1,slave_state=online slave_lag=0,slave_offset=4046", + "redis_replica,foo=bar,host=HOST,slave_addr=127.0.0.1:6380,slave_id=0,slave_state=online slave_lag=0,slave_offset=4046", }, wantErr: false, }, @@ -62,9 +63,11 @@ func TestInput_parseReplicData(t *testing.T) { list: mockReplicaData01, }, want: []string{ - "redis_replica,election=TRUE,foo=bar,slave_addr=127.0.0.1:6379,slave_id=1 repl_delay=10", - "redis_replica,election=TRUE,foo=bar,slave_addr=127.0.0.1:6380,slave_id=0 repl_delay=10", + "redis_replica,election=TRUE,foo=bar master_repl_offset=4056", + "redis_replica,election=TRUE,foo=bar,slave_addr=127.0.0.1:6379,slave_id=1,slave_state=online slave_lag=0,slave_offset=4046", + "redis_replica,election=TRUE,foo=bar,slave_addr=127.0.0.1:6380,slave_id=0,slave_state=online slave_lag=0,slave_offset=4046", }, + wantErr: false, }, { @@ -78,7 +81,9 @@ func TestInput_parseReplicData(t *testing.T) { args: args{ list: mockReplicaData02, }, - want: []string{}, + want: []string{ + "redis_replica,foo=bar,host=HOST master_repl_offset=0", + }, wantErr: false, }, }