SlideShare una empresa de Scribd logo
1 de 46
Descargar para leer sin conexión
Noah Crowley / Developer Advocate
How to Build a Telegraf
Plugin
© 2018 InfluxData. All rights reserved.
What we’ll be covering
✔ Telegraf overview

✔ Examples of Telegraf plugins

✔ Telegraf plugin architecture

✔ How to a write a Telegraf plugin
Telegraf Overview
© 2018 InfluxData. All rights reserved.
© 2018 InfluxData. All rights reserved.
Telegraf Overview
• Telegraf is an open source, plugin-driven agent for collecting
and reporting metrics

• “Push” or “Pull”

• Large collection of plugins: 192 (as of 3/13/2019)

• Inputs: 149

• Outputs: 30

• Aggregators: 9

• Processors: 4
© 2018 InfluxData. All rights reserved.
Telegraf Overview
• Can act as:

• Agent

• Collector

• Part of an Ingest Pipeline
© 2018 InfluxData. All rights reserved.
Telegraf Plugins
• Outputs

• Allows you to send metrics to a variety of datastores

• Plugins for both InfluxDB 1.x and 2.0

• Supports Kafka, MQTT, NSQ, OpenMetrics, and more

• Aggregators and Processors allow you to manipulate data as it
flows through Telegraf

• Transform tags, convert types, calculate histograms
© 2018 InfluxData. All rights reserved.
Telegraf Input Plugins
• Metrics ingestion from host system: CPU, I/O, Network…

• Common Applications like NGINX, Postgres, Redis…

• Third Party APIs: Mailchimp, Cloudwatch, Google Analytics…

• General-Purpose Protocols: HTTP, Socket, MQTT…

• …and more!
© 2018 InfluxData. All rights reserved.
Telegraf Benefits
• Minimal memory footprint

• Tagging of metrics

• Batching of metrics to reduce the number of atomic writes

• Easy contribution of functionality thanks to Go

• Static Binary

• Strong Community Contributions
Examples of Plugins
© 2018 InfluxData. All rights reserved.
statsd
• Listens for incoming data
• Acts as a statsd instance
• Outputs to any configured output
© 2018 InfluxData. All rights reserved.
postgresql
• Collects performance data
• Uses data from built-in views:
• pg_stat_database
• pg_stat_bgwriter
© 2018 InfluxData. All rights reserved.
apache
• Connects over HTTP
• Reads data from:
• /server-status?auto
© 2018 InfluxData. All rights reserved.
win_perf_counters
• Collects performance data from
Windows machines
© 2018 InfluxData. All rights reserved.
Input Plugin Considerations
• Where does the data exist?

• How can it be collected?

• What shape is the data?
© 2018 InfluxData. All rights reserved.
prometheus_client
• Output plugin
• Exposes metrics in Prometheus
(now OpenMetrics) format.
© 2018 InfluxData. All rights reserved.
mqtt
• Output half of the MQTT plugins
• Allows you to write messages to
an MQTT broker.
© 2018 InfluxData. All rights reserved.
Output Plugin Considerations
• How should the data be shaped?

• How is the data output?

• Where is the data output to?
Plugin Architecture
© 2018 InfluxData. All rights reserved.
Plugin Architecture
• Built to be extensible from the beginning

• Make use of interfaces in Go

• Each plugin type has an interface
© 2018 InfluxData. All rights reserved.
© 2018 InfluxData. All rights reserved.
package simple
// simple.go
import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)
type Simple struct {
Ok bool
}
func (s *Simple) Description() string {
return "a demo plugin"
}
func (s *Simple) SampleConfig() string {
return `
## Indicate if everything is fine
ok = true
`
}
func (s *Simple) Gather(acc telegraf.Accumulator) error {
if s.Ok {
acc.AddFields("state", map[string]interface{}{"value":
"pretty good"}, nil)
} else {
acc.AddFields("state", map[string]interface{}{"value":
"not great"}, nil)
}
return nil
}
func init() {
inputs.Add("simple", func() telegraf.Input { return
&Simple{} })
}
Building a Plugin
© 2018 InfluxData. All rights reserved.
Getting started…
• Recommend using Go 1.11

• Understanding of Git & Pull Requests

• What is our plugin going to do?

• Generate data using “sine” and “cosine” trigonometric
functions

• Read the CONTRIBUTING.md file to ensure you can build & test
Telegraf before continuing
© 2018 InfluxData. All rights reserved.
Getting started…
• Set up your workspace, get the code, and create a branch:

• Create the required directories and files:

• Add boilerplate…
$ go get github.com/influxdata/telegraf

$ cd $GOPATH/github.com/influxdata/telegraf

$ git checkout -b trig-demo
$ cd plugins/inputs
$ mkdir trig
$ touch trig/trig.go
© 2018 InfluxData. All rights reserved.
package trig



import (

    "github.com/influxdata/telegraf"

    "github.com/influxdata/telegraf/plugins/inputs"

)



type Trig struct {

}



func (s *Trig) SampleConfig() string {

    return ""

}



func (s *Trig) Description() string {

    return ""

}



func (s *Trig) Gather(acc telegraf.Accumulator) error {

    return nil

}



func init() {

    inputs.Add("trig", func() telegraf.Input { return &Trig{} })

}
trig.go
© 2018 InfluxData. All rights reserved.
Import your plugin
• Add the following to telegraf/plugins/inputs/all/all.go

• This will your plugin into the main Telegraf package and ensure
that it can run.
_ "github.com/influxdata/telegraf/plugins/inputs/trig"
© 2018 InfluxData. All rights reserved.
Add configuration variables
• Each plugin has a struct

• There must be a property on the struct to hold any
configuration variables

• Any other variables should be unexported

• We’ll add two variables:

• “x” will hold the state of the plugin between collection intervals

• “Amplitude” will hold the amplitude value for the sin wave
© 2018 InfluxData. All rights reserved.
package trig



import (

    "github.com/influxdata/telegraf"

    "github.com/influxdata/telegraf/plugins/inputs"

)



type Trig struct {
    x         float64
    Amplitude float64

}



func (s *Trig) SampleConfig() string {

    return ""

}



func (s *Trig) Description() string {

    return ""

}



func (s *Trig) Gather(acc telegraf.Accumulator) error {

trig.go
© 2018 InfluxData. All rights reserved.
Add sample configuration
• Telegraf can dynamically construct configuration files

• Aggregates sample configs for all plugins

• The CLI allows you to provide arguments to filter which plugins
are included

• In 2.0, InfluxDB will use this info to generate Telegraf configs
© 2018 InfluxData. All rights reserved.
package trig



import (

    "github.com/influxdata/telegraf"

    "github.com/influxdata/telegraf/plugins/inputs"

)



type Trig struct {
    x         float64
    Amplitude float64

}



var TrigConfig = `

 ## Set the amplitude

 amplitude = 10.0

`



func (s *Trig) SampleConfig() string {

    return TrigConfig

}


trig.go
© 2018 InfluxData. All rights reserved.
Add a simple description
• This description is included above the plugin configuration and
should briefly describe what your plugin does
© 2018 InfluxData. All rights reserved.
type Trig struct {
    x         float64
    Amplitude float64

}



var TrigConfig = `

 ## Set the amplitude

 amplitude = 10.0

`



func (s *Trig) SampleConfig() string {

    return TrigConfig

}


func (s *Trig) Description() string {

    return "Inserts sine and cosine waves for demonstration
purposes"

}



func (s *Trig) Gather(acc telegraf.Accumulator) error {

trig.go
© 2018 InfluxData. All rights reserved.
Test your config!
• First, build Telegraf

• Generate a config:
$ make telegraf
$ telegraf -sample-config -input-filter trig
-output-filter influxdb -debug
© 2018 InfluxData. All rights reserved.
...

###############################################################################
# INPUT PLUGINS #
###############################################################################
# Inserts sine and cosine waves for demonstration purposes
[[inputs.trig]]
## Set the amplitude
amplitude = 10.0
© 2018 InfluxData. All rights reserved.
The “Gather” function
• The heart of the plugin, it is run every time telegraf collects
metrics

• This is where you’ll do the majority of the work

• We’ll generate sine and cosine values

• At the end of the function, we add any data we’ve collected
and to the Telegraf “Accumulator” by
calling .AddFields(measurement, tags, fields). 

• This defines a new point in InfluxDB with the timestamp being
the collection time.
© 2018 InfluxData. All rights reserved.
    return "Inserts sine and cosine waves for demonstration
purposes"

}



func (s *Trig) Gather(acc telegraf.Accumulator) error {

    sinner := math.Sin((s.x*math.Pi)/5.0) * s.Amplitude

    cosinner := math.Cos((s.x*math.Pi)/5.0) * s.Amplitude



    fields := make(map[string]interface{})

    fields["sine"] = sinner

    fields["cosine"] = cosinner



    tags := make(map[string]string)



    s.x += 1.0

    acc.AddFields("trig", fields, tags)



    return nil

}
trig.go
© 2018 InfluxData. All rights reserved.
Add initial state
• In our boilerplate, we added an init() function

• Called when the plugin is first initialized

• Makes the plugin available to the Telegraf agent

• We can define starting state as part of this function

• We need to initialize our “x” variable
© 2018 InfluxData. All rights reserved.
    cosinner := math.Cos((s.x*math.Pi)/5.0) * s.Amplitude



    fields := make(map[string]interface{})

    fields["sine"] = sinner

    fields["cosine"] = cosinner



    tags := make(map[string]string)



    s.x += 1.0

    acc.AddFields("trig", fields, tags)



    return nil

}




func init() {
    inputs.Add("trig", func() telegraf.Input { return &Trig{x: 0.0} })
}
trig.go
© 2018 InfluxData. All rights reserved.
Compile and test
• Once again, build Telegraf:

• Generate a config and write it to a file:
$ make telegraf
$ ./telegraf -sample-config -input-filter
trig -output-filter influxdb -debug >>
telegraf.conf.test
• Run Telegraf with the new config:
$ ./telegraf -config telegraf.conf.test
-debug
© 2018 InfluxData. All rights reserved.
$ ./telegraf -config telegraf.conf.test -debug
2019-03-14T06:32:12Z I! Starting Telegraf
2019-03-14T06:32:12Z I! Loaded inputs: trig
2019-03-14T06:32:12Z I! Loaded aggregators:
2019-03-14T06:32:12Z I! Loaded processors:
2019-03-14T06:32:12Z I! Loaded outputs: influxdb
2019-03-14T06:32:12Z I! Tags enabled: host=noah-mbp.local
2019-03-14T06:32:12Z I! [agent] Config: Interval:10s, Quiet:false,
Hostname:"noah-mbp.local", Flush Interval:10s
2019-03-14T06:32:12Z D! [agent] Connecting outputs
2019-03-14T06:32:12Z D! [agent] Attempting connection to output: influxdb
2019-03-14T06:32:12Z D! [agent] Successfully connected to output: influxdb
2019-03-14T06:32:12Z D! [agent] Starting service inputs
2019-03-14T06:32:30Z D! [outputs.influxdb] wrote batch of 1 metrics in
23.857525ms
2019-03-14T06:32:30Z D! [outputs.influxdb] buffer fullness: 1 / 10000 metrics.
© 2018 InfluxData. All rights reserved.
Final steps before submitting
• Add a few things to your plugin…

• a README.md

• a LICENSE

• A sample of the input/output format

• Tests!
© 2018 InfluxData. All rights reserved.
package trig



import (

    "math"

    "testing"

    "github.com/influxdata/telegraf/testutil"

)



func TestTrig(t *testing.T) {



    s := &Trig{



        Amplitude: 10.0,

    }



    for i := 0.0; i < 10.0; i++ {

        var acc testutil.Accumulator

        sine := math.Sin((i*math.Pi)/5.0) * s.Amplitude

        cosine := math.Cos((i*math.Pi)/5.0) * s.Amplitude

        s.Gather(&acc)

        fields := make(map[string]interface{})

        fields["sine"] = sine

        fields["cosine"] = cosine



        acc.AssertContainsFields(t, "trig", fields)



    }



}
trig_test.go
© 2018 InfluxData. All rights reserved.
Final steps before submitting
• Add a few things to your plugin…

• a README.md

• a LICENSE

• A sample of the input/output format

• Tests!

• …and submit! 

• github.com/influxdata/telegraf/pulls
Thank You!

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

InfluxDB + Telegraf Operator: Easy Kubernetes Monitoring
InfluxDB + Telegraf Operator: Easy Kubernetes MonitoringInfluxDB + Telegraf Operator: Easy Kubernetes Monitoring
InfluxDB + Telegraf Operator: Easy Kubernetes Monitoring
 
Timeseries - data visualization in Grafana
Timeseries - data visualization in GrafanaTimeseries - data visualization in Grafana
Timeseries - data visualization in Grafana
 
Monitoring with Prometheus
Monitoring with PrometheusMonitoring with Prometheus
Monitoring with Prometheus
 
Cloud Monitoring tool Grafana
Cloud Monitoring  tool Grafana Cloud Monitoring  tool Grafana
Cloud Monitoring tool Grafana
 
Observability, Distributed Tracing, and Open Source: The Missing Primer
Observability, Distributed Tracing, and Open Source: The Missing PrimerObservability, Distributed Tracing, and Open Source: The Missing Primer
Observability, Distributed Tracing, and Open Source: The Missing Primer
 
InfluxDB & Grafana
InfluxDB & GrafanaInfluxDB & Grafana
InfluxDB & Grafana
 
Prometheus and Grafana
Prometheus and GrafanaPrometheus and Grafana
Prometheus and Grafana
 
cLoki: Like Loki but for ClickHouse
cLoki: Like Loki but for ClickHousecLoki: Like Loki but for ClickHouse
cLoki: Like Loki but for ClickHouse
 
Grafana Loki: like Prometheus, but for Logs
Grafana Loki: like Prometheus, but for LogsGrafana Loki: like Prometheus, but for Logs
Grafana Loki: like Prometheus, but for Logs
 
Grafana introduction
Grafana introductionGrafana introduction
Grafana introduction
 
Monitoring With Prometheus
Monitoring With PrometheusMonitoring With Prometheus
Monitoring With Prometheus
 
Grafana.pptx
Grafana.pptxGrafana.pptx
Grafana.pptx
 
Monitoring using Prometheus and Grafana
Monitoring using Prometheus and GrafanaMonitoring using Prometheus and Grafana
Monitoring using Prometheus and Grafana
 
Intro to InfluxDB
Intro to InfluxDBIntro to InfluxDB
Intro to InfluxDB
 
Monitoring on Kubernetes using prometheus
Monitoring on Kubernetes using prometheusMonitoring on Kubernetes using prometheus
Monitoring on Kubernetes using prometheus
 
ELK Stack
ELK StackELK Stack
ELK Stack
 
Monitoring Kubernetes with Prometheus
Monitoring Kubernetes with PrometheusMonitoring Kubernetes with Prometheus
Monitoring Kubernetes with Prometheus
 
Infrastructure-as-Code (IaC) using Terraform
Infrastructure-as-Code (IaC) using TerraformInfrastructure-as-Code (IaC) using Terraform
Infrastructure-as-Code (IaC) using Terraform
 
Monitoring with prometheus
Monitoring with prometheusMonitoring with prometheus
Monitoring with prometheus
 
OSMC 2022 | OpenTelemetry 101 by Dotan Horovit s.pdf
OSMC 2022 | OpenTelemetry 101 by Dotan Horovit s.pdfOSMC 2022 | OpenTelemetry 101 by Dotan Horovit s.pdf
OSMC 2022 | OpenTelemetry 101 by Dotan Horovit s.pdf
 

Similar a How to Build a Telegraf Plugin by Noah Crowley

Similar a How to Build a Telegraf Plugin by Noah Crowley (20)

Sensor Data in InfluxDB by David Simmons, IoT Developer Evangelist | InfluxData
Sensor Data in InfluxDB by David Simmons, IoT Developer Evangelist | InfluxDataSensor Data in InfluxDB by David Simmons, IoT Developer Evangelist | InfluxData
Sensor Data in InfluxDB by David Simmons, IoT Developer Evangelist | InfluxData
 
How to Introduce Telemetry Streaming (gNMI) in Your Network with SNMP with Te...
How to Introduce Telemetry Streaming (gNMI) in Your Network with SNMP with Te...How to Introduce Telemetry Streaming (gNMI) in Your Network with SNMP with Te...
How to Introduce Telemetry Streaming (gNMI) in Your Network with SNMP with Te...
 
Finding OOMS in Legacy Systems with the Syslog Telegraf Plugin
Finding OOMS in Legacy Systems with the Syslog Telegraf PluginFinding OOMS in Legacy Systems with the Syslog Telegraf Plugin
Finding OOMS in Legacy Systems with the Syslog Telegraf Plugin
 
Nicolas Steinmetz [CérénIT] | Sustain Your Observability from Bare Metal TICK...
Nicolas Steinmetz [CérénIT] | Sustain Your Observability from Bare Metal TICK...Nicolas Steinmetz [CérénIT] | Sustain Your Observability from Bare Metal TICK...
Nicolas Steinmetz [CérénIT] | Sustain Your Observability from Bare Metal TICK...
 
Webinar - Building Custom Extensions With AppDynamics
Webinar - Building Custom Extensions With AppDynamicsWebinar - Building Custom Extensions With AppDynamics
Webinar - Building Custom Extensions With AppDynamics
 
Anais Dotis-Georgiou & Faith Chikwekwe [InfluxData] | Top 10 Hurdles for Flux...
Anais Dotis-Georgiou & Faith Chikwekwe [InfluxData] | Top 10 Hurdles for Flux...Anais Dotis-Georgiou & Faith Chikwekwe [InfluxData] | Top 10 Hurdles for Flux...
Anais Dotis-Georgiou & Faith Chikwekwe [InfluxData] | Top 10 Hurdles for Flux...
 
Python and trending_data_ops
Python and trending_data_opsPython and trending_data_ops
Python and trending_data_ops
 
INTERFACE by apidays 2023 - Data Collection Basics, Anais Dotis-Georgiou, Inf...
INTERFACE by apidays 2023 - Data Collection Basics, Anais Dotis-Georgiou, Inf...INTERFACE by apidays 2023 - Data Collection Basics, Anais Dotis-Georgiou, Inf...
INTERFACE by apidays 2023 - Data Collection Basics, Anais Dotis-Georgiou, Inf...
 
P4 Introduction
P4 Introduction P4 Introduction
P4 Introduction
 
The Flink - Apache Bigtop integration
The Flink - Apache Bigtop integrationThe Flink - Apache Bigtop integration
The Flink - Apache Bigtop integration
 
Sam Dillard [InfluxData] | Performance Optimization in InfluxDB | InfluxDays...
Sam Dillard [InfluxData] | Performance Optimization in InfluxDB  | InfluxDays...Sam Dillard [InfluxData] | Performance Optimization in InfluxDB  | InfluxDays...
Sam Dillard [InfluxData] | Performance Optimization in InfluxDB | InfluxDays...
 
InfluxDB 101 – Concepts and Architecture by Michael DeSa, Software Engineer |...
InfluxDB 101 – Concepts and Architecture by Michael DeSa, Software Engineer |...InfluxDB 101 – Concepts and Architecture by Michael DeSa, Software Engineer |...
InfluxDB 101 – Concepts and Architecture by Michael DeSa, Software Engineer |...
 
Why Open Source Works for DevOps Monitoring
Why Open Source Works for DevOps MonitoringWhy Open Source Works for DevOps Monitoring
Why Open Source Works for DevOps Monitoring
 
Helm Charts Security 101
Helm Charts Security 101Helm Charts Security 101
Helm Charts Security 101
 
Lessons Learned Running InfluxDB Cloud and Other Cloud Services at Scale by T...
Lessons Learned Running InfluxDB Cloud and Other Cloud Services at Scale by T...Lessons Learned Running InfluxDB Cloud and Other Cloud Services at Scale by T...
Lessons Learned Running InfluxDB Cloud and Other Cloud Services at Scale by T...
 
Monitoring InfluxEnterprise
Monitoring InfluxEnterpriseMonitoring InfluxEnterprise
Monitoring InfluxEnterprise
 
Taming the Tiger: Tips and Tricks for Using Telegraf
Taming the Tiger: Tips and Tricks for Using TelegrafTaming the Tiger: Tips and Tricks for Using Telegraf
Taming the Tiger: Tips and Tricks for Using Telegraf
 
Samantha Wang [InfluxData] | Data Collection Overview | InfluxDays 2022
Samantha Wang [InfluxData] | Data Collection Overview | InfluxDays 2022Samantha Wang [InfluxData] | Data Collection Overview | InfluxDays 2022
Samantha Wang [InfluxData] | Data Collection Overview | InfluxDays 2022
 
Taming the Tiger: Tips and Tricks for Using Telegraf
Taming the Tiger: Tips and Tricks for Using TelegrafTaming the Tiger: Tips and Tricks for Using Telegraf
Taming the Tiger: Tips and Tricks for Using Telegraf
 
Intro to InfluxDB 2.0 and Your First Flux Query by Sonia Gupta
Intro to InfluxDB 2.0 and Your First Flux Query by Sonia GuptaIntro to InfluxDB 2.0 and Your First Flux Query by Sonia Gupta
Intro to InfluxDB 2.0 and Your First Flux Query by Sonia Gupta
 

Más de InfluxData

How Teréga Replaces Legacy Data Historians with InfluxDB, AWS and IO-Base
How Teréga Replaces Legacy Data Historians with InfluxDB, AWS and IO-Base How Teréga Replaces Legacy Data Historians with InfluxDB, AWS and IO-Base
How Teréga Replaces Legacy Data Historians with InfluxDB, AWS and IO-Base
InfluxData
 
How Delft University's Engineering Students Make Their EV Formula-Style Race ...
How Delft University's Engineering Students Make Their EV Formula-Style Race ...How Delft University's Engineering Students Make Their EV Formula-Style Race ...
How Delft University's Engineering Students Make Their EV Formula-Style Race ...
InfluxData
 
Steinkamp, Clifford [InfluxData] | Welcome to InfluxDays 2022 - Day 2 | Influ...
Steinkamp, Clifford [InfluxData] | Welcome to InfluxDays 2022 - Day 2 | Influ...Steinkamp, Clifford [InfluxData] | Welcome to InfluxDays 2022 - Day 2 | Influ...
Steinkamp, Clifford [InfluxData] | Welcome to InfluxDays 2022 - Day 2 | Influ...
InfluxData
 
Steinkamp, Clifford [InfluxData] | Closing Thoughts Day 1 | InfluxDays 2022
Steinkamp, Clifford [InfluxData] | Closing Thoughts Day 1 | InfluxDays 2022Steinkamp, Clifford [InfluxData] | Closing Thoughts Day 1 | InfluxDays 2022
Steinkamp, Clifford [InfluxData] | Closing Thoughts Day 1 | InfluxDays 2022
InfluxData
 

Más de InfluxData (20)

Announcing InfluxDB Clustered
Announcing InfluxDB ClusteredAnnouncing InfluxDB Clustered
Announcing InfluxDB Clustered
 
Best Practices for Leveraging the Apache Arrow Ecosystem
Best Practices for Leveraging the Apache Arrow EcosystemBest Practices for Leveraging the Apache Arrow Ecosystem
Best Practices for Leveraging the Apache Arrow Ecosystem
 
How Bevi Uses InfluxDB and Grafana to Improve Predictive Maintenance and Redu...
How Bevi Uses InfluxDB and Grafana to Improve Predictive Maintenance and Redu...How Bevi Uses InfluxDB and Grafana to Improve Predictive Maintenance and Redu...
How Bevi Uses InfluxDB and Grafana to Improve Predictive Maintenance and Redu...
 
Power Your Predictive Analytics with InfluxDB
Power Your Predictive Analytics with InfluxDBPower Your Predictive Analytics with InfluxDB
Power Your Predictive Analytics with InfluxDB
 
How Teréga Replaces Legacy Data Historians with InfluxDB, AWS and IO-Base
How Teréga Replaces Legacy Data Historians with InfluxDB, AWS and IO-Base How Teréga Replaces Legacy Data Historians with InfluxDB, AWS and IO-Base
How Teréga Replaces Legacy Data Historians with InfluxDB, AWS and IO-Base
 
Build an Edge-to-Cloud Solution with the MING Stack
Build an Edge-to-Cloud Solution with the MING StackBuild an Edge-to-Cloud Solution with the MING Stack
Build an Edge-to-Cloud Solution with the MING Stack
 
Meet the Founders: An Open Discussion About Rewriting Using Rust
Meet the Founders: An Open Discussion About Rewriting Using RustMeet the Founders: An Open Discussion About Rewriting Using Rust
Meet the Founders: An Open Discussion About Rewriting Using Rust
 
Introducing InfluxDB Cloud Dedicated
Introducing InfluxDB Cloud DedicatedIntroducing InfluxDB Cloud Dedicated
Introducing InfluxDB Cloud Dedicated
 
Gain Better Observability with OpenTelemetry and InfluxDB
Gain Better Observability with OpenTelemetry and InfluxDB Gain Better Observability with OpenTelemetry and InfluxDB
Gain Better Observability with OpenTelemetry and InfluxDB
 
How a Heat Treating Plant Ensures Tight Process Control and Exceptional Quali...
How a Heat Treating Plant Ensures Tight Process Control and Exceptional Quali...How a Heat Treating Plant Ensures Tight Process Control and Exceptional Quali...
How a Heat Treating Plant Ensures Tight Process Control and Exceptional Quali...
 
How Delft University's Engineering Students Make Their EV Formula-Style Race ...
How Delft University's Engineering Students Make Their EV Formula-Style Race ...How Delft University's Engineering Students Make Their EV Formula-Style Race ...
How Delft University's Engineering Students Make Their EV Formula-Style Race ...
 
Introducing InfluxDB’s New Time Series Database Storage Engine
Introducing InfluxDB’s New Time Series Database Storage EngineIntroducing InfluxDB’s New Time Series Database Storage Engine
Introducing InfluxDB’s New Time Series Database Storage Engine
 
Start Automating InfluxDB Deployments at the Edge with balena
Start Automating InfluxDB Deployments at the Edge with balena Start Automating InfluxDB Deployments at the Edge with balena
Start Automating InfluxDB Deployments at the Edge with balena
 
Understanding InfluxDB’s New Storage Engine
Understanding InfluxDB’s New Storage EngineUnderstanding InfluxDB’s New Storage Engine
Understanding InfluxDB’s New Storage Engine
 
Streamline and Scale Out Data Pipelines with Kubernetes, Telegraf, and InfluxDB
Streamline and Scale Out Data Pipelines with Kubernetes, Telegraf, and InfluxDBStreamline and Scale Out Data Pipelines with Kubernetes, Telegraf, and InfluxDB
Streamline and Scale Out Data Pipelines with Kubernetes, Telegraf, and InfluxDB
 
Ward Bowman [PTC] | ThingWorx Long-Term Data Storage with InfluxDB | InfluxDa...
Ward Bowman [PTC] | ThingWorx Long-Term Data Storage with InfluxDB | InfluxDa...Ward Bowman [PTC] | ThingWorx Long-Term Data Storage with InfluxDB | InfluxDa...
Ward Bowman [PTC] | ThingWorx Long-Term Data Storage with InfluxDB | InfluxDa...
 
Scott Anderson [InfluxData] | New & Upcoming Flux Features | InfluxDays 2022
Scott Anderson [InfluxData] | New & Upcoming Flux Features | InfluxDays 2022Scott Anderson [InfluxData] | New & Upcoming Flux Features | InfluxDays 2022
Scott Anderson [InfluxData] | New & Upcoming Flux Features | InfluxDays 2022
 
Steinkamp, Clifford [InfluxData] | Closing Thoughts | InfluxDays 2022
Steinkamp, Clifford [InfluxData] | Closing Thoughts | InfluxDays 2022Steinkamp, Clifford [InfluxData] | Closing Thoughts | InfluxDays 2022
Steinkamp, Clifford [InfluxData] | Closing Thoughts | InfluxDays 2022
 
Steinkamp, Clifford [InfluxData] | Welcome to InfluxDays 2022 - Day 2 | Influ...
Steinkamp, Clifford [InfluxData] | Welcome to InfluxDays 2022 - Day 2 | Influ...Steinkamp, Clifford [InfluxData] | Welcome to InfluxDays 2022 - Day 2 | Influ...
Steinkamp, Clifford [InfluxData] | Welcome to InfluxDays 2022 - Day 2 | Influ...
 
Steinkamp, Clifford [InfluxData] | Closing Thoughts Day 1 | InfluxDays 2022
Steinkamp, Clifford [InfluxData] | Closing Thoughts Day 1 | InfluxDays 2022Steinkamp, Clifford [InfluxData] | Closing Thoughts Day 1 | InfluxDays 2022
Steinkamp, Clifford [InfluxData] | Closing Thoughts Day 1 | InfluxDays 2022
 

Último

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Último (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

How to Build a Telegraf Plugin by Noah Crowley

  • 1. Noah Crowley / Developer Advocate How to Build a Telegraf Plugin
  • 2. © 2018 InfluxData. All rights reserved. What we’ll be covering ✔ Telegraf overview ✔ Examples of Telegraf plugins ✔ Telegraf plugin architecture ✔ How to a write a Telegraf plugin
  • 4. © 2018 InfluxData. All rights reserved.
  • 5. © 2018 InfluxData. All rights reserved. Telegraf Overview • Telegraf is an open source, plugin-driven agent for collecting and reporting metrics • “Push” or “Pull” • Large collection of plugins: 192 (as of 3/13/2019) • Inputs: 149 • Outputs: 30 • Aggregators: 9 • Processors: 4
  • 6. © 2018 InfluxData. All rights reserved. Telegraf Overview • Can act as: • Agent • Collector • Part of an Ingest Pipeline
  • 7. © 2018 InfluxData. All rights reserved. Telegraf Plugins • Outputs • Allows you to send metrics to a variety of datastores • Plugins for both InfluxDB 1.x and 2.0 • Supports Kafka, MQTT, NSQ, OpenMetrics, and more • Aggregators and Processors allow you to manipulate data as it flows through Telegraf • Transform tags, convert types, calculate histograms
  • 8. © 2018 InfluxData. All rights reserved. Telegraf Input Plugins • Metrics ingestion from host system: CPU, I/O, Network… • Common Applications like NGINX, Postgres, Redis… • Third Party APIs: Mailchimp, Cloudwatch, Google Analytics… • General-Purpose Protocols: HTTP, Socket, MQTT… • …and more!
  • 9. © 2018 InfluxData. All rights reserved. Telegraf Benefits • Minimal memory footprint • Tagging of metrics • Batching of metrics to reduce the number of atomic writes • Easy contribution of functionality thanks to Go • Static Binary • Strong Community Contributions
  • 11. © 2018 InfluxData. All rights reserved. statsd • Listens for incoming data • Acts as a statsd instance • Outputs to any configured output
  • 12. © 2018 InfluxData. All rights reserved. postgresql • Collects performance data • Uses data from built-in views: • pg_stat_database • pg_stat_bgwriter
  • 13. © 2018 InfluxData. All rights reserved. apache • Connects over HTTP • Reads data from: • /server-status?auto
  • 14. © 2018 InfluxData. All rights reserved. win_perf_counters • Collects performance data from Windows machines
  • 15. © 2018 InfluxData. All rights reserved. Input Plugin Considerations • Where does the data exist? • How can it be collected? • What shape is the data?
  • 16. © 2018 InfluxData. All rights reserved. prometheus_client • Output plugin • Exposes metrics in Prometheus (now OpenMetrics) format.
  • 17. © 2018 InfluxData. All rights reserved. mqtt • Output half of the MQTT plugins • Allows you to write messages to an MQTT broker.
  • 18. © 2018 InfluxData. All rights reserved. Output Plugin Considerations • How should the data be shaped? • How is the data output? • Where is the data output to?
  • 20. © 2018 InfluxData. All rights reserved. Plugin Architecture • Built to be extensible from the beginning • Make use of interfaces in Go • Each plugin type has an interface
  • 21. © 2018 InfluxData. All rights reserved.
  • 22. © 2018 InfluxData. All rights reserved. package simple // simple.go import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/plugins/inputs" ) type Simple struct { Ok bool } func (s *Simple) Description() string { return "a demo plugin" } func (s *Simple) SampleConfig() string { return ` ## Indicate if everything is fine ok = true ` } func (s *Simple) Gather(acc telegraf.Accumulator) error { if s.Ok { acc.AddFields("state", map[string]interface{}{"value": "pretty good"}, nil) } else { acc.AddFields("state", map[string]interface{}{"value": "not great"}, nil) } return nil } func init() { inputs.Add("simple", func() telegraf.Input { return &Simple{} }) }
  • 24. © 2018 InfluxData. All rights reserved. Getting started… • Recommend using Go 1.11 • Understanding of Git & Pull Requests • What is our plugin going to do? • Generate data using “sine” and “cosine” trigonometric functions • Read the CONTRIBUTING.md file to ensure you can build & test Telegraf before continuing
  • 25. © 2018 InfluxData. All rights reserved. Getting started… • Set up your workspace, get the code, and create a branch: • Create the required directories and files: • Add boilerplate… $ go get github.com/influxdata/telegraf
 $ cd $GOPATH/github.com/influxdata/telegraf
 $ git checkout -b trig-demo $ cd plugins/inputs $ mkdir trig $ touch trig/trig.go
  • 26. © 2018 InfluxData. All rights reserved. package trig
 
 import (
     "github.com/influxdata/telegraf"
     "github.com/influxdata/telegraf/plugins/inputs"
 )
 
 type Trig struct {
 }
 
 func (s *Trig) SampleConfig() string {
     return ""
 }
 
 func (s *Trig) Description() string {
     return ""
 }
 
 func (s *Trig) Gather(acc telegraf.Accumulator) error {
     return nil
 }
 
 func init() {
     inputs.Add("trig", func() telegraf.Input { return &Trig{} })
 } trig.go
  • 27. © 2018 InfluxData. All rights reserved. Import your plugin • Add the following to telegraf/plugins/inputs/all/all.go • This will your plugin into the main Telegraf package and ensure that it can run. _ "github.com/influxdata/telegraf/plugins/inputs/trig"
  • 28. © 2018 InfluxData. All rights reserved. Add configuration variables • Each plugin has a struct • There must be a property on the struct to hold any configuration variables • Any other variables should be unexported • We’ll add two variables: • “x” will hold the state of the plugin between collection intervals • “Amplitude” will hold the amplitude value for the sin wave
  • 29. © 2018 InfluxData. All rights reserved. package trig
 
 import (
     "github.com/influxdata/telegraf"
     "github.com/influxdata/telegraf/plugins/inputs"
 )
 
 type Trig struct {     x         float64     Amplitude float64
 }
 
 func (s *Trig) SampleConfig() string {
     return ""
 }
 
 func (s *Trig) Description() string {
     return ""
 }
 
 func (s *Trig) Gather(acc telegraf.Accumulator) error {
 trig.go
  • 30. © 2018 InfluxData. All rights reserved. Add sample configuration • Telegraf can dynamically construct configuration files • Aggregates sample configs for all plugins • The CLI allows you to provide arguments to filter which plugins are included • In 2.0, InfluxDB will use this info to generate Telegraf configs
  • 31. © 2018 InfluxData. All rights reserved. package trig
 
 import (
     "github.com/influxdata/telegraf"
     "github.com/influxdata/telegraf/plugins/inputs"
 )
 
 type Trig struct {     x         float64     Amplitude float64
 }
 
 var TrigConfig = `
  ## Set the amplitude
  amplitude = 10.0
 `
 
 func (s *Trig) SampleConfig() string {
     return TrigConfig
 } 
 trig.go
  • 32. © 2018 InfluxData. All rights reserved. Add a simple description • This description is included above the plugin configuration and should briefly describe what your plugin does
  • 33. © 2018 InfluxData. All rights reserved. type Trig struct {     x         float64     Amplitude float64
 }
 
 var TrigConfig = `
  ## Set the amplitude
  amplitude = 10.0
 `
 
 func (s *Trig) SampleConfig() string {
     return TrigConfig
 } 
 func (s *Trig) Description() string {
     return "Inserts sine and cosine waves for demonstration purposes"
 }
 
 func (s *Trig) Gather(acc telegraf.Accumulator) error {
 trig.go
  • 34. © 2018 InfluxData. All rights reserved. Test your config! • First, build Telegraf • Generate a config: $ make telegraf $ telegraf -sample-config -input-filter trig -output-filter influxdb -debug
  • 35. © 2018 InfluxData. All rights reserved. ...
 ############################################################################### # INPUT PLUGINS # ############################################################################### # Inserts sine and cosine waves for demonstration purposes [[inputs.trig]] ## Set the amplitude amplitude = 10.0
  • 36. © 2018 InfluxData. All rights reserved. The “Gather” function • The heart of the plugin, it is run every time telegraf collects metrics • This is where you’ll do the majority of the work • We’ll generate sine and cosine values • At the end of the function, we add any data we’ve collected and to the Telegraf “Accumulator” by calling .AddFields(measurement, tags, fields). • This defines a new point in InfluxDB with the timestamp being the collection time.
  • 37. © 2018 InfluxData. All rights reserved.     return "Inserts sine and cosine waves for demonstration purposes"
 }
 
 func (s *Trig) Gather(acc telegraf.Accumulator) error {
     sinner := math.Sin((s.x*math.Pi)/5.0) * s.Amplitude
     cosinner := math.Cos((s.x*math.Pi)/5.0) * s.Amplitude
 
     fields := make(map[string]interface{})
     fields["sine"] = sinner
     fields["cosine"] = cosinner
 
     tags := make(map[string]string)
 
     s.x += 1.0
     acc.AddFields("trig", fields, tags)
 
     return nil
 } trig.go
  • 38. © 2018 InfluxData. All rights reserved. Add initial state • In our boilerplate, we added an init() function • Called when the plugin is first initialized • Makes the plugin available to the Telegraf agent • We can define starting state as part of this function • We need to initialize our “x” variable
  • 39. © 2018 InfluxData. All rights reserved.     cosinner := math.Cos((s.x*math.Pi)/5.0) * s.Amplitude
 
     fields := make(map[string]interface{})
     fields["sine"] = sinner
     fields["cosine"] = cosinner
 
     tags := make(map[string]string)
 
     s.x += 1.0
     acc.AddFields("trig", fields, tags)
 
     return nil
 } 
 
 func init() {     inputs.Add("trig", func() telegraf.Input { return &Trig{x: 0.0} }) } trig.go
  • 40. © 2018 InfluxData. All rights reserved. Compile and test • Once again, build Telegraf: • Generate a config and write it to a file: $ make telegraf $ ./telegraf -sample-config -input-filter trig -output-filter influxdb -debug >> telegraf.conf.test • Run Telegraf with the new config: $ ./telegraf -config telegraf.conf.test -debug
  • 41. © 2018 InfluxData. All rights reserved. $ ./telegraf -config telegraf.conf.test -debug 2019-03-14T06:32:12Z I! Starting Telegraf 2019-03-14T06:32:12Z I! Loaded inputs: trig 2019-03-14T06:32:12Z I! Loaded aggregators: 2019-03-14T06:32:12Z I! Loaded processors: 2019-03-14T06:32:12Z I! Loaded outputs: influxdb 2019-03-14T06:32:12Z I! Tags enabled: host=noah-mbp.local 2019-03-14T06:32:12Z I! [agent] Config: Interval:10s, Quiet:false, Hostname:"noah-mbp.local", Flush Interval:10s 2019-03-14T06:32:12Z D! [agent] Connecting outputs 2019-03-14T06:32:12Z D! [agent] Attempting connection to output: influxdb 2019-03-14T06:32:12Z D! [agent] Successfully connected to output: influxdb 2019-03-14T06:32:12Z D! [agent] Starting service inputs 2019-03-14T06:32:30Z D! [outputs.influxdb] wrote batch of 1 metrics in 23.857525ms 2019-03-14T06:32:30Z D! [outputs.influxdb] buffer fullness: 1 / 10000 metrics.
  • 42.
  • 43. © 2018 InfluxData. All rights reserved. Final steps before submitting • Add a few things to your plugin… • a README.md • a LICENSE • A sample of the input/output format • Tests!
  • 44. © 2018 InfluxData. All rights reserved. package trig
 
 import (
     "math"
     "testing"
     "github.com/influxdata/telegraf/testutil"
 )
 
 func TestTrig(t *testing.T) {
 
     s := &Trig{
 
         Amplitude: 10.0,
     }
 
     for i := 0.0; i < 10.0; i++ {
         var acc testutil.Accumulator
         sine := math.Sin((i*math.Pi)/5.0) * s.Amplitude
         cosine := math.Cos((i*math.Pi)/5.0) * s.Amplitude
         s.Gather(&acc)
         fields := make(map[string]interface{})
         fields["sine"] = sine
         fields["cosine"] = cosine
 
         acc.AssertContainsFields(t, "trig", fields)
 
     }
 
 } trig_test.go
  • 45. © 2018 InfluxData. All rights reserved. Final steps before submitting • Add a few things to your plugin… • a README.md • a LICENSE • A sample of the input/output format • Tests! • …and submit! • github.com/influxdata/telegraf/pulls