Continuing on the the previous post on simple use of Tgres components, let’s try to count something that goes by really fast.
This time let’s start out with creating a memory-based SerDe. This means that all our data is in memory and there is no database backing our series.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
Now let’s create a goroutine which creates data points as fast as it can, the difference from the previous blog post is that we are using QueueGauge(), which is a paced metric, meaning that it flushes to the time series only periodically (once per second by default) so as to not overwhelm the I/O and or network (even though in this case it doesn’t really matter since we’re using a memory-based SerDe anyway).
1 2 3 4 5 6 7 8 9 10 |
|
And finally, as before, we need to hook up a couple of http handlers:
1 2 3 4 5 6 7 8 9 10 |
|
Now if we run the above code with something like
go run simpletgres.go
, we’ll notice that unlike with the previous
example, the web server starts right away, and the data points are
being written while the server is running. If we aim Grafana at it,
we should be able to see the chart update in real time.
After a couple of minutes, mine looks like this:
So my macbook can crank these out at about 2.5 million per second.
In my experience instrumenting my apps with simple counters like this and having them available directly from the app without having to send them to a separate statsd server somewhere has been extremely useful in helping understand performance and other issues.