Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add separate metrics for non-numeric fields #71

Merged
merged 6 commits into from
Dec 10, 2024
Merged
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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ directly using [Go][golang] or [GoReleaser][goreleaser].
## Example metrics

```
lvm_pv_info{pv_fmt="lvm2",pv_name="/dev/sda1",pv_uuid="yc1zVe-…"} 1
lvm_pv_info{pv_fmt="lvm2",pv_name="/dev/sdb1",pv_uuid="WVIH97-…"} 1
lvm_pv_info{pv_name="/dev/sda1",pv_uuid="yc1zVe-…"} 1
lvm_pv_info{pv_name="/dev/sdb1",pv_uuid="WVIH97-…"} 1

lvm_pv_fmt{pv_fmt="lvm2",pv_uuid="yc1zVe-…"} 1
lvm_pv_fmt{pv_fmt="lvm2",pv_uuid="WVIH97-…"} 1

lvm_pv_free_bytes{pv_uuid="WVIH97-…"} 9.14358272e+08
lvm_pv_free_bytes{pv_uuid="yc1zVe-…"} 1.040187392e+09
Expand Down
8 changes: 4 additions & 4 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ func newEmptyCollector() *collector {
}
}

func newCollector() *collector {
func newCollector(enableLegacyInfoLabels bool) *collector {
c := newEmptyCollector()

for _, i := range allGroups {
c.gc = append(c.gc, newGroupCollector(i))
c.gc = append(c.gc, newGroupCollector(enableLegacyInfoLabels, i))
}

return c
}

func newCommandCollector(args []string) *collector {
func newCommandCollector(enableLegacyInfoLabels bool, args []string) *collector {
cmd := lvmreport.NewCommand(args)

var sfg singleflight.Group

c := newCollector()
c := newCollector(enableLegacyInfoLabels)
c.load = func(ctx context.Context) (*lvmreport.ReportData, error) {
// Avoid concurrent invocations
data, err, _ := sfg.Do("", func() (interface{}, error) {
Expand Down
23 changes: 17 additions & 6 deletions collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"log"
"strconv"
"testing"

"github.com/hansmi/prometheus-lvm-exporter/lvmreport"
Expand Down Expand Up @@ -72,13 +73,23 @@ func TestCollector(t *testing.T) {
{name: "issue30-lockargs"},
} {
t.Run(tc.name, func(t *testing.T) {
c := newCollector()
c.load = func(ctx context.Context) (*lvmreport.ReportData, error) {
return lvmreport.FromFile(fmt.Sprintf("testdata/%s.json", tc.name))
for _, enableLegacyInfoLabels := range []bool{false, true} {
expectedName := tc.name

if enableLegacyInfoLabels {
expectedName += "-legacy"
}

t.Run(strconv.FormatBool(enableLegacyInfoLabels), func(t *testing.T) {
c := newCollector(enableLegacyInfoLabels)
c.load = func(ctx context.Context) (*lvmreport.ReportData, error) {
return lvmreport.FromFile(fmt.Sprintf("testdata/%s.json", tc.name))
}

g := goldie.New(t)
g.Assert(t, expectedName, gatherAndFormat(t, c))
})
}

g := goldie.New(t)
g.Assert(t, tc.name, gatherAndFormat(t, c))
})
}
}
10 changes: 0 additions & 10 deletions descriptor.go

This file was deleted.

71 changes: 71 additions & 0 deletions field.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import "strconv"

type metricValueFunc func(string) (float64, error)

func fromNumeric(value string) (float64, error) {
return strconv.ParseFloat(value, 64)
}

type fieldFlag uint

const (
// Whether the field should be included as a label on an info metric.
asInfoLabel fieldFlag = 1 << iota
)

type field interface {
Name() string
MetricName() string
Help() string
}

// textField is an LVM report field whose value can not be made numeric, e.g.
// a device name or path.
type textField struct {
fieldName string
desc string

flags fieldFlag

metricName string
}

var _ field = (*textField)(nil)

func (f *textField) Name() string {
return f.fieldName
}

func (f *textField) MetricName() string {
return f.metricName
}

func (f *textField) Help() string {
return f.desc
}

// numericField is an LVM report field whose value is numeric or can be
// converted to a number.
type numericField struct {
fieldName string
desc string

metricName string
metricValue metricValueFunc
}

var _ field = (*numericField)(nil)

func (f *numericField) Name() string {
return f.fieldName
}

func (f *numericField) MetricName() string {
return f.metricName
}

func (f *numericField) Help() string {
return f.desc
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/hansmi/prometheus-lvm-exporter

go 1.22
go 1.22.0

toolchain go1.22.7

Expand Down Expand Up @@ -33,6 +33,7 @@ require (
github.com/sergi/go-diff v1.2.0 // indirect
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/sys v0.26.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs=
Expand Down
26 changes: 6 additions & 20 deletions group.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"slices"

"github.com/hansmi/prometheus-lvm-exporter/lvmreport"
)

Expand All @@ -11,26 +9,14 @@ type group struct {

infoMetricName string

keyFields []*descriptor
infoFields []*descriptor
metricFields []*descriptor
}

func (r *group) allDescriptors() []*descriptor {
d := slices.Clone(r.keyFields)
d = append(d, r.infoFields...)
d = append(d, r.metricFields...)
return d
}

func (r *group) fieldNames() []string {
var names []string
// Fields applied to all metrics from the group.
keyFields []*textField

for _, d := range r.allDescriptors() {
names = append(names, d.fieldName)
}
// Non-numeric fields, e.g. a device path.
textFields []*textField

return names
// Numeric fields, either directly or after conversion.
numericFields []*numericField
}

var allGroups = []*group{pvGroup, vgGroup, lvGroup}
Loading