Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
chore: reduce diag API surface exposed to user
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan committed Feb 24, 2021
1 parent 92df4aa commit badceec
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 279 deletions.
51 changes: 23 additions & 28 deletions src/api/diag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@
* limitations under the License.
*/

import {
DiagLogger,
DiagLogFunction,
createNoopDiagLogger,
diagLoggerFunctions,
FilteredDiagLogger,
} from '../diag/logger';
import { DiagLogLevel, createLogLevelDiagLogger } from '../diag/logLevel';
import { createLogLevelDiagLogger } from '../diag/internal/logLevelLogger';
import { createNoopDiagLogger } from '../diag/internal/noopLogger';
import { DiagLogFunction, DiagLogger, DiagLogLevel } from '../diag/types';
import {
API_BACKWARDS_COMPATIBILITY_VERSION,
GLOBAL_DIAG_LOGGER_API_KEY,
Expand All @@ -34,7 +29,6 @@ function noopDiagApi(): DiagAPI {
const noopLogger = createNoopDiagLogger();
return {
disable: () => {},
getLogger: () => noopLogger,
setLogger: () => {},
...noopLogger,
};
Expand Down Expand Up @@ -73,7 +67,7 @@ export class DiagAPI implements DiagLogger {
*/
private constructor() {
const _noopLogger = createNoopDiagLogger();
let _filteredLogger: FilteredDiagLogger | undefined;
let _filteredLogger: DiagLogger | undefined;

function _logProxy(funcName: keyof DiagLogger): DiagLogFunction {
return function () {
Expand All @@ -94,37 +88,38 @@ export class DiagAPI implements DiagLogger {

// DiagAPI specific functions

self.getLogger = (): FilteredDiagLogger => {
return _filteredLogger || _noopLogger;
};

self.setLogger = (
logger: DiagLogger,
logLevel: DiagLogLevel = DiagLogLevel.INFO
) => {
// This is required to prevent an endless loop in the case where the diag
// is used as a child of itself accidentally.
logger = logger === self ? self.getLogger().getChild() : logger;
logger = logger ?? _noopLogger;
if (logger === self) {
if (_filteredLogger) {
const err = new Error(
'Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation'
);
_filteredLogger.error(err.stack ?? err.message);
logger = _filteredLogger;
} else {
// There isn't much we can do here.
// Logging to the console might break the user application.
return;
}
}

_filteredLogger = createLogLevelDiagLogger(logLevel, logger);
};

self.disable = () => {
_filteredLogger = undefined;
};

for (let i = 0; i < diagLoggerFunctions.length; i++) {
const name = diagLoggerFunctions[i];
self[name] = _logProxy(name);
}
self.verbose = _logProxy('verbose');
self.debug = _logProxy('debug');
self.info = _logProxy('info');
self.warn = _logProxy('warn');
self.error = _logProxy('error');
}

/**
* Return the currently configured logger instance, if no logger has been configured
* it will return itself so any log level filtering will still be applied in this case.
*/
public getLogger!: () => FilteredDiagLogger;

/**
* Set the global DiagLogger and DiagLogLevel.
* If a global diag logger is already set, this will override it.
Expand Down
2 changes: 1 addition & 1 deletion src/diag/consoleLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { DiagLogger, DiagLogFunction } from './logger';
import { DiagLogger, DiagLogFunction } from './types';

const consoleMap: { n: keyof DiagLogger; c: keyof Console }[] = [
{ n: 'error', c: 'error' },
Expand Down
18 changes: 18 additions & 0 deletions src/diag/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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.
*/

export * from './consoleLogger';
export * from './types';
62 changes: 62 additions & 0 deletions src/diag/internal/logLevelLogger.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 { DiagLogFunction, DiagLogger, DiagLogLevel } from '../types';
import { createNoopDiagLogger } from './noopLogger';

export function createLogLevelDiagLogger(
maxLevel: DiagLogLevel,
logger: DiagLogger
): DiagLogger {
if (maxLevel < DiagLogLevel.NONE) {
maxLevel = DiagLogLevel.NONE;
} else if (maxLevel > DiagLogLevel.ALL) {
maxLevel = DiagLogLevel.ALL;
}

if (!logger) {
// this shouldn't happen, but return a noop logger to not break the user
return createNoopDiagLogger();
}

function _filterFunc(
theLogger: DiagLogger,
funcName: keyof DiagLogger,
theLevel: DiagLogLevel
): DiagLogFunction {
if (maxLevel >= theLevel) {
return function () {
const orgArguments = arguments as unknown;
const theFunc = theLogger[funcName];
if (theFunc && typeof theFunc === 'function') {
return theFunc.apply(
logger,
orgArguments as Parameters<DiagLogFunction>
);
}
};
}
return function () {};
}

return {

This comment has been minimized.

Copy link
@MSNev

MSNev Feb 24, 2021

Contributor

Ok, I know I said id mention that "m" word less, buuuutttt...

Does this result in smaller output? Looks like it might be close -- enough?

As this definitely looks easier to read. And while I can think of a few additional tricks, I'm not sure its worth it, basically

  • keep the diagLoggerFunctions but don't export from main project (effectively keeping internal)
  • still use for loop for diag.ts, here (which some funcky toUpper() for the level lookup) and in the noopLoger.ts.
error: _filterFunc(logger, 'error', DiagLogLevel.ERROR),
warn: _filterFunc(logger, 'warn', DiagLogLevel.WARN),
info: _filterFunc(logger, 'info', DiagLogLevel.INFO),
debug: _filterFunc(logger, 'debug', DiagLogLevel.DEBUG),
verbose: _filterFunc(logger, 'verbose', DiagLogLevel.VERBOSE),
};
}
34 changes: 34 additions & 0 deletions src/diag/internal/noopLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 { DiagLogger } from '../types';

function noopLogFunction() {}

/**
* Returns a No-Op Diagnostic logger where all messages do nothing.
* @implements {@link DiagLogger}
* @returns {DiagLogger}
*/
export function createNoopDiagLogger(): DiagLogger {
return {
verbose: noopLogFunction,
debug: noopLogFunction,
info: noopLogFunction,
warn: noopLogFunction,
error: noopLogFunction,
};
}
124 changes: 0 additions & 124 deletions src/diag/logLevel.ts

This file was deleted.

Loading

0 comments on commit badceec

Please sign in to comment.