Skip to content

Commit

Permalink
fix(sdk-logs): ensure default resource attributes are used as fallbac…
Browse files Browse the repository at this point in the history
…ks 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>
  • Loading branch information
trentm and pichlermarc authored Mar 21, 2024
1 parent 5489797 commit 6547440
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion experimental/packages/sdk-logs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
13 changes: 6 additions & 7 deletions experimental/packages/sdk-logs/src/LoggerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions experimental/packages/sdk-logs/test/common/LoggerProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down

0 comments on commit 6547440

Please sign in to comment.