Skip to content

Commit

Permalink
feat(sdk-node): install diag log with OTEL_LOG_LEVEL (#3627)
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Feb 22, 2023
1 parent 86973eb commit 9b4f2a5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
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
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
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
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

0 comments on commit 9b4f2a5

Please sign in to comment.