Build a streaming data source plugin

This guide explains how to build a streaming data source plugin.

This guide assumes that you’re already familiar with how to Build a data source plugin.

When monitoring critical applications, you want your dashboard to refresh as soon as your data does. In Grafana, you can set your dashboards to automatically refresh at a certain interval, no matter what data source you use. Unfortunately, this means that your queries are requesting all the data to be sent again, regardless of whether the data has actually changed.

By enabling streaming for your data source plugin, you can update your dashboard as soon as new data becomes available.

For example, a streaming data source plugin can connect to a websocket, or subscribe to a message bus, and update the visualization whenever a new message is available.

Let’s see how you can add streaming to an existing data source!

Grafana uses RxJS to continuously send data from a data source to a panel visualization. There’s a lot more to RxJS than what’s covered in this guide. If you want to learn more, check out the RxJS documentation.

  1. Enable streaming for your data source in the plugin.json file.

    1. {
    2. "streaming": true
    3. }
  2. Change the signature of the query method to return an Observable from the rxjs package. Make sure you remove the async keyword.

    1. import { Observable } from 'rxjs';
    1. query(options: DataQueryRequest<MyQuery>): Observable<DataQueryResponse> {
    2. // ...
    3. }
  3. Create an Observable for each query, and then combine them all using the merge function from the rxjs package.

    1. import { Observable, merge } from 'rxjs';
    1. const observables = options.targets.map((target) => {
    2. return new Observable<DataQueryResponse>((subscriber) => {
    3. // ...
    4. });
    5. });
    6. return merge(...observables);
  4. In the subscribe function, create a CircularDataFrame.

    1. import { CircularDataFrame } from '@grafana/data';
    1. const frame = new CircularDataFrame({
    2. append: 'tail',
    3. capacity: 1000,
    4. });
    5. frame.refId = query.refId;
    6. frame.addField({ name: 'time', type: FieldType.time });
    7. frame.addField({ name: 'value', type: FieldType.number });

    Circular data frames have a limited capacity. When a circular data frame reaches its capacity, the oldest data point is removed.

  5. Use subscriber.next() to send the updated data frame whenever you receive new updates.

    1. import { LoadingState } from '@grafana/data';
    1. const intervalId = setInterval(() => {
    2. frame.add({ time: Date.now(), value: Math.random() });
    3. subscriber.next({
    4. data: [frame],
    5. key: query.refId,
    6. state: LoadingState.Streaming,
    7. });
    8. }, 500);
    9. return () => {
    10. clearInterval(intervalId);
    11. };

    Note: In practice, you’d call subscriber.next as soon as you receive new data from a websocket or a message bus. The example above simulates data being received every 500 milliseconds.

Here’s the final query method.

  1. query(options: DataQueryRequest<MyQuery>): Observable<DataQueryResponse> {
  2. const streams = options.targets.map(target => {
  3. const query = defaults(target, defaultQuery);
  4. return new Observable<DataQueryResponse>(subscriber => {
  5. const frame = new CircularDataFrame({
  6. append: 'tail',
  7. capacity: 1000,
  8. });
  9. frame.refId = query.refId;
  10. frame.addField({ name: 'time', type: FieldType.time });
  11. frame.addField({ name: 'value', type: FieldType.number });
  12. const intervalId = setInterval(() => {
  13. frame.add({ time: Date.now(), value: Math.random() });
  14. subscriber.next({
  15. data: [frame],
  16. key: query.refId,
  17. state: LoadingState.Streaming,
  18. });
  19. }, 100);
  20. return () => {
  21. clearInterval(intervalId);
  22. };
  23. });
  24. });
  25. return merge(...streams);
  26. }

One limitation with this example is that the panel visualization is cleared every time you update the dashboard. If you have access to historical data, you can add, or backfill, it to the data frame before the first call to subscriber.next().