...

Source file src/github.com/letsencrypt/boulder/redis/metrics_test.go

Documentation: github.com/letsencrypt/boulder/redis

     1  package redis
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/letsencrypt/boulder/metrics"
     8  	"github.com/prometheus/client_golang/prometheus"
     9  	"github.com/redis/go-redis/v9"
    10  )
    11  
    12  type mockPoolStatGetter struct{}
    13  
    14  var _ poolStatGetter = mockPoolStatGetter{}
    15  
    16  func (mockPoolStatGetter) PoolStats() *redis.PoolStats {
    17  	return &redis.PoolStats{
    18  		Hits:       13,
    19  		Misses:     7,
    20  		Timeouts:   4,
    21  		TotalConns: 1000,
    22  		IdleConns:  500,
    23  		StaleConns: 10,
    24  	}
    25  }
    26  
    27  func TestMetrics(t *testing.T) {
    28  	mets := newClientMetricsCollector(mockPoolStatGetter{},
    29  		prometheus.Labels{
    30  			"foo": "bar",
    31  		})
    32  	// Check that it has the correct type to satisfy MustRegister
    33  	metrics.NoopRegisterer.MustRegister(mets)
    34  
    35  	expectedMetrics := 6
    36  	outChan := make(chan prometheus.Metric, expectedMetrics)
    37  	mets.Collect(outChan)
    38  
    39  	results := make(map[string]bool)
    40  	for i := 0; i < expectedMetrics; i++ {
    41  		metric := <-outChan
    42  		results[metric.Desc().String()] = true
    43  	}
    44  
    45  	expected := strings.Split(
    46  		`Desc{fqName: "redis_connection_pool_lookups", help: "Number of lookups for a connection in the pool, labeled by hit/miss", constLabels: {foo="bar"}, variableLabels: [{result <nil>}]}
    47  Desc{fqName: "redis_connection_pool_lookups", help: "Number of lookups for a connection in the pool, labeled by hit/miss", constLabels: {foo="bar"}, variableLabels: [{result <nil>}]}
    48  Desc{fqName: "redis_connection_pool_lookups", help: "Number of lookups for a connection in the pool, labeled by hit/miss", constLabels: {foo="bar"}, variableLabels: [{result <nil>}]}
    49  Desc{fqName: "redis_connection_pool_total_conns", help: "Number of total connections in the pool.", constLabels: {foo="bar"}, variableLabels: []}
    50  Desc{fqName: "redis_connection_pool_idle_conns", help: "Number of idle connections in the pool.", constLabels: {foo="bar"}, variableLabels: []}
    51  Desc{fqName: "redis_connection_pool_stale_conns", help: "Number of stale connections removed from the pool.", constLabels: {foo="bar"}, variableLabels: []}`,
    52  		"\n")
    53  
    54  	for _, e := range expected {
    55  		if !results[e] {
    56  			t.Errorf("expected metrics to contain %q, but they didn't", e)
    57  		}
    58  	}
    59  
    60  	if len(results) > len(expected) {
    61  		t.Errorf("expected metrics to contain %d entries, but they contained %d",
    62  			len(expected), len(results))
    63  	}
    64  }
    65  
    66  func TestMustRegisterClientMetricsCollector(t *testing.T) {
    67  	client := mockPoolStatGetter{}
    68  	stats := prometheus.NewRegistry()
    69  	// First registration should succeed.
    70  	MustRegisterClientMetricsCollector(client, stats, map[string]string{"foo": "bar"}, "baz")
    71  	// Duplicate registration should succeed.
    72  	MustRegisterClientMetricsCollector(client, stats, map[string]string{"foo": "bar"}, "baz")
    73  	// Registration with different label values should succeed.
    74  	MustRegisterClientMetricsCollector(client, stats, map[string]string{"f00": "b4r"}, "b4z")
    75  }
    76  

View as plain text