diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 648beacf2ae..ad7e5593911 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -26,6 +26,7 @@ All notable changes to experimental packages in this project will be documented * fix(exporter-*-otlp-*): use parseHeaders() to ensure header-values are not 'undefined' #4540 * Fixes a bug where passing `undefined` as a header value would crash the end-user app after the export timeout elapsed. +* fix(sdk-logs): ensure default resource attributes are used as fallbacks when a resource is passed to LoggerProvider. ### :books: (Refine Doc) diff --git a/experimental/packages/sdk-logs/README.md b/experimental/packages/sdk-logs/README.md index e68654f991c..5136bab22c9 100644 --- a/experimental/packages/sdk-logs/README.md +++ b/experimental/packages/sdk-logs/README.md @@ -46,7 +46,7 @@ const logger = logsAPI.logs.getLogger('default'); // emit a log record logger.emit({ - severityNumber: SeverityNumber.INFO, + severityNumber: logsAPI.SeverityNumber.INFO, severityText: 'INFO', body: 'this is a log record body', attributes: { 'log.type': 'LogRecord' }, diff --git a/experimental/packages/sdk-logs/src/LoggerProvider.ts b/experimental/packages/sdk-logs/src/LoggerProvider.ts index b8fd9851f85..8681ab364d7 100644 --- a/experimental/packages/sdk-logs/src/LoggerProvider.ts +++ b/experimental/packages/sdk-logs/src/LoggerProvider.ts @@ -33,15 +33,14 @@ export class LoggerProvider implements logsAPI.LoggerProvider { private readonly _sharedState: LoggerProviderSharedState; constructor(config: LoggerProviderConfig = {}) { - const { - resource = Resource.default(), - logRecordLimits, - forceFlushTimeoutMillis, - } = merge({}, loadDefaultConfig(), config); + const mergedConfig = merge({}, loadDefaultConfig(), config); + const resource = Resource.default().merge( + mergedConfig.resource ?? Resource.empty() + ); this._sharedState = new LoggerProviderSharedState( resource, - forceFlushTimeoutMillis, - reconfigureLimits(logRecordLimits) + mergedConfig.forceFlushTimeoutMillis, + reconfigureLimits(mergedConfig.logRecordLimits) ); this._shutdownOnce = new BindOnceFuture(this._shutdown, this); } diff --git a/experimental/packages/sdk-logs/test/common/LoggerProvider.test.ts b/experimental/packages/sdk-logs/test/common/LoggerProvider.test.ts index e4d80265c66..0c4060f0ebc 100644 --- a/experimental/packages/sdk-logs/test/common/LoggerProvider.test.ts +++ b/experimental/packages/sdk-logs/test/common/LoggerProvider.test.ts @@ -63,6 +63,16 @@ describe('LoggerProvider', () => { assert.deepStrictEqual(resource, Resource.default()); }); + it('should fallback to default resource attrs', () => { + const passedInResource = new Resource({ foo: 'bar' }); + const provider = new LoggerProvider({ resource: passedInResource }); + const { resource } = provider['_sharedState']; + assert.deepStrictEqual( + resource, + Resource.default().merge(passedInResource) + ); + }); + it('should have default forceFlushTimeoutMillis if not pass', () => { const provider = new LoggerProvider(); const sharedState = provider['_sharedState'];