| 1 | package exporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/ |
| 2 | |
| 3 | import ( |
| 4 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 5 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 6 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 7 | |
| 8 | "github.com/gomodule/redigo/redisscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/" |
| 9 | "github.com/prometheus/client_golang/prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/" |
| 10 | logscip-go gomod github.com/sirupsen/logrus v1.9.4 `github.com/sirupsen/logrus`/ "github.com/sirupsen/logrusscip-go gomod github.com/sirupsen/logrus v1.9.4 `github.com/sirupsen/logrus`/" |
| 11 | ) |
| 12 | |
| 13 | func setupSearchIndexscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/setupSearchIndex().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, addr string) error { |
| 14 | c, err := redisscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/.DialURLscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/DialURL().(addr) |
| 15 | if err != nil { |
| 16 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("setupSearchIndex() - couldn't setup redis for uri %s, err: %s ", addr, err) |
| 17 | return err |
| 18 | } |
| 19 | defer c.Closescip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Close().() |
| 20 | |
| 21 | // create search index, based on https://redis.io/docs/latest/commands/ft.create and https://valkey.io/commands/ft.create |
| 22 | if _, err := doRedisCmdscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/doRedisCmd().(c, "FT.CREATE", "test_index", "SCHEMA", "my_hash_field_key", "VECTOR", "HNSW", "10", "TYPE", "FLOAT32", "DIM", "20", "DISTANCE_METRIC", "COSINE", "M", "4", "EF_CONSTRUCTION", "100"); err != nil { |
| 23 | logscip-go gomod github.com/sirupsen/logrus v1.9.4 `github.com/sirupsen/logrus`/.Printfscip-go gomod github.com/sirupsen/logrus v1.9.4 `github.com/sirupsen/logrus`/Printf().("setupSearchIndex() - couldn't create search index, err: %s ", err) |
| 24 | return err |
| 25 | } |
| 26 | return nil |
| 27 | } |
| 28 | |
| 29 | func TestExtractSearchIndexesMetricsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestExtractSearchIndexesMetrics().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 30 | test_redis8_uri := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.0 os/Getenv().("TEST_REDIS8_URI") |
| 31 | test_valkey8_bundle_uri := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.0 os/Getenv().("TEST_VALKEY8_BUNDLE_URI") |
| 32 | if test_redis8_uri == "" || test_valkey8_bundle_uri == "" { |
| 33 | t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("TEST_REDIS8_URI or TEST_VALKEY8_BUNDLE_URI aren't set - skipping") |
| 34 | } |
| 35 | if err := setupSearchIndexscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/setupSearchIndex().(t, test_redis8_uri); err != nil { |
| 36 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("couldn't create search index in TEST_REDIS8_URI (%s), err: %s ", test_redis8_uri, err) |
| 37 | } |
| 38 | if err := setupSearchIndexscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/setupSearchIndex().(t, test_valkey8_bundle_uri); err != nil { |
| 39 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("couldn't create search index in TEST_VALKEY8_BUNDLE_URI (%s), err: %s ", test_valkey8_bundle_uri, err) |
| 40 | } |
| 41 | |
| 42 | tsts := []struct { |
| 43 | addr string |
| 44 | inclSearchIndexesMetrics bool |
| 45 | wantSearchIndexesMetrics bool |
| 46 | }{ |
| 47 | {addr: test_redis8_uri, inclSearchIndexesMetrics: true, wantSearchIndexesMetrics: true}, |
| 48 | {addr: test_redis8_uri, inclSearchIndexesMetrics: false, wantSearchIndexesMetrics: false}, |
| 49 | {addr: test_valkey8_bundle_uri, inclSearchIndexesMetrics: true, wantSearchIndexesMetrics: true}, |
| 50 | {addr: test_valkey8_bundle_uri, inclSearchIndexesMetrics: false, wantSearchIndexesMetrics: false}, |
| 51 | } |
| 52 | |
| 53 | for _, tst := range tsts { |
| 54 | e, _ := NewRedisExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/NewRedisExporter().(tst.addr, Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{Namespacescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#Namespace.: "test", InclSearchIndexesMetricsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#InclSearchIndexesMetrics.: tst.inclSearchIndexesMetrics}) |
| 55 | |
| 56 | chM := make(chan prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Metricscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#) |
| 57 | go func() { |
| 58 | e.Collectscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#Collect().(chM) |
| 59 | close(chM) |
| 60 | }() |
| 61 | |
| 62 | wantedMetrics := map[string]bool{ |
| 63 | "search_index_num_docs": false, |
| 64 | "search_index_max_doc_id": false, |
| 65 | "search_index_num_terms": false, |
| 66 | "search_index_num_records": false, |
| 67 | "search_index_inverted_size_bytes": false, |
| 68 | "search_index_total_inverted_index_blocks": false, |
| 69 | "search_index_vector_index_size_bytes": false, |
| 70 | "search_index_offset_vectors_size_bytes": false, |
| 71 | "search_index_doc_table_size_bytes": false, |
| 72 | "search_index_sortable_values_size_bytes": false, |
| 73 | "search_index_key_table_size_bytes": false, |
| 74 | "search_index_tag_overhead_size_bytes": false, |
| 75 | "search_index_text_overhead_size_bytes": false, |
| 76 | "search_index_total_index_memory_size_bytes": false, |
| 77 | "search_index_geoshapes_size_bytes": false, |
| 78 | "search_index_avg_per_doc_records": false, |
| 79 | "search_index_avg_per_record_bytes": false, |
| 80 | "search_index_avg_per_term_offsets": false, |
| 81 | "search_index_avg_per_record_offset_bits": false, |
| 82 | "search_index_indexing": false, |
| 83 | "search_index_percent_indexed": false, |
| 84 | "search_index_hash_indexing_failures": false, |
| 85 | "search_index_number_of_uses_total": false, |
| 86 | "search_index_cleaning": false, |
| 87 | } |
| 88 | |
| 89 | for m := range chM { |
| 90 | for want := range wantedMetrics { |
| 91 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(m.Descscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#Desc().().Stringscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Desc#String().(), want) { |
| 92 | wantedMetrics[want] = true |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if tst.wantSearchIndexesMetrics { |
| 98 | for want, found := range wantedMetrics { |
| 99 | if !found { |
| 100 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s was *not* found in Redis Search indexes metrics but expected", want) |
| 101 | } |
| 102 | } |
| 103 | } else if !tst.wantSearchIndexesMetrics { |
| 104 | for want, found := range wantedMetrics { |
| 105 | if found { |
| 106 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s was *found* in Redis Search indexes metrics but *not* expected", want) |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |