Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Karlie-777 committed Nov 27, 2024
1 parent 0ab3d8d commit 6a52c29
Show file tree
Hide file tree
Showing 20 changed files with 751 additions and 119 deletions.
38 changes: 27 additions & 11 deletions experimental/examples/logs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import {
ConsoleLogRecordExporter,
SimpleLogRecordProcessor,
} from '@opentelemetry/sdk-logs';
import {logs as experimentalLogs } from '@opentelemetry/api-logs/experimental';
import {
LoggerProvider as ExperimentalLoggerProvider,
ConsoleLogRecordExporter as ExperimentalConsoleLogRecordExporter,
SimpleLogRecordProcessor as ExperimentalSimpleLogRecordProcessor,
} from '@opentelemetry/sdk-logs/experimental';

// Optional and only needed to see the internal diagnostic logging (during development)
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
Expand All @@ -42,14 +48,24 @@ logger.emit({
attributes: { 'log.type': 'custom' },
});

// emit an event record
// logger.emitEvent({
// name: 'my-domain.my-event',
// data: {
// a: 1,
// b: 'hello',
// c: {
// d: 123
// }
// },
// });

// emit an event record from experimental logger
const experimentalLoggerProvider = new ExperimentalLoggerProvider();
experimentalLoggerProvider.addLogRecordProcessor(
new ExperimentalSimpleLogRecordProcessor(new ExperimentalConsoleLogRecordExporter())
);

experimentalLogs.setGlobalLoggerProvider(experimentalLoggerProvider);

const experimentalLogger = experimentalLogs.getLogger('example', '1.0.0');

experimentalLogger.emitEvent({
name: 'my-domain.my-event',
data: {
a: 1,
b: 'hello',
c: {
d: 123
}
},
});
10 changes: 10 additions & 0 deletions experimental/packages/api-logs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
"default": "./build/src/experimental/index.js"
}
},
"typesVersions": {
"*": {
"*": [
"./build/src/index.d.ts"
],
"experimental": [
"./build/src/experimental/index.d.ts"
]
}
},
"repository": "open-telemetry/opentelemetry-js",
"scripts": {
"prepublishOnly": "npm run compile",
Expand Down
2 changes: 0 additions & 2 deletions experimental/packages/api-logs/src/NoopLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import { LogRecord } from './types/LogRecord';

export class NoopLogger implements Logger {
emit(_logRecord: LogRecord): void {}

// emitEvent(_eventRecord: EventRecord): void {}
}

export const NOOP_LOGGER = new NoopLogger();
4 changes: 0 additions & 4 deletions experimental/packages/api-logs/src/ProxyLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ export class ProxyLogger implements Logger {
this._getLogger().emit(logRecord);
}

// emitEvent(eventRecord: EventRecord): void {
// this._getLogger().emitEvent(eventRecord);
// }

/**
* Try to get a logger from the proxy logger provider.
* If the proxy logger provider has no delegate, return a noop logger.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export class ProxyLogger extends BaseProxyLogger implements Logger {
* @param logRecord
*/
emitEvent(eventRecord: EventRecord): void {
this._getLogger().emitEvent(eventRecord);
const logger = this._getLogger();
// make sure that emitEvent function exists
if (typeof logger.emitEvent === 'function') {
this._getLogger().emitEvent(eventRecord);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

import { Logger } from './Logger';
import { LoggerOptions } from '../../types/LoggerOptions';
// import { LoggerProvider as BaseLoggerProvider } from '../../types/LoggerProvider';
import { LoggerProvider as BaseLoggerProvider } from '../../types/LoggerProvider';

/**
* A registry for creating named {@link Logger}s.
*/
export interface LoggerProvider {
export interface LoggerProvider extends BaseLoggerProvider {
/**
* Returns a Logger, creating one if one with the given name, version, and
* schemaUrl pair is not already created.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { _globalThis } from '../platform';
import { LoggerProvider as ExperimentalLoggerProvider } from '../experimental/types/LoggerProvider';

export const GLOBAL_LOGS_API_KEY = Symbol.for('io.opentelemetry.js.api.logs');
// TODO: confirm the name
// NOTE: added extra global key for experimental logs
export const GLOBAL_EXPERIMENTAL_LOGS_API_KEY = Symbol.for(
'io.opentelemetry.js.api.experimental.logs'
);
Expand Down
4 changes: 0 additions & 4 deletions experimental/packages/api-logs/src/types/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,4 @@ export interface Logger {
* @param logRecord
*/
emit(logRecord: LogRecord): void;
/**
* Emit an event.
*/
// emitEvent(eventRecord: EventRecord): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,4 @@ describe('NoopLogger', () => {
body: 'log body',
});
});

// it('calling emitEvent should not crash', () => {
// const logger = new NoopLoggerProvider().getLogger('test-noop');
// logger.emitEvent({
// severityNumber: SeverityNumber.TRACE,
// name: 'event name',
// });
// });
});
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,13 @@ describe('ProxyLogger', () => {

let delegateLogger: Logger;
let emitCalled: boolean;
// let emitEventCalled: boolean;

beforeEach(() => {
emitCalled = false;
delegateLogger = {
emit() {
emitCalled = true;
},
// emitEvent() {
// emitEventCalled = true;
// },
};

logger = provider.getLogger('test');
Expand All @@ -117,11 +113,5 @@ describe('ProxyLogger', () => {
});
assert.ok(emitCalled);
});
// it('should emitEvent from the delegate logger', () => {
// logger.emitEvent({
// name: 'Test event',
// });
// assert.ok(emitEventCalled);
// });
});
});
1 change: 0 additions & 1 deletion experimental/packages/sdk-events/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ import { LogRecord, Logger } from '@opentelemetry/api-logs';

export class TestLogger implements Logger {
emit(logRecord: LogRecord): void {}
// emitEvent(eventRecord: EventRecord): void {}
}
24 changes: 24 additions & 0 deletions experimental/packages/sdk-logs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@
"module": "build/esm/index.js",
"esnext": "build/esnext/index.js",
"types": "build/src/index.d.ts",
"exports": {
".": {
"module": "./build/esm/index.js",
"esnext": "./build/esnext/index.js",
"types": "./build/src/index.d.ts",
"default": "./build/src/index.js"
},
"./incubating": {
"module": "./build/esm/index_experimental.js",
"esnext": "./build/esnext/index_experimental.js",
"types": "./build/src/index_experimental.d.ts",
"default": "./build/src/index_experimental.js"
}
},
"typesVersions": {
"*": {
"*": [
"./build/src/index.d.ts"
],
"experimental": [
"./build/src/index_experimental.d.ts"
]
}
},
"browser": {
"./src/platform/index.ts": "./src/platform/browser/index.ts",
"./build/esm/platform/index.js": "./build/esm/platform/browser/index.js",
Expand Down
53 changes: 53 additions & 0 deletions experimental/packages/sdk-logs/src/Experimental_Logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 * as logsAPI from '@opentelemetry/api-logs/experimental';
import { Logger as BaseLogger } from './Logger';
import type { InstrumentationScope } from '@opentelemetry/core';
import { context } from '@opentelemetry/api';
import { LoggerProviderSharedState } from './internal/LoggerProviderSharedState';
import { SeverityNumber } from '@opentelemetry/api-logs/experimental';

export class Logger extends BaseLogger implements logsAPI.Logger {
constructor(
instrumentationScope: InstrumentationScope,
_sharedState: LoggerProviderSharedState
) {
super(instrumentationScope, _sharedState);
}

public emitEvent(eventRecord: logsAPI.EventRecord): void {
const attributes = eventRecord.attributes || {};

// TODO: change to ATTR_EVENT_NAME, it is under experimental_attributes currently.
// this will add more dependencies.
attributes['event.name'] = eventRecord.name;

const logRecord: logsAPI.LogRecord = {
attributes: attributes,
context: eventRecord.context || context.active(),
severityNumber: eventRecord.severityNumber || SeverityNumber.INFO,
timestamp: eventRecord.timestamp || Date.now(),
};

if (eventRecord.data) {
logRecord.body = eventRecord.data;
}
if (this.emit) {
this.emit(logRecord);
}
}
}
64 changes: 64 additions & 0 deletions experimental/packages/sdk-logs/src/Experimental_LoggerProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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 { diag } from '@opentelemetry/api';
import type * as logsAPI from '@opentelemetry/api-logs/experimental';
import { NOOP_LOGGER } from '@opentelemetry/api-logs/experimental';
import { LoggerProvider as BaseLoggerProvider } from './LoggerProvider';

import type { LoggerProviderConfig } from './types';
import { Logger } from './Experimental_Logger';

export const DEFAULT_LOGGER_NAME = 'unknown';

export class LoggerProvider
extends BaseLoggerProvider
implements logsAPI.LoggerProvider
{
constructor(config: LoggerProviderConfig = {}) {
super(config);
}

/**
* Get a logger with the configuration of the LoggerProvider.
*/
override getLogger(
name: string,
version?: string,
options?: logsAPI.LoggerOptions
): logsAPI.Logger {
if (this._shutdownOnce.isCalled) {
diag.warn('A shutdown LoggerProvider cannot provide a Logger');
return NOOP_LOGGER;
}

if (!name) {
diag.warn('Logger requested without instrumentation scope name.');
}
const loggerName = name || DEFAULT_LOGGER_NAME;
const key = `${loggerName}@${version || ''}:${options?.schemaUrl || ''}`;
if (!this._sharedState.loggers.has(key)) {
this._sharedState.loggers.set(
key,
new Logger(
{ name: loggerName, version, schemaUrl: options?.schemaUrl },
this._sharedState
)
);
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return this._sharedState.loggers.get(key)! as logsAPI.Logger;
}
}
4 changes: 2 additions & 2 deletions experimental/packages/sdk-logs/src/LoggerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import { LoggerProviderSharedState } from './internal/LoggerProviderSharedState'
export const DEFAULT_LOGGER_NAME = 'unknown';

export class LoggerProvider implements logsAPI.LoggerProvider {
private _shutdownOnce: BindOnceFuture<void>;
private readonly _sharedState: LoggerProviderSharedState;
protected _shutdownOnce: BindOnceFuture<void>;
protected readonly _sharedState: LoggerProviderSharedState;

constructor(config: LoggerProviderConfig = {}) {
const mergedConfig = merge({}, loadDefaultConfig(), config);
Expand Down
32 changes: 32 additions & 0 deletions experimental/packages/sdk-logs/src/index_experimental.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 {
LoggerProviderConfig,
LogRecordLimits,
BufferConfig,
BatchLogRecordProcessorBrowserConfig,
} from './types';
export { LoggerProvider } from './Experimental_LoggerProvider';
export { LogRecord } from './LogRecord';
export { LogRecordProcessor } from './LogRecordProcessor';
export { ReadableLogRecord } from './export/ReadableLogRecord';
export { NoopLogRecordProcessor } from './export/NoopLogRecordProcessor';
export { ConsoleLogRecordExporter } from './export/ConsoleLogRecordExporter';
export { LogRecordExporter } from './export/LogRecordExporter';
export { SimpleLogRecordProcessor } from './export/SimpleLogRecordProcessor';
export { InMemoryLogRecordExporter } from './export/InMemoryLogRecordExporter';
export { BatchLogRecordProcessor } from './platform';
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import { IResource } from '@opentelemetry/resources';
import { LogRecordProcessor } from '../LogRecordProcessor';
import { LogRecordLimits } from '../types';
import { NoopLogRecordProcessor } from '../export/NoopLogRecordProcessor';
import { Logger as ExperimentalLogger } from '@opentelemetry/api-logs/experimental';

export class LoggerProviderSharedState {
readonly loggers: Map<string, Logger> = new Map();
readonly loggers: Map<string, Logger | ExperimentalLogger> = new Map();
activeProcessor: LogRecordProcessor;
readonly registeredLogRecordProcessors: LogRecordProcessor[] = [];

Expand Down
Loading

0 comments on commit 6a52c29

Please sign in to comment.