Response handling from External NPM Module

In a Function Node, I added this NPM module in the setup tab:

It's working with the following function code, adapted from the module's readme:

// Send "UP" Heartbeat to Prometheus endpoint using Prometheus-Remote-Write NPM module
// https://www.npmjs.com/package/prometheus-remote-write
// Module imported as prometheusRemoteWrite in Setup Tab
// incoming msg.payload = 1

const { pushTimeseries, pushMetrics } = prometheusRemoteWrite;

const config = {
    url: "http://victoriametrics_vmagent:8429/api/v1/write",
    verbose: true,  // Enable verbose logging
    timing: true // Enable response timing data
};

async function Push() {
    try {
        node.status({ fill: "blue", shape: "dot", text: "connecting" });
        await pushTimeseries({
            labels: {
                __name__: "up",
                instance: "nodered.domain.com",
                job: "node-red"
            },
            samples: [{
                value: msg.payload,
                timestamp: Date.now()
            }]
        }, config);
  
        msg.result = "Data push successful";
        node.status({fill:"green", shape:"dot", text:"connected"});
        node.send(msg);
        
    } catch (error) {
        node.error("Error encountered during data push:", error);
        node.status({fill:"red", shape:"ring", text:"Error"});
    }
}

Push();

On the container's console, I get this return:

Failed to send write request, error 204 No Content  { timeseries: [ { labels: [Array], samples: [Array] } ] }
Serialized in 1 ms

The status code 204 is being returned although the data is written into the database without issue.

The relevant line in the module is here:

logger.warn("Failed to send write request, error", r.status + " " + r.statusText + " " + text, writeRequest);

I don't know why it's sending this response when the data is being written successfully?

Looking at the code, it almost seems as though it would only return 200 if you send it nothing to write. I was hoping for confirmation that the write indeed occurred.

In general, a 200 status code is used for most types of successful requests, while a 204 status code is used when the server has processed the request successfully, but there is no content to return to the client

Basically, 204 == "GOOD"

Thanks! that's my understanding as well, but the module is returning "Failed to send write request, error" for any non-200 response code.

it was a bug, the dev fixed it with this commit:

2 Likes

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