bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/scollector/collectors/rabbitmq_test.go (about)

     1  package collectors
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"reflect"
     8  	"testing"
     9  
    10  	"bosun.org/opentsdb"
    11  )
    12  
    13  func TestRmqOverview(t *testing.T) {
    14  	t.Parallel()
    15  	ht := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    16  		fmt.Fprintln(w, testRmqOverviewJSON)
    17  	}))
    18  	defer ht.Close()
    19  
    20  	md, err := rabbitmqOverview(ht.URL)
    21  	if err != nil {
    22  		t.Error(err)
    23  	}
    24  	for _, emd := range testRmqOverviewMD {
    25  		if !mdContains(t, md, emd) {
    26  			t.Errorf("md must contain %v", emd)
    27  		}
    28  	}
    29  }
    30  
    31  func TestRmqQueues(t *testing.T) {
    32  	t.Parallel()
    33  	ht := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    34  		fmt.Fprintln(w, testRmqQueuesJSON)
    35  	}))
    36  	defer ht.Close()
    37  
    38  	md, err := rabbitmqQueues(ht.URL)
    39  	if err != nil {
    40  		t.Error(err)
    41  	}
    42  	for _, emd := range testRmqQueuesMD {
    43  		if !mdContains(t, md, emd) {
    44  			t.Errorf("md must contain: '%v'", emd)
    45  		}
    46  	}
    47  
    48  }
    49  
    50  func TestRmqNodes(t *testing.T) {
    51  	t.Parallel()
    52  	ht := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    53  		fmt.Fprintln(w, testRmqNodesJSON)
    54  	}))
    55  	defer ht.Close()
    56  
    57  	md, err := rabbitmqNodes(ht.URL)
    58  	if err != nil {
    59  		t.Error(err)
    60  	}
    61  	for _, emd := range testRmqNodesMD {
    62  		if !mdContains(t, md, emd) {
    63  			t.Errorf("md must contain: '%v'", emd)
    64  		}
    65  	}
    66  }
    67  
    68  func mdContains(t *testing.T, md opentsdb.MultiDataPoint, e *opentsdb.DataPoint) bool {
    69  	for _, d := range md {
    70  		if d.Metric == e.Metric && d.Tags.Equal(e.Tags) {
    71  			if reflect.DeepEqual(d.Value, e.Value) {
    72  				return true
    73  			}
    74  			t.Errorf("values differ, got: %v, expected: %v",
    75  				d.Value, e.Value)
    76  
    77  		}
    78  	}
    79  	return false
    80  }
    81  
    82  var testRmqOverviewJSON = `
    83  {
    84    "rabbitmq_version": "3.4.0",
    85    "cluster_name": "rabbit@rabbit1",
    86    "erlang_version": "R16B03-1",
    87    "erlang_full_version": "Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:4:4] [async-threads:30] [hipe] [kernel-poll:true]",
    88    "message_stats": {
    89      "publish": 87902,
    90      "deliver_get": 87231,
    91      "deliver_no_ack": 87231
    92    },
    93    "queue_totals": {
    94      "messages": 0,
    95      "messages_ready": 0,
    96      "messages_unacknowledged": 0
    97    },
    98    "object_totals": {
    99      "channels": 2,
   100      "connections": 2,
   101      "consumers": 1,
   102      "exchanges": 8,
   103      "queues": 1
   104    },
   105    "node": "rabbit@rabbit1"
   106  }
   107  `
   108  var testRmqOverviewMD = opentsdb.MultiDataPoint{
   109  	&opentsdb.DataPoint{
   110  		Metric:    "rabbitmq.overview.message_stats",
   111  		Timestamp: 0,
   112  		Value:     87231,
   113  		Tags:      opentsdb.TagSet{"host": "rabbit1", "method": "deliver_get"},
   114  	},
   115  }
   116  
   117  var testRmqQueuesJSON = `
   118  [
   119  {
   120    "arguments": {},
   121    "auto_delete": false,
   122    "backing_queue_status": {
   123      "avg_ack_egress_rate": 0,
   124      "avg_ack_ingress_rate": 0,
   125      "avg_egress_rate": 6.278889329693902e-16,
   126      "avg_ingress_rate": 5.174965294975286e-16,
   127      "delta": [
   128        "delta",
   129        "undefined",
   130        0,
   131        "undefined"
   132      ],
   133      "len": 0,
   134      "mirror_seen": 0,
   135      "mirror_senders": 1,
   136      "next_seq_id": 917,
   137      "q1": 0,
   138      "q2": 0,
   139      "q3": 0,
   140      "q4": 0,
   141      "target_ram_count": "infinity"
   142    },
   143    "consumer_utilisation": 1,
   144    "consumers": 1,
   145    "down_slave_nodes": "",
   146    "durable": false,
   147    "memory": 68512,
   148    "message_bytes": 0,
   149    "message_bytes_persistent": 0,
   150    "message_bytes_ram": 0,
   151    "message_bytes_ready": 0,
   152    "message_bytes_unacknowledged": 0,
   153    "message_stats": {
   154      "deliver_get": 90345,
   155      "deliver_no_ack": 90345,
   156      "publish": 91028
   157    },
   158    "messages": 0,
   159    "messages_persistent": 0,
   160    "messages_ram": 0,
   161    "messages_ready": 0,
   162    "messages_ready_ram": 0,
   163    "messages_unacknowledged": 0,
   164    "messages_unacknowledged_ram": 0,
   165    "name": "hello",
   166    "node": "rabbit@rabbit1",
   167    "slave_nodes": [
   168      "rabbit@rabbit2",
   169      "rabbit@rabbit3"
   170    ],
   171    "state": "running",
   172    "synchronised_slave_nodes": [
   173      "rabbit@rabbit2",
   174      "rabbit@rabbit3"
   175    ],
   176    "vhost": "/"
   177  }
   178  ]
   179  `
   180  
   181  var testRmqQueuesMD = opentsdb.MultiDataPoint{
   182  	&opentsdb.DataPoint{
   183  		Metric:    "rabbitmq.queue.consumer_utilisation",
   184  		Timestamp: 0,
   185  		Value:     1.0,
   186  		Tags:      opentsdb.TagSet{"host": "rabbit1", "queue": "hello", "vhost": "/"},
   187  	},
   188  	&opentsdb.DataPoint{
   189  		Metric:    "rabbitmq.queue.slave_nodes",
   190  		Timestamp: 0,
   191  		Value:     2,
   192  		Tags:      opentsdb.TagSet{"host": "rabbit1", "queue": "hello", "vhost": "/"},
   193  	},
   194  	&opentsdb.DataPoint{
   195  		Metric:    "rabbitmq.queue.state",
   196  		Timestamp: 0,
   197  		Value:     0,
   198  		Tags:      opentsdb.TagSet{"host": "rabbit1", "queue": "hello", "vhost": "/"},
   199  	},
   200  }
   201  
   202  var testRmqNodesJSON = `
   203  [
   204  {
   205    "disk_free": 3393945600,
   206    "disk_free_alarm": false,
   207    "disk_free_limit": 50000000,
   208    "fd_total": 1048576,
   209    "fd_used": 29,
   210    "mem_alarm": false,
   211    "mem_limit": 6639666790,
   212    "mem_used": 129488008,
   213    "name": "rabbit@rabbit1",
   214    "net_ticktime": 60,
   215    "os_pid": "13",
   216    "partitions": [
   217       ["rabbit1","rabbit2"],
   218       ["rabbit4","rabbit5"]
   219    ],
   220    "proc_total": 1048576,
   221    "proc_used": 233,
   222    "processors": 4,
   223    "rates_mode": "basic",
   224    "run_queue": 0,
   225    "running": true,
   226    "sockets_total": 943626,
   227    "sockets_used": 5,
   228    "type": "disc",
   229    "uptime": 305499
   230  }
   231  ]
   232  `
   233  
   234  var testRmqNodesMD = opentsdb.MultiDataPoint{
   235  	&opentsdb.DataPoint{
   236  		Metric:    "rabbitmq.node.partitions",
   237  		Timestamp: 0,
   238  		Value:     2,
   239  		Tags:      opentsdb.TagSet{"host": "rabbit1"},
   240  	},
   241  }