<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>InfluxData Blog - Steven Soroka</title>
    <description>Posts by Steven Soroka on the InfluxData Blog</description>
    <link>https://www.influxdata.com/blog/author/steven-soroka/</link>
    <language>en-us</language>
    <lastBuildDate>Thu, 10 Sep 2020 08:00:55 -0700</lastBuildDate>
    <pubDate>Thu, 10 Sep 2020 08:00:55 -0700</pubDate>
    <ttl>1800</ttl>
    <item>
      <title>Getting Started with Sending StatsD Metrics to Telegraf &amp; InfluxDB</title>
      <description>&lt;div class="old-version"&gt;

This tutorial will walk you through sending &lt;a href="https://github.com/statsd/statsd#statsd---"&gt;StatsD&lt;/a&gt; metrics to &lt;a href="https://www.influxdata.com/time-series-platform/telegraf/"&gt;Telegraf&lt;/a&gt;.

StatsD is a simple protocol for sending application metrics via UDP. These metrics can be sent to a Telegraf instance, where they are aggregated and periodically flushed to &lt;a href="https://www.influxdata.com/time-series-platform/influxdb/"&gt;InfluxDB&lt;/a&gt; or other output sinks that you have configured. At the time of writing, we have 37 different &lt;a href="https://github.com/influxdata/telegraf/tree/master/plugins/outputs"&gt;output plugins&lt;/a&gt; supported.
&lt;h2&gt;Why StatsD?&lt;/h2&gt;
There are many good reasons to use StatsD, and &lt;a href="https://www.datadoghq.com/blog/statsd/"&gt;many&lt;/a&gt; &lt;a href="https://codeascraft.com/2011/02/15/measure-anything-measure-everything/"&gt;good&lt;/a&gt; &lt;a href="https://www.digitalocean.com/community/tutorials/an-introduction-to-tracking-statistics-with-graphite-statsd-and-collectd"&gt;blogs&lt;/a&gt; about why it's great. StatsD is simple, has a tiny footprint, and has a fantastic ecosystem of client libraries. It can't crash your application and has become the standard for large-scale metric collection.
&lt;h2&gt;How it works&lt;/h2&gt;
&lt;a href="https://www.influxdata.com/time-series-platform/telegraf/"&gt;Telegraf&lt;/a&gt; is an agent written in Go and accepts StatsD protocol metrics over UDP, then periodically forwards the metrics to InfluxDB.

&lt;a href="https://github.com/statsd/statsd#statsd---"&gt;StatsD&lt;/a&gt; metrics can be sent from applications using any of the many available &lt;a href="https://github.com/etsy/statsd/wiki#client-implementations"&gt;client libraries.&lt;/a&gt; Here is a standard setup for collecting StatsD metrics:

&lt;/div&gt;
&lt;p style="text-align: center;"&gt;&lt;img class="alignnone size-full wp-image-250193" src="/images/legacy-uploads/statsd-telegraf-influxdb.png" alt="statsd telegraf influxdb" width="920" height="327" /&gt;&lt;/p&gt;

&lt;h2&gt;Why UDP?&lt;/h2&gt;
&lt;p&gt;UDP is often called the “fire and forget” protocol. UDP is a connectionless protocol that does not wait for a response. UDP packets are sent without waiting for a response, aren’t slowed down by network latency, and aren’t affected by network blips that would drop connections. This means UDP is lossy, but that’s typically okay for metric data.&lt;/p&gt;
&lt;h2&gt;Setup&lt;/h2&gt;
&lt;p&gt;First, you need to have &lt;a href="https://portal.influxdata.com/downloads/"&gt;Telegraf installed&lt;/a&gt;. Next, you will need to setup Telegraf with the &lt;a href="https://github.com/influxdata/telegraf/tree/master/plugins/inputs/statsd"&gt;statsd plugin&lt;/a&gt;. The &lt;code class="language-markup"&gt;--sample-config&lt;/code&gt; option tells Telegraf to output a config file. &lt;code class="language-markup"&gt;--input-filter&lt;/code&gt; and &lt;code class="language-markup"&gt;--output-filter&lt;/code&gt; tell Telegraf which plugins (StatsD) and outputs (InfluxDB) to configure. (Note you should use influxdb_v2 for InfluxDB 2.0 cloud or OSS beta, and influxdb for a local InfluxDB pre 2.0):&lt;/p&gt;
&lt;pre class="line-numbers"&gt;&lt;code class="language-bash"&gt;$ telegraf --sample-config --input-filter statsd --output-filter influxdb_v2 &amp;gt; telegraf.conf
$ telegraf --config telegraf.conf&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The config file (telegraf.conf) will assume that your InfluxDB instance is running on localhost, and will need to be edited if that’s not the case. There are also many configuration options for the StatsD server, but I won’t go into all of them in this guide, see the &lt;a href="https://github.com/influxdata/telegraf/blob/master/plugins/inputs/statsd/README.md"&gt;documentation&lt;/a&gt; for more details.&lt;/p&gt;
&lt;h2&gt;Sending StatsD metrics&lt;/h2&gt;
&lt;p&gt;By default, Telegraf will begin listening on port 8125 for UDP packets. StatsD metrics can be sent to it using echo and netcat:&lt;/p&gt;

&lt;p&gt;&lt;code class="language-bash"&gt;$ echo "mycounter:10|c" | nc -C -w 1 -u localhost 8125&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Or using your &lt;a href="https://github.com/etsy/statsd/wiki#client-implementations"&gt;favorite client library.&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="introducing-influx-statsd" style="text-align: left;"&gt;&lt;strong&gt;Influx StatsD&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Since InfluxDB supports tags, our StatsD implementation does too! Adding tags to a StatsD metric is similar to how they appear in &lt;a href="https://influxdb.com/docs/v0.9/write_protocols/line.html"&gt;line-protocol.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This means that you can tag your StatsD metrics like below. This particular metric increments &lt;code class="language-javascript"&gt;theuser_login counter&lt;/code&gt; by 1, with tags for the service and region we are using.&lt;/p&gt;

&lt;p&gt;&lt;code class="language-javascript"&gt;user.logins,service=payroll,region=us-west:1|c&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That’s it! Simply add a comma-separated list of tags in key=value format.&lt;/p&gt;

&lt;p&gt;For those of you using a StatsD client, this extra bit can be added to the bucket. I’ll use the &lt;a href="https://pypi.python.org/pypi/statsd"&gt;Python client&lt;/a&gt; as an example:&lt;/p&gt;
&lt;pre class="line-numbers"&gt;&lt;code class="language-python"&gt;&amp;gt;&amp;gt;&amp;gt; import statsd
&amp;gt;&amp;gt;&amp;gt; c = statsd.StatsClient('localhost', 8125)
&amp;gt;&amp;gt;&amp;gt; c.incr('user.logins,service=payroll,region=us-west')  # Increment counter&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once flushed, the metric will be available in InfluxDB in all its tagged glory.&lt;/p&gt;
&lt;pre class="line-numbers"&gt;&lt;code class="language-javascript"&gt;&amp;gt; SELECT * FROM statsd_user_logins
name: statsd_user_logins
------------------------
time                    host    metric_type  region   service  value
2015-10-27T03:26:40Z    tyrion  counter      us-west  payroll  1
2015-10-27T03:26:50Z    tyrion  counter      us-west  payroll  1&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id="metrics" style="text-align: left;"&gt;Metrics&lt;/h2&gt;
&lt;p&gt;Telegraf supports all of the standard &lt;a href="https://github.com/etsy/statsd/blob/master/docs/metric_types.md"&gt;StatsD metrics,&lt;/a&gt; which are detailed below.&lt;/p&gt;
&lt;h3&gt;Counters&lt;/h3&gt;
&lt;pre&gt;&lt;code class="language-bash"&gt;logins.total:1|c
logins.total:15|c&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A simple counter, the &lt;code class="language-bash"&gt;logins_total metric&lt;/code&gt; will be incremented by 1 and 15 in the above examples. Counters can be always-increasing, or you can opt to have them cleared with each flush using thedelete_counters config option.&lt;/p&gt;
&lt;h3&gt;Sampling&lt;/h3&gt;
&lt;p&gt;&lt;code class="language-markup"&gt;logins.total:1|c|@0.1&lt;/code&gt;
Tells StatsD that this counter is being sent sampled every 1/10th of the time. In this example, &lt;code class="language-bash"&gt;logins_total metric&lt;/code&gt; will be incremented by 10.&lt;/p&gt;
&lt;h3 id="gauges"&gt;Gauges&lt;/h3&gt;
&lt;p&gt;&lt;code class="language-bash"&gt;current.users:105|g&lt;/code&gt;
Gauges are changed with each subsequent value sent. The value that makes it to InfluxDB will be the last recorded value. Gauges will remain at the same value until a new value is sent. You can opt to have them cleared with each flush using the &lt;code class="language-bash"&gt;delete_gauges&lt;/code&gt; config option.&lt;/p&gt;

&lt;p&gt;Adding a sign can change the value of a gauge rather than overwriting it:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-markup"&gt;current.users:-10|g
current.users:+12|g&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id="timings-histograms"&gt;Timings &amp;amp; histograms&lt;/h3&gt;
&lt;pre&gt;&lt;code class="language-markup"&gt;response.time:301|ms
response.time:301|h&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Timings are meant to track how long something took. They are an invaluable tool for tracking application performance.&lt;/p&gt;

&lt;p&gt;When Telegraf receives timing metrics, it will aggregate them and write the following statistics to InfluxDB, more details on each of these can be found in the &lt;a href="https://github.com/influxdata/telegraf/tree/master/plugins/inputs/statsd#measurements"&gt;documentation&lt;/a&gt;.&lt;/p&gt;
&lt;ul&gt;
 	&lt;li&gt;&lt;code class="language-bash" style="font-style: inherit; font-weight: inherit;"&gt;stat_name_lower&lt;/code&gt;&lt;/li&gt;
 	&lt;li&gt;&lt;code class="language-bash" style="font-style: inherit; font-weight: inherit;"&gt;stat_name_upper&lt;/code&gt;&lt;/li&gt;
 	&lt;li&gt;&lt;code class="language-bash" style="font-style: inherit; font-weight: inherit;"&gt;stat_name_mean&lt;/code&gt;&lt;/li&gt;
 	&lt;li&gt;&lt;code class="language-bash" style="font-style: inherit; font-weight: inherit;"&gt;stat_name_stddev&lt;/code&gt;&lt;/li&gt;
 	&lt;li&gt;&lt;code class="language-bash" style="font-style: inherit; font-weight: inherit;"&gt;stat_name_count&lt;/code&gt;&lt;/li&gt;
 	&lt;li&gt;&lt;code class="language-bash" style="font-style: inherit; font-weight: inherit;"&gt;stat_name_percentile_90&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="sampling-1"&gt;Sampling&lt;/h3&gt;
&lt;p&gt;&lt;code class="language-bash"&gt;response.time:301|ms|@0.1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Timings, like counters, can also be sampled. This will let Telegraf know that this timing was only taken once every 10 runs.&lt;/p&gt;
&lt;h3&gt;Notes&lt;/h3&gt;
&lt;p&gt;Telegraf aggregates stats as they arrive, and limits the number of timings cached to keep its memory footprint low. By default, Telegraf will keep track of 1000 timings per-stat when calculating percentiles. This can be adjusted using the percentile_limit config option.&lt;/p&gt;
&lt;h3&gt;Sets&lt;/h3&gt;
&lt;p&gt;&lt;code class="language-bash"&gt;unique.users:100|s&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Sets can be used to count unique occurrences. In the above example, the unique.users metric will be incremented by 1, then will not be incremented no matter how many times the value 100 is sent.&lt;/p&gt;
&lt;h2&gt;Templates&lt;/h2&gt;
&lt;p&gt;The plugin supports specifying templates for transforming statsd buckets into InfluxDB measurement names, tags, and fields using keywords. These can be used to specify parts of the bucket that are to be used in the measurement name. Other words in the template are used as tag names. For example, the following template:&lt;/p&gt;

&lt;p&gt;&lt;code class="language-ini"&gt;templates = ["cpu.* measurement.field.region"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This means for every metric starting with cpu. you intend to split the bucket name into three segments, the first belongs to the measurement name, the second to a field, and the last will be split off, and turned into a tag named “region”. If the original metric looked like this:&lt;/p&gt;

&lt;p&gt;&lt;code class="language-ini"&gt;cpu.load.us-west:100|g&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The resulting metric created in InfluxDB would look like this:&lt;/p&gt;

&lt;p&gt;&lt;code class="language-ini"&gt;cpu,metric_type=gauge,region=us-west load=100&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Where the metric name is &lt;code class="language-ini"&gt;cpu_load&lt;/code&gt;, and it has a tag named region with the value us-west.&lt;/p&gt;

&lt;p&gt;This gives you detailed control over mapping StatsD bucket names to metrics and tags in influxdb.&lt;/p&gt;

&lt;p&gt;Also, let us know what you’d like to see by &lt;a href="https://github.com/influxdata/telegraf/issues/new/choose"&gt;opening an issue on GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="future-work"&gt;Next steps&lt;/h2&gt;
&lt;ul&gt;
 	&lt;li&gt;&lt;a href="https://portal.influxdata.com/downloads"&gt;Download Telegraf&lt;/a&gt;&lt;/li&gt;
 	&lt;li&gt;Start a Free trial of &lt;a href="https://www.influxdata.com/products/influxdb-cloud/"&gt;InfluxDB Cloud&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
      <pubDate>Thu, 10 Sep 2020 08:00:55 -0700</pubDate>
      <link>https://www.influxdata.com/blog/getting-started-with-sending-statsd-metrics-to-telegraf-influxdb/</link>
      <guid isPermaLink="true">https://www.influxdata.com/blog/getting-started-with-sending-statsd-metrics-to-telegraf-influxdb/</guid>
      <category>Use Cases</category>
      <category>Product</category>
      <category>Developer</category>
      <category>Getting Started</category>
      <author>Steven Soroka (InfluxData)</author>
    </item>
  </channel>
</rss>
