| 1 | // Copyright 2021 The Prometheus Authors |
| 2 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | // you may not use this file except in compliance with the License. |
| 4 | // You may obtain a copy of the License at |
| 5 | // |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | // |
| 8 | // Unless required by applicable law or agreed to in writing, software |
| 9 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | // See the License for the specific language governing permissions and |
| 12 | // limitations under the License. |
| 13 | |
| 14 | package collectorsscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/collectors`/ |
| 15 | |
| 16 | import ( |
| 17 | "database/sqlscip-go gomod github.com/golang/go/src go1.23.0 `database/sql`/" |
| 18 | "testingscip-go gomod github.com/golang/go/src go1.23.0 testing/" |
| 19 | |
| 20 | "github.com/prometheus/client_golang/prometheusscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus`/" |
| 21 | ) |
| 22 | |
| 23 | func TestDBStatsCollectorscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/collectors`/TestDBStatsCollector().(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#) { |
| 24 | reg := prometheusscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus`/.NewPedanticRegistryscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus`/NewPedanticRegistry().() |
| 25 | { |
| 26 | db := new(sqlscip-go gomod github.com/golang/go/src go1.23.0 `database/sql`/.DBscip-go gomod github.com/golang/go/src go1.23.0 `database/sql`/DB#) |
| 27 | if err := reg.Registerscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus`/Registry#Register().(NewDBStatsCollectorscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/collectors`/NewDBStatsCollector().(db, "db_A")); err != nil { |
| 28 | t.Fatalscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatal().(err) |
| 29 | } |
| 30 | } |
| 31 | { |
| 32 | db := new(sqlscip-go gomod github.com/golang/go/src go1.23.0 `database/sql`/.DBscip-go gomod github.com/golang/go/src go1.23.0 `database/sql`/DB#) |
| 33 | if err := reg.Registerscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus`/Registry#Register().(NewDBStatsCollectorscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/collectors`/NewDBStatsCollector().(db, "db_B")); err != nil { |
| 34 | t.Fatalscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatal().(err) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | mfs, err := reg.Gatherscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus`/Registry#Gather().() |
| 39 | if err != nil { |
| 40 | t.Fatalscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatal().(err) |
| 41 | } |
| 42 | |
| 43 | names := []string{ |
| 44 | "go_sql_max_open_connections", |
| 45 | "go_sql_open_connections", |
| 46 | "go_sql_in_use_connections", |
| 47 | "go_sql_idle_connections", |
| 48 | "go_sql_wait_count_total", |
| 49 | "go_sql_wait_duration_seconds_total", |
| 50 | "go_sql_max_idle_closed_total", |
| 51 | "go_sql_max_lifetime_closed_total", |
| 52 | "go_sql_max_idle_time_closed_total", |
| 53 | } |
| 54 | type result struct { |
| 55 | found bool |
| 56 | } |
| 57 | results := make(map[string]result) |
| 58 | for _, name := range names { |
| 59 | results[name] = result{found: false} |
| 60 | } |
| 61 | for _, mf := range mfs { |
| 62 | m := mf.GetMetricscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/MetricFamily#GetMetric().() |
| 63 | if len(m) != 2 { |
| 64 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("expected 2 metrics bug got %d", len(m)) |
| 65 | } |
| 66 | labelA := m[0].GetLabelscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Metric#GetLabel().()[0] |
| 67 | if name := labelA.GetNamescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/LabelPair#GetName().(); name != "db_name" { |
| 68 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("expected to get label \"db_name\" but got %s", name) |
| 69 | } |
| 70 | if value := labelA.GetValuescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/LabelPair#GetValue().(); value != "db_A" { |
| 71 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("expected to get value \"db_A\" but got %s", value) |
| 72 | } |
| 73 | labelB := m[1].GetLabelscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Metric#GetLabel().()[0] |
| 74 | if name := labelB.GetNamescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/LabelPair#GetName().(); name != "db_name" { |
| 75 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("expected to get label \"db_name\" but got %s", name) |
| 76 | } |
| 77 | if value := labelB.GetValuescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/LabelPair#GetValue().(); value != "db_B" { |
| 78 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("expected to get value \"db_B\" but got %s", value) |
| 79 | } |
| 80 | |
| 81 | for _, name := range names { |
| 82 | if name == mf.GetNamescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/MetricFamily#GetName().() { |
| 83 | results[name] = result{found: true} |
| 84 | break |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | for name, result := range results { |
| 90 | if !result.found { |
| 91 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("%s not found", name) |
| 92 | } |
| 93 | } |
| 94 | } |