Skip to content

Commit

Permalink
Remove dependency on service for FS metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
arunvelsriram committed May 15, 2021
1 parent 7fffd66 commit 91a5370
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
22 changes: 18 additions & 4 deletions pkg/collector/sftp_collector.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package collector

import (
"github.com/arunvelsriram/sftp-exporter/pkg/constants/viperkeys"
"github.com/arunvelsriram/sftp-exporter/pkg/service"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"

"github.com/arunvelsriram/sftp-exporter/pkg/client"
c "github.com/arunvelsriram/sftp-exporter/pkg/constants"
Expand Down Expand Up @@ -68,14 +70,26 @@ func (s SFTPCollector) Collect(ch chan<- prometheus.Metric) {
return
}
defer s.sftpClient.Close()
log.Debug("connected to SFTP")
ch <- prometheus.MustNewConstMetric(up, prometheus.GaugeValue, 1)

fsStats := s.sftpService.FSStats()
for _, stat := range fsStats {
ch <- prometheus.MustNewConstMetric(fsTotalSpace, prometheus.GaugeValue, stat.TotalSpace, stat.Path)
ch <- prometheus.MustNewConstMetric(fsFreeSpace, prometheus.GaugeValue, stat.FreeSpace, stat.Path)
log.Debug("collecting filesystem metrics")
paths := viper.GetStringSlice(viperkeys.SFTPPaths)
for _, path := range paths {
log.Debugf("collecting filesystem metrics for path: %s", path)
statVFS, err := s.sftpClient.StatVFS(path)
if err != nil {
log.WithField("when", "collecting filesystem metrics").Error(err)
} else {
totalSpace := float64(statVFS.TotalSpace())
freeSpace := float64(statVFS.FreeSpace())
log.Debugf("writing filesystem metrics for path: %s", path)
ch <- prometheus.MustNewConstMetric(fsTotalSpace, prometheus.GaugeValue, totalSpace, path)
ch <- prometheus.MustNewConstMetric(fsFreeSpace, prometheus.GaugeValue, freeSpace, path)
}
}

log.Debug("collecting object metrics")
objectStats := s.sftpService.ObjectStats()
for _, stat := range objectStats {
ch <- prometheus.MustNewConstMetric(objectCount, prometheus.GaugeValue, stat.ObjectCount, stat.Path)
Expand Down
54 changes: 26 additions & 28 deletions pkg/collector/sftp_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import (
"fmt"
"testing"

"github.com/arunvelsriram/sftp-exporter/pkg/constants/viperkeys"
"github.com/arunvelsriram/sftp-exporter/pkg/model"
"github.com/pkg/sftp"
"github.com/spf13/viper"

"github.com/arunvelsriram/sftp-exporter/pkg/internal/mocks"
"github.com/golang/mock/gomock"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/suite"

log "github.com/sirupsen/logrus"
)

type SFTPCollectorSuite struct {
Expand All @@ -26,6 +31,7 @@ func TestSFTPCollectorSuite(t *testing.T) {
}

func (s *SFTPCollectorSuite) SetupTest() {
log.SetLevel(log.DebugLevel)
s.ctrl = gomock.NewController(s.T())
s.sftpService = mocks.NewMockSFTPService(s.ctrl)
s.sftpClient = mocks.NewMockSFTPClient(s.ctrl)
Expand Down Expand Up @@ -74,10 +80,10 @@ func (s *SFTPCollectorSuite) TestSFTPCollectorDescribe() {
}

func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldWriteUpMetric() {
viper.Set(viperkeys.SFTPPaths, []string{})
s.sftpClient.EXPECT().Connect().Return(nil)
s.sftpService.EXPECT().FSStats()
s.sftpService.EXPECT().ObjectStats()
s.sftpClient.EXPECT().Close()
s.sftpClient.EXPECT().Close().Return(nil)
ch := make(chan prometheus.Metric)
done := make(chan bool)

Expand All @@ -98,6 +104,7 @@ func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldWriteUpMetric() {
}

func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldWriteUpMetricAndReturnIfClientCreationFails() {
viper.Set(viperkeys.SFTPPaths, []string{})
s.sftpClient.EXPECT().Connect().Return(fmt.Errorf("failed to connect to SFTP"))
ch := make(chan prometheus.Metric)
done := make(chan bool)
Expand All @@ -118,21 +125,11 @@ func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldWriteUpMetricAndRetur
<-done
}

func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldWriteFSStats() {
fsStats := model.FSStats([]model.FSStat{
{
Path: "/path1",
TotalSpace: 1111.11,
FreeSpace: 2222.22,
},
{
Path: "/path2",
TotalSpace: 3333.33,
FreeSpace: 4444.44,
},
})
func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldWriteFSMetrics() {
viper.Set(viperkeys.SFTPPaths, []string{"/path0", "/path1"})
s.sftpClient.EXPECT().Connect().Return(nil)
s.sftpService.EXPECT().FSStats().Return(fsStats)
s.sftpClient.EXPECT().StatVFS("/path0").Return(&sftp.StatVFS{Frsize: 10, Blocks: 1000, Bfree: 100}, nil)
s.sftpClient.EXPECT().StatVFS("/path1").Return(&sftp.StatVFS{Frsize: 5, Blocks: 1000, Bfree: 500}, nil)
s.sftpService.EXPECT().ObjectStats()
s.sftpClient.EXPECT().Close()
ch := make(chan prometheus.Metric)
Expand All @@ -152,43 +149,44 @@ func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldWriteFSStats() {
_ = totalSpace1.Write(metric)
s.Equal(`Desc{fqName: "sftp_filesystem_total_space_bytes", help: "Total space in the filesystem containing the path", `+
`constLabels: {}, variableLabels: [path]}`, desc.String())
s.Equal(1111.11, metric.GetGauge().GetValue())
s.Equal(10000.0, metric.GetGauge().GetValue())
s.Equal("path", metric.GetLabel()[0].GetName())
s.Equal("/path1", metric.GetLabel()[0].GetValue())
s.Equal("/path0", metric.GetLabel()[0].GetValue())

freeSpace1 := <-ch
desc = freeSpace1.Desc()
_ = freeSpace1.Write(metric)
s.Equal(`Desc{fqName: "sftp_filesystem_free_space_bytes", help: "Free space in the filesystem containing the path", `+
`constLabels: {}, variableLabels: [path]}`, desc.String())
s.Equal(2222.22, metric.GetGauge().GetValue())
s.Equal(1000.0, metric.GetGauge().GetValue())
s.Equal("path", metric.GetLabel()[0].GetName())
s.Equal("/path1", metric.GetLabel()[0].GetValue())
s.Equal("/path0", metric.GetLabel()[0].GetValue())

totalSpace2 := <-ch
desc = totalSpace2.Desc()
_ = totalSpace2.Write(metric)
s.Equal(`Desc{fqName: "sftp_filesystem_total_space_bytes", help: "Total space in the filesystem containing the path", `+
`constLabels: {}, variableLabels: [path]}`, desc.String())
s.Equal(3333.33, metric.GetGauge().GetValue())
s.Equal(5000.0, metric.GetGauge().GetValue())
s.Equal("path", metric.GetLabel()[0].GetName())
s.Equal("/path2", metric.GetLabel()[0].GetValue())
s.Equal("/path1", metric.GetLabel()[0].GetValue())

freeSpace2 := <-ch
desc = freeSpace2.Desc()
_ = freeSpace2.Write(metric)
s.Equal(`Desc{fqName: "sftp_filesystem_free_space_bytes", help: "Free space in the filesystem containing the path", `+
`constLabels: {}, variableLabels: [path]}`, desc.String())
s.Equal(4444.44, metric.GetGauge().GetValue())
s.Equal(2500.0, metric.GetGauge().GetValue())
s.Equal("path", metric.GetLabel()[0].GetName())
s.Equal("/path2", metric.GetLabel()[0].GetValue())
s.Equal("/path1", metric.GetLabel()[0].GetValue())

<-done
}

func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldNotWriteFSStatsForEmpty() {
func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldNotWriteFSMetricsForError() {
viper.Set(viperkeys.SFTPPaths, []string{"/path0"})
s.sftpClient.EXPECT().Connect().Return(nil)
s.sftpService.EXPECT().FSStats().Return(model.FSStats([]model.FSStat{}))
s.sftpClient.EXPECT().StatVFS("/path0").Return(nil, fmt.Errorf("failed to get VFS stats"))
s.sftpService.EXPECT().ObjectStats()
s.sftpClient.EXPECT().Close()
ch := make(chan prometheus.Metric)
Expand All @@ -206,6 +204,7 @@ func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldNotWriteFSStatsForEmp
}

func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldWriteObjectStats() {
viper.Set(viperkeys.SFTPPaths, []string{})
objectStats := model.ObjectStats([]model.ObjectStat{
{
Path: "/path1",
Expand All @@ -219,7 +218,6 @@ func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldWriteObjectStats() {
},
})
s.sftpClient.EXPECT().Connect().Return(nil)
s.sftpService.EXPECT().FSStats()
s.sftpService.EXPECT().ObjectStats().Return(objectStats)
s.sftpClient.EXPECT().Close()
ch := make(chan prometheus.Metric)
Expand Down Expand Up @@ -274,8 +272,8 @@ func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldWriteObjectStats() {
}

func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldNotWriteObjectStatsForEmpty() {
viper.Set(viperkeys.SFTPPaths, []string{})
s.sftpClient.EXPECT().Connect().Return(nil)
s.sftpService.EXPECT().FSStats()
s.sftpService.EXPECT().ObjectStats().Return(model.ObjectStats(model.ObjectStats{}))
s.sftpClient.EXPECT().Close()
ch := make(chan prometheus.Metric)
Expand Down

0 comments on commit 91a5370

Please sign in to comment.