Telegraf - Provide timestamp as input to InfluxDB

I am using Telegraf to send a measurement ("Test1") to InfluxDB. Currently, I have two fields read from DataLayer nodes: "Value" & "Timestamp".

Configuration:

[[inputs.ctrlx_datalayer.subscription]]
measurement="Test1"

[inputs.ctrlx_datalayer.subscription.tags]
SensorNum="Sensor1"

[[inputs.ctrlx_datalayer.subscription.nodes]]
name ="Value"
address="plc/app/Application/sym/GVL/Sensor1/Value"

[[inputs.ctrlx_datalayer.subscription.nodes]]
name ="Timestamp"
address="plc/app/Application/sym/GVL/Sensor1/PLCTimeStamp"

Example of the DataLayer nodes:

"plc/app/Application/sym/GVL/Sensor1/Value" : 20
"plc/app/Application/sym/GVL/Sensor1/PLCTimeStamp" : 1720520212398

The PLC variable "PLCTimeStamp" is a timestamp in milliseconds. Instead of providing it as a field, I wanted to provide it as an InfluxDB timestamp. Is this possible using Telegraf? 

Thanks in advance 🙂 

Best reply by Sgilk

Hi star_board ,

Something like this should work for your use case, utilizing the Starlark processor nickH mentioned.

 

[[outputs.influxdb_v2]]
  urls = ["https://127.0.0.1/influxdb"]
  token = "eaN73XZ4kWNzTwqkzWrhJpcH2ZJsZjkzuRwOPl-fe0cNMrMrg-2yRoJQvMYpnMVzBxLa8GN6EcXx42JEnEU2w="
  organization = "boschrexroth"
  bucket = "boschrexroth"
  insecure_skip_verify = true

## Configure the input plugin with the username and password to ctrlX Core  
[[inputs.ctrlx_datalayer]]
   server = "localhost"

   ## Authentication credentials
   username = "boschrexroth"
   password = "boschrexroth"

   ## Use TLS but skip chain & host verification
   insecure_skip_verify = true

   [[inputs.ctrlx_datalayer.subscription]]
      measurement = "metrics"
      nodes=[
      		{name="cpu_usage_percent", address="framework/metrics/system/cpu-utilisation-percent"},
        	{name ="timestamp",address="system/info/time"}
      ]

      ## The switch "output_json_string" enables output of the measurement as json. 
      ## That way it can be used in in a subsequent processor plugin, e.g. "Starlark Processor Plugin".
	output_json_string = false
      
[[processors.starlark]]
	source = '''
load("logging.star","log")
def apply(metric):
	log.error("----------------------")
	log.error(metric.tags['node'])
	if "timestamp" in metric.fields:
		log.error("Timestamp old: " + str(metric.time))
		metric.time = metric.fields['timestamp']
		log.error("Timestamp new: " + str(metric.time))
	return metric
'''

 

In the starlark processor, you would look for your PLCTimeStamp field and set the metric.time to that value. Here I used the time coming from the ctrlX system.

View original
7 replies