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

[Feature] otel context injection #133

Merged
merged 8 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ logger.log('This is a log message');
```

## Update log
**2.2.0**
- Add `traceId` and `spanId` fields to logs when opentelemetry context is available.
**2.1.8**
- Make `User-Agent` not optional and add the version to it.

Expand Down
62 changes: 38 additions & 24 deletions lib/logzio-nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const assign = require('lodash.assign');
const dgram = require('dgram');
const zlib = require('zlib');
const axiosInstance = require('./axiosInstance');

const { trace, context } = require('@opentelemetry/api');

const nanoSecDigits = 9;

Expand Down Expand Up @@ -231,31 +231,45 @@ class LogzioLogger {
}
}
}

/**
* Attach OpenTelemetry context to the log record.
* @param msg - The message (Object) to append the OpenTelemetry context to.
* @private
*/
_addOpentelemetryContext(msg) {
let span = trace.getSpan(context.active());
if (span) {
msg.traceId = span.spanContext().traceId;
msg.spanId = span.spanContext().spanId;
}
}

log(msg, obj) {
if (this.closed === true) {
throw new Error('Logging into a logger that has been closed!');
}
if (![null, undefined].includes(obj)) {
msg += JSON.stringify(obj);
}
if (typeof msg === 'string') {
msg = {
message: msg,
};
}
this._addSourceIP(msg);
msg = assign(msg, this.extraFields);
if (!msg.type) {
msg.type = this.type;
}
this._addTimestamp(msg);

this.messages.push(msg);
if (this.messages.length >= this.bufferSize) {
this._debug('Buffer is full - sending bulk');
this._popMsgsAndSend();
}
if (this.closed === true) {
throw new Error('Logging into a logger that has been closed!');
}
if (![null, undefined].includes(obj)) {
msg += JSON.stringify(obj);
}
if (typeof msg === 'string') {
msg = { message: msg };
}

this._addSourceIP(msg);
msg = assign(msg, this.extraFields);
if (!msg.type) {
msg.type = this.type;
}
this._addTimestamp(msg);
this._addOpentelemetryContext(msg);


this.messages.push(msg);
if (this.messages.length >= this.bufferSize) {
this._debug('Buffer is full - sending bulk');
this._popMsgsAndSend();
}
}

_popMsgsAndSend() {
Expand Down
226 changes: 224 additions & 2 deletions package-lock.json

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

5 changes: 4 additions & 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.8",
"version": "2.2.0",
"author": "Gilly Barr <[email protected]>",
"maintainers": [
{
Expand Down Expand Up @@ -47,6 +47,9 @@
"logzio"
],
"dependencies": {
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/context-async-hooks": "^1.30.0",
"@opentelemetry/sdk-trace-node": "^1.30.0",
"axios": "^1.6.4",
"json-stringify-safe": "5.0.1",
"lodash.assign": "4.2.0",
Expand Down
Loading
Loading