Skip to content

Commit 05e8010

Browse files
committed
tweak how logs are processed
1 parent b0e963b commit 05e8010

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/otel.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,28 @@ type ISpan = opentelemetry.proto.trace.v1.ISpan
6868

6969
function mapLogRecord(logRecord: ILogRecord): ISpan {
7070
// console.log(logRecord)
71-
const time = logRecord.observedTimeUnixNano || logRecord.timeUnixNano
71+
const time = logRecord.timeUnixNano || logRecord.observedTimeUnixNano
7272
const attributes = logRecord.attributes || []
73+
attributes.push({ key: 'logfire.span_type', value: { stringValue: 'log' } })
7374
if (logRecord.severityNumber) {
7475
attributes.push({ key: 'logfire.level_num', value: { intValue: logRecord.severityNumber } })
7576
}
7677
if (logRecord.eventName) {
7778
attributes.push({ key: 'log_event_name', value: { stringValue: logRecord.eventName } })
7879
}
79-
let name: string | null = null
80+
if (logRecord.timeUnixNano) {
81+
attributes.push({ key: 'TimeUnixNano', value: { intValue: logRecord.timeUnixNano } })
82+
}
83+
if (logRecord.observedTimeUnixNano) {
84+
attributes.push({ key: 'ObservedTimestampUnixNano', value: { intValue: logRecord.observedTimeUnixNano } })
85+
}
86+
let name: string = 'unknown log'
8087

8188
if (logRecord.body) {
8289
if (logRecord.body.stringValue) {
8390
name = logRecord.body.stringValue
8491
} else {
92+
name = JSON.stringify(logRecord.body)
8593
attributes.push({ key: 'log_body', value: logRecord.body })
8694
}
8795
}
@@ -99,15 +107,25 @@ function mapLogRecord(logRecord: ILogRecord): ISpan {
99107
startTimeUnixNano: time,
100108
endTimeUnixNano: time,
101109
attributes,
102-
name,
110+
name: truncate(name),
103111
droppedAttributesCount: logRecord.droppedAttributesCount,
104112
flags: logRecord.flags,
105113
}
106114
}
107115

108116
/// generate a random traceID or spanID
109-
export function generateRand(bytes: number): Uint8Array {
117+
function generateRand(bytes: number): Uint8Array {
110118
const array = new Uint8Array(bytes)
111119
crypto.getRandomValues(array)
112120
return array
113121
}
122+
123+
const MAX_LENGTH = 120
124+
125+
function truncate(text: string): string {
126+
if (text.length <= MAX_LENGTH) {
127+
return text
128+
} else {
129+
return text.slice(0, MAX_LENGTH) + '…'
130+
}
131+
}

0 commit comments

Comments
 (0)