-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loki: use structured metadata rather than a JSON line #1918
Draft
Page-
wants to merge
1
commit into
master
Choose a base branch
from
loki-structured-metadata
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,16 +38,10 @@ import { | |
incrementPublishCallTotal, | ||
} from './metrics.js'; | ||
import { setTimeout } from 'timers/promises'; | ||
import { omitNanoTimestamp } from '../config.js'; | ||
import { requestAsync } from '../../../../infra/request-promise/index.js'; | ||
|
||
const { BadRequestError } = errors; | ||
|
||
interface LokiDeviceLog extends Omit<InternalDeviceLog, 'nanoTimestamp'> { | ||
version?: number; | ||
createdAt?: number; | ||
} | ||
|
||
// invert status object for quick lookup of status identifier using status code | ||
const statusKeys = _.transform( | ||
loki.status, | ||
|
@@ -62,7 +56,6 @@ const lokiIngesterAddress = `${LOKI_INGESTER_HOST}:${LOKI_INGESTER_GRPC_PORT}`; | |
|
||
const MIN_BACKOFF = 100; | ||
const MAX_BACKOFF = 10 * 1000; | ||
const VERSION = 2; | ||
|
||
function createTimestampFromDate(date = new Date()) { | ||
const timestamp = new loki.Timestamp(); | ||
|
@@ -215,21 +208,43 @@ export class LokiBackend implements DeviceLogsBackend { | |
|
||
return _( | ||
body.data.result as Array<{ | ||
stream: { | ||
application_id: string; | ||
device_id: string; | ||
[name: string]: string; | ||
}; | ||
values: Array<[timestamp: string, logLine: string]>; | ||
}>, | ||
) | ||
.flatMap(({ values }) => values) | ||
.map(([timestamp, logLine]): [bigint, OutputDeviceLog] => { | ||
const log: LokiDeviceLog = JSON.parse(logLine); | ||
if (log.version !== VERSION) { | ||
throw new Error( | ||
`Invalid Loki serialization version: ${JSON.stringify(log)}`, | ||
); | ||
.flatMap(({ stream, values }) => { | ||
const baseLog: Partial<OutputDeviceLog> = {}; | ||
for (const [key, value] of Object.entries(stream)) { | ||
switch (key) { | ||
case 'timestamp': | ||
baseLog.timestamp = Number(value); | ||
break; | ||
case 'is_system': | ||
baseLog.isSystem = value === 'true'; | ||
break; | ||
case 'is_stderr': | ||
baseLog.isStdErr = value === 'true'; | ||
break; | ||
case 'service_id': | ||
baseLog.serviceId = Number(value); | ||
break; | ||
} | ||
} | ||
delete log.version; | ||
const nanoTimestamp = BigInt(timestamp); | ||
log.createdAt = Math.floor(Number(nanoTimestamp / 1000000n)); | ||
return [nanoTimestamp, log as OutputDeviceLog]; | ||
return values.map(([timestamp, message]): [bigint, OutputDeviceLog] => { | ||
const nanoTimestamp = BigInt(timestamp); | ||
return [ | ||
nanoTimestamp, | ||
{ | ||
...baseLog, | ||
createdAt: Math.floor(Number(nanoTimestamp / 1000000n)), | ||
message, | ||
} as OutputDeviceLog, | ||
]; | ||
}); | ||
}) | ||
.sortBy(([timestamp]) => timestamp) | ||
.map(([, log]) => log) | ||
|
@@ -238,7 +253,7 @@ export class LokiBackend implements DeviceLogsBackend { | |
|
||
public async publish( | ||
ctx: LogContext, | ||
logs: Array<InternalDeviceLog & { version?: number }>, | ||
logs: InternalDeviceLog[], | ||
): Promise<any> { | ||
const logEntries = this.fromDeviceLogsToEntries(ctx, logs); | ||
|
||
|
@@ -349,12 +364,6 @@ export class LokiBackend implements DeviceLogsBackend { | |
return `o${ctx.orgId}:a${ctx.appId}:d${ctx.id}`; | ||
} | ||
|
||
private getStructuredMetadata(ctx: LogContext): loki.LabelPairAdapter[] { | ||
return [ | ||
new loki.LabelPairAdapter().setName('device_id').setValue(`${ctx.id}`), | ||
]; | ||
} | ||
|
||
private getLabels(ctx: LokiLogContext): string { | ||
return `{fleet_id="${ctx.appId}"}`; | ||
} | ||
|
@@ -364,18 +373,32 @@ export class LokiBackend implements DeviceLogsBackend { | |
): OutputDeviceLog[] { | ||
try { | ||
return stream.getEntriesList().map((entry) => { | ||
const log: LokiDeviceLog = JSON.parse(entry.getLine()); | ||
if (log.version !== VERSION) { | ||
throw new Error( | ||
`Invalid Loki serialization version: ${JSON.stringify(log)}`, | ||
); | ||
} | ||
delete log.version; | ||
const timestampEntry = entry.getTimestamp()!; | ||
const message = entry.getLine(); | ||
const structuredMetadataList = entry.getStructuredmetadataList(); | ||
const timestamp = entry.getTimestamp()!; | ||
const nanoTimestamp = | ||
BigInt(timestampEntry.getSeconds()) * 1000000000n + | ||
BigInt(timestampEntry.getNanos()); | ||
log.createdAt = Math.floor(Number(nanoTimestamp / 1000000n)); | ||
BigInt(timestamp.getSeconds()) * 1000000000n + | ||
BigInt(timestamp.getNanos()); | ||
const log: Partial<OutputDeviceLog> = { | ||
createdAt: Math.floor(Number(nanoTimestamp / 1000000n)), | ||
message, | ||
}; | ||
for (const structuredMetadata of structuredMetadataList) { | ||
switch (structuredMetadata.getName()) { | ||
case 'timestamp': | ||
log.timestamp = Number(structuredMetadata.getValue()); | ||
break; | ||
case 'is_system': | ||
log.isSystem = structuredMetadata.getValue() === 'true'; | ||
break; | ||
case 'is_stderr': | ||
log.isStdErr = structuredMetadata.getValue() === 'true'; | ||
break; | ||
case 'service_id': | ||
log.serviceId = Number(structuredMetadata.getValue()); | ||
break; | ||
} | ||
} | ||
return log as OutputDeviceLog; | ||
}); | ||
} catch (err) { | ||
|
@@ -384,23 +407,36 @@ export class LokiBackend implements DeviceLogsBackend { | |
} | ||
} | ||
|
||
private fromDeviceLogsToEntries( | ||
ctx: LogContext, | ||
logs: Array<InternalDeviceLog & { version?: number }>, | ||
) { | ||
const structuredMetadata = this.getStructuredMetadata(ctx); | ||
private fromDeviceLogsToEntries(ctx: LogContext, logs: InternalDeviceLog[]) { | ||
const deviceId = new loki.LabelPairAdapter() | ||
.setName('device_id') | ||
.setValue(`${ctx.id}`); | ||
return logs.map((log) => { | ||
const timestamp = new loki.Timestamp(); | ||
timestamp.setSeconds(Math.floor(Number(log.nanoTimestamp / 1000000000n))); | ||
timestamp.setNanos(Number(log.nanoTimestamp % 1000000000n)); | ||
// store log line as JSON | ||
const logJson = JSON.stringify( | ||
{ ...log, version: VERSION }, | ||
omitNanoTimestamp, | ||
); | ||
const structuredMetadata = [ | ||
deviceId, | ||
new loki.LabelPairAdapter() | ||
.setName('timestamp') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also questionable as to whether we should store it as structured metadata because every entry for it will be unique and possibly make a bunch of other things more painful and is probably not ergonomic or even useful to query by |
||
.setValue(`${log.timestamp}`), | ||
new loki.LabelPairAdapter() | ||
.setName('is_system') | ||
.setValue(`${log.isSystem}`), | ||
new loki.LabelPairAdapter() | ||
.setName('is_stderr') | ||
.setValue(`${log.isStdErr}`), | ||
]; | ||
if (log.serviceId) { | ||
structuredMetadata.push( | ||
new loki.LabelPairAdapter() | ||
.setName('service_id') | ||
.setValue(`${log.serviceId}`), | ||
); | ||
} | ||
// create entry with labels, line and timestamp | ||
return new loki.EntryAdapter() | ||
.setLine(logJson) | ||
.setLine(log.message) | ||
.setTimestamp(timestamp) | ||
.setStructuredmetadataList(structuredMetadata); | ||
}); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we name this piece of metadata as "device_timestamp" instead to make it clearer what it actually is?