Skip to content

Commit

Permalink
feature: specify headers in options
Browse files Browse the repository at this point in the history
  • Loading branch information
huksley committed Sep 16, 2023
1 parent 2dc2d9a commit 63a391d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ const config = {
// Override used node-fetch
fetch: undefined,
// Additional labels to apply to each timeseries, i.e. [{ service: "SQS" }]
labels: undefined
labels: undefined,
// Additional HTTP headers to send with each request
headers: undefined
};
```

Expand Down
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const SnappyJS = require("snappyjs");
const fetch = require("node-fetch");
const protobuf = require("protobufjs");
const btoa = (s) => Buffer.from(s, "binary").toString("base64");
const prom = require("./prom");
Expand Down Expand Up @@ -92,7 +91,7 @@ async function pushTimeseries(timeseries, options) {

if (options?.url) {
/** @type import("./types").MinimalFetch */
let fetch = options.fetch || require("node-fetch").default
let fetch = options.fetch || require("node-fetch").default;
return fetch(options?.url, {
method: "POST",
headers: {
Expand All @@ -102,6 +101,7 @@ async function pushTimeseries(timeseries, options) {
Authorization: "Basic " + btoa(options?.auth.username + ":" + options?.auth?.password),
}
: undefined),
...(options.headers || {}),
},
body: SnappyJS.compress(buffer),
timeout: options.timeout,
Expand Down Expand Up @@ -137,6 +137,13 @@ async function pushTimeseries(timeseries, options) {
}
}

/**
* Sends metrics over HTTP(s)
*
* @param {Record<string,number>} metrics
* @param {import("./types").Options=} options
* @return {Promise<import("./types").Result>}
*/
async function pushMetrics(metrics, options) {
return pushTimeseries(
Object.entries(metrics).map((c) => ({
Expand Down
20 changes: 17 additions & 3 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const push = require("./index");

push.pushMetrics({ users_total: 22}, { timeout: 1})
push.pushMetrics({ users_total: 22 }, { timeout: 1 });

const config = {
url: process.env.GRAFANA_PUSH_URL,
Expand Down Expand Up @@ -43,7 +43,7 @@ for (let i = 0; i < 100; i++) {
labels: {
__name__: "test_exemplar_metric_total",
instance: "localhost:8090",
job: "prometheus"
job: "prometheus",
},
samples: [
{
Expand All @@ -59,4 +59,18 @@ for (let i = 0; i < 100; i++) {
});
}

push.pushMetrics({ users_total: 11 })
push.pushMetrics({ users_total: 11 });

setTimeout(
() =>
push.pushMetrics(
{ max_users_total: 31 },
{
headers: {
"X-extra-key": "test2",
},
verbose: true,
}
),
1000
);
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prometheus-remote-write",
"version": "0.3.1",
"version": "0.4.0",
"description": "Send samples to prometheus via remote_write from NodeJS",
"main": "index.js",
"types": "types.d.ts",
Expand Down
1 change: 1 addition & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ interface Options {
timeout?: number;
console?: Console;
fetch?: MinimalFetch;
headers?: { [key: string]: string };
}

interface Result {
Expand Down

0 comments on commit 63a391d

Please sign in to comment.