diff --git a/cmd/vela-influx/config.go b/cmd/vela-influx/config.go index b044122..6767003 100644 --- a/cmd/vela-influx/config.go +++ b/cmd/vela-influx/config.go @@ -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 } @@ -29,6 +29,8 @@ 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, @@ -36,13 +38,13 @@ func (c *Config) New() (influx.Client, error) { } // 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 } @@ -50,12 +52,12 @@ func (c *Config) New() (influx.Client, error) { 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") } diff --git a/cmd/vela-influx/plugin.go b/cmd/vela-influx/plugin.go index 45e27da..f78fdb5 100644 --- a/cmd/vela-influx/plugin.go +++ b/cmd/vela-influx/plugin.go @@ -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") diff --git a/cmd/vela-influx/write.go b/cmd/vela-influx/write.go index 1df3d15..02147c6 100644 --- a/cmd/vela-influx/write.go +++ b/cmd/vela-influx/write.go @@ -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 @@ -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) } @@ -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")