Skip to content

Commit

Permalink
chore: clean up comments and add code links
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockopp committed Oct 28, 2021
1 parent 5fe20ed commit a3b766d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
20 changes: 11 additions & 9 deletions cmd/vela-influx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
"github.com/sirupsen/logrus"
)

// Config represents the plugin configuration for Kubernetes information.
// Config represents the plugin configuration for InfluxDB information.
type Config struct {
// is the address to an influx server
// the address to the Influx instance
Addr string
// is the database for interaction on the server
// the database to send data on Influx instance
Database string
// is the user password for authenticating to server
// the user password for authenticating to Influx instance
Password string
// is the user name for authenticating to server
// the user name for authenticating to Influx instance
Username string
}

Expand All @@ -29,33 +29,35 @@ func (c *Config) New() (influx.Client, error) {
logrus.Trace("creating new influx client from plugin configuration")

// create config for InfluxDB client
//
// https://pkg.go.dev/github.com/influxdata/influxdb1-client/v2#HTTPConfig
conf := influx.HTTPConfig{
Addr: c.Addr,
Password: c.Password,
Username: c.Username,
}

// create new InfluxDB client
//
// https://pkg.go.dev/github.com/influxdata/influxdb1-client/v2#NewHTTPClient
client, err := influx.NewHTTPClient(conf)
if err != nil {
return nil, err
}

defer client.Close()

return client, nil
}

// Validate verifies the Config is properly configured.
func (c *Config) Validate() error {
logrus.Trace("validating config configuration")

// verify Addr is provided
// verify influx address is provided
if len(c.Addr) == 0 {
return fmt.Errorf("no config addr provided")
}

// verify Database is provided
// verify influx database is provided
if len(c.Database) == 0 {
return fmt.Errorf("no config database provided")
}
Expand Down
23 changes: 12 additions & 11 deletions cmd/vela-influx/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,33 @@ import (
"github.com/sirupsen/logrus"
)

type (
// Plugin struct represents fields user can present to plugin
Plugin struct {
// config arguments loaded for the plugin
Config *Config
// write arguments loaded for the plugin
Write *Write
}
)
// Plugin represents the configuration loaded for the plugin.
type Plugin struct {
// config arguments loaded for the plugin
Config *Config
// write arguments loaded for the plugin
Write *Write
}

// Exec formats and runs the commands for sending metrics to InfluxDB.
func (p *Plugin) Exec() error {
logrus.Debug("running plugin with provided configuration")

// create new Influx client from config configuration
// create new Influx client from configuration
client, err := p.Config.New()
if err != nil {
return err
}

// defer closing client after usage
defer client.Close()

logrus.Info("writing metric")

return p.Write.Exec(client, p.Config.Database)
}

// Validate function to validate plugin configuration
// Validate function to validate plugin configuration.
func (p *Plugin) Validate() error {
logrus.Debug("validating plugin configuration")

Expand Down
16 changes: 11 additions & 5 deletions cmd/vela-influx/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type (
Fields *Field
// raw input of fields provided for plugin
RawFields string
// raw input of fields provided for plugin
// raw input of tags provided for plugin
RawTags string
// set of tags to be created with the data point
Tags *Tag
Expand All @@ -39,23 +39,29 @@ type (
}
)

// Exec formats and runs the commands for removing artifacts in Artifactory.
// Exec formats and runs the commands for sending data to Influx.
func (w *Write) Exec(client influx.Client, database string) error {
logrus.Trace("running delete with provided configuration")

// Create a new point batch
// create batch of data points to send to the database
//
// https://pkg.go.dev/github.com/influxdata/influxdb1-client/v2#NewBatchPoints
bp, _ := influx.NewBatchPoints(influx.BatchPointsConfig{
Database: database,
})

// create new data point to add to the batch
//
// https://pkg.go.dev/github.com/influxdata/influxdb1-client/v2#NewPoint
pt, err := influx.NewPoint(w.Name, w.Tags.Data, w.Fields.Data, time.Now())
if err != nil {
return fmt.Errorf("unable to create point: %w", err)
}

// add the point to request
// add the data point to the batch to send to the database
bp.AddPoint(pt)

// send the batch point to the database
return client.Write(bp)
}

Expand Down Expand Up @@ -96,7 +102,7 @@ func (w *Write) Unmarshal() error {
return nil
}

// Validate verifies the Config is properly configured.
// Validate verifies the Write is properly configured.
func (w *Write) Validate() error {
logrus.Trace("validating write configuration")

Expand Down

0 comments on commit a3b766d

Please sign in to comment.