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

feat: add support for LUMIGO_DEBUG_SPANDUMP to print to console.log/error #179

Merged
merged 1 commit into from
May 25, 2023
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Specifically supported are:

* `LUMIGO_TRACER_TOKEN=<token>`: Configure the Lumigo token to enable to upload of telemetry to Lumigo; without this environment variable, your Node.js process will not send telemetry to Lumigo.
* `LUMIGO_DEBUG=TRUE`: Enables debug logging
* `LUMIGO_DEBUG_SPANDUMP=<path>`: Log all spans collected to the `<path>` file; this is an option intended only for debugging purposes and should *not* be used in production.
* `LUMIGO_DEBUG_SPANDUMP=<path|console:log|console:error>`: Log all spans collected to the `<path>` file or, the value is `console:log` or `console:error`, to `console.log` or `console.error`; this is an option intended only for debugging purposes and should *not* be used in production.
This setting is independent from `LUMIGO_DEBUG`, that is, `LUMIGO_DEBUG` does not need to additionally be set for `LUMIGO_DEBUG_SPANDUMP` to work.
* `LUMIGO_SWITCH_OFF=TRUE`: This option disables the Lumigo OpenTelemetry Distro entirely; no instrumentation will be injected, no tracing data will be collected.
* `LUMIGO_SECRET_MASKING_REGEX='["regex1", "regex2"]'`: Prevents Lumigo from sending keys that match the supplied regular expressions in process environment data, HTTP headers, payloads and queries. All regular expressions are case-insensitive. The "magic" value `all` will redact everything. By default, Lumigo applies the following regular expressions: `[".*pass.*", ".*key.*", ".*secret.*", ".*credential.*", ".*passphrase.*"]`. More fine-grained settings can be applied via the following environment variables, which will override `LUMIGO_SECRET_MASKING_REGEX` for a specific type of data:
Expand Down
39 changes: 34 additions & 5 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"jest-chain": "^1.1.6",
"jest-expect-message": "^1.1.3",
"jest-json": "^2.0.0",
"jest-mock-console": "^2.0.0",
"jest-summarizing-reporter": "^1.1.4",
"log-timestamp": "^0.3.0",
"mock-fs": "^5.2.0",
Expand Down
59 changes: 59 additions & 0 deletions src/exporters/FileSpanExporter.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BasicTracerProvider, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { Span, SpanKind } from '@opentelemetry/api';
import mock from 'mock-fs';
import mockConsole from 'jest-mock-console';

import { FileSpanExporter } from './index';

Expand Down Expand Up @@ -51,6 +52,64 @@ describe('FileSpanExporter tests', () => {
);
});

test('should write one span to console.log', async () => {
const restoreConsole = mockConsole();
try {
const exporterUnderTest = new FileSpanExporter('console:log');

const provider = new BasicTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(exporterUnderTest));

const root: Span = provider.getTracer('default').startSpan('root');
root.setAttribute('foo', 'bar');
root.end();

await provider.shutdown();

expect(console.log).toHaveBeenCalledTimes(1);
const actualSpan = console.log.mock.calls[0][0];

expect(JSON.parse(actualSpan)).toEqual(
expect.objectContaining({
name: 'root',
attributes: { foo: 'bar' },
kind: SpanKind.INTERNAL,
})
);
} finally {
restoreConsole();
}
});

test('should write one span to console error', async () => {
const restoreConsole = mockConsole();
try {
const exporterUnderTest = new FileSpanExporter('console:error');

const provider = new BasicTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(exporterUnderTest));

const root: Span = provider.getTracer('default').startSpan('root');
root.setAttribute('foo', 'bar');
root.end();

await provider.shutdown();

expect(console.error).toHaveBeenCalledTimes(1);
const actualSpan = console.error.mock.calls[0][0];

expect(JSON.parse(actualSpan)).toEqual(
expect.objectContaining({
name: 'root',
attributes: { foo: 'bar' },
kind: SpanKind.INTERNAL,
})
);
} finally {
restoreConsole();
}
});

test('should write two spans to file', async () => {
const tmpFile = './test-spans.json';

Expand Down
34 changes: 24 additions & 10 deletions src/exporters/FileSpanExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import { logger } from '../logging';
* exporter in production.
*/

const PRINT_SPANS_TO_CONSOLE_LOG = 'console:log';
const PRINT_SPANS_TO_CONSOLE_ERROR = 'console:error';

/* eslint-disable no-console */
export class FileSpanExporter implements SpanExporter {
private readonly file: string;
Expand All @@ -39,8 +42,11 @@ export class FileSpanExporter implements SpanExporter {

constructor(file: string) {
this.file = file;
this._fd = openSync(file, 'w');
this._shutdownOnce = new BindOnceFuture(this._shutdown.bind(this), this);

if (![PRINT_SPANS_TO_CONSOLE_LOG, PRINT_SPANS_TO_CONSOLE_ERROR].includes(file)) {
this._fd = openSync(file, 'w');
this._shutdownOnce = new BindOnceFuture(this._shutdown.bind(this), this);
}
}

/**
Expand All @@ -59,7 +65,13 @@ export class FileSpanExporter implements SpanExporter {
spans.map((span) => JSON.stringify(this._exportInfo(span), undefined, 0)).join('\n') + '\n';

try {
appendFileSync(this._fd, spansJson);
if (this._fd) {
appendFileSync(this._fd, spansJson);
} else if (this.file === PRINT_SPANS_TO_CONSOLE_LOG) {
console.log(spansJson);
} else if (this.file === PRINT_SPANS_TO_CONSOLE_ERROR) {
console.error(spansJson);
}
} catch (err) {
return resultCallback({
code: ExportResultCode.FAILED,
Expand Down Expand Up @@ -103,7 +115,7 @@ export class FileSpanExporter implements SpanExporter {
* Shutdown the exporter.
*/
shutdown(): Promise<void> {
return this._shutdownOnce.call();
return this._shutdownOnce?.call();
}

/**
Expand Down Expand Up @@ -136,12 +148,14 @@ export class FileSpanExporter implements SpanExporter {

private _flushAll = async (): Promise<void> =>
new Promise((resolve, reject) => {
try {
fsyncSync(this._fd);
} catch (err) {
logger.error(`An error occured while flushing the spandump to file '${this.file}'`, err);
reject(err);
return;
if (this._fd) {
try {
fsyncSync(this._fd);
} catch (err) {
logger.error(`An error occured while flushing the spandump to file '${this.file}'`, err);
reject(err);
return;
}
}

resolve();
Expand Down