Skip to content

Commit

Permalink
disable automatic log sending for bunyan
Browse files Browse the repository at this point in the history
  • Loading branch information
seemk committed Dec 12, 2023
1 parent 5265f83 commit 5cc2d2a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
11 changes: 11 additions & 0 deletions src/instrumentations/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import { Span } from '@opentelemetry/sdk-trace-base';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import type { BunyanInstrumentation } from '@opentelemetry/instrumentation-bunyan';
import { getEnvBoolean } from '../utils';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type LogRecord = Record<string, any>;
Expand Down Expand Up @@ -59,3 +61,12 @@ export function configureLogInjection(
return instrumentation.setConfig(config);
}
}

export function disableLogSending(instrumentation: BunyanInstrumentation) {
const enabled = getEnvBoolean('SPLUNK_AUTOMATIC_LOG_COLLECTION', false);
instrumentation.setConfig(
Object.assign({}, instrumentation.getConfig(), {
disableLogSending: !enabled,
})
);
}
8 changes: 7 additions & 1 deletion src/tracing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ import {
} from '@opentelemetry/context-async-hooks';

import { configureHttpInstrumentation } from '../instrumentations/http';
import { configureLogInjection } from '../instrumentations/logging';
import {
configureLogInjection,
disableLogSending,
} from '../instrumentations/logging';
import { allowedTracingOptions, Options, _setDefaultOptions } from './options';
import { configureRedisInstrumentation } from '../instrumentations/redis';
import {
Expand Down Expand Up @@ -221,6 +224,9 @@ function configureInstrumentations(options: Options) {
configureRedisInstrumentation(instr, options);
break;
case '@opentelemetry/instrumentation-bunyan':
disableLogSending(instr);
configureLogInjection(instr);
break;
case '@opentelemetry/instrumentation-pino':
case '@opentelemetry/instrumentation-winston':
configureLogInjection(instr);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type EnvVarKey =
| 'OTEL_TRACES_EXPORTER'
| 'SPLUNK_ACCESS_TOKEN'
| 'SPLUNK_AUTOINSTRUMENT_PACKAGE_NAMES'
| 'SPLUNK_AUTOMATIC_LOG_COLLECTION'
| 'SPLUNK_DEBUG_METRICS_ENABLED'
| 'SPLUNK_INSTRUMENTATION_METRICS_ENABLED'
| 'SPLUNK_METRICS_ENABLED'
Expand Down
36 changes: 7 additions & 29 deletions test/loginjection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { startTracing, stopTracing } from '../src/tracing';
import { defaultLogHook } from '../src/instrumentations/logging';
import type * as pino from 'pino';
import type * as bunyan from 'bunyan';
import type * as winston from 'winston';
import { PinoInstrumentation } from '@opentelemetry/instrumentation-pino';

describe('log injection', () => {
Expand Down Expand Up @@ -60,35 +59,14 @@ describe('log injection', () => {
record = {};
});

describe('default flow', () => {
before(() => {
startTracing({ serviceName: 'test-service' });
});

after(() => {
stopTracing();
});

it('injects context to bunyan records', () => {
const logger: bunyan = require('bunyan').createLogger({
name: 'test',
stream,
});
assertInjection(logger);
});

it('injects context to pino records', () => {
const logger: pino.Logger = require('pino')(stream);
assertInjection(logger);
});

it('injects context to winston records', () => {
const winston: winston = require('winston');
const logger = winston.createLogger({
transports: [new winston.transports.Stream({ stream })],
});
assertInjection(logger);
it('injects context to winston records', () => {
startTracing({ serviceName: 'test-service' });
const winston: winston = require('winston');
const logger = winston.createLogger({
transports: [new winston.transports.Stream({ stream })],
});
assertInjection(logger);
stopTracing();
});

describe('injecting with custom hook', () => {
Expand Down

0 comments on commit 5cc2d2a

Please sign in to comment.