Sending data to Graphite using Node-RED

Greetings. The subject of how to send data into a Graphite database has come up in this forum, but to my knowledge, the nut has not been cracked. I'd like to do that now, but need a little help.

This post concluded that this nodejs package was simple enough that it should be possible to just use it from a function node. I went through the process of installing Graphite in a docker container and have it running now (v.1.1.10 is now running and collecting basic stats on my machine, which I can view in localhost:80).

I next did npm install graphite and that gave me added 1 package, and audited 2 packages in 1s. found 0 vulnerabilities.

With that done, I put the code from the NPM usage into the function node, and this is where I am in over my head.

Questions (silly, I know)...

  1. How should lines 1 and 2 be changed? As is, I am getting the error (in the debug pane):
    "ReferenceError: require is not defined (line 1, col 16)"

  2. If I send a msg.payload value of 4 into the function node, I presume that I would just swap out the value "23" in the above and put msg.payload. Same thing with sending tag names in line 7.

Thanks in advance.

There is no need to install. Simply enter graphite in the function node setup tab

Remove line 1 - the require is performed by node-red because you entered graphite in the function node setup tab.

Thank you! I felt silly asking, but I knew it would be easy.

As a quick n' dirty test, I am sending a random value between 1400 and 1475 every 10 seconds via an MQTT node like this:
image

The function node details:


(not yet doing anything with the tags...more on that tomorrow)

Viewed in Graphite:

Viewed in Grafana:

Thank you @Steve-Mcl Let's consider this nut cracked open!

In case anyone wants to send fields and tags into Graphite, here is a sample flow using the fields temperature and humidity and the tags kitchen and basement.

[{"id":"018b2e9311f9102f","type":"inject","z":"d1c87ae83bc41c60","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"field","payload":"76","payloadType":"num","x":230,"y":140,"wires":[["8360371aecdeedd7"]]},{"id":"26dc41e58f0ae76a","type":"inject","z":"d1c87ae83bc41c60","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tag","payload":"kitchen","payloadType":"str","x":220,"y":200,"wires":[["8360371aecdeedd7"]]},{"id":"8360371aecdeedd7","type":"join","z":"d1c87ae83bc41c60","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":430,"y":160,"wires":[["761b71bf3299516d","f5bd3f179bf7d970"]]},{"id":"f5bd3f179bf7d970","type":"function","z":"d1c87ae83bc41c60","name":"temperature","func":"var client = graphite.createClient('http://localhost');\n\nvar metrics = { temperature: msg.payload.field };\nclient.write(metrics, function (err) {\n    // if err is null, your data was sent to graphite!\n});\nvar tags = { 'room': msg.payload.tag };\nclient.writeTagged(metrics, tags, function (err) {\n    // if err is null, your data was sent to graphite!\n});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"graphite","module":"graphite"}],"x":670,"y":160,"wires":[["b418ad50d7acd19e"]]},{"id":"b418ad50d7acd19e","type":"debug","z":"d1c87ae83bc41c60","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":840,"y":160,"wires":[]},{"id":"761b71bf3299516d","type":"debug","z":"d1c87ae83bc41c60","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":660,"y":200,"wires":[]},{"id":"c907ffb8790843d8","type":"inject","z":"d1c87ae83bc41c60","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"field","payload":"67","payloadType":"num","x":230,"y":280,"wires":[["48c62bb750bb8215"]]},{"id":"16d38e28389bda55","type":"inject","z":"d1c87ae83bc41c60","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tag","payload":"basement","payloadType":"str","x":215,"y":336,"wires":[["48c62bb750bb8215"]]},{"id":"48c62bb750bb8215","type":"join","z":"d1c87ae83bc41c60","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":430,"y":300,"wires":[["e8e228b0db792efb","59dd5a55f5e267ca"]]},{"id":"59dd5a55f5e267ca","type":"function","z":"d1c87ae83bc41c60","name":"temperature","func":"var client = graphite.createClient('http://localhost');\n\nvar metrics = { temperature: msg.payload.field };\nclient.write(metrics, function (err) {\n    // if err is null, your data was sent to graphite!\n});\nvar tags = { 'room': msg.payload.tag };\nclient.writeTagged(metrics, tags, function (err) {\n    // if err is null, your data was sent to graphite!\n});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"graphite","module":"graphite"}],"x":680,"y":300,"wires":[["e9405e9ad624500f"]]},{"id":"e9405e9ad624500f","type":"debug","z":"d1c87ae83bc41c60","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":300,"wires":[]},{"id":"e8e228b0db792efb","type":"debug","z":"d1c87ae83bc41c60","name":"debug 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":685,"y":336,"wires":[]},{"id":"2fc8e4f361ac3191","type":"inject","z":"d1c87ae83bc41c60","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"field","payload":"41","payloadType":"num","x":210,"y":500,"wires":[["eb2eb3f2df63881c"]]},{"id":"df6a6a624fc6b877","type":"inject","z":"d1c87ae83bc41c60","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tag","payload":"kitchen","payloadType":"str","x":200,"y":560,"wires":[["eb2eb3f2df63881c"]]},{"id":"eb2eb3f2df63881c","type":"join","z":"d1c87ae83bc41c60","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":410,"y":520,"wires":[["f38a3ab4453ec72a","c7502d45273d040e"]]},{"id":"c7502d45273d040e","type":"function","z":"d1c87ae83bc41c60","name":"humidity","func":"var client = graphite.createClient('http://localhost');\n\nvar metrics = { humidity: msg.payload.field };\nclient.write(metrics, function (err) {\n    // if err is null, your data was sent to graphite!\n});\nvar tags = { 'room': msg.payload.tag };\nclient.writeTagged(metrics, tags, function (err) {\n    // if err is null, your data was sent to graphite!\n});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"graphite","module":"graphite"}],"x":640,"y":520,"wires":[["2b979d8421835efc"]]},{"id":"2b979d8421835efc","type":"debug","z":"d1c87ae83bc41c60","name":"debug 7","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":820,"y":520,"wires":[]},{"id":"f38a3ab4453ec72a","type":"debug","z":"d1c87ae83bc41c60","name":"debug 8","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":560,"wires":[]},{"id":"85cd83cb406bcc2e","type":"inject","z":"d1c87ae83bc41c60","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"field","payload":"54","payloadType":"num","x":210,"y":640,"wires":[["f32ad0b6ace1b8a1"]]},{"id":"049bd3fc819e32dd","type":"inject","z":"d1c87ae83bc41c60","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tag","payload":"basement","payloadType":"str","x":195,"y":696,"wires":[["f32ad0b6ace1b8a1"]]},{"id":"f32ad0b6ace1b8a1","type":"join","z":"d1c87ae83bc41c60","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":410,"y":660,"wires":[["813823ad5cc66aba","80a59bdbe37854b0"]]},{"id":"80a59bdbe37854b0","type":"function","z":"d1c87ae83bc41c60","name":"humidity","func":"var client = graphite.createClient('http://localhost');\n\nvar metrics = { humidity: msg.payload.field };\nclient.write(metrics, function (err) {\n    // if err is null, your data was sent to graphite!\n});\nvar tags = { 'room': msg.payload.tag };\nclient.writeTagged(metrics, tags, function (err) {\n    // if err is null, your data was sent to graphite!\n});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"graphite","module":"graphite"}],"x":650,"y":660,"wires":[["0b985e785fb07972"]]},{"id":"0b985e785fb07972","type":"debug","z":"d1c87ae83bc41c60","name":"debug 9","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":660,"wires":[]},{"id":"813823ad5cc66aba","type":"debug","z":"d1c87ae83bc41c60","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":665,"y":696,"wires":[]}]

In Grafana's query editor, the nomenclature is as follows:

fieldname;tagname=tagvalue
example: humidity;room=basement

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.