Skip to content

Commit

Permalink
Loki: Use structured metadata for device ids instead of labels
Browse files Browse the repository at this point in the history
Update loki-grpc-client from 2.0.0 to 2.2.0

Change-type: major
  • Loading branch information
Page- committed Dec 11, 2024
1 parent 8c032a0 commit 6d28d0d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"ipaddr.js": "^2.2.0",
"jsonwebtoken": "^9.0.2",
"lodash": "^4.17.21",
"loki-grpc-client": "^2.0.0",
"loki-grpc-client": "^2.2.0",
"memoizee": "^0.4.17",
"morgan": "^1.10.0",
"ndjson": "^2.0.0",
Expand Down
28 changes: 18 additions & 10 deletions src/features/device-logs/lib/backends/loki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,16 @@ export class LokiBackend implements DeviceLogsBackend {
}

public async publish(
ctx: LogContext,
$ctx: LogContext,
logs: Array<DeviceLog & { version?: number }>,
): Promise<any> {
const ctx = await assertLokiLogContext($ctx);
const countLogs = logs.length;
incrementPublishCallTotal();
incrementPublishLogMessagesTotal(countLogs);
const stream = this.fromDeviceLogsToStream(ctx, logs);
const lokiCtx = await assertLokiLogContext(ctx);
try {
await this.push(lokiCtx.belongs_to__application, stream);
await this.push(ctx.belongs_to__application, stream);
incrementPublishCallSuccessTotal();
} catch (err) {
incrementPublishCallFailedTotal();
Expand Down Expand Up @@ -315,16 +315,22 @@ export class LokiBackend implements DeviceLogsBackend {
call?.cancel();
}

private getDeviceQuery(ctx: LogContext) {
return `{device_id="${ctx.id}"}`;
private getDeviceQuery(ctx: LokiLogContext) {
return `{application_id="${ctx.belongs_to__application}"} | device_id="${ctx.id}"`;
}

private getKey(ctx: LokiLogContext, suffix = 'logs') {
return `app:${ctx.belongs_to__application}:device:${ctx.id}:${suffix}`;
}

private getLabels(ctx: LogContext): string {
return `{device_id="${ctx.id}"}`;
private getStructuredMetadata(ctx: LogContext): loki.LabelPairAdapter[] {
return [
new loki.LabelPairAdapter().setName('device_id').setValue(`${ctx.id}`),
];
}

private getLabels(ctx: LokiLogContext): string {
return `{application_id="${ctx.belongs_to__application}"}`;
}

private validateLog(log: DeviceLog): asserts log is DeviceLog {
Expand All @@ -348,7 +354,7 @@ export class LokiBackend implements DeviceLogsBackend {

private fromStreamToDeviceLogs(stream: loki.StreamAdapter): DeviceLog[] {
try {
return stream.getEntriesList().map((entry: loki.EntryAdapter) => {
return stream.getEntriesList().map((entry) => {
const log = JSON.parse(entry.getLine());
const timestamp = entry.getTimestamp()!;
log.nanoTimestamp =
Expand All @@ -369,7 +375,7 @@ export class LokiBackend implements DeviceLogsBackend {
}

private fromDeviceLogsToStream(
ctx: LogContext,
ctx: LokiLogContext,
logs: Array<DeviceLog & { version?: number }>,
) {
const labels = this.getLabels(ctx);
Expand All @@ -383,10 +389,12 @@ export class LokiBackend implements DeviceLogsBackend {
timestamp.setNanos(Number(log.nanoTimestamp % 1000000000n));
// store log line as JSON
const logJson = JSON.stringify(log, omitNanoTimestamp);
const structuredMetadata = this.getStructuredMetadata(ctx);
// create entry with labels, line and timestamp
const entry = new loki.EntryAdapter()
.setLine(logJson)
.setTimestamp(timestamp);
.setTimestamp(timestamp)
.setStructuredmetadataList(structuredMetadata);
// append entry to stream
stream.addEntries(entry);
}
Expand Down

0 comments on commit 6d28d0d

Please sign in to comment.