Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User agent header update #111

Merged
merged 6 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ logger.log(obj);
* **addTimestampWithNanoSecs** - Add a timestamp with nano seconds granularity. This is needed when many logs are sent in the same millisecond, so you can properly order the logs in kibana. The added timestamp field will be `@timestamp_nano` Default: `false`
* **compress** - If true the the logs are compressed in gzip format. Default: `false`
* **internalLogger** - set internal logger that supports the function log. Default: console.
* **setUserAgent** - Set `false` to send logs without user-agent field in request header. Default:`true`.
* **extraFields** - Adds your own custom fields to each log. Add in JSON Format, for example: `extraFields : { field_1: "val_1", field_2: "val_2" , ... }`.


Expand All @@ -62,6 +61,9 @@ A few notes are worth mentioning regarding the use of the UDP protocol:


## Update log
**2.1.8**
- Make `User-Agent` not optional and add the version to it.

**2.1.7**
- upgrade `axios` to `v1.6.4` (contributed by @gcagle3)

Expand Down
1 change: 0 additions & 1 deletion lib/logzio-nodejs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ interface ILoggerOptions {
compress?: boolean;
internalLogger?: { log(message: string, ...args: any[]): any } & Record<string, any>;
protocol?: string;
setUserAgent?: boolean;
port?: string;
timeout?: number;
sleepUntilNextRetry?: number;
Expand Down
6 changes: 2 additions & 4 deletions lib/logzio-nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const protocolToPortMap = {
https: 8071,
};

const USER_AGENT = 'Logzio-Logger NodeJS';
const prop = require('../package.json');

class LogzioLogger {
constructor({
Expand All @@ -55,7 +55,6 @@ class LogzioLogger {
timeout,
sleepUntilNextRetry = 2 * 1000,
callback = this._defaultCallback,
setUserAgent = true,
extraFields = {},
}) {
if (!token) {
Expand All @@ -74,7 +73,6 @@ class LogzioLogger {
this.compress = compress;
this.internalLogger = internalLogger;
this.sleepUntilNextRetry = sleepUntilNextRetry;
this.setUserAgent = setUserAgent;
this.timer = null;
this.closed = false;

Expand All @@ -87,7 +85,7 @@ class LogzioLogger {
Host: this.host,
Accept: '*/*',
'Content-Type': 'text/plain',
...(this.setUserAgent ? { 'user-agent': USER_AGENT } : {}),
'user-agent': `NodeJS/${prop.version} logs`,
...(this.compress ? { 'content-encoding': 'gzip' } : {}),

};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "logzio-nodejs",
"description": "A nodejs implementation for sending logs to Logz.IO cloud service Copy of logzio-nodejs",
"version": "2.1.7",
"version": "2.1.8",
"author": "Gilly Barr <[email protected]>",
"maintainers": [
{
Expand Down
8 changes: 4 additions & 4 deletions test/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const zlib = require('zlib');
const logzioLogger = require('../lib/logzio-nodejs.js');
const hrtimemock = require('hrtimemock');
const axiosInstance = require('../lib/axiosInstance.js');
const prop = require('../package.json');
axiosInstance.defaults.adapter = 'http';


Expand Down Expand Up @@ -61,19 +62,18 @@ describe('logger', () => {
logger.close();
});

it('sends log without user-agent header', (done) => {
it('sends log with user-agent header', (done) => {
const logger = createLogger({
bufferSize: 1,
callback: onDone,
setUserAgent:false
callback: onDone
});
sinon.spy(logger, '_tryToSend');

const logMsg = 'hello there from test';
logger.log(logMsg);

function onDone() {
assert.equal(axiosInstance.defaults.headers.common['user-agent'], undefined);
assert.equal(axiosInstance.defaults.headers.post['user-agent'], `NodeJS/${prop.version} logs`);
logger._tryToSend.restore();
logger.close();
done();
Expand Down
Loading