Skip to content

Commit

Permalink
Move internal Console API to the @opentelemetry/core from `@opentel…
Browse files Browse the repository at this point in the history
…emetry/api`
  • Loading branch information
serkan-ozal committed Aug 23, 2024
1 parent 62d8d78 commit 1ddddfb
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 32 deletions.
26 changes: 21 additions & 5 deletions api/src/diag/consoleLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import { DiagLogger, DiagLogFunction } from './types';
import { Console } from '../internal/console';

type ConsoleMapKeys = 'error' | 'warn' | 'info' | 'debug' | 'trace';
const consoleMap: { n: keyof DiagLogger; c: ConsoleMapKeys }[] = [
Expand All @@ -35,11 +34,28 @@ const consoleMap: { n: keyof DiagLogger; c: ConsoleMapKeys }[] = [
*/
export class DiagConsoleLogger implements DiagLogger {
constructor() {
function _consoleFunc(funcName: ConsoleMapKeys): DiagLogFunction {
return function (...args) {
if (console) {
// Some environments only expose the console when the F12 developer console is open
// eslint-disable-next-line no-console
let theFunc = console[funcName];
if (typeof theFunc !== 'function') {
// Not all environments support all functions
// eslint-disable-next-line no-console
theFunc = console.log;
}

// One last final check
if (typeof theFunc === 'function') {
return theFunc.apply(console, args);
}
}
};
}

for (let i = 0; i < consoleMap.length; i++) {
this[consoleMap[i].n] = Console.getFunction(
consoleMap[i].c,
'log'
)! as DiagLogFunction;
this[consoleMap[i].n] = _consoleFunc(consoleMap[i].c);
}
}

Expand Down
7 changes: 0 additions & 7 deletions api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,6 @@ export {
} from './trace/invalid-span-constants';
export type { TraceAPI } from './api/trace';

export {
Console,
ConsoleConfig,
ConsoleLogFunction,
ConsoleFunctionNames,
} from './internal/console';

// Split module-level variable definition into separate files to allow
// tree-shaking on each api instance.
import { context } from './context-api';
Expand Down
4 changes: 2 additions & 2 deletions experimental/packages/sdk-logs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@
],
"sideEffects": false,
"peerDependencies": {
"@opentelemetry/api": ">=1.9.0 <1.10.0"
"@opentelemetry/api": ">=1.4.0 <1.10.0"
},
"devDependencies": {
"@babel/core": "7.25.2",
"@babel/preset-env": "7.25.3",
"@opentelemetry/api": ">=1.9.0 <1.10.0",
"@opentelemetry/api": ">=1.4.0 <1.10.0",
"@opentelemetry/api-logs": "0.52.0",
"@opentelemetry/resources_1.9.0": "npm:@opentelemetry/[email protected]",
"@types/mocha": "10.0.7",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
* limitations under the License.
*/

import { Console } from '@opentelemetry/api';
import { ExportResult, hrTimeToMicroseconds } from '@opentelemetry/core';
import { ExportResultCode } from '@opentelemetry/core';
import { internal, ExportResultCode } from '@opentelemetry/core';

import type { ReadableLogRecord } from './ReadableLogRecord';
import type { LogRecordExporter } from './LogRecordExporter';
Expand Down Expand Up @@ -77,7 +76,7 @@ export class ConsoleLogRecordExporter implements LogRecordExporter {
done?: (result: ExportResult) => void
): void {
for (const logRecord of logRecords) {
Console.dir(this._exportInfo(logRecord), { depth: 3 });
internal.Console.dir(this._exportInfo(logRecord), { depth: 3 });
}
done?.({ code: ExportResultCode.SUCCESS });
}
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,14 @@ export { isUrlIgnored, urlMatches } from './utils/url';
export { isWrapped } from './utils/wrap';
export { BindOnceFuture } from './utils/callback';
export { VERSION } from './version';
export {
ConsoleConfig,
ConsoleLogFunction,
ConsoleFunctionNames,
} from './internal/console';
import { _export } from './internal/exporter';
import { Console } from './internal/console';
export const internal = {
_export,
Console,
};
107 changes: 107 additions & 0 deletions packages/opentelemetry-core/src/internal/console.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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 type ConsoleLogFunction = (message: string, ...args: unknown[]) => void;

/**
* Internal API to encapsulate console related calls to manage them from a single point.
*
* @since 1.25.2
*/
export interface ConsoleConfig {
log?: ConsoleLogFunction;
error?: ConsoleLogFunction;
warn?: ConsoleLogFunction;
info?: ConsoleLogFunction;
debug?: ConsoleLogFunction;
trace?: ConsoleLogFunction;
dir?: (item?: any, options?: any) => void;
}

export type ConsoleFunctionNames =
| 'log'
| 'error'
| 'warn'
| 'info'
| 'debug'
| 'trace'
| 'dir';

let activeConfig: ConsoleConfig = {};
let scopeObj: any;

export class Console {
private constructor() {}

static configure(config: ConsoleConfig, obj: any): void {
// Copy the properties onto our own config,
// because properties in the given config object might change, but we want snapshot of them.
activeConfig = { ...config };
scopeObj = obj;
}

private static _callFunction(
funcName: ConsoleFunctionNames,
fallbackFuncName?: ConsoleFunctionNames,
...args: any[]
): void {
let theFunc: Function | undefined = activeConfig[funcName];
let theFuncObj: any = scopeObj;
if (!(typeof theFunc === 'function')) {
theFunc =
console &&
// eslint-disable-next-line no-console
(console[funcName] || (fallbackFuncName && console[fallbackFuncName]));
theFuncObj = console;
}
if (typeof theFunc === 'function') {
theFunc.apply(theFuncObj, args as any);
}
}

static getFunction(
funcName: ConsoleFunctionNames,
fallbackFuncName?: ConsoleFunctionNames
): Function {
return function (...args: any[]): void {
Console._callFunction(funcName, fallbackFuncName, ...args);
};
}

static log(message?: any, ...optionalParams: any[]): void {
Console._callFunction('log', undefined, message, ...optionalParams);
}

static error(message?: any, ...optionalParams: any[]): void {
Console._callFunction('error', undefined, message, ...optionalParams);
}

static warn(message?: any, ...optionalParams: any[]): void {
Console._callFunction('warn', undefined, message, ...optionalParams);
}

static info(message?: any, ...optionalParams: any[]): void {
Console._callFunction('info', undefined, message, ...optionalParams);
}

static debug(message?: any, ...optionalParams: any[]): void {
Console._callFunction('debug', undefined, message, ...optionalParams);
}

static dir(item?: any, options?: any): void {
Console._callFunction('dir', undefined, item, options);
}
}
4 changes: 2 additions & 2 deletions packages/opentelemetry-sdk-trace-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"access": "public"
},
"devDependencies": {
"@opentelemetry/api": ">=1.9.0 <1.10.0",
"@opentelemetry/api": ">=1.0.0 <1.10.0",
"@types/mocha": "10.0.7",
"@types/node": "18.6.5",
"@types/sinon": "17.0.3",
Expand All @@ -90,7 +90,7 @@
"webpack": "5.89.0"
},
"peerDependencies": {
"@opentelemetry/api": ">=1.9.0 <1.10.0"
"@opentelemetry/api": ">=1.0.0 <1.10.0"
},
"dependencies": {
"@opentelemetry/core": "1.25.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

import { SpanExporter } from './SpanExporter';
import { ReadableSpan } from './ReadableSpan';
import { Console } from '@opentelemetry/api';
import {
internal,
ExportResult,
ExportResultCode,
hrTimeToMicroseconds,
Expand Down Expand Up @@ -91,7 +91,7 @@ export class ConsoleSpanExporter implements SpanExporter {
done?: (result: ExportResult) => void
): void {
for (const span of spans) {
Console.dir(this._exportInfo(span), { depth: 3 });
internal.Console.dir(this._exportInfo(span), { depth: 3 });
}
if (done) {
return done({ code: ExportResultCode.SUCCESS });
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-metrics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"webpack-merge": "5.10.0"
},
"peerDependencies": {
"@opentelemetry/api": ">=1.9.0 <1.10.0"
"@opentelemetry/api": ">=1.3.0 <1.10.0"
},
"dependencies": {
"@opentelemetry/core": "1.25.1",
Expand Down
5 changes: 2 additions & 3 deletions packages/sdk-metrics/src/export/ConsoleMetricExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* limitations under the License.
*/

import { Console } from '@opentelemetry/api';
import { ExportResult, ExportResultCode } from '@opentelemetry/core';
import { internal, ExportResult, ExportResultCode } from '@opentelemetry/core';
import { InstrumentType } from '../InstrumentDescriptor';
import { AggregationTemporality } from './AggregationTemporality';
import { ResourceMetrics } from './MetricData';
Expand Down Expand Up @@ -72,7 +71,7 @@ export class ConsoleMetricExporter implements PushMetricExporter {
): void {
for (const scopeMetrics of metrics.scopeMetrics) {
for (const metric of scopeMetrics.metrics) {
Console.dir(
internal.Console.dir(
{
descriptor: metric.descriptor,
dataPointType: metric.dataPointType,
Expand Down

0 comments on commit 1ddddfb

Please sign in to comment.