Skip to content

Commit

Permalink
move log tests to separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
seemk committed Dec 12, 2023
1 parent 703b6b4 commit e538d05
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 150 deletions.
55 changes: 55 additions & 0 deletions test/loginjection.bunyan.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright Splunk Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { TestLogStream, assertInjection } from './utils';
import { startTracing, stopTracing } from '../src/tracing';
import type * as bunyan from 'bunyan';

describe('log injection', () => {
let logStream: TestLogStream;

beforeEach(() => {
logStream = new TestLogStream();
});

describe('injecting version and environment', () => {
before(() => {
process.env.OTEL_RESOURCE_ATTRIBUTES =
'service.version=1,deployment.environment=test';
});

after(() => {
delete process.env.OTEL_RESOURCE_ATTRIBUTES;
});

it('injects service version and service environment if available', () => {
startTracing({ serviceName: 'test-service' });

const logger: bunyan = require('bunyan').createLogger({
name: 'test',
stream: logStream.stream,
});

assertInjection(logStream, logger, [
['service.name', 'test-service'],
['service.version', '1'],
['service.environment', 'test'],
]);

stopTracing();
});
});
});
77 changes: 77 additions & 0 deletions test/loginjection.pino.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright Splunk Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type * as pino from 'pino';
import { startTracing, stopTracing } from '../src/tracing';
import { TestLogStream, assertInjection } from './utils';
import { defaultLogHook } from '../src/instrumentations/logging';
import { PinoInstrumentation } from '@opentelemetry/instrumentation-pino';

describe('pino with with custom hooks', () => {
let logStream: TestLogStream;

beforeEach(() => {
logStream = new TestLogStream();
});
afterEach(() => {
stopTracing();
});

it('is possible to opt out from injecting resource attributes', () => {
const MY_VALUE = 'myValue';
const MY_ATTRIBUTE = 'myAttribute';
startTracing({
serviceName: 'test-service',
instrumentations: [
new PinoInstrumentation({
logHook: (span, logRecord) => {
logRecord[MY_ATTRIBUTE] = MY_VALUE;
},
}),
],
});

const logger: pino.Logger = require('pino')(logStream.stream);

assertInjection(logStream, logger, [
['service.name', undefined],
[MY_ATTRIBUTE, MY_VALUE],
]);
});

it('is easy enough do do both', () => {
const MY_VALUE = 'myValueBoth';
const MY_ATTRIBUTE = 'myAttributeBoth';
startTracing({
serviceName: 'test-service',
instrumentations: [
new PinoInstrumentation({
logHook: (span, logRecord) => {
defaultLogHook(span, logRecord);
logRecord[MY_ATTRIBUTE] = MY_VALUE;
},
}),
],
});

const logger: pino.Logger = require('pino')(logStream.stream);

assertInjection(logStream, logger, [
['service.name', 'test-service'],
[MY_ATTRIBUTE, MY_VALUE],
]);
});
});
150 changes: 0 additions & 150 deletions test/loginjection.test.ts

This file was deleted.

36 changes: 36 additions & 0 deletions test/loginjection.winston.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright Splunk Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { startTracing, stopTracing } from '../src/tracing';
import { TestLogStream, assertInjection } from './utils';

describe('winston log injection', () => {
let logStream: TestLogStream;

beforeEach(() => {
logStream = new TestLogStream();
});

it('injects context to winston records', () => {
startTracing({ serviceName: 'test-service' });
const winston = require('winston');
const logger = winston.createLogger({
transports: [new winston.transports.Stream({ stream: logStream.stream })],
});
assertInjection(logStream, logger);
stopTracing();
});
});
43 changes: 43 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import {
InstrumentType,
MetricReader,
} from '@opentelemetry/sdk-metrics';
import * as assert from 'assert';
import * as util from 'util';
import { Writable } from 'stream';
import { context, trace } from '@opentelemetry/api';

const isConfigVarEntry = (key) => {
const lowercased = key.toLowerCase();
Expand Down Expand Up @@ -63,3 +67,42 @@ export class TestMetricReader extends MetricReader {
protected async onForceFlush() {}
protected async onShutdown() {}
}

export class TestLogStream {
public stream: Writable;
public record = {};

constructor() {
this.stream = new Writable({
write: (chunk) => {
this.record = JSON.parse(chunk);
},
});
}
}

export function assertInjection(
stream: TestLogStream,
logger: any,
extra = [['service.name', 'test-service']]
) {
const span = trace.getTracer('test').startSpan('main');
let traceId;
let spanId;
context.with(trace.setSpan(context.active(), span), () => {
traceId = span.spanContext().traceId;
spanId = span.spanContext().spanId;
logger.info('my-log-message');
});

assert.strictEqual(stream.record['trace_id'], traceId);
assert.strictEqual(stream.record['span_id'], spanId);

for (const [key, value] of extra || []) {
assert.strictEqual(
stream.record[key],
value,
`Invalid value for "${key}": ${util.inspect(stream.record[key])}`
);
}
}

0 comments on commit e538d05

Please sign in to comment.