| 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 collectors |
| 15 |
|
| 16 |
import ( |
| 17 |
"database/sql" |
| 18 |
"testing" |
| 19 |
|
| 20 |
"github.com/prometheus/client_golang/prometheus" |
| 21 |
) |
| 22 |
|
| 23 |
func TestDBStatsCollector(t *testing.T) { |
| 24 |
reg := prometheus.NewPedanticRegistry() |
| 25 |
{ |
| 26 |
db := new(sql.DB) |
| 27 |
if err := reg.Register(NewDBStatsCollector(db, "db_A")); err != nil { |
| 28 |
t.Fatal(err) |
| 29 |
} |
| 30 |
} |
| 31 |
{ |
| 32 |
db := new(sql.DB) |
| 33 |
if err := reg.Register(NewDBStatsCollector(db, "db_B")); err != nil { |
| 34 |
t.Fatal(err) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
mfs, err := reg.Gather() |
| 39 |
if err != nil { |
| 40 |
t.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.GetMetric() |
| 63 |
if len(m) != 2 { |
| 64 |
t.Errorf("expected 2 metrics bug got %d", len(m)) |
| 65 |
} |
| 66 |
labelA := m[0].GetLabel()[0] |
| 67 |
if name := labelA.GetName(); name != "db_name" { |
| 68 |
t.Errorf("expected to get label \"db_name\" but got %s", name) |
| 69 |
} |
| 70 |
if value := labelA.GetValue(); value != "db_A" { |
| 71 |
t.Errorf("expected to get value \"db_A\" but got %s", value) |
| 72 |
} |
| 73 |
labelB := m[1].GetLabel()[0] |
| 74 |
if name := labelB.GetName(); name != "db_name" { |
| 75 |
t.Errorf("expected to get label \"db_name\" but got %s", name) |
| 76 |
} |
| 77 |
if value := labelB.GetValue(); value != "db_B" { |
| 78 |
t.Errorf("expected to get value \"db_B\" but got %s", value) |
| 79 |
} |
| 80 |
|
| 81 |
for _, name := range names { |
| 82 |
if name == mf.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.Errorf("%s not found", name) |
| 92 |
} |
| 93 |
} |
| 94 |
} |