Skip to content

Commit

Permalink
feat(metrics): testing the metrics output directly
Browse files Browse the repository at this point in the history
  • Loading branch information
stvnksslr committed Sep 12, 2024
1 parent 4d8a005 commit a35dffa
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ package main

import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"

"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
Expand Down Expand Up @@ -76,3 +83,66 @@ func TestMonitorQueue(t *testing.T) {
func TestHealthcheck(t *testing.T) {
assert.NotPanics(t, func() { healthcheck(nil, nil) })
}

func TestSQSMetrics(t *testing.T) {
// Set up mock SQS client
mockClient := new(MockSQSClient)
originalSvc := svc
svc = mockClient
defer func() { svc = originalSvc }()

// Set up test queue URL and expected metrics
queueURL := "https://sqs.us-west-2.amazonaws.com/123456789012/TestQueue"
expectedOutput := &sqs.GetQueueAttributesOutput{
Attributes: map[string]string{
"ApproximateNumberOfMessages": "10",
"ApproximateNumberOfMessagesDelayed": "5",
"ApproximateNumberOfMessagesNotVisible": "2",
},
}

// Configure mock client to return the expected output
mockClient.On("GetQueueAttributes", mock.Anything, mock.Anything, mock.Anything).Return(expectedOutput, nil)

// Set environment variables
os.Setenv("SQS_QUEUE_URLS", queueURL)
os.Setenv("SQS_MONITOR_INTERVAL_SECONDS", "1")
defer os.Unsetenv("SQS_QUEUE_URLS")
defer os.Unsetenv("SQS_MONITOR_INTERVAL_SECONDS")

// Start the monitor in a goroutine
go monitorQueues([]string{queueURL})

// Wait for metrics to be collected
time.Sleep(2 * time.Second)

// Set up test HTTP server
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
testServer := httptest.NewServer(mux)
defer testServer.Close()

// Send request to /metrics endpoint
resp, err := http.Get(testServer.URL + "/metrics")
assert.NoError(t, err)
defer resp.Body.Close()

// Read response body
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)

// Check for expected metrics
bodyString := string(body)
expectedMetrics := []string{
`sqs_approximatenumberofmessages{queue="TestQueue"} 10`,
`sqs_approximatenumberofmessagesdelayed{queue="TestQueue"} 5`,
`sqs_approximatenumberofmessagesnotvisible{queue="TestQueue"} 2`,
}

for _, metric := range expectedMetrics {
assert.True(t, strings.Contains(bodyString, metric), fmt.Sprintf("Metric not found: %s", metric))
}

// Clear registered metrics to avoid conflicts in other tests
prometheus.DefaultRegisterer = prometheus.NewRegistry()
}

0 comments on commit a35dffa

Please sign in to comment.