Did you know you can use Tgres components in your code without PostgreSQL, and in just a dozen lines of code instrument your program with a time series. This example shows a complete server emulating Graphite API which you can use with Grafana (or any other tool).
In this example we will be using three Tgres packages like so (in addition to a few standard ones, I’m skipping them here for brevity - complete source code gist):
1 2 3 4 5 |
|
First we need a Data Source. This will create a Data Source containing one Round Robin Archive with a 10 second resolution spanning 1000 seconds.
1 2 3 4 5 6 7 8 9 |
|
Let’s shove a bunch of data points into it. To make it look extra nice, we can make these points look like a sinusoid with this little function:
1 2 3 4 |
|
And now for the actual population of the series:
1 2 3 4 5 6 |
|
We will also need to create a NamedDSFetcher, the structure which knows how to search dot-separated series names a la Graphite.
1
|
|
Finally, we need to create two http handlers which will mimic a Graphite server and start listening for requests:
1 2 3 4 5 6 |
|
Now if you point Grafana at it, it will happily think it’s Graphite and should show you a chart like this:
Note that you can use all kinds of Graphite functions at this point - it all “just works”.
Enjoy!