Skip to content

Commit 265df17

Browse files
committed
Add options to include additional fields as log attributes
1 parent c1852e7 commit 265df17

5 files changed

Lines changed: 53 additions & 46 deletions

File tree

package-lock.json

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

package.json

Lines changed: 1 addition & 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.0-beta.3",
3+
"version": "7.4.0-beta.4",
44
"description": "Logging tool for Cloud Foundry",
55
"keywords": [
66
"logging",

src/lib/logger/record.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ export class RecordMetadata {
1818
stacktrace?: string[]
1919
errorName?: string
2020
errorMessage?: string
21+
customFieldNames: string[]
2122

2223
constructor(type: RecordType, level: Level) {
2324
this.type = type
2425
this.level = level
26+
this.customFieldNames = new Array<string>()
2527
}
2628
}
2729

src/lib/logger/recordFactory.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ export default class RecordFactory {
136136
indexedCustomFields[key] = value;
137137
}
138138
}
139+
140+
record.metadata.customFieldNames.push(key)
139141
});
140142

141143
// Write custom fields in the correct order and correlates i to the place in registeredCustomFields

src/lib/plugins/otelOutput.ts

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ import { Level } from '../logger/level'
66

77
export class OpenTelemetryLogsOutputPlugin implements OutputPlugin {
88
private logger: Logger
9+
private includeFieldsAsAttributes: FieldInclusionMode
910

1011
public constructor(loggerProvider?: LoggerProvider) {
1112
if (loggerProvider) {
1213
this.logger = loggerProvider.getLogger('default')
1314
} else {
1415
this.logger = logsAPI.getLoggerProvider().getLogger("default")
1516
}
17+
this.includeFieldsAsAttributes = FieldInclusionMode.CustomFieldsOnly
18+
}
19+
20+
public setIncludeFieldsAsAttributes(includeFieldsAsAttributes: FieldInclusionMode) {
21+
this.includeFieldsAsAttributes = includeFieldsAsAttributes
1622
}
1723

1824
public writeRecord(record: Record): void {
@@ -21,16 +27,8 @@ export class OpenTelemetryLogsOutputPlugin implements OutputPlugin {
2127
}
2228

2329
let attributes = {} as LogAttributes
24-
25-
if (record.metadata.errorName) {
26-
attributes["exception.type"] = record.metadata.errorName
27-
}
28-
if (record.metadata.errorMessage) {
29-
attributes["exception.message"] = record.metadata.errorMessage
30-
}
31-
if (record.metadata.rawStacktrace) {
32-
attributes["exception.stacktrace"] = record.metadata.rawStacktrace
33-
}
30+
this.populateExceptionAttributes(record, attributes)
31+
this.populateAdditionalAttributes(record, attributes)
3432

3533
let severityNumber = this.mapLevelToSeverityNumber(record.metadata.level)
3634

@@ -58,4 +56,42 @@ export class OpenTelemetryLogsOutputPlugin implements OutputPlugin {
5856
}
5957
return SeverityNumber.UNSPECIFIED
6058
}
59+
60+
private populateExceptionAttributes(record: Record, attributes: LogAttributes) {
61+
if (record.metadata.errorName) {
62+
attributes["exception.type"] = record.metadata.errorName
63+
}
64+
if (record.metadata.errorMessage) {
65+
attributes["exception.message"] = record.metadata.errorMessage
66+
}
67+
if (record.metadata.rawStacktrace) {
68+
attributes["exception.stacktrace"] = record.metadata.rawStacktrace
69+
}
70+
}
71+
72+
private populateAdditionalAttributes(record: Record, attributes: LogAttributes) {
73+
switch(this.includeFieldsAsAttributes) {
74+
case FieldInclusionMode.AllFields:
75+
for (let key in record.payload) {
76+
attributes[key] = record.payload[key]
77+
}
78+
break;
79+
case FieldInclusionMode.CustomFieldsOnly:
80+
for (let key of record.metadata.customFieldNames) {
81+
if (record.payload[key] !== undefined) {
82+
attributes[key] = record.payload[key]
83+
}
84+
}
85+
break;
86+
case FieldInclusionMode.None:
87+
default:
88+
return;
89+
}
90+
}
91+
}
92+
93+
export enum FieldInclusionMode {
94+
AllFields = "all",
95+
CustomFieldsOnly = "custom-fields",
96+
None = "none"
6197
}

0 commit comments

Comments
 (0)