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
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 { SDKOptions } from './types';
import { diag, DiagConsoleLogger } from '@opentelemetry/api';
import { setupContextManager } from './utils';

/**
* Experimental function to start the OpenTelemetry Node SDK
* @param sdkOptions
Comment thread
maryliag marked this conversation as resolved.
*/
export const startNodeSDK = (sdkOptions: Partial<SDKOptions> = {}) => {
Comment thread
maryliag marked this conversation as resolved.
Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make the return type explicit, something that also makes clear that shutdown is always async

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not exported through index.ts - is this on purpose?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, there is a reason... I forgot 😎
added 😄

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(sdkOptions?.contextManager);

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

const NOOP_SDK = {
shutdown: () => {},
};
4 changes: 4 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ export interface NodeSDKConfiguration {
spanLimits: SpanLimits;
idGenerator: IdGenerator;
}

export interface SDKOptions {
contextManager: ContextManager | null;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my experience, people add things based on what they see is already there. So a contextManager option here could be seen as an invitation to do the same as NodeSDK does today, eliminating the benefits of having a separate function for SDK setup.

Since the only option for Node.js is really the AsyncLocalContextManager, I'd suggest we use that as the default without having any options for now.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
This means I had to remove that interface for now since it was the only parameter, but I have another PR ready to go that adds it back with other parameters

}
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/start';
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
);
}
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