Skip to content

Commit dfe75b4

Browse files
committed
Implement OpenTelemetry plugin
1 parent 02ad6b0 commit dfe75b4

19 files changed

Lines changed: 913 additions & 610 deletions

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
build/
21
node_modules/
32
coverage/
3+
build/
44
.vscode/
55
.nyc_output/

package-lock.json

Lines changed: 610 additions & 486 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cf-nodejs-logging-support",
3-
"version": "7.4.3",
3+
"version": "7.5.4-beta.0",
44
"description": "Logging tool for Cloud Foundry",
55
"keywords": [
66
"logging",
@@ -62,6 +62,7 @@
6262
"README.md"
6363
],
6464
"dependencies": {
65+
"@opentelemetry/api-logs": "^0.51.0",
6566
"ajv": "^8.11.0",
6667
"json-stringify-safe": "^5.0.1",
6768
"jsonwebtoken": "^9.0.3",

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ exports = module.exports; // re-assign exports
66

77
export default rootLogger;
88
export * from "./lib/config/interfaces";
9+
export * from "./lib/logger/record";
910
export * from "./lib/logger/level";
1011
export * from "./lib/logger/logger";
12+
export * from "./lib/plugins/interfaces";
13+
export * from "./lib/plugins/defaultOutput";
14+
export * from "./lib/plugins/otelOutput";

src/lib/config/default/config-schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,11 @@
114114
},
115115
"DetailName": {
116116
"enum": [
117+
"errorMessage",
118+
"errorName",
117119
"level",
118120
"message",
121+
"rawStacktrace",
119122
"requestReceivedAt",
120123
"responseSentAt",
121124
"responseTimeMs",

src/lib/config/interfaces.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ export enum DetailName {
8686
WrittenTs = "writtenTs",
8787
Message = "message",
8888
Stacktrace = "stacktrace",
89+
RawStacktrace = "rawStacktrace",
90+
ErrorName = "errorName",
91+
ErrorMessage = "errorMessage",
8992
Level = "level"
9093
}
9194

src/lib/helper/levelUtils.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/lib/helper/pluginProvider.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { OutputPlugin } from "../plugins/interfaces";
2+
3+
export default class PluginProvider {
4+
private static instance: PluginProvider;
5+
private outputPlugins: OutputPlugin[];
6+
7+
private constructor() {
8+
this.outputPlugins = [];
9+
}
10+
11+
static getInstance(): PluginProvider {
12+
if (!PluginProvider.instance) {
13+
PluginProvider.instance = new PluginProvider();
14+
}
15+
return PluginProvider.instance;
16+
}
17+
18+
addOutputPlugin(outputPlugin: OutputPlugin) {
19+
this.outputPlugins.push(outputPlugin);
20+
}
21+
22+
setOutputPlugins(outputPlugins: OutputPlugin[]) {
23+
this.outputPlugins = outputPlugins;
24+
}
25+
26+
getOutputPlugins(): OutputPlugin[] {
27+
return this.outputPlugins;
28+
}
29+
}
30+
31+

src/lib/logger/level.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,29 @@ export enum Level {
88
Debug = 4,
99
Silly = 5
1010
}
11+
12+
export class LevelUtils {
13+
private static readonly defaultLevel: Level = Level.Info
14+
15+
static getLevel(level: String | Level): Level {
16+
if (typeof level === 'string') {
17+
const key = level.charAt(0).toUpperCase() + level.slice(1).toLowerCase();
18+
const lvl: Level = Level[key as keyof typeof Level]
19+
if (lvl !== undefined) {
20+
return lvl;
21+
}
22+
} else {
23+
return level as Level
24+
}
25+
return LevelUtils.defaultLevel;
26+
}
27+
28+
static getName(level: Level): string {
29+
return Level[level].toLowerCase()
30+
}
31+
32+
static isLevelEnabled(threshold: Level, level: Level) {
33+
if (level <= Level.Off) return false;
34+
return level <= threshold
35+
}
36+
}

src/lib/logger/logger.ts

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import LevelUtils from '../helper/levelUtils';
21
import { isValidObject } from '../middleware/utils';
3-
import { Level } from './level';
2+
import { Level, LevelUtils } from './level';
43
import RecordFactory from './recordFactory';
5-
import RecordWriter from './recordWriter';
64
import Context from './context';
5+
import PluginProvider from '../helper/pluginProvider';
76

87
export class Logger {
98
private parent?: Logger = undefined
109
private context?: Context;
1110
private registeredCustomFields: Array<string> = [];
1211
private customFields: Map<string, any> = new Map<string, any>()
1312
private recordFactory: RecordFactory;
14-
private recordWriter: RecordWriter;
13+
1514
protected loggingLevelThreshold: Level = Level.Inherit
1615

1716
constructor(parent?: Logger, context?: Context) {
@@ -23,7 +22,6 @@ export class Logger {
2322
this.context = context;
2423
}
2524
this.recordFactory = RecordFactory.getInstance();
26-
this.recordWriter = RecordWriter.getInstance();
2725
}
2826

2927
createLogger(customFields?: Map<string, any> | Object, createNewContext?: boolean): Logger {
@@ -37,11 +35,7 @@ export class Logger {
3735
}
3836

3937
setLoggingLevel(level: string | Level) {
40-
if (typeof level === 'string') {
41-
this.loggingLevelThreshold = LevelUtils.getLevel(level)
42-
} else {
43-
this.loggingLevelThreshold = level
44-
}
38+
this.loggingLevelThreshold = LevelUtils.getLevel(level)
4539
}
4640

4741
getLoggingLevel(): string {
@@ -55,26 +49,14 @@ export class Logger {
5549
if (this.loggingLevelThreshold == Level.Inherit) {
5650
return this.parent!.isLoggingLevel(level)
5751
}
58-
if (typeof level === 'string') {
59-
return LevelUtils.isLevelEnabled(this.loggingLevelThreshold, LevelUtils.getLevel(level))
60-
} else {
61-
return LevelUtils.isLevelEnabled(this.loggingLevelThreshold, level)
62-
}
52+
return LevelUtils.isLevelEnabled(this.loggingLevelThreshold, LevelUtils.getLevel(level))
6353
}
6454

6555
logMessage(level: string | Level, ...args: any) {
6656
if (!this.isLoggingLevel(level)) return;
6757
const loggerCustomFields = this.getCustomFieldsFromLogger(this);
68-
69-
let levelName: string;
70-
if (typeof level === 'string') {
71-
levelName = level;
72-
} else {
73-
levelName = LevelUtils.getName(level);
74-
}
75-
76-
const record = this.recordFactory.buildMsgRecord(this.registeredCustomFields, loggerCustomFields, levelName, args, this.context);
77-
this.recordWriter.writeLog(record);
58+
const record = this.recordFactory.buildMsgRecord(this.registeredCustomFields, loggerCustomFields, LevelUtils.getLevel(level), args, this.context);
59+
PluginProvider.getInstance().getOutputPlugins().forEach(output => { output.writeRecord(record) })
7860
}
7961

8062
error(...args: any) {

0 commit comments

Comments
 (0)