import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/plugins/outputs/influxdb" ) func main() { config := influxdb.Config{ URLs: []string{"http://localhost:8086"}, Database: "mydb", } output, err := influxdb.New(config) if err != nil { log.Fatal(err) } defer output.Close() metric := telegraf.NewMetric( "my_metric", map[string]interface{}{"value": 42}, map[string]string{}, time.Now(), ) output.Write([]telegraf.Metric{metric}) }
import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/plugins/inputs/cpu" "github.com/influxdata/telegraf/plugins/inputs/mem" "github.com/influxdata/telegraf/plugins/outputs/influxdb" ) func main() { cpuInput := &cpu.CPU{} memInput := &mem.Mem{} config := influxdb.Config{ URLs: []string{"http://localhost:8086"}, Database: "mydb", } output, err := influxdb.New(config) if err != nil { log.Fatal(err) } defer output.Close() telegrafConfig := telegraf.Config{ Inputs: []telegraf.Input{cpuInput, memInput}, Output: output, } tg, err := telegraf.New(telegrafConfig) if err != nil { log.Fatal(err) } defer tg.Stop() tg.Start() }Overall, the github.com/influxdata/telegraf package is a powerful tool for collecting and reporting metrics in Go, with a wide range of built-in plugins and the ability to create custom ones.