From 5ffa5ccd91a835955a3f4f98efdf7e2e98280691 Mon Sep 17 00:00:00 2001 From: Yuri Nikolic Date: Fri, 31 Oct 2025 17:04:26 +0100 Subject: [PATCH] feat: expose Desc error through public Err() method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a public Err() method to Desc type to allow users to check if an error occurred during descriptor construction. Previously, the error field was private and inaccessible. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Signed-off-by: Yuri Nikolic --- prometheus/desc.go | 5 +++++ prometheus/desc_test.go | 26 ++++++++++++++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/prometheus/desc.go b/prometheus/desc.go index 2331b8b4f..e5c69b1ad 100644 --- a/prometheus/desc.go +++ b/prometheus/desc.go @@ -182,6 +182,11 @@ func NewInvalidDesc(err error) *Desc { } } +// Err returns an error that occurred during construction, if any. +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 { diff --git a/prometheus/desc_test.go b/prometheus/desc_test.go index 5a8429009..0570d5bd1 100644 --- a/prometheus/desc_test.go +++ b/prometheus/desc_test.go @@ -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 := "__label__" + 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) } } @@ -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()) } }