Skip to content
1 change: 0 additions & 1 deletion experimental/packages/configuration/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "@opentelemetry/configuration",
"private": true,
"version": "0.208.0",
"description": "OpenTelemetry Configuration",
"main": "build/src/index.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
},
"dependencies": {
"@opentelemetry/api-logs": "0.208.0",
"@opentelemetry/configuration": "0.208.0",
"@opentelemetry/core": "2.2.0",
"@opentelemetry/exporter-logs-otlp-grpc": "0.208.0",
"@opentelemetry/exporter-logs-otlp-http": "0.208.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ export * as tracing from '@opentelemetry/sdk-trace-base';
export { NodeSDK } from './sdk';
export type { LoggerProviderConfig, MeterProviderConfig } from './sdk';
export type { NodeSDKConfiguration } from './types';
export { startNodeSDK } from './start';
51 changes: 51 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/src/start.ts
Comment thread
maryliag marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright The OpenTelemetry Authors
*
* 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
*
* https://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 {
ConfigFactory,
createConfigFactory,
} from '@opentelemetry/configuration';
import { diag, DiagConsoleLogger } from '@opentelemetry/api';
import { setupDefaultContextManager } from './utils';

/**
* @experimental Function to start the OpenTelemetry Node SDK
* @param sdkOptions
Comment thread
maryliag marked this conversation as resolved.
*/
export function startNodeSDK(): {
shutdown: () => Promise<void>;
} {
const configFactory: ConfigFactory = createConfigFactory();
const config = configFactory.getConfigModel();

if (config.disabled) {
diag.info('OpenTelemetry SDK is disabled');
return NOOP_SDK;
}
if (config.log_level != null) {
diag.setLogger(new DiagConsoleLogger(), { logLevel: config.log_level });
}

setupDefaultContextManager();

const shutdownFn = async () => {
const promises: Promise<unknown>[] = [];
await Promise.all(promises);
};
return { shutdown: shutdownFn };
}
const NOOP_SDK = {
shutdown: async () => {},
};
6 changes: 6 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ export function setupContextManager(
context.setGlobalContextManager(contextManager);
}

export function setupDefaultContextManager() {
const defaultContextManager = new AsyncLocalStorageContextManager();
defaultContextManager.enable();
context.setGlobalContextManager(defaultContextManager);
}

export function setupPropagator(
propagator: TextMapPropagator | null | undefined
) {
Expand Down
62 changes: 62 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/test/start.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright The OpenTelemetry Authors
*
* 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
*
* https://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 * as assert from 'assert';
import { startNodeSDK } from '../src/start';
import { context, diag } from '@opentelemetry/api';
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
import * as Sinon from 'sinon';

describe('startNodeSDK', function () {
const _origEnvVariables = { ...process.env };

afterEach(function () {
for (const key of Object.keys(process.env)) {
delete process.env[key];
}
for (const [key, value] of Object.entries(_origEnvVariables)) {
process.env[key] = value;
}
Sinon.restore();
});

it('should return NOOP_SDK when disabled is true', () => {
const info = Sinon.spy(diag, 'info');
process.env.OTEL_SDK_DISABLED = 'true';
const sdk = startNodeSDK();

Sinon.assert.calledWith(info, 'OpenTelemetry SDK is disabled');

sdk.shutdown();
});

it('should return NOOP_SDK when disabled is true', () => {
process.env.OTEL_EXPERIMENTAL_CONFIG_FILE =
'test/fixtures/kitchen-sink.yaml';
const sdk = startNodeSDK();

assertDefaultContextManagerRegistered();

sdk.shutdown();
});
});

function assertDefaultContextManagerRegistered() {
assert.ok(
context['_getContextManager']().constructor.name ===
AsyncLocalStorageContextManager.name
);
}
3 changes: 3 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
{
"path": "../api-logs"
},
{
"path": "../configuration"
},
{
"path": "../exporter-logs-otlp-grpc"
},
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"entryPoints": [
"api",
"experimental/packages/api-logs",
"experimental/packages/configuration",
"experimental/packages/exporter-logs-otlp-grpc",
"experimental/packages/exporter-logs-otlp-http",
"experimental/packages/exporter-logs-otlp-proto",
Expand Down