Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #36 from IzabellaRaulin/removed_panics
Browse files Browse the repository at this point in the history
Added error handling with failure reason
  • Loading branch information
katarzyna-z authored Jan 30, 2017
2 parents c9469d4 + b8e58ef commit f40d793
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
6 changes: 2 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ import (

// plugin bootstrap
func main() {
p, err := pcm.NewPCMCollector()
if err != nil {
panic(err)
}
p := pcm.NewPCMCollector()

plugin.Start(
pcm.Meta(),
p,
Expand Down
58 changes: 38 additions & 20 deletions pcm/pcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package pcm
import (
"bufio"
"fmt"
"io"
"math"
"os"
"os/exec"
"path/filepath"
Expand All @@ -30,9 +32,7 @@ import (
"sync"
"time"

"io"

"math"
log "github.com/Sirupsen/logrus"

"github.com/intelsdi-x/snap-plugin-utilities/ns"
"github.com/intelsdi-x/snap/control/plugin"
Expand All @@ -44,7 +44,7 @@ const (
// Name of plugin
name = "pcm"
// Version of plugin
version = 9
version = 10
// Type of plugin
pluginType = plugin.CollectorPluginType
)
Expand All @@ -55,9 +55,10 @@ func Meta() *plugin.PluginMeta {

// PCM
type PCM struct {
keys []string
data map[string]float64
mutex *sync.RWMutex
keys []string
data map[string]float64
mutex *sync.RWMutex
initialized bool
}

func (p *PCM) Keys() []string {
Expand All @@ -70,6 +71,17 @@ func (p *PCM) Data() map[string]float64 {

// // CollectMetrics returns metrics from pcm
func (p *PCM) CollectMetrics(mts []plugin.MetricType) ([]plugin.MetricType, error) {
if p.initialized == false {
err := p.run()
if err != nil {
log.WithFields(log.Fields{
"block": "CollectMetrics",
"function": "run",
}).Error(err)
return nil, err
}
p.initialized = true
}
p.mutex.RLock()
defer p.mutex.RUnlock()
for i := range mts {
Expand All @@ -84,11 +96,24 @@ func (p *PCM) CollectMetrics(mts []plugin.MetricType) ([]plugin.MetricType, erro

// GetMetricTypes returns the metric types exposed by pcm
func (p *PCM) GetMetricTypes(_ plugin.ConfigType) ([]plugin.MetricType, error) {
mts := make([]plugin.MetricType, len(p.keys))
mts := []plugin.MetricType{}
if p.initialized == false {
err := p.run()
if err != nil {
log.WithFields(log.Fields{
"block": "GetMetricTypes",
"function": "run",
}).Error(err)
return nil, err
}
p.initialized = true
}
p.mutex.RLock()
defer p.mutex.RUnlock()
for i, k := range p.keys {
mts[i] = plugin.MetricType{Namespace_: core.NewNamespace(strings.Split(strings.TrimPrefix(k, "/"), "/")...)}

for _, k := range p.keys {
mt := plugin.MetricType{Namespace_: core.NewNamespace(strings.Split(strings.TrimPrefix(k, "/"), "/")...)}
mts = append(mts, mt)
}
return mts, nil
}
Expand All @@ -99,15 +124,8 @@ func (p *PCM) GetConfigPolicy() (*cpolicy.ConfigPolicy, error) {
return c, nil
}

func NewPCMCollector() (*PCM, error) {
pcm := &PCM{mutex: &sync.RWMutex{}, data: map[string]float64{}}

err := pcm.run()
if err != nil {
return nil, err
}

return pcm, nil
func NewPCMCollector() *PCM {
return &PCM{mutex: &sync.RWMutex{}, data: map[string]float64{}, initialized: false}
}
func (pcm *PCM) run() error {
var cmd *exec.Cmd
Expand All @@ -117,7 +135,7 @@ func (pcm *PCM) run() error {
c, err := exec.LookPath("pcm.x")
if err != nil {
fmt.Fprint(os.Stderr, "Unable to find PCM. Ensure it's in your path or set SNAP_PCM_PATH.")
panic(err)
return err
}
cmd = exec.Command(c, "/csv", "-nc", "-r", "1")
}
Expand Down

0 comments on commit f40d793

Please sign in to comment.