Skip to content

Commit

Permalink
Use interface for collector.
Browse files Browse the repository at this point in the history
  • Loading branch information
tetafro committed Sep 8, 2023
1 parent 300b620 commit f45b30b
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 6 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
dep:
@ go mod tidy && go mod verify

.PHONY: mock
mock:
@ mockgen -package=main \
-source=connectbox.go \
-destination=connectbox_mock.go

.PHONY: test
test:
@ go test ./...
Expand Down
8 changes: 4 additions & 4 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (

// Collector collects metrics from a remote ConnectBox router.
type Collector struct {
targets map[string]*ConnectBox
targets map[string]MetricsClient
}

// NewCollector creates new collector.
func NewCollector(targets map[string]*ConnectBox) *Collector {
func NewCollector(targets map[string]MetricsClient) *Collector {
return &Collector{targets: targets}
}

Expand Down Expand Up @@ -51,7 +51,7 @@ func (c *Collector) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (c *Collector) collectCMSSystemInfo(
ctx context.Context,
reg *prometheus.Registry,
client *ConnectBox,
client MetricsClient,
) {
cmDocsisModeGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_cm_docsis_mode",
Expand Down Expand Up @@ -106,7 +106,7 @@ func (c *Collector) collectCMSSystemInfo(
func (c *Collector) collectCMState(
ctx context.Context,
reg *prometheus.Registry,
client *ConnectBox,
client MetricsClient,
) {
tunnerTemperatureGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_tunner_temperature",
Expand Down
2 changes: 1 addition & 1 deletion collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import (
)

func TestNewCollector(t *testing.T) {
c := NewCollector(map[string]*ConnectBox{"test": {}})
c := NewCollector(map[string]MetricsClient{"test": &ConnectBox{}})
require.Len(t, c.targets, 1)
}
8 changes: 8 additions & 0 deletions connectbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ const (
xmlSetter = "/xml/setter.xml"
)

// MetricsClient is a general purpose client, that gets metrics from
// a remote source.
type MetricsClient interface {
Login(ctx context.Context) error
Logout(ctx context.Context) error
GetMetrics(ctx context.Context, fn string, out any) error
}

// ConnectBox is a client for ConnectBox HTTP API.
type ConnectBox struct {
http *http.Client
Expand Down
77 changes: 77 additions & 0 deletions connectbox_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/prometheus/client_golang v1.16.0
github.com/stretchr/testify v1.8.4
go.uber.org/mock v0.2.0
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDN
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.uber.org/mock v0.2.0 h1:TaP3xedm7JaAgScZO7tlvlKrqT0p7I6OsdGB5YNSMDU=
go.uber.org/mock v0.2.0/go.mod h1:J0y0rp9L3xiff1+ZBfKxlC1fz2+aO16tw0tsDOixfuM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
}

// Create a client for each target
targets := map[string]*ConnectBox{}
targets := map[string]MetricsClient{}
for _, t := range conf.Targets {
client, err := NewConnectBox(
t.Addr,
Expand Down

0 comments on commit f45b30b

Please sign in to comment.