From 589242958d96d3ae81c0bbe4cca119ee6afaa1ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Kronm=C3=BCller?= Date: Fri, 5 Mar 2021 21:01:31 +0100 Subject: [PATCH] feat: configurable timeout --- README.md | 1 + index.d.ts | 3 ++- index.js | 3 ++- src/batcher.js | 2 +- src/requests.js | 5 +++-- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3121786..e7a0722 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ LokiTransport() takes a Javascript object as an input. These are the options tha | `labels` | custom labels, key-value pairs | { module: 'http' } | null | | `format` | winston format (https://github.com/winstonjs/winston#formats) | simple() | null | | `gracefulShutdown` | Enable/disable graceful shutdown (wait for any unsent batches) | false | true | +| `timeout` | timeout for requests to grafana loki in ms | 30000 | null | ### Example With default formatting: diff --git a/index.d.ts b/index.d.ts index 83ec888..539e29c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -11,7 +11,8 @@ declare interface LokiTransportOptions extends TransportStream.TransportStreamOp clearOnError?: boolean, replaceOnError?: boolean, replaceTimestamp?: boolean, - gracefulShutdown?: boolean + gracefulShutdown?: boolean, + timeout?: number, } declare class LokiTransport extends TransportStream { diff --git a/index.js b/index.js index a9083d7..301600d 100644 --- a/index.js +++ b/index.js @@ -28,7 +28,8 @@ class LokiTransport extends Transport { clearOnError: options.clearOnError, replaceOnError: options.replaceOnError, replaceTimestamp: options.replaceTimestamp, - gracefulShutdown: options.gracefulShutdown !== false + gracefulShutdown: options.gracefulShutdown !== false, + timeout: options.timeout }) this.useCustomFormat = options.format !== undefined diff --git a/src/batcher.js b/src/batcher.js index 6634547..2cf47e2 100644 --- a/src/batcher.js +++ b/src/batcher.js @@ -192,7 +192,7 @@ class Batcher { } // Send the data to Grafana Loki - req.post(this.url, this.contentType, this.options.headers, reqBody) + req.post(this.url, this.contentType, this.options.headers, reqBody, this.options.timeout) .then(() => { // No need to clear the batch if batching is disabled logEntry === undefined && this.clearBatch() diff --git a/src/requests.js b/src/requests.js index d6c6d43..5f57fdc 100644 --- a/src/requests.js +++ b/src/requests.js @@ -1,7 +1,7 @@ const http = require('http') const https = require('https') -const post = async (lokiUrl, contentType, headers = {}, data = '') => { +const post = async (lokiUrl, contentType, headers = {}, data = '', timeout) => { // Construct a buffer from the data string to have deterministic data size const dataBuffer = Buffer.from(data, 'utf8') @@ -21,7 +21,8 @@ const post = async (lokiUrl, contentType, headers = {}, data = '') => { port: lokiUrl.port !== '' ? lokiUrl.port : (lokiUrl.protocol === 'https:' ? 443 : 80), path: lokiUrl.pathname, method: 'POST', - headers: Object.assign(defaultHeaders, headers) + headers: Object.assign(defaultHeaders, headers), + timeout: timeout } // Construct the request