-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
- Loading branch information
Showing
2 changed files
with
239 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package prometheus | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
"testing" | ||
|
||
dto "github.com/prometheus/client_model/go" | ||
) | ||
|
||
func TestCachedTGatherer(t *testing.T) { | ||
c := NewCachedTGatherer() | ||
mfs, done, err := c.Gather() | ||
if err != nil { | ||
t.Error("gather failed:", err) | ||
} | ||
done() | ||
if got := mfsToString(mfs); got != "" { | ||
t.Error("unexpected metric family", got) | ||
} | ||
|
||
s, err := c.NewSession() | ||
if err != nil { | ||
t.Error("session failed:", err) | ||
} | ||
|
||
_, err = c.NewSession() | ||
if err == nil { | ||
t.Error("second session expected to fail, got nil") | ||
} | ||
|
||
if err := s.AddMetric("a", "help a", []string{"b", "c"}, []string{"valb", "valc"}, GaugeValue, 1, nil); err != nil { | ||
t.Error("add metric:", err) | ||
} | ||
|
||
// Without commit we should see still empty list. | ||
mfs, done, err = c.Gather() | ||
if err != nil { | ||
t.Error("gather failed:", err) | ||
} | ||
done() | ||
if got := mfsToString(mfs); got != "" { | ||
t.Error("unexpected metric family", got) | ||
} | ||
|
||
s.Commit() | ||
mfs, done, err = c.Gather() | ||
if err != nil { | ||
t.Error("gather failed:", err) | ||
} | ||
done() | ||
if got := mfsToString(mfs); got != "name:\"a\" help:\"help a\" type:GAUGE metric:<label:<name:\"b\" value:\"valb\" > label:<name:\"c\" value:\"valc\" > gauge:<value:1 > > " { | ||
t.Error("unexpected metric family, got", got) | ||
} | ||
} | ||
|
||
func mfsToString(mfs []*dto.MetricFamily) string { | ||
ret := make([]string, 0, len(mfs)) | ||
for _, m := range mfs { | ||
ret = append(ret, m.String()) | ||
} | ||
return strings.Join(ret, ",") | ||
} | ||
|
||
type tGatherer struct { | ||
done bool | ||
err error | ||
} | ||
|
||
func (g *tGatherer) Gather() (_ []*dto.MetricFamily, done func(), err error) { | ||
name := "g1" | ||
val := 1.0 | ||
return []*dto.MetricFamily{ | ||
{Name: &name, Metric: []*dto.Metric{{Gauge: &dto.Gauge{Value: &val}}}}, | ||
}, func() { g.done = true }, g.err | ||
} | ||
|
||
func TestNewMultiTRegistry(t *testing.T) { | ||
treg := &tGatherer{} | ||
|
||
t.Run("one registry", func(t *testing.T) { | ||
m := NewMultiTRegistry(treg) | ||
ret, done, err := m.Gather() | ||
if err != nil { | ||
t.Error("gather failed:", err) | ||
} | ||
done() | ||
if len(ret) != 1 { | ||
t.Error("unexpected number of metric families, expected 1, got", ret) | ||
} | ||
if !treg.done { | ||
t.Error("inner transactional registry not marked as done") | ||
} | ||
}) | ||
|
||
reg := NewRegistry() | ||
if err := reg.Register(NewCounter(CounterOpts{Name: "c1", Help: "help c1"})); err != nil { | ||
t.Error("registration failed:", err) | ||
} | ||
|
||
// Note on purpose two registries will have exactly same metric family name (but with different string). | ||
// This behaviour is undefined at the moment. | ||
if err := reg.Register(NewGauge(GaugeOpts{Name: "g1", Help: "help g1"})); err != nil { | ||
t.Error("registration failed:", err) | ||
} | ||
treg.done = false | ||
|
||
t.Run("two registries", func(t *testing.T) { | ||
m := NewMultiTRegistry(ToTransactionalGatherer(reg), treg) | ||
ret, done, err := m.Gather() | ||
if err != nil { | ||
t.Error("gather failed:", err) | ||
} | ||
done() | ||
if len(ret) != 3 { | ||
t.Error("unexpected number of metric families, expected 3, got", ret) | ||
} | ||
if !treg.done { | ||
t.Error("inner transactional registry not marked as done") | ||
} | ||
}) | ||
|
||
treg.done = false | ||
// Inject error. | ||
treg.err = errors.New("test err") | ||
|
||
t.Run("two registries, one with error", func(t *testing.T) { | ||
m := NewMultiTRegistry(ToTransactionalGatherer(reg), treg) | ||
ret, done, err := m.Gather() | ||
if err != treg.err { | ||
t.Error("unexpected error:", err) | ||
} | ||
done() | ||
if len(ret) != 3 { | ||
t.Error("unexpected number of metric families, expected 3, got", ret) | ||
} | ||
// Still on error, we expect done to be triggered. | ||
if !treg.done { | ||
t.Error("inner transactional registry not marked as done") | ||
} | ||
}) | ||
} |