Getting Started
7 Lessons
Section
















The FixedIT Data Agent can use Telegraf input plugins to collect data from varying data sources. One such plugin is the 'inputs.http' plugin that can be used as a web client to fetch API data from an HTTP(S) API endpoint. This plugin is commonly used to fetch analytics data or status information from other applications or web services.
Oftentimes, the user implementing the client does not have control over the API's response format. This means that the Data Agent must be flexible enough to consume any type of metric data from any type of API. The HTTP plugin handles the transport protocol, but the input parser is what handles the unpacking and interpretation of the data in the response.
JSON is a common data format that is often used for web-based REST APIs, but also for serializing data to files and much more. Parsing JSON data is therefore a common task when creating a Telegraf configuration file for the FixedIT Data Agent ACAP application.
In this example, we are going to look at how we can parse essentially any type of JSON data in a Telegraf input and create a standardized metric from it. To keep things simple and easy to test, we're not going to use the HTTP input, instead, we're going to use a simple file input. The JSON parsing is, however, defined in the same way regardless if the data source is a local text file, an HTTP(S) API or a source handled by any other Telegraf input plugin.
Let's start with a simple example. Create a new text file called test_data.jsonl with the following line in it:
{"id":123,"text":"Hello, world!"}This is a simple JSON object with two keys, id and text.
NOTE: If you are using Linux, make sure that there is a new line character at the end of the file. In Windows, this is typically automatically added by text editors. The tail plugin will not read the line unless it ends with a new line character since that could mean that it's not fully written yet.
We can now create a simple Telegraf configuration file that will read this JSON file, parse the content as JSON data and emit a Telegraf metric from it:
# Create an input plugin that reads the JSON file and
# parses the data into metrics.
[[inputs.tail]]
# Override metric name
name_override = "sample_data"
# Specify the path to the JSON file
files = ["${HELPER_FILES_DIR:-.}/test_data.jsonl"]
# Poll the file for changes and read all existing
# data from the file when we start polling.
watch_method = "poll"
initial_read_offset = "beginning"
# Parse incoming data as JSON
data_format = "json_v2"
[[inputs.tail.json_v2]]
[[inputs.tail.json_v2.object]]
path = "@this"
# Create an output plugin that writes the metrics
# so the standard output.
[[outputs.file]]
files = ["stdout"]
data_format = "json"
# Flush data directly when it is available.
metric_batch_size = 1Let's break down the configuration file and analyze each part:
[[inputs.tail]] creates a new input plugin that will monitor a file and read any newly added content to it. We use this instead of the inputs.file plugin since we would otherwise read the whole content of the file on every interval. Tail will instead remember what it has already read.
name_override which change the name of the metric. Without this, the default name would be used which is the name of the plugin (tail).
files specifies which files (or a single file in this case) that should be monitored by the plugin. We point this to the JSON file we just created. The HELPER_FILES_DIR variable is set in the FixedIT Data Agent to the directory where helper files are uploaded. If you run the file locally on your computer where this variable is not set, the default value will instead be to look in the current directory.
watch_method tells the plugin how to monitor the file. On Windows, only poll is valid, but on Linux and in the FixedIT Data Agent, also inotify can be used for better efficiency.
initial_read_offset tells the plugin to start reading the file from the start. We do this since we want to read the JSON line that's already there.
data_format tells the parser in the plugin that we expect JSON formatted text in the file. The [[inputs.tail.json_v2]] starts a subconfiguration for the json_v2 parser where we can configure it. [[inputs.tail.json_v2.object]] tells the parser how to parse the JSON object and path = "@this" tells it to start parsing the object from the object root. The notation used here is called GJSON and you can read more about it here. Note that no top-level fields for the plugin can be used after specifying subconfigurations, they have to be at the end of the plugin's configuration.
[[outputs.file]] starts a new plugin that will consume all metrics and write them to the stdout file which is a special keyword. If you run this in the FixedIT Data Agent, content written to stdout will be visible in the Logs tab of the web UI and if you run it locally on your computer, it will be visible in the terminal.
metric_batch_size = 1 tells the output plugin to write the output directly when it is accessible instead of buffering it internally.
If we run this file, you will see the following output in the logs:
{"fields":{"id":"123","text":"Hello, world!"},"name":"sample_data","tags":{"host":"Daniels-MacBook-Pro-2.local","path":"test_data1.json"},"timestamp":1775211797}We can pretty print this for easier reading:
{
"fields": {
"id": 123,
"text": "Hello, world!"
},
"name": "sample_data",
"tags": {
"host": "Daniels-MacBook-Pro-2.local",
"path": "test_data.json"
},
"timestamp": 1775211870
}We can see that the JSON object has been parsed and converted to the internal metric format in Telegraf. Telegraf metrics always have a fields section, one tags section, a name and a timestamp.
By default, the JSON parser converted the fields in the JSON objects to measurements that are included in the fields section. We can see that it also added two tags, one with the host name of the computer that it was running on and one with the path to the file that the object came from. The metric name comes from the name_override in the configuration of the plugin and the timestamp was set automatically to the time when the metric was read.
Consider the following JSON which is a nested structure, meaning that there's a dict (called message) inside the root dict.
{
"id": 123,
"source": "DetectorApp",
"message": {
"log_type": "detection",
"object_type": "Human",
"norm_center_x": 0.5833333134651184,
"norm_center_y": 0.41111111640930176,
"id": 456
}
}With the default parsing (as specified in the configuration file above) we'll get a flattened metric when running the file:
{
"fields": {
"id": 123,
"message_id": 456,
"message_log_type": "detection",
"message_norm_center_x": 0.5833333134651184,
"message_norm_center_y": 0.41111111640930176,
"message_object_type": "Human",
"source": "DetectorApp"
},
"name": "sample_data",
"tags": {
"host": "Daniels-MacBook-Pro-2.local",
"path": "test_data.json"
},
"timestamp": 1775213213
}This is because Telegraf metrics cannot have nested data with multiple levels; A Telegraf metric is always flat. We can see that the two top-level fields id and source) were kept as they are. All fields in the message subdict have simply been prefixed with message_ and added to the top level.
If we want to ignore the sub dict, we can do so by using the excluded_keys field in the configuration (see the docs for the json_v2 parser). The parser configuration would now look like the following:
# Parse incoming data as JSON
data_format = "json_v2"
[[inputs.tail.json_v2]]
[[inputs.tail.json_v2.object]]
path = "@this"
excluded_keys = ["message"]The metric will now simply ignore all fields in the message dict:
{
"fields": {
"id": 123,
"source": "DetectorApp"
},
"name": "sample_data",
"tags": {
"host": "Daniels-MacBook-Pro-2.local",
"path": "test_data.json"
},
"timestamp": 1775213459
}We can instead use the disable_prepend_keys field to keep all fields in the inner dict without adding the automatic prefix to them:
# Parse incoming data as JSON
data_format = "json_v2"
[[inputs.tail.json_v2]]
[[inputs.tail.json_v2.object]]
path = "@this"
disable_prepend_keys = trueThe metric now looks like this:
{
"fields": {
"id": 123,
"log_type": "detection",
"norm_center_x": 0.5833333134651184,
"norm_center_y": 0.41111111640930176,
"object_type": "Human",
"source": "DetectorApp"
},
"name": "sample_data",
"tags": {
"host": "Daniels-MacBook-Pro-2.local",
"path": "test_data.json"
},
"timestamp": 1775213671
}If you look carefully, you can see that we are now missing one field. The original JSON object had one id with value 123 in the outer dict and one id with value 456 in the inner dict. Since we are now flattening the JSON object and not appending any prefixes, we will get a naming conflict. The result is that only the outer id field was preserved.
We can handle this by renaming one or both id fields:
renames = { "id" = "source_id", "message_id" = "detector_id" }It's worth noting that all configurations fields such as renames operates on the names before the disabling of prepend keys. I.e. the original name message_id is specified here.
The parser configuration now looks like this:
# Parse incoming data as JSON
data_format = "json_v2"
[[inputs.tail.json_v2]]
[[inputs.tail.json_v2.object]]
path = "@this"
disable_prepend_keys = true
renames = { "id" = "source_id", "message_id" = "detector_id" }This results in the following metric where the outer id is instead called source_id and the inner is called detector_id:
{
"fields": {
"detector_id": 456,
"log_type": "detection",
"norm_center_x": 0.5833333134651184,
"norm_center_y": 0.41111111640930176,
"object_type": "Human",
"source": "DetectorApp",
"source_id": 123
},
"name": "sample_data",
"tags": {
"host": "Daniels-MacBook-Pro-2.local",
"path": "test_data.json"
},
"timestamp": 1775213861
}When using Telegraf and the FixedIT Data Agent together with InfluxDB, it becomes very important to understand tags vs fields. For other Telegraf use cases, it might not be as important. You do, however, need to understand that each metric needs at least one field since tags are only used to describe fields.
When parsing a JSON object, we can tell the parser which JSON fields should become fields in Telegraf and which should become tags. You can e.g. try to change the parser configuration to this:
# Parse incoming data as JSON
data_format = "json_v2"
[[inputs.tail.json_v2]]
[[inputs.tail.json_v2.object]]
path = "@this"
disable_prepend_keys = true
renames = { "id" = "source_id", "message_id" = "detector_id" }
tags = ["level", "id", "message_id", "source"]Like before, it's worth noting that all configurations fields such as tags operates on the names before the renaming and disabling of prepend keys. I.e. the original names id and message_id are specified, but they will be added to the metric as the renamed source_id and detection_id.
This will result in the following metric where we can see that the four specified fields are not included in the tags section instead:
{
"fields": {
"log_type": "detection",
"norm_center_x": 0.5833333134651184,
"norm_center_y": 0.41111111640930176,
"object_type": "Human"
},
"name": "sample_data",
"tags": {
"detector_id": 456,
"host": "Daniels-MacBook-Pro-2.local",
"path": "test_data.json",
"source": "DetectorApp",
"source_id": 123
},
"timestamp": 1775214277
}When using Telegraf to send data to InfluxDB, each field has it's own data type and different data types can't be mixed in the same bucket. Therefore, it's important to know if a value is a floating-point value or an integer.
When using the json_v2 parser, it will try to guess the type, but you can also instruct it which data types to use to avoid confusion.
# Parse incoming data as JSON
data_format = "json_v2"
[[inputs.tail.json_v2]]
[[inputs.tail.json_v2.object]]
path = "@this"
disable_prepend_keys = true
renames = { "id" = "source_id", "message_id" = "detector_id" }
tags = ["level", "id", "message_id", "source"]
# Define field types explicitly
[inputs.tail.json_v2.object.fields]
message_norm_center_x = "float"
message_norm_center_y = "float"This will force the Telegraf metric to store the two values as floating point values instead of integers. This is particularly important if a value is expected to be a float, but where the first value might happen to be a whole integer without any decimal sign.
You can also change the type to int which will truncate any decimals and store it as a whole number. If you are unsure what type the variable is stored as, you can change the data format in the output by setting data_format = "influx". In this format, an integer value is suffixed with an i:
sample_data,detector_id=456,host=Daniels-MacBook-Pro-2.local,path=test_data.json,source=DetectorApp,source_id=123 log_type="detection",object_type="Human",norm_center_x=0i,norm_center_y=0i 1775215391614016000Especially note the part that say norm_center_x=0i. This tells that it's parsed as an integer.