forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api-logs): Add delegating no-op logger provider (open-telemetry#…
- Loading branch information
1 parent
1aeb93b
commit a2c25d0
Showing
9 changed files
with
276 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* 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 { NOOP_LOGGER } from './NoopLogger'; | ||
import { Logger } from './types/Logger'; | ||
import { LoggerOptions } from './types/LoggerOptions'; | ||
import { LogRecord } from './types/LogRecord'; | ||
|
||
export class ProxyLogger implements Logger { | ||
// When a real implementation is provided, this will be it | ||
private _delegate?: Logger; | ||
|
||
constructor( | ||
private _provider: LoggerDelegator, | ||
public readonly name: string, | ||
public readonly version?: string | undefined, | ||
public readonly options?: LoggerOptions | undefined | ||
) {} | ||
|
||
/** | ||
* Emit a log record. This method should only be used by log appenders. | ||
* | ||
* @param logRecord | ||
*/ | ||
emit(logRecord: LogRecord): void { | ||
this._getLogger().emit(logRecord); | ||
} | ||
|
||
/** | ||
* Try to get a logger from the proxy logger provider. | ||
* If the proxy logger provider has no delegate, return a noop logger. | ||
*/ | ||
private _getLogger() { | ||
if (this._delegate) { | ||
return this._delegate; | ||
} | ||
const logger = this._provider.getDelegateLogger( | ||
this.name, | ||
this.version, | ||
this.options | ||
); | ||
if (!logger) { | ||
return NOOP_LOGGER; | ||
} | ||
this._delegate = logger; | ||
return this._delegate; | ||
} | ||
} | ||
|
||
export interface LoggerDelegator { | ||
getDelegateLogger( | ||
name: string, | ||
version?: string, | ||
options?: LoggerOptions | ||
): Logger | undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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 { LoggerProvider } from './types/LoggerProvider'; | ||
import { Logger } from './types/Logger'; | ||
import { LoggerOptions } from './types/LoggerOptions'; | ||
import { NOOP_LOGGER_PROVIDER } from './NoopLoggerProvider'; | ||
import { ProxyLogger } from './ProxyLogger'; | ||
|
||
export class ProxyLoggerProvider implements LoggerProvider { | ||
private _delegate?: LoggerProvider; | ||
|
||
getLogger( | ||
name: string, | ||
version?: string | undefined, | ||
options?: LoggerOptions | undefined | ||
): Logger { | ||
return ( | ||
this.getDelegateLogger(name, version, options) ?? | ||
new ProxyLogger(this, name, version, options) | ||
); | ||
} | ||
|
||
getDelegate(): LoggerProvider { | ||
return this._delegate ?? NOOP_LOGGER_PROVIDER; | ||
} | ||
|
||
/** | ||
* Set the delegate logger provider | ||
*/ | ||
setDelegate(delegate: LoggerProvider) { | ||
this._delegate = delegate; | ||
} | ||
|
||
getDelegateLogger( | ||
name: string, | ||
version?: string | undefined, | ||
options?: LoggerOptions | undefined | ||
): Logger | undefined { | ||
return this._delegate?.getLogger(name, version, options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
experimental/packages/api-logs/test/proxy-implementations/proxy-logger.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* 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 * as sinon from 'sinon'; | ||
import { | ||
Logger, | ||
LoggerProvider, | ||
ProxyLogger, | ||
ProxyLoggerProvider, | ||
} from '../../src'; | ||
import { NoopLogger } from '../../src/NoopLogger'; | ||
|
||
describe('ProxyLogger', () => { | ||
let provider: ProxyLoggerProvider; | ||
const sandbox = sinon.createSandbox(); | ||
|
||
beforeEach(() => { | ||
provider = new ProxyLoggerProvider(); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
describe('when no delegate is set', () => { | ||
it('should return proxy loggers', () => { | ||
const logger = provider.getLogger('test'); | ||
assert.ok(logger instanceof ProxyLogger); | ||
}); | ||
}); | ||
|
||
describe('when delegate is set before getLogger', () => { | ||
let delegate: LoggerProvider; | ||
let getLoggerStub: sinon.SinonStub; | ||
|
||
beforeEach(() => { | ||
getLoggerStub = sandbox.stub().returns(new NoopLogger()); | ||
delegate = { | ||
getLogger: getLoggerStub, | ||
}; | ||
provider.setDelegate(delegate); | ||
}); | ||
|
||
it('should return loggers directly from the delegate', () => { | ||
const logger = provider.getLogger('test', 'v0'); | ||
|
||
sandbox.assert.calledOnce(getLoggerStub); | ||
assert.strictEqual(getLoggerStub.firstCall.returnValue, logger); | ||
assert.deepStrictEqual(getLoggerStub.firstCall.args, [ | ||
'test', | ||
'v0', | ||
undefined, | ||
]); | ||
}); | ||
|
||
it('should return loggers directly from the delegate with schema url', () => { | ||
const logger = provider.getLogger('test', 'v0', { | ||
schemaUrl: 'https://opentelemetry.io/schemas/1.7.0', | ||
}); | ||
|
||
sandbox.assert.calledOnce(getLoggerStub); | ||
assert.strictEqual(getLoggerStub.firstCall.returnValue, logger); | ||
assert.deepStrictEqual(getLoggerStub.firstCall.args, [ | ||
'test', | ||
'v0', | ||
{ schemaUrl: 'https://opentelemetry.io/schemas/1.7.0' }, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('when delegate is set after getLogger', () => { | ||
let logger: Logger; | ||
let delegateProvider: LoggerProvider; | ||
|
||
let delegateLogger: Logger; | ||
let emitCalled: boolean; | ||
|
||
beforeEach(() => { | ||
emitCalled = false; | ||
delegateLogger = { | ||
emit() { | ||
emitCalled = true; | ||
}, | ||
}; | ||
|
||
logger = provider.getLogger('test'); | ||
|
||
delegateProvider = { | ||
getLogger() { | ||
return delegateLogger; | ||
}, | ||
}; | ||
provider.setDelegate(delegateProvider); | ||
}); | ||
|
||
it('should emit from the delegate logger', () => { | ||
logger.emit({ | ||
body: 'Test', | ||
}); | ||
assert.ok(emitCalled); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.