Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions prometheus/desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ func NewInvalidDesc(err error) *Desc {
}
}

// Err returns an error that occurred during construction, if any.
//
// Calling this method is optional. It can be used to detect construction
// errors early, before invoking other methods on the Desc. If an error is
// present, later operations may not behave as expected.
func (d *Desc) Err() error {
return d.err
}

func (d *Desc) String() string {
lpStrings := make([]string, 0, len(d.constLabelPairs))
for _, lp := range d.constLabelPairs {
Expand Down
26 changes: 20 additions & 6 deletions prometheus/desc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,29 @@ import (
"testing"
)

func TestNewDescInvalidLabelValues(t *testing.T) {
func TestNewDescInvalidConstLabelValues(t *testing.T) {
labelValue := "\xFF"
desc := NewDesc(
"sample_label",
"sample label",
nil,
Labels{"a": "\xFF"},
Labels{"a": labelValue},
)
if desc.err == nil {
t.Errorf("NewDesc: expected error because: %s", desc.err)
if desc.Err() == nil {
t.Errorf("NewDesc: expected error because const label value is invalid: %s", labelValue)
}
}

func TestNewDescInvalidVariableLabelName(t *testing.T) {
labelValue := "1label__"
desc := NewDesc(
"sample_label",
"sample label",
[]string{labelValue},
Labels{"a": "b"},
)
if desc.Err() == nil {
t.Errorf("NewDesc: expected error because variable label name is invalid: %s", labelValue)
}
}

Expand All @@ -36,8 +50,8 @@ func TestNewDescNilLabelValues(t *testing.T) {
nil,
nil,
)
if desc.err != nil {
t.Errorf("NewDesc: unexpected error: %s", desc.err)
if desc.Err() != nil {
t.Errorf("NewDesc: unexpected error: %s", desc.Err())
}
}

Expand Down
Loading