Skip to content

Commit b6bdd4c

Browse files
committed
fix: Report more accurate time to APM on heavy used deployments
1 parent c3ca3c4 commit b6bdd4c

File tree

6 files changed

+25
-18
lines changed

6 files changed

+25
-18
lines changed

packages/cubejs-backend-shared/src/track.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import { internalExceptions } from './errors';
55

66
export type BaseEvent = {
77
event: string,
8+
timestamp: string,
89
[key: string]: any,
910
};
1011

11-
export type Event = BaseEvent & {
12+
export type Event = {
1213
id: string,
1314
clientTimestamp: string,
1415
anonymousId: string,
1516
platform: string,
17+
arch: string,
1618
nodeVersion: string,
1719
sentFrom: 'backend';
1820
};
@@ -79,8 +81,8 @@ export async function track(opts: BaseEvent) {
7981

8082
trackEvents.push({
8183
...opts,
84+
clientTimestamp: opts.timestamp || new Date().toJSON(),
8285
id: crypto.randomBytes(16).toString('hex'),
83-
clientTimestamp: new Date().toJSON(),
8486
platform: process.platform,
8587
arch: process.arch,
8688
nodeVersion: process.version,

packages/cubejs-server-core/src/core/DevServer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import jwt from 'jsonwebtoken';
1111
import isDocker from 'is-docker';
1212
import type { Application as ExpressApplication, Request, Response } from 'express';
1313
import type { ChildProcess } from 'child_process';
14-
import { executeCommand, getEnv, keyByDataSource, packageExists } from '@cubejs-backend/shared';
14+
import {executeCommand, getAnonymousId, getEnv, keyByDataSource, packageExists} from '@cubejs-backend/shared';
1515
import crypto from 'crypto';
1616

1717
import type { BaseDriver } from '@cubejs-backend/query-orchestrator';
@@ -115,7 +115,7 @@ export class DevServer {
115115
res.json({
116116
cubejsToken,
117117
basePath: options.basePath,
118-
anonymousId: this.cubejsServer.anonymousId,
118+
anonymousId: getAnonymousId(),
119119
coreServerVersion: this.cubejsServer.coreServerVersion,
120120
dockerVersion: this.options.dockerVersion || null,
121121
projectFingerprint: this.cubejsServer.projectFingerprint,

packages/cubejs-server-core/src/core/agentCollect.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import crypto from 'crypto';
88
import WebSocket from 'ws';
99
import zlib from 'zlib';
1010
import { promisify } from 'util';
11+
import {LoggerFn} from "./types";
1112

1213
const deflate = promisify(zlib.deflate);
1314
interface AgentTransport {
@@ -175,11 +176,11 @@ const clearTransport = () => {
175176
agentInterval = null;
176177
};
177178

178-
export default async (event: Record<string, any>, endpointUrl: string, logger: any) => {
179+
export default async (event: Record<string, any>, endpointUrl: string, logger: LoggerFn) => {
179180
trackEvents.push({
181+
timestamp: new Date().toJSON(),
180182
...event,
181183
id: crypto.randomBytes(16).toString('hex'),
182-
timestamp: new Date().toJSON(),
183184
instanceId: getEnv('instanceId'),
184185
});
185186
lastEvent = new Date();

packages/cubejs-server-core/src/core/server.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
CancelableInterval,
1515
createCancelableInterval,
1616
formatDuration,
17-
getAnonymousId,
1817
getEnv,
1918
assertDataSource,
2019
getRealType,
@@ -59,7 +58,7 @@ import type {
5958
LoggerFn,
6059
DriverConfig,
6160
ScheduledRefreshTimeZonesFn,
62-
ContextToCubeStoreRouterIdFn,
61+
ContextToCubeStoreRouterIdFn, LoggerEventParams,
6362
} from './types';
6463
import {
6564
ContextToOrchestratorIdFn,
@@ -171,8 +170,6 @@ export class CubejsServerCore {
171170

172171
public projectFingerprint: string | null = null;
173172

174-
public anonymousId: string | null = null;
175-
176173
public coreServerVersion: string | null = null;
177174

178175
protected contextAcceptor: ContextAcceptor;
@@ -233,7 +230,7 @@ export class CubejsServerCore {
233230

234231
this.startScheduledRefreshTimer();
235232

236-
this.event = async (name, props) => {
233+
this.event = async (event, props: LoggerEventParams) => {
237234
if (!this.options.telemetry) {
238235
return;
239236
}
@@ -248,15 +245,12 @@ export class CubejsServerCore {
248245
}
249246
}
250247

251-
if (!this.anonymousId) {
252-
this.anonymousId = getAnonymousId();
253-
}
254-
255248
const internalExceptionsEnv = getEnv('internalExceptions');
256249

257250
try {
258251
await track({
259-
event: name,
252+
timestamp: new Date().toJSON(),
253+
event,
260254
projectFingerprint: this.projectFingerprint,
261255
coreServerVersion: this.coreServerVersion,
262256
dockerVersion: getEnv('dockerImageVersion'),
@@ -410,7 +404,12 @@ export class CubejsServerCore {
410404
if (agentEndpointUrl) {
411405
const oldLogger = this.logger;
412406
this.preAgentLogger = oldLogger;
407+
413408
this.logger = (msg, params) => {
409+
// Filling timestamp as much as earlier as we can, otherwise it can be incorrect. Because next code is async
410+
// with await points which can be delayed with Node.js micro-tasking.
411+
params.timestamp = params.timestamp || new Date().toJSON();
412+
414413
oldLogger(msg, params);
415414
agentCollect(
416415
{

packages/cubejs-server-core/src/core/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ export type ExternalDbTypeFn = (context: RequestContext) => DatabaseType;
167167
export type ExternalDriverFactoryFn = (context: RequestContext) => Promise<BaseDriver> | BaseDriver;
168168
export type ExternalDialectFactoryFn = (context: RequestContext) => BaseQuery;
169169

170-
export type LoggerFn = (msg: string, params: Record<string, any>) => void;
170+
export type LoggerEventParams = {
171+
timestamp?: string,
172+
[key: string]: any,
173+
};
174+
export type LoggerFn = (msg: string, params: LoggerEventParams) => void;
171175

172176
export type BiToolSyncConfig = {
173177
type: string;

rust/cubesql/cubesql/src/sql/compiler_cache.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ impl CompilerCache for CompilerCacheImpl {
191191
.get(&(compiler_id, protocol.clone()))
192192
.cloned()
193193
};
194-
// Double checked locking
194+
195+
// Double-checked locking
195196
let cache_entry = if let Some(cache_entry) = cache_entry {
196197
cache_entry
197198
} else {

0 commit comments

Comments
 (0)