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

Allow LoggerProvider to be specified in Instrumentations #4314

Merged
merged 21 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to experimental packages in this project will be documented

### :rocket: (Enhancement)

* feat(instrumentation): allow LoggerProvider to be specified in Instrumentations [#4314](https://github.com/open-telemetry/opentelemetry-js/pull/4314) @hectorhdzg
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved

### :bug: (Bug Fix)

* fix(sdk-logs): avoid map attribute set when count limit exceeded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@
"shimmer": "^1.2.1"
},
"peerDependencies": {
"@opentelemetry/api": "^1.3.0"
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/api-logs": "^0.45.1"
},
"devDependencies": {
"@babel/core": "7.23.3",
"@opentelemetry/api": "1.7.0",
"@opentelemetry/api-logs": "0.45.1",
"@opentelemetry/sdk-metrics": "1.18.1",
"@types/mocha": "10.0.4",
"@types/node": "18.6.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { trace, metrics } from '@opentelemetry/api';
import { logs } from '@opentelemetry/api-logs';
import {
disableInstrumentations,
enableInstrumentations,
Expand All @@ -36,8 +37,14 @@ export function registerInstrumentations(
);
const tracerProvider = options.tracerProvider || trace.getTracerProvider();
const meterProvider = options.meterProvider || metrics.getMeterProvider();
const loggerProvider = options.loggerProvider || logs.getLoggerProvider();

enableInstrumentations(instrumentations, tracerProvider, meterProvider);
enableInstrumentations(
instrumentations,
tracerProvider,
meterProvider,
loggerProvider
);

return () => {
disableInstrumentations(instrumentations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { TracerProvider, MeterProvider } from '@opentelemetry/api';
import { Instrumentation } from './types';
import { AutoLoaderResult, InstrumentationOption } from './types_internal';
import { LoggerProvider } from '@opentelemetry/api-logs';

/**
* Parses the options and returns instrumentations, node plugins and
Expand Down Expand Up @@ -52,7 +53,8 @@ export function parseInstrumentationOptions(
export function enableInstrumentations(
instrumentations: Instrumentation[],
tracerProvider?: TracerProvider,
meterProvider?: MeterProvider
meterProvider?: MeterProvider,
loggerProvider?: LoggerProvider
): void {
for (let i = 0, j = instrumentations.length; i < j; i++) {
const instrumentation = instrumentations[i];
Expand All @@ -62,6 +64,9 @@ export function enableInstrumentations(
if (meterProvider) {
instrumentation.setMeterProvider(meterProvider);
}
if (loggerProvider) {
instrumentation.setLoggerProvider(loggerProvider);
}
// instrumentations have been already enabled during creation
// so enable only if user prevented that by setting enabled to false
// this is to prevent double enabling but when calling register all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Tracer,
TracerProvider,
} from '@opentelemetry/api';
import { Logger, LoggerProvider, logs } from '@opentelemetry/api-logs';
import * as shimmer from 'shimmer';
import { InstrumentationModuleDefinition } from './platform/node';
import * as types from './types';
Expand All @@ -38,6 +39,7 @@

private _tracer: Tracer;
private _meter: Meter;
private _logger: Logger;
protected _diag: DiagLogger;

constructor(
Expand All @@ -55,8 +57,8 @@
});

this._tracer = trace.getTracer(instrumentationName, instrumentationVersion);

this._meter = metrics.getMeter(instrumentationName, instrumentationVersion);
this._logger = logs.getLogger(instrumentationName, instrumentationVersion);
this._updateMetricInstruments();
}

Expand Down Expand Up @@ -87,6 +89,22 @@
this._updateMetricInstruments();
}

/* Returns logger */
protected get logger(): Logger {
return this._logger;

Check warning on line 94 in experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts#L93-L94

Added lines #L93 - L94 were not covered by tests
}

/**
* Sets LoggerProvider to this plugin
* @param loggerProvider
*/
public setLoggerProvider(loggerProvider: LoggerProvider): void {
this._logger = loggerProvider.getLogger(

Check warning on line 102 in experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts#L101-L102

Added lines #L101 - L102 were not covered by tests
this.instrumentationName,
this.instrumentationVersion
);
}

/**
* Sets the new metric instruments with the current Meter.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { TracerProvider, MeterProvider } from '@opentelemetry/api';
import { LoggerProvider } from '@opentelemetry/api-logs';

/** Interface Instrumentation to apply patch. */
export interface Instrumentation {
Expand Down Expand Up @@ -43,6 +44,9 @@ export interface Instrumentation {
/** Method to set meter provider */
setMeterProvider(meterProvider: MeterProvider): void;

/** Method to set logger provider */
setLoggerProvider(loggerProvider: LoggerProvider): void;
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved

/** Method to set instrumentation config */
setConfig(config: InstrumentationConfig): void;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { TracerProvider, MeterProvider } from '@opentelemetry/api';
import { InstrumentationBase } from './platform';
import { Instrumentation } from './types';
import { LoggerProvider } from '@opentelemetry/api-logs';

export type InstrumentationOption =
| typeof InstrumentationBase
Expand All @@ -32,4 +33,5 @@ export interface AutoLoaderOptions {
instrumentations?: InstrumentationOption[];
tracerProvider?: TracerProvider;
meterProvider?: MeterProvider;
loggerProvider?: LoggerProvider;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import * as assert from 'assert';
import * as sinon from 'sinon';
import { InstrumentationBase, registerInstrumentations } from '../../src';
import { Logger, LoggerOptions, LoggerProvider } from '@opentelemetry/api-logs';

class DummyTracerProvider implements TracerProvider {
getTracer(name: string, version?: string): Tracer {
Expand All @@ -37,6 +38,12 @@ class DummyMeterProvider implements MeterProvider {
}
}

class DummyLoggerProvider implements LoggerProvider {
getLogger(name: string, version?: string, options?: LoggerOptions): Logger {
throw new Error('not implemented');
}
}

class FooInstrumentation extends InstrumentationBase {
init() {
return [];
Expand All @@ -63,17 +70,21 @@ describe('autoLoader', () => {
let enableSpy: sinon.SinonSpy;
let setTracerProviderSpy: sinon.SinonSpy;
let setMeterProviderSpy: sinon.SinonSpy;
let setLoggerProviderSpy: sinon.SinonSpy;
const tracerProvider = new DummyTracerProvider();
const meterProvider = new DummyMeterProvider();
const loggerProvider = new DummyLoggerProvider();
beforeEach(() => {
instrumentation = new FooInstrumentation('foo', '1', {});
enableSpy = sinon.spy(instrumentation, 'enable');
setTracerProviderSpy = sinon.stub(instrumentation, 'setTracerProvider');
setMeterProviderSpy = sinon.stub(instrumentation, 'setMeterProvider');
setLoggerProviderSpy = sinon.stub(instrumentation, 'setLoggerProvider');
unload = registerInstrumentations({
instrumentations: [instrumentation],
tracerProvider,
meterProvider,
loggerProvider,
});
});

Expand All @@ -96,10 +107,12 @@ describe('autoLoader', () => {
enableSpy = sinon.spy(instrumentation, 'enable');
setTracerProviderSpy = sinon.stub(instrumentation, 'setTracerProvider');
setMeterProviderSpy = sinon.stub(instrumentation, 'setMeterProvider');
setLoggerProviderSpy = sinon.stub(instrumentation, 'setLoggerProvider');
unload = registerInstrumentations({
instrumentations: [instrumentation],
tracerProvider,
meterProvider,
loggerProvider,
});
assert.strictEqual(enableSpy.callCount, 1);
});
Expand All @@ -119,6 +132,12 @@ describe('autoLoader', () => {
assert.ok(setMeterProviderSpy.lastCall.args[0] === meterProvider);
assert.strictEqual(setMeterProviderSpy.lastCall.args.length, 1);
});

it('should set LoggerProvider', () => {
assert.strictEqual(setLoggerProviderSpy.callCount, 1);
assert.ok(setLoggerProviderSpy.lastCall.args[0] === loggerProvider);
assert.strictEqual(setLoggerProviderSpy.lastCall.args.length, 1);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
},
{
"path": "../../../packages/sdk-metrics"
},
{
"path": "../api-logs"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
},
{
"path": "../../../packages/sdk-metrics"
},
{
"path": "../api-logs"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
},
{
"path": "../../../packages/sdk-metrics"
},
{
"path": "../api-logs"
}
]
}
9 changes: 6 additions & 3 deletions package-lock.json

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