Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support filtering queues by prefix. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package collector

import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"log"
"net/http"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
)

type MetricHandler struct{}
type MetricHandler struct {
QueueNamePrefix string
}

func (h MetricHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
queues,listQueueTags := getQueues()
queues, listQueueTags := getQueues(h.QueueNamePrefix)
for queue, attr := range queues {
msgAvailable := *attr.Attributes["ApproximateNumberOfMessages"]
msgDelayed := *attr.Attributes["ApproximateNumberOfMessagesDelayed"]
Expand All @@ -34,10 +37,10 @@ func getQueueName(url string) (queueName string) {
return
}

func getQueues() (queues map[string]*sqs.GetQueueAttributesOutput, tags map[string]*sqs.ListQueueTagsOutput) {
func getQueues(queueNamePrefix string) (queues map[string]*sqs.GetQueueAttributesOutput, tags map[string]*sqs.ListQueueTagsOutput) {
sess := session.Must(session.NewSession())
client := sqs.New(sess)
result, err := client.ListQueues(nil)
result, err := client.ListQueues(&sqs.ListQueuesInput{QueueNamePrefix: &queueNamePrefix})
if err != nil {
log.Fatal("Error ", err)
}
Expand All @@ -48,6 +51,7 @@ func getQueues() (queues map[string]*sqs.GetQueueAttributesOutput, tags map[stri
if result.QueueUrls == nil {
log.Println("Couldnt find any queues in region:", *sess.Config.Region)
}

for _, urls := range result.QueueUrls {
params := &sqs.GetQueueAttributesInput{
QueueUrl: aws.String(*urls),
Expand All @@ -68,5 +72,5 @@ func getQueues() (queues map[string]*sqs.GetQueueAttributesOutput, tags map[stri
queues[queueName] = resp
tags[queueName] = tagsResp
}
return queues,tags
return queues, tags
}
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ func logRequest(handler http.Handler) http.Handler {

func main() {
var (
listenAddress = flag.String("web.listen-address", ":9434", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
listenAddress = flag.String("web.listen-address", ":9434", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
queueNamePrefix = flag.String("queue-name-prefix", "", "Optional: Filter for queues named with the given prefix.")
)
flag.Parse()

http.Handle(*metricsPath, collector.MetricHandler{})
http.Handle(*metricsPath, collector.MetricHandler{QueueNamePrefix: *queueNamePrefix})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
Expand Down