Build a logs data source plugin
Data sources in Grafana supports both metrics and log data. The steps to build a logs data source plugin are largely the same as for a metrics data source. This guide assumes that you’re already familiar with how to Build a data source plugin for metrics.
To add logs support to an existing data source, you need to:
- Enable logs support
- Construct the log data
- (Optional) Add preferred visualisation type hint to the data frame
Tell Grafana that your data source plugin can return log data, by adding to the file.
Just like for metrics data, Grafana expects your plugin to return log data as a .
To return log data, return a data frame with at least one time field and one text field from the data source’s query
method.
Example:
const frame = new MutableDataFrame({
refId: query.refId,
{ name: 'time', type: FieldType.time },
{ name: 'content', type: FieldType.string },
],
});
frame.add({ time: 1589189388597, content: 'user registered' });
frame.add({ time: 1589189406480, content: 'user logged in' });
Congratulations, you just wrote your first logs data source plugin! Next, let’s look at a couple of features that can further improve the experience for the user.
To make sure Grafana recognizes data as logs and shows logs visualization automatically in Explore you have do set meta.preferredVisualisationType
to in the returned data frame. See
Example:
To help filter log lines, many log systems let you query logs based on metadata, or labels.
You can add labels to a stream of logs by setting the labels property on the Field.
Example:
const frame = new MutableDataFrame({
refId: query.refId,
fields: [
{ name: 'time', type: FieldType.time },
{ name: 'content', type: FieldType.string, labels: { filename: 'file.txt' } },
],
frame.add({ time: 1589189388597, content: 'user registered' });
frame.add({ time: 1589189406480, content: 'user logged in' });
If a data frame has more than one text field, then Grafana assumes the first field in the data frame to be the actual log line. Any subsequent text fields are treated as detected fields.
While you can add any number of custom fields to your data frame, Grafana comes with a couple of dedicated fields: levels
and id
. Let’s have a closer look at each one.
To set the level for each log line, add a field.
Example:
By default, Grafana offers basic support for deduplicating log lines. You can improve the support by adding an id
field to explicitly assign identifiers to each log line.
Example:
const frame = new MutableDataFrame({
refId: query.refId,
fields: [
{ name: 'time', type: FieldType.time },
{ name: 'content', type: FieldType.string, labels: { filename: 'file.txt' } },
{ name: 'level', type: FieldType.string },
{ name: 'id', type: FieldType.string },
],
});