Skip to content

Commit

Permalink
refactor(api-logs): refactor api-logs
Browse files Browse the repository at this point in the history
  • Loading branch information
fuaiyi committed Nov 26, 2022
1 parent db0ecc3 commit 632b195
Show file tree
Hide file tree
Showing 25 changed files with 281 additions and 216 deletions.
12 changes: 6 additions & 6 deletions experimental/packages/api-logs/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
"env": {
"mocha": true,
"commonjs": true,
"shared-node-browser": true
env: {
mocha: true,
commonjs: true,
"shared-node-browser": true,
},
...require('../../../eslint.config.js')
}
...require("../../../eslint.config.js"),
};
8 changes: 5 additions & 3 deletions experimental/packages/api-logs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ api.logs.setGlobalLoggerProvider(loggerProvider);
/* returns loggerProvider (no-op if a working provider has not been initialized) */
api.logs.getLoggerProvider();
/* returns a logger from the registered global logger provider (no-op if a working provider has not been initialized) */
const logger = api.logs.getLogger(name, version);
const logger = api.logs.getLogger(name, version, {
eventDomain: "event-domain",
});

// logging an event in an instrumentation library
logger.emitEvent({ name: 'event-name', domain: 'event-domain' });
logger.getLogEvent("event-name", { body: "this is a body" }).emit();

// logging an event in a log appender
logger.emitLogRecord({ severityNumber: 1, body: 'log data' });
logger.getLogRecord({ body: "this is a body", severityNumber: SeverityNumber.INFO, severityText: "INFO" }).emit();
```

## Useful links
Expand Down
12 changes: 7 additions & 5 deletions experimental/packages/api-logs/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
* limitations under the License.
*/

const karmaWebpackConfig = require('../../../karma.webpack');
const karmaBaseConfig = require('../../../karma.base');
const karmaWebpackConfig = require("../../../karma.webpack");
const karmaBaseConfig = require("../../../karma.base");

module.exports = (config) => {
config.set(Object.assign({}, karmaBaseConfig, {
webpack: karmaWebpackConfig
}))
config.set(
Object.assign({}, karmaBaseConfig, {
webpack: karmaWebpackConfig,
})
);
};
31 changes: 31 additions & 0 deletions experimental/packages/api-logs/src/NoopLogRecord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 type { Attributes, AttributeValue } from "@opentelemetry/api";

import type { LogRecord } from "./types/LogRecord";

export class NoopLogRecord implements LogRecord {
setAttribute(_key: string, _value?: AttributeValue): LogRecord {
return this;
}

setAttributes(_attributes: Attributes): LogRecord {
return this;
}

emit(): void {}
}
14 changes: 9 additions & 5 deletions experimental/packages/api-logs/src/NoopLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
* limitations under the License.
*/

import { Logger } from './types/Logger';
import { LogEvent } from './types/LogEvent';
import { LogRecord } from './types/LogRecord';
import type { Logger } from "./types/Logger";
import type { LogRecord } from "./types/LogRecord";
import { NoopLogRecord } from "./NoopLogRecord";

export class NoopLogger implements Logger {
emitLogRecord(_logRecord: LogRecord): void {}
emitEvent(_event: LogEvent): void {}
getLogRecord(): LogRecord {
return new NoopLogRecord();
}
getLogEvent(_eventName: string): LogRecord {
return new NoopLogRecord();
}
}
10 changes: 5 additions & 5 deletions experimental/packages/api-logs/src/NoopLoggerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* limitations under the License.
*/

import { LoggerProvider } from './types/LoggerProvider';
import { Logger } from './types/Logger';
import { LoggerOptions } from './types/LoggerOptions';
import { NoopLogger } from './NoopLogger';
import type { LoggerProvider } from "./types/LoggerProvider";
import type { Logger } from "./types/Logger";
import type { LoggerOptions } from "./types/LoggerOptions";
import { NoopLogger } from "./NoopLogger";

export class NoopLoggerProvider implements LoggerProvider {
getLogger(_name: string, _version?: string | undefined, _options?: LoggerOptions | undefined): Logger {
getLogger(_name: string, _version?: string, _options?: LoggerOptions): Logger {
return new NoopLogger();
}
}
Expand Down
17 changes: 7 additions & 10 deletions experimental/packages/api-logs/src/api/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
* limitations under the License.
*/

import type { LoggerProvider } from "../types/LoggerProvider";
import type { Logger } from "../types/Logger";
import type { LoggerOptions } from "../types/LoggerOptions";
import {
API_BACKWARDS_COMPATIBILITY_VERSION,
GLOBAL_LOGS_API_KEY,
_global,
makeGetter
} from '../internal/global-utils';
import { LoggerProvider } from '../types/LoggerProvider';
import { NOOP_LOGGER_PROVIDER } from '../NoopLoggerProvider';
import { Logger } from '../types/Logger';
import { LoggerOptions } from '../types/LoggerOptions';
makeGetter,
} from "../internal/global-utils";
import { NOOP_LOGGER_PROVIDER } from "../NoopLoggerProvider";

export class LogsAPI {
private static _instance?: LogsAPI;
Expand Down Expand Up @@ -58,10 +58,7 @@ export class LogsAPI {
* @returns LoggerProvider
*/
public getLoggerProvider(): LoggerProvider {
return (
_global[GLOBAL_LOGS_API_KEY]?.(API_BACKWARDS_COMPATIBILITY_VERSION) ??
NOOP_LOGGER_PROVIDER
);
return _global[GLOBAL_LOGS_API_KEY]?.(API_BACKWARDS_COMPATIBILITY_VERSION) ?? NOOP_LOGGER_PROVIDER;
}

/**
Expand Down
11 changes: 5 additions & 6 deletions experimental/packages/api-logs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
* limitations under the License.
*/

export * from './types/Logger';
export * from './types/LoggerProvider';
export * from './types/LogRecord';
export * from './types/LogEvent';
export * from './types/LoggerOptions';
export * from "./types/Logger";
export * from "./types/LoggerProvider";
export * from "./types/LogRecord";
export * from "./types/LoggerOptions";

import { LogsAPI } from './api/logs';
import { LogsAPI } from "./api/logs";
export const logs = LogsAPI.getInstance();
8 changes: 3 additions & 5 deletions experimental/packages/api-logs/src/internal/global-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
* limitations under the License.
*/

import { LoggerProvider } from '../types/LoggerProvider';
import { _globalThis } from '../platform';
import type { LoggerProvider } from "../types/LoggerProvider";
import { _globalThis } from "../platform";

export const GLOBAL_LOGS_API_KEY = Symbol.for(
'io.opentelemetry.js.api.logs'
);
export const GLOBAL_LOGS_API_KEY = Symbol.for("io.opentelemetry.js.api.logs");

type Get<T> = (version: number) => T;
type OtelGlobal = Partial<{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@
/** only globals that common to node and browsers are allowed */
// eslint-disable-next-line node/no-unsupported-features/es-builtins, no-undef
export const _globalThis: typeof globalThis =
typeof globalThis === 'object' ? globalThis :
typeof self === 'object' ? self :
typeof window === 'object' ? window :
typeof global === 'object' ? global :
{} as typeof globalThis;
typeof globalThis === "object"
? globalThis
: typeof self === "object"
? self
: typeof window === "object"
? window
: typeof global === "object"
? global
: ({} as typeof globalThis);
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
* limitations under the License.
*/

export * from './globalThis';
export * from "./globalThis";
2 changes: 1 addition & 1 deletion experimental/packages/api-logs/src/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
* limitations under the License.
*/

export * from './node';
export * from "./node";
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

/** only globals that common to node and browsers are allowed */
// eslint-disable-next-line node/no-unsupported-features/es-builtins
export const _globalThis = typeof globalThis === 'object' ? globalThis : global;
export const _globalThis = typeof globalThis === "object" ? globalThis : global;
2 changes: 1 addition & 1 deletion experimental/packages/api-logs/src/platform/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
* limitations under the License.
*/

export * from './globalThis';
export * from "./globalThis";
54 changes: 0 additions & 54 deletions experimental/packages/api-logs/src/types/LogEvent.ts

This file was deleted.

82 changes: 42 additions & 40 deletions experimental/packages/api-logs/src/types/LogRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,48 @@
* limitations under the License.
*/

import { Attributes } from '@opentelemetry/api';
import type { Attributes, AttributeValue } from "@opentelemetry/api";

export enum SeverityNumber {
UNSPECIFIED = 0,
TRACE = 1,
TRACE2 = 2,
TRACE3 = 3,
TRACE4 = 4,
DEBUG = 5,
DEBUG2 = 6,
DEBUG3 = 7,
DEBUG4 = 8,
INFO = 9,
INFO2 = 10,
INFO3 = 11,
INFO4 = 12,
WARN = 13,
WARN2 = 14,
WARN3 = 15,
WARN4 = 16,
ERROR = 17,
ERROR2 = 18,
ERROR3 = 19,
ERROR4 = 20,
FATAL = 21,
FATAL2 = 22,
FATAL3 = 23,
FATAL4 = 24,
}

export interface LogRecord {
/**
* The time when the log record occurred as UNIX Epoch time in nanoseconds.
*/
timestamp?: number;

/**
* Numerical value of the severity.
*/
severityNumber?: number;

/**
* The severity text.
*/
severityText?: string;

/**
* A value containing the body of the log record.
*/
body?: string;

/**
* Attributes that define the log record.
*/
attributes?: Attributes;

/**
* 8 least significant bits are the trace flags as defined in W3C Trace Context specification.
*/
traceFlags?: number;

/**
* A unique identifier for a trace.
*/
traceId?: string;

/**
* A unique identifier for a span within a trace.
*/
spanId?: string;
/**
* Sets attributes. If the {@link LoggerOptions} previously contained a mapping for any of the
* keys, the old values are replaced by the specified values.
*/
setAttribute(key: string, value?: AttributeValue): LogRecord;
setAttributes(attributes: Attributes): LogRecord;

/**
* Emit the log record.
*/
emit(): void;
}

export type LogEvent = LogRecord;
Loading

0 comments on commit 632b195

Please sign in to comment.