Skip to content

Commit

Permalink
lint:fix
Browse files Browse the repository at this point in the history
  • Loading branch information
trentm committed Oct 3, 2023
1 parent e8c7064 commit d392f10
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ import { VERSION } from './version';
// code that defines log level value and names. These values won't ever change
// in bunyan@1. This file is part of *instrumenting* Bunyan, so we want to
// avoid a dependency on the library.
var TRACE = 10;
var DEBUG = 20;
var INFO = 30;
var WARN = 40;
var ERROR = 50;
var FATAL = 60;
var levelFromName: { [name in BunyanLogger.LogLevelString]: number } = {
'trace': TRACE,
'debug': DEBUG,
'info': INFO,
'warn': WARN,
'error': ERROR,
'fatal': FATAL
const TRACE = 10;
const DEBUG = 20;
const INFO = 30;
const WARN = 40;
const ERROR = 50;
const FATAL = 60;
const levelFromName: { [name in BunyanLogger.LogLevelString]: number } = {
trace: TRACE,
debug: DEBUG,
info: INFO,
warn: WARN,
error: ERROR,
fatal: FATAL,
};
var nameFromLevel: { [level: number]: string } = {};
const nameFromLevel: { [level: number]: string } = {};
Object.keys(levelFromName).forEach(function (name) {
nameFromLevel[levelFromName[name as BunyanLogger.LogLevelString]] = name;
nameFromLevel[levelFromName[name as BunyanLogger.LogLevelString]] = name;
});

const OTEL_SEV_NUM_FROM_BUNYAN_LEVEL = {
Expand All @@ -48,8 +48,8 @@ const OTEL_SEV_NUM_FROM_BUNYAN_LEVEL = {
[INFO]: SeverityNumber.INFO,
[WARN]: SeverityNumber.WARN,
[ERROR]: SeverityNumber.ERROR,
[FATAL]: SeverityNumber.FATAL
}
[FATAL]: SeverityNumber.FATAL,
};

const DEFAULT_INSTRUMENTATION_SCOPE_NAME = 'io.opentelemetry.contrib.bunyan';
const DEFAULT_INSTRUMENTATION_SCOPE_VERSION = VERSION;
Expand All @@ -76,9 +76,9 @@ export class OpenTelemetryBunyanStream {
options?: LoggerOptions
) {
this._otelLogger = logs.getLogger(name, version, options);
}
}

write (rec: any) {
write(rec: any) {
// Convert from https://github.com/trentm/node-bunyan#log-record-fields
// to https://opentelemetry.io/docs/specs/otel/logs/data-model/
//
Expand All @@ -91,15 +91,15 @@ export class OpenTelemetryBunyanStream {
// pid -> process.pid
// However, AFAIK there isn't a way to influence the LoggerProvider's
// `resource` from here.
const { time, level, msg, v, ...fields } = rec
let timestamp = undefined
const { time, level, msg, v, ...fields } = rec;
let timestamp = undefined;
if (typeof time.getTime === 'function') {
// TODO: Spec and Logs API types say `timestamp` is nanoseconds, but the Logs SDK takes milliseconds
// https://github.com/open-telemetry/opentelemetry-js/blob/f2fc0d8787a82f33b1ce7e315759ddc61caf4f52/experimental/packages/sdk-logs/src/LogRecord.ts#L88
timestamp = time.getTime() // ms
timestamp = time.getTime(); // ms
} else {
// TODO: Add a test case for this: `log.info({time: 'foo'}, 'hi')`
fields.time = time // Expose non-Date "time" field on attributes.
fields.time = time; // Expose non-Date "time" field on attributes.
}
const otelRec = {
timestamp,
Expand All @@ -108,8 +108,8 @@ export class OpenTelemetryBunyanStream {
severityNumber: OTEL_SEV_NUM_FROM_BUNYAN_LEVEL[level],
severityText: nameFromLevel[level],
body: msg,
attributes: fields
}
this._otelLogger.emit(otelRec)
attributes: fields,
};
this._otelLogger.emit(otelRec);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@
*/

import { inherits } from 'util';
import {
context,
trace,
isSpanContextValid,
Span,
} from '@opentelemetry/api';
import { context, trace, isSpanContextValid, Span } from '@opentelemetry/api';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
Expand Down Expand Up @@ -69,21 +64,23 @@ export class BunyanInstrumentation extends InstrumentationBase<
);

function LoggerTraced(this: any, ...args: unknown[]) {
let inst
let retval = undefined
if (this instanceof LoggerTraced) { // called with `new Logger()`
let inst;
let retval = undefined;
if (this instanceof LoggerTraced) {
// called with `new Logger()`
inst = this;
Logger.call(this, ...args);
} else { // called without `new`
} else {
// called without `new`
inst = Logger.call(null, ...args);
retval = inst
retval = inst;
}
// If `_childOptions` is defined, this is a `Logger#child(...)`
// call. We must not add an OTel stream again.
if (args[1] /* _childOptions */ === undefined) {
instrumentation._addStream(inst);
}
return retval
return retval;
}
// Must use the deprecated `inherits` to support this style:
// const log = require('bunyan')({name: 'foo'});
Expand All @@ -100,7 +97,7 @@ export class BunyanInstrumentation extends InstrumentationBase<
);

return patchedExports;
},
}
),
];
}
Expand Down Expand Up @@ -158,13 +155,13 @@ export class BunyanInstrumentation extends InstrumentationBase<
}

private _addStream(logger: any) {
const config: BunyanInstrumentationConfig = this._config
const config: BunyanInstrumentationConfig = this._config;
if (!this.isEnabled() || !config.enableLogsBridge) {
return
return;
}
this._diag.debug('Adding OpenTelemetryBunyanStream to logger')
this._diag.debug('Adding OpenTelemetryBunyanStream to logger');
// XXX includeTraceContext pass in, and test
logger.addStream({ type: 'raw', stream: new OpenTelemetryBunyanStream() })
logger.addStream({ type: 'raw', stream: new OpenTelemetryBunyanStream() });
}

private _callHook(span: Span, record: Record<string, string>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { logs, SeverityNumber } from '@opentelemetry/api-logs';
import {
LoggerProvider,
SimpleLogRecordProcessor,
InMemoryLogRecordExporter
InMemoryLogRecordExporter,
} from '@opentelemetry/sdk-logs';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { isWrapped } from '@opentelemetry/instrumentation';
Expand All @@ -36,7 +36,6 @@ import { Writable } from 'stream';
import { BunyanInstrumentation } from '../src';
import { VERSION } from '../src/version';


import type * as BunyanLogger from 'bunyan';

// XXX
Expand All @@ -45,13 +44,15 @@ import type * as BunyanLogger from 'bunyan';

const tracerProvider = new NodeTracerProvider();
tracerProvider.register();
tracerProvider.addSpanProcessor(new SimpleSpanProcessor(new InMemorySpanExporter()));
tracerProvider.addSpanProcessor(
new SimpleSpanProcessor(new InMemorySpanExporter())
);
const tracer = tracerProvider.getTracer('default');

const resource = new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'test-instrumentation-bunyan',
});
const loggerProvider = new LoggerProvider({resource});
const loggerProvider = new LoggerProvider({ resource });
const memExporter = new InMemoryLogRecordExporter();
loggerProvider.addLogRecordProcessor(new SimpleLogRecordProcessor(memExporter));
logs.setGlobalLoggerProvider(loggerProvider);
Expand Down Expand Up @@ -176,9 +177,12 @@ describe('BunyanInstrumentation', () => {
assert.strictEqual(record['span_id'], undefined);
assert.strictEqual(record['trace_flags'], undefined);
assert.strictEqual(record['resource.service.name'], undefined);
assert.strictEqual(memExporter.getFinishedLogRecords().length, 1,
'Logs Bridge still works');
span.end()
assert.strictEqual(
memExporter.getFinishedLogRecords().length,
1,
'Logs Bridge still works'
);
span.end();
});
});

Expand All @@ -203,31 +207,37 @@ describe('BunyanInstrumentation', () => {
assert.strictEqual(logRecords[3].severityText, 'fatal');

// attributes, resource, instrumentationScope, etc.
log.info({foo: 'bar'}, 'a message');
let rec = logRecords[logRecords.length - 1];
log.info({ foo: 'bar' }, 'a message');
const rec = logRecords[logRecords.length - 1];
assert.strictEqual(rec.body, 'a message');
assert.deepStrictEqual(rec.attributes, {
name: 'test-logger-name',
hostname: os.hostname(),
pid: process.pid,
foo: 'bar',
});
assert.strictEqual(rec.resource.attributes['service.name'], 'test-instrumentation-bunyan');
assert.strictEqual(rec.instrumentationScope.name, 'io.opentelemetry.contrib.bunyan');
assert.strictEqual(
rec.resource.attributes['service.name'],
'test-instrumentation-bunyan'
);
assert.strictEqual(
rec.instrumentationScope.name,
'io.opentelemetry.contrib.bunyan'
);
assert.strictEqual(rec.instrumentationScope.version, VERSION);
assert.strictEqual(rec.spanContext, undefined);

// spanContext
tracer.startActiveSpan('abc', span => {
const { traceId, spanId, traceFlags } = span.spanContext();
log.info('in active span');
let rec = logRecords[logRecords.length - 1];
const rec = logRecords[logRecords.length - 1];
assert.strictEqual(rec?.spanContext?.traceId, traceId);
assert.strictEqual(rec?.spanContext?.spanId, spanId);
assert.strictEqual(rec?.spanContext?.traceFlags, traceFlags);
// XXX This rec should *NOT* have the `trace_id` et al attributes.
span.end();
})
});
});

it('does not emit to the Logs Bridge API if enableLogsBridge=false', () => {
Expand All @@ -251,7 +261,7 @@ describe('BunyanInstrumentation', () => {
assert.strictEqual('foo', record['msg']);
assert.strictEqual(record['trace_id'], traceId);
assert.strictEqual(record['span_id'], spanId);
span.end()
span.end();
});
});

Expand Down

0 comments on commit d392f10

Please sign in to comment.