summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/activemq/collect.go
blob: 0dbaf5544f879e241d1eb48fe74c89a7e5bd8804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// SPDX-License-Identifier: GPL-3.0-or-later

package activemq

import (
	"fmt"
	"strings"
)

const (
	keyQueues   = "queues"
	keyTopics   = "topics"
	keyAdvisory = "Advisory"
)

var nameReplacer = strings.NewReplacer(".", "_", " ", "")

func (a *ActiveMQ) collect() (map[string]int64, error) {
	metrics := make(map[string]int64)

	var (
		queues *queues
		topics *topics
		err    error
	)

	if queues, err = a.apiClient.getQueues(); err != nil {
		return nil, err
	}

	if topics, err = a.apiClient.getTopics(); err != nil {
		return nil, err
	}

	a.processQueues(queues, metrics)
	a.processTopics(topics, metrics)

	return metrics, nil
}

func (a *ActiveMQ) processQueues(queues *queues, metrics map[string]int64) {
	var (
		count   = len(a.activeQueues)
		updated = make(map[string]bool)
		unp     int
	)

	for _, q := range queues.Items {
		if strings.Contains(q.Name, keyAdvisory) {
			continue
		}

		if !a.activeQueues[q.Name] {
			if a.MaxQueues != 0 && count > a.MaxQueues {
				unp++
				continue
			}

			if !a.filterQueues(q.Name) {
				continue
			}

			a.activeQueues[q.Name] = true
			a.addQueueTopicCharts(q.Name, keyQueues)
		}

		rname := nameReplacer.Replace(q.Name)

		metrics["queues_"+rname+"_consumers"] = q.Stats.ConsumerCount
		metrics["queues_"+rname+"_enqueued"] = q.Stats.EnqueueCount
		metrics["queues_"+rname+"_dequeued"] = q.Stats.DequeueCount
		metrics["queues_"+rname+"_unprocessed"] = q.Stats.EnqueueCount - q.Stats.DequeueCount

		updated[q.Name] = true
	}

	for name := range a.activeQueues {
		if !updated[name] {
			delete(a.activeQueues, name)
			a.removeQueueTopicCharts(name, keyQueues)
		}
	}

	if unp > 0 {
		a.Debugf("%d queues were unprocessed due to max_queues limit (%d)", unp, a.MaxQueues)
	}
}

func (a *ActiveMQ) processTopics(topics *topics, metrics map[string]int64) {
	var (
		count   = len(a.activeTopics)
		updated = make(map[string]bool)
		unp     int
	)

	for _, t := range topics.Items {
		if strings.Contains(t.Name, keyAdvisory) {
			continue
		}

		if !a.activeTopics[t.Name] {
			if a.MaxTopics != 0 && count > a.MaxTopics {
				unp++
				continue
			}

			if !a.filterTopics(t.Name) {
				continue
			}

			a.activeTopics[t.Name] = true
			a.addQueueTopicCharts(t.Name, keyTopics)
		}

		rname := nameReplacer.Replace(t.Name)

		metrics["topics_"+rname+"_consumers"] = t.Stats.ConsumerCount
		metrics["topics_"+rname+"_enqueued"] = t.Stats.EnqueueCount
		metrics["topics_"+rname+"_dequeued"] = t.Stats.DequeueCount
		metrics["topics_"+rname+"_unprocessed"] = t.Stats.EnqueueCount - t.Stats.DequeueCount

		updated[t.Name] = true
	}

	for name := range a.activeTopics {
		if !updated[name] {
			// TODO: delete after timeout?
			delete(a.activeTopics, name)
			a.removeQueueTopicCharts(name, keyTopics)
		}
	}

	if unp > 0 {
		a.Debugf("%d topics were unprocessed due to max_topics limit (%d)", unp, a.MaxTopics)
	}
}

func (a *ActiveMQ) filterQueues(line string) bool {
	if a.queuesFilter == nil {
		return true
	}
	return a.queuesFilter.MatchString(line)
}

func (a *ActiveMQ) filterTopics(line string) bool {
	if a.topicsFilter == nil {
		return true
	}
	return a.topicsFilter.MatchString(line)
}

func (a *ActiveMQ) addQueueTopicCharts(name, typ string) {
	rname := nameReplacer.Replace(name)

	charts := charts.Copy()

	for _, chart := range *charts {
		chart.ID = fmt.Sprintf(chart.ID, typ, rname)
		chart.Title = fmt.Sprintf(chart.Title, name)
		chart.Fam = typ

		for _, dim := range chart.Dims {
			dim.ID = fmt.Sprintf(dim.ID, typ, rname)
		}
	}

	_ = a.charts.Add(*charts...)

}

func (a *ActiveMQ) removeQueueTopicCharts(name, typ string) {
	rname := nameReplacer.Replace(name)

	chart := a.charts.Get(fmt.Sprintf("%s_%s_messages", typ, rname))
	chart.MarkRemove()
	chart.MarkNotCreated()

	chart = a.charts.Get(fmt.Sprintf("%s_%s_unprocessed_messages", typ, rname))
	chart.MarkRemove()
	chart.MarkNotCreated()

	chart = a.charts.Get(fmt.Sprintf("%s_%s_consumers", typ, rname))
	chart.MarkRemove()
	chart.MarkNotCreated()
}