fix(sdk-logs): ensure default resource attributes are used as fallbacks when a resource is passed to LoggerProvider (#4564)
Before this Resource.default() attributes would only be used if *no* resource was given to LoggerProvider. That would mean that 'service.name' and others could be missing, e.g. when called from NodeSDK. Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
This commit is contained in:
parent
5489797344
commit
6547440432
|
|
@ -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
|
* 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.
|
* 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)
|
### :books: (Refine Doc)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ const logger = logsAPI.logs.getLogger('default');
|
||||||
|
|
||||||
// emit a log record
|
// emit a log record
|
||||||
logger.emit({
|
logger.emit({
|
||||||
severityNumber: SeverityNumber.INFO,
|
severityNumber: logsAPI.SeverityNumber.INFO,
|
||||||
severityText: 'INFO',
|
severityText: 'INFO',
|
||||||
body: 'this is a log record body',
|
body: 'this is a log record body',
|
||||||
attributes: { 'log.type': 'LogRecord' },
|
attributes: { 'log.type': 'LogRecord' },
|
||||||
|
|
|
||||||
|
|
@ -33,15 +33,14 @@ export class LoggerProvider implements logsAPI.LoggerProvider {
|
||||||
private readonly _sharedState: LoggerProviderSharedState;
|
private readonly _sharedState: LoggerProviderSharedState;
|
||||||
|
|
||||||
constructor(config: LoggerProviderConfig = {}) {
|
constructor(config: LoggerProviderConfig = {}) {
|
||||||
const {
|
const mergedConfig = merge({}, loadDefaultConfig(), config);
|
||||||
resource = Resource.default(),
|
const resource = Resource.default().merge(
|
||||||
logRecordLimits,
|
mergedConfig.resource ?? Resource.empty()
|
||||||
forceFlushTimeoutMillis,
|
);
|
||||||
} = merge({}, loadDefaultConfig(), config);
|
|
||||||
this._sharedState = new LoggerProviderSharedState(
|
this._sharedState = new LoggerProviderSharedState(
|
||||||
resource,
|
resource,
|
||||||
forceFlushTimeoutMillis,
|
mergedConfig.forceFlushTimeoutMillis,
|
||||||
reconfigureLimits(logRecordLimits)
|
reconfigureLimits(mergedConfig.logRecordLimits)
|
||||||
);
|
);
|
||||||
this._shutdownOnce = new BindOnceFuture(this._shutdown, this);
|
this._shutdownOnce = new BindOnceFuture(this._shutdown, this);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,16 @@ describe('LoggerProvider', () => {
|
||||||
assert.deepStrictEqual(resource, Resource.default());
|
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', () => {
|
it('should have default forceFlushTimeoutMillis if not pass', () => {
|
||||||
const provider = new LoggerProvider();
|
const provider = new LoggerProvider();
|
||||||
const sharedState = provider['_sharedState'];
|
const sharedState = provider['_sharedState'];
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue