Skip to content
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
* feat(configuration): set meter provider exporter type from env variable [#6105](https://github.com/open-telemetry/opentelemetry-js/pull/6105) @maryliag
* refactor(configuration): throw warning and not error for invalid files [#6124](https://github.com/open-telemetry/opentelemetry-js/pull/6124) @maryliag
* refactor(configuration): dont have a default value for node resource detectors [#x](https://github.com/open-telemetry/opentelemetry-js/pull/x) @maryliag
* feat(opentelemetry-sdk-node): new function to start SDK [#6145](https://github.com/open-telemetry/opentelemetry-js/pull/6145) @maryliag
Comment thread
maryliag marked this conversation as resolved.
Outdated

### :bug: Bug Fixes

Expand Down
53 changes: 53 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/src/sdkNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 { NodeSDKConfiguration } from './types';
import { diag, DiagConsoleLogger } from '@opentelemetry/api';
import { setupContextManager } from './utils';

/**
* Experimental function to start the OpenTelemetry Node SDK
* @param configurationSDK
*/
export const startNodeSDK = (
configurationSDK: Partial<NodeSDKConfiguration> = {}
) => {
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 });
}

setupContextManager(configurationSDK?.contextManager);

const shutdownFn = async () => {
const promises: Promise<unknown>[] = [];
await Promise.all(promises);
};
return { shutdown: shutdownFn };
};

const NOOP_SDK = {
shutdown: () => {},
};
62 changes: 62 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/test/sdkNode.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/sdkNode';
import { context, diag } from '@opentelemetry/api';
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
import * as Sinon from 'sinon';

describe('startNodeSDK', () => {
Comment thread
maryliag marked this conversation as resolved.
Outdated
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
);
}