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
10 changes: 6 additions & 4 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (collector *Collector) Collect(metrics chan<- prometheus.Metric) {
for _, component := range device.Components {
componentStatus, err := collector.client.GetDeviceComponentStatus(ctx, device.DeviceID, component.ID)
if err == nil {
registerComponentMetrics(device.DeviceID, componentStatus, metrics)
registerComponentMetrics(device.DeviceID, component.ID, componentStatus, metrics)
} else {
log.Println("getDeviceComponentStatus deviceID:", device.DeviceID, "componentID:", component.ID, "failed, error:", err)
}
Expand Down Expand Up @@ -73,11 +73,13 @@ func registerDeviceMetrics(device *smartthings.Device, metrics chan<- prometheus
}
}

func registerComponentMetrics(deviceId string, componentStatus smartthings.ComponentStatus, metrics chan<- prometheus.Metric) {
// componentStatus is keyed by capability, which is exported as "componentId"
// for backwards compatibility; the real component goes in "component".
func registerComponentMetrics(deviceId, component string, componentStatus smartthings.ComponentStatus, metrics chan<- prometheus.Metric) {
for componentId, attributes := range componentStatus {
for attributeId, properties := range attributes {
labels := []string{"deviceId", "componentId"}
values := []string{deviceId, componentId}
labels := []string{"deviceId", "component", "componentId"}
values := []string{deviceId, component, componentId}

var extras map[string]string
metricValue := float64(0)
Expand Down
72 changes: 72 additions & 0 deletions collector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"context"
"testing"

"github.com/prometheus/client_golang/prometheus"
"github.com/setheck/smartthings-exporter/smartthings"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type fakeClient struct {
devices []*smartthings.Device
status map[string]smartthings.ComponentStatus
}

func (client *fakeClient) ListDevices(ctx context.Context) ([]*smartthings.Device, error) {
return client.devices, nil
}

func (client *fakeClient) GetDeviceComponentStatus(ctx context.Context, deviceId, componentId string) (smartthings.ComponentStatus, error) {
return client.status[componentId], nil
}

// A device with several components sharing a capability (e.g. a 4 button
// remote) must not produce duplicate series, or the whole scrape fails.
func TestCollectMultiComponentDevice(t *testing.T) {
buttonStatus := smartthings.ComponentStatus{
"button": smartthings.ComponentAttributes{
"button": smartthings.ComponentProperties{"value": "pushed"},
"numberOfButtons": smartthings.ComponentProperties{"value": float64(1)},
},
}
client := &fakeClient{
devices: []*smartthings.Device{{
DeviceID: "dev1",
Components: []*smartthings.Component{
{ID: "main"}, {ID: "button1"}, {ID: "button2"},
},
}},
status: map[string]smartthings.ComponentStatus{
"main": {
"battery": smartthings.ComponentAttributes{
"battery": smartthings.ComponentProperties{"value": float64(87)},
},
},
"button1": buttonStatus,
"button2": buttonStatus,
},
}

registry := prometheus.NewRegistry()
require.NoError(t, registry.Register(NewCollector(client)))

families, err := registry.Gather()
require.NoError(t, err)

components := map[string][]string{}
for _, family := range families {
for _, metric := range family.GetMetric() {
for _, label := range metric.GetLabel() {
if label.GetName() == "component" {
components[family.GetName()] = append(components[family.GetName()], label.GetValue())
}
}
}
}
assert.ElementsMatch(t, []string{"button1", "button2"}, components["smartthings_attribute_button"])
assert.ElementsMatch(t, []string{"button1", "button2"}, components["smartthings_attribute_numberOfButtons"])
assert.ElementsMatch(t, []string{"main"}, components["smartthings_attribute_battery"])
}