JaniAnttonen / winston-loki

Grafana Loki transport for the nodejs logging library Winston.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enable `replaceTimestamp` by default

clouedoc opened this issue · comments

Changes in the upstream Loki project require us to activate the replaceTimestamp option, or otherwise, logs uploading will silently fail.
See #90 and #75

I suggest enabling this option by default and adding a warning in the option's description as to why users should not turn it off.

I can contribute a PR for this.
Thank you for your attention.

Checklist
  • Modify index.jsc0d23d2 Edit
  • Running GitHub Actions for index.jsEdit
  • Modify README.mde279641 Edit
  • Running GitHub Actions for README.mdEdit

🚀 Here's the PR! #145

See Sweep's progress at the progress dashboard!
💎 Sweep Pro: I'm using GPT-4. You have unlimited GPT-4 tickets. (tracking ID: 406a8ac041)
Install Sweep Configs: Pull Request

Tip

I can email you next time I complete a pull request if you set up your email here!


Actions (click)

  • ↻ Restart Sweep

Step 1: 🔎 Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I think are relevant in decreasing order of relevance (click to expand). If some file is missing from here, you can mention the path in the ticket description.

winston-loki/README.md

Lines 16 to 30 in 88399c8

### Options
LokiTransport() takes a Javascript object as an input. These are the options that are available, __required in bold__:
| **Parameter** | **Description** | **Example** | **Default** |
| ------------------ | --------------------------------------------------------- | -----------------------| ------------- |
| __`host`__ | URL for Grafana Loki | http://127.0.0.1:3100 | null |
| `interval` | The interval at which batched logs are sent in seconds | 30 | 5 |
| `json` | Use JSON instead of Protobuf for transport | true | false |
| `batching` | If batching is not used, the logs are sent as they come | true | true |
| `clearOnError` | Discard any logs that result in an error during transport | true | false |
| `replaceTimestamp` | Replace any log timestamps with Date.now() | true | false |
| `labels` | custom labels, key-value pairs | { module: 'http' } | undefined |
| `format` | winston format (https://github.com/winstonjs/winston#formats) | simple() | undefined |
| `gracefulShutdown` | Enable/disable graceful shutdown (wait for any unsent batches) | false | true |

winston-loki/index.js

Lines 10 to 36 in 88399c8

*/
class LokiTransport extends Transport {
/**
* Creates an instance of LokiTransport.
* @param {*} options
* @memberof LokiTransport
*/
constructor (options) {
super(options)
// Pass all the given options to batcher
this.batcher = new Batcher({
host: options.host,
basicAuth: options.basicAuth,
headers: options.headers || {},
interval: options.interval,
json: options.json,
batching: options.batching !== false,
clearOnError: options.clearOnError,
onConnectionError: options.onConnectionError,
replaceTimestamp: options.replaceTimestamp,
gracefulShutdown: options.gracefulShutdown !== false,
timeout: options.timeout
})
this.useCustomFormat = options.format !== undefined
this.labels = options.labels

/**
* Creates an instance of Batcher.
* Starts the batching loop if enabled.
* @param {*} options
* @memberof Batcher
*/
constructor (options) {
// Load given options to the object
this.options = options
// Construct Grafana Loki push API url
const URL = this.loadUrl()
this.url = new URL(this.options.host + '/loki/api/v1/push')
// Parse basic auth parameters if given
if (options.basicAuth) {
const btoa = require('btoa')
const basicAuth = 'Basic ' + btoa(options.basicAuth)
this.options.headers = Object.assign(this.options.headers, { Authorization: basicAuth })
}
// Define the batching intervals
this.interval = this.options.interval
? Number(this.options.interval) * 1000
: 5000
this.circuitBreakerInterval = 60000
// Initialize the log batch
this.batch = {
streams: []
}
// If snappy binaries have not been built, fallback to JSON transport
if (!this.options.json) {
try {
snappy = this.loadSnappy()
} catch (error) {
this.options.json = true
}
if (!snappy) {
this.options.json = true
}
}
// Define the content type headers for the POST request based on the data type
this.contentType = 'application/x-protobuf'
if (this.options.json) {
this.contentType = 'application/json'
}
this.batchesSending = 0
this.onBatchesFlushed = () => {}
// If batching is enabled, run the loop
this.options.batching && this.run()
if (this.options.gracefulShutdown) {
exitHook(callback => {
this.close(() => callback())
})
}
}
/**
* Marks the start of batch submitting.
*
* Must be called right before batcher starts sending logs.
*/
batchSending () {
this.batchesSending++
}
/**
* Marks the end of batch submitting
*
* Must be called after the response from Grafana Loki push endpoint
* is received and completely processed, right before
* resolving/rejecting the promise.
*/
batchSent () {
if (--this.batchesSending) return
this.onBatchesFlushed()
}
/**
* Returns a promise that resolves after all the logs sent before
* via log(), info(), etc calls are sent to Grafana Loki push endpoint
* and the responses for all of them are received and processed.
*
* @returns {Promise}
*/
waitFlushed () {
return new Promise((resolve, reject) => {
if (!this.batchesSending && !this.batch.streams.length) { return resolve() }
this.onBatchesFlushed = () => {
this.onBatchesFlushed = () => {}
return resolve()
}
})
}
/**
* Returns a promise that resolves after the given duration.
*
* @param {*} duration
* @returns {Promise}
*/
wait (duration) {
return new Promise(resolve => {
setTimeout(resolve, duration)
})
}
/**
* Pushes logs into the batch.
* If logEntry is given, pushes it straight to this.sendBatchToLoki()
*
* @param {*} logEntry
*/
async pushLogEntry (logEntry) {
const noTimestamp =
logEntry && logEntry.entries && logEntry.entries[0].ts === undefined
// If user has decided to replace the given timestamps with a generated one, generate it


Step 2: ⌨️ Coding

Modify index.js with contents:
• In the `LokiTransport` class constructor, update the handling of the `replaceTimestamp` option to default to true if not provided. This can be achieved by modifying the line initializing the `replaceTimestamp` option to `replaceTimestamp: options.replaceTimestamp !== false`. This change ensures that unless explicitly set to false, `replaceTimestamp` will be true by default.
--- 
+++ 
@@ -27,7 +27,7 @@
       batching: options.batching !== false,
       clearOnError: options.clearOnError,
       onConnectionError: options.onConnectionError,
-      replaceTimestamp: options.replaceTimestamp,
+      replaceTimestamp: options.replaceTimestamp !== false,
       gracefulShutdown: options.gracefulShutdown !== false,
       timeout: options.timeout
     })
  • Running GitHub Actions for index.jsEdit
Check index.js with contents:

Ran GitHub Actions for c0d23d2b349b3598ea670458a70e3e385a214da8:

Modify README.md with contents:
• Update the description of the `replaceTimestamp` option in the options table to indicate that it is enabled by default. Change the default value from `false` to `true`.
• Add a warning note under the `replaceTimestamp` option description: "Warning: Disabling `replaceTimestamp` may result in logs failing to upload due to recent changes in the upstream Loki project. It is recommended to leave this option enabled unless you have a specific reason to disable it."
--- 
+++ 
@@ -24,7 +24,7 @@
 | `json`             | Use JSON instead of Protobuf for transport                | true                   | false         |
 | `batching`         | If batching is not used, the logs are sent as they come   | true                   | true          |
 | `clearOnError`     | Discard any logs that result in an error during transport | true                   | false         |
-| `replaceTimestamp` | Replace any log timestamps with Date.now()                | true                   | false         |
+| `replaceTimestamp` | Replace any log timestamps with Date.now(). Warning: Disabling `replaceTimestamp` may result in logs failing to upload due to recent changes in the upstream Loki project. It is recommended to leave this option enabled unless you have a specific reason to disable it. | true                   | true          |
 | `labels`           | custom labels, key-value pairs                            | { module: 'http' }     | undefined     |
 | `format`           | winston format (https://github.com/winstonjs/winston#formats) | simple()           | undefined     |
 | `gracefulShutdown` | Enable/disable graceful shutdown (wait for any unsent batches) | false             | true          |
  • Running GitHub Actions for README.mdEdit
Check README.md with contents:

Ran GitHub Actions for e2796413cbd3fa63c98b0b866a2de20fa950815b:


Step 3: 🔁 Code Review

I have finished reviewing the code for completeness. I did not find errors for sweep/enable_replacetimestamp_by_default.


🎉 Latest improvements to Sweep:
  • New dashboard launched for real-time tracking of Sweep issues, covering all stages from search to coding.
  • Integration of OpenAI's latest Assistant API for more efficient and reliable code planning and editing, improving speed by 3x.
  • Use the GitHub issues extension for creating Sweep issues directly from your editor.

💡 To recreate the pull request edit the issue title or description.
Something wrong? Let us know.

This is an automated message generated by Sweep AI.