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(sdk-node): install diag logger with OTEL_LOG_LEVEL #3627

Merged
merged 1 commit into from
Feb 22, 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
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to experimental packages in this project will be documented
### :rocket: (Enhancement)

* feat: add HTTP_ROUTE attribute to http incoming metrics if present [#3581](https://github.com/open-telemetry/opentelemetry-js/pull/3581) @hermogenes
* feat(sdk-node): install diag logger with OTEL_LOG_LEVEL [#3627](https://github.com/open-telemetry/opentelemetry-js/pull/3627) @legendecas

### :bug: (Bug Fix)

Expand Down
16 changes: 15 additions & 1 deletion experimental/packages/opentelemetry-sdk-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,21 @@ Configure the [service name](https://github.com/open-telemetry/opentelemetry-spe

Disable the SDK by setting the `OTEL_SDK_DISABLED` environment variable to `true`.

## Configure Trace Exporter from Environment
## Configure log level from the environment

Set the log level by setting the `OTEL_LOG_LEVEL` environment variable to enums:

- `NONE`,
- `ERROR`,
- `WARN`,
- `INFO`,
- `DEBUG`,
- `VERBOSE`,
- `ALL`.

The default level is `INFO`.

## Configure Trace Exporter from environment

This is an alternative to programmatically configuring an exporter or span processor. This package will auto setup the default `otlp` exporter with `http/protobuf` protocol if `traceExporter` or `spanProcessor` hasn't been passed into the `NodeSDK` constructor.

Expand Down
16 changes: 14 additions & 2 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
* limitations under the License.
*/

import { ContextManager, TextMapPropagator, metrics } from '@opentelemetry/api';
import {
ContextManager,
TextMapPropagator,
metrics,
diag,
DiagConsoleLogger,
} from '@opentelemetry/api';
import {
InstrumentationOption,
registerInstrumentations,
Expand Down Expand Up @@ -80,11 +86,17 @@ export class NodeSDK {
* Create a new NodeJS SDK instance
*/
public constructor(configuration: Partial<NodeSDKConfiguration> = {}) {
if (getEnv().OTEL_SDK_DISABLED) {
const env = getEnv();
if (env.OTEL_SDK_DISABLED) {
this._disabled = true;
// Functions with possible side-effects are set
// to no-op via the _disabled flag
}
if (env.OTEL_LOG_LEVEL) {
diag.setLogger(new DiagConsoleLogger(), {
logLevel: env.OTEL_LOG_LEVEL,
});
}

this._resource = configuration.resource ?? new Resource({});
this._resourceDetectors = configuration.resourceDetectors ?? [
Expand Down
25 changes: 25 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
diag,
DiagLogLevel,
metrics,
DiagConsoleLogger,
} from '@opentelemetry/api';
import {
AsyncHooksContextManager,
Expand Down Expand Up @@ -68,6 +69,7 @@ describe('Node SDK', () => {
let delegate: any;

beforeEach(() => {
diag.disable();
context.disable();
trace.disable();
propagation.disable();
Expand All @@ -78,6 +80,10 @@ describe('Node SDK', () => {
delegate = (trace.getTracerProvider() as ProxyTracerProvider).getDelegate();
});

afterEach(() => {
Sinon.restore();
});

describe('Basic Registration', () => {
it('should not register any unconfigured SDK components', async () => {
// need to set OTEL_TRACES_EXPORTER to none since default value is otlp
Expand Down Expand Up @@ -108,6 +114,25 @@ describe('Node SDK', () => {
delete env.OTEL_TRACES_EXPORTER;
});

it('should register a diag logger with OTEL_LOG_LEVEL', () => {
env.OTEL_LOG_LEVEL = 'ERROR';

const spy = Sinon.spy(diag, 'setLogger');
const sdk = new NodeSDK({
autoDetectResources: false,
});

sdk.start();

assert.strictEqual(spy.callCount, 1);
assert.ok(spy.args[0][0] instanceof DiagConsoleLogger);
assert.deepStrictEqual(spy.args[0][1], {
logLevel: DiagLogLevel.ERROR,
});

delete env.OTEL_LOG_LEVEL;
});

it('should register a tracer provider if an exporter is provided', async () => {
const sdk = new NodeSDK({
traceExporter: new ConsoleSpanExporter(),
Expand Down