Skip to content

Commit

Permalink
Merge pull request #55 from johakr/development
Browse files Browse the repository at this point in the history
feat: configurable timeout
  • Loading branch information
JaniAnttonen authored Mar 8, 2021
2 parents 73c5e38 + 5892429 commit 91fe992
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/batcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions src/requests.js
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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
Expand Down

0 comments on commit 91fe992

Please sign in to comment.