github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/velodrome/token-counter/influx.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"time"
    21  
    22  	"github.com/golang/glog"
    23  	influxdb "github.com/influxdata/influxdb/client/v2"
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  // InfluxConfig creates an InfluxDB
    28  type InfluxConfig struct {
    29  	Host     string
    30  	DB       string
    31  	User     string
    32  	Password string
    33  }
    34  
    35  // AddFlags parses options for database configuration
    36  func (config *InfluxConfig) AddFlags(cmd *cobra.Command) {
    37  	cmd.PersistentFlags().StringVar(&config.User, "influx-user", "root", "InfluxDB user")
    38  	cmd.PersistentFlags().StringVar(&config.Password, "influx-password", "", "InfluxDB password")
    39  	cmd.PersistentFlags().StringVar(&config.Host, "influx-host", "http://localhost:8086", "InfluxDB http server")
    40  	cmd.PersistentFlags().StringVar(&config.DB, "influx-database", "monitoring", "InfluxDB database name")
    41  }
    42  
    43  // CreateDatabaseClient creates and connects a new instance of an InfluxDB
    44  // It is created based on the fields set in the configuration.
    45  func (config *InfluxConfig) CreateDatabaseClient() (*InfluxDB, error) {
    46  	client, err := influxdb.NewHTTPClient(influxdb.HTTPConfig{
    47  		Addr:     config.Host,
    48  		Username: config.User,
    49  		Password: config.Password,
    50  	})
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	return &InfluxDB{
    56  		client:   client,
    57  		database: config.DB,
    58  	}, nil
    59  }
    60  
    61  // InfluxDB is a connection handler to a Influx database
    62  type InfluxDB struct {
    63  	client   influxdb.Client
    64  	database string
    65  }
    66  
    67  // Push a point to the database
    68  func (i *InfluxDB) Push(measurement string, tags map[string]string, fields map[string]interface{}, date time.Time) error {
    69  	batch, err := influxdb.NewBatchPoints(influxdb.BatchPointsConfig{
    70  		Database:  i.database,
    71  		Precision: "s",
    72  	})
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	pt, err := influxdb.NewPoint(measurement, tags, fields, date)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	batch.AddPoint(pt)
    83  
    84  	err = i.client.Write(batch)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	glog.Infof("Sent to influx: %s %+v %+v %s", measurement, tags, fields, date)
    89  
    90  	return nil
    91  }