-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatadog.ts
688 lines (618 loc) · 17.3 KB
/
datadog.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
import { exec } from "child_process";
import { promisify } from "util";
import type { WorkerManager } from "@workers/manager";
import metrics from "datadog-metrics";
// Types definitions
interface MemberThresholds {
creategroup: number;
creategroupbyidentifiers: number;
sendgroupmessage: number;
syncgroup: number;
updategroupname: number;
removemembers: number;
addmembers: number;
receivegroupmessage: number;
[key: string]: number;
}
interface ThresholdsData {
core: {
[key: string]: number;
};
network: {
[key: string]: number;
};
memberBasedThresholds: {
[memberCount: string]: MemberThresholds;
};
regionMultipliers: {
[region: string]: number;
};
GEO_TO_COUNTRY_CODE: {
[region: string]: string;
};
reliability: number;
}
interface NetworkStats {
"DNS Lookup": number;
"TCP Connection": number;
"TLS Handshake": number;
Processing: number;
"Server Call": number;
}
interface MetricData {
values: number[];
threshold: number;
members?: string;
}
interface ParsedTestName {
metricName: string;
metricDescription: string;
testNameExtracted: string;
operationType: string;
operationName: string;
members: string;
}
type OperationType = "core" | "group" | "network";
type MetricSubType = "stream" | "poll" | "recovery";
type MetricType = "delivery" | "order";
// Constants
const THRESHOLDS: ThresholdsData = {
core: {
clientcreate: 350,
inboxstate: 350,
newdm: 350,
newdmwithidentifiers: 350,
sendgm: 200,
receivegm: 200,
creategroup: 350,
creategroupbyidentifiers: 350,
syncgroup: 200,
updategroupname: 200,
removemembers: 250,
sendgroupmessage: 200,
receivegroupmessage: 200,
},
network: {
dns_lookup: 100,
tcp_connection: 150,
tls_handshake: 250,
processing: 100,
server_call: 350,
},
memberBasedThresholds: {
"50": {
creategroup: 350,
creategroupbyidentifiers: 350,
receivegroupmessage: 350,
sendgroupmessage: 200,
syncgroup: 200,
updategroupname: 200,
removemembers: 250,
addmembers: 200,
},
"100": {
creategroup: 400,
creategroupbyidentifiers: 400,
receivegroupmessage: 400,
sendgroupmessage: 200,
syncgroup: 200,
updategroupname: 200,
removemembers: 300,
addmembers: 200,
},
"150": {
creategroup: 500,
creategroupbyidentifiers: 500,
receivegroupmessage: 500,
sendgroupmessage: 200,
syncgroup: 200,
updategroupname: 200,
removemembers: 300,
addmembers: 200,
},
"200": {
creategroup: 700,
creategroupbyidentifiers: 700,
receivegroupmessage: 700,
sendgroupmessage: 200,
syncgroup: 200,
updategroupname: 200,
removemembers: 300,
addmembers: 200,
},
"250": {
creategroup: 900,
creategroupbyidentifiers: 900,
receivegroupmessage: 900,
sendgroupmessage: 200,
syncgroup: 200,
updategroupname: 200,
removemembers: 400,
addmembers: 200,
},
"300": {
creategroup: 1100,
creategroupbyidentifiers: 1100,
receivegroupmessage: 1100,
sendgroupmessage: 200,
syncgroup: 200,
updategroupname: 200,
removemembers: 400,
addmembers: 200,
},
"350": {
creategroup: 1300,
creategroupbyidentifiers: 1300,
receivegroupmessage: 1300,
sendgroupmessage: 200,
syncgroup: 200,
updategroupname: 200,
removemembers: 400,
addmembers: 200,
},
"400": {
creategroup: 1500,
creategroupbyidentifiers: 1500,
receivegroupmessage: 1500,
sendgroupmessage: 200,
syncgroup: 200,
updategroupname: 200,
removemembers: 500,
addmembers: 200,
},
},
regionMultipliers: {
"us-east": 1,
"us-west": 1,
europe: 1,
asia: 1.5,
"south-america": 3,
},
GEO_TO_COUNTRY_CODE: {
"us-east": "US",
"us-west": "US",
europe: "FR",
asia: "JP",
"south-america": "BR",
},
reliability: 99.9,
};
// Global state with proper initialization
const state = {
isInitialized: false,
currentGeo: "",
collectedMetrics: {} as Record<string, MetricData>,
};
// Helper functions
/**
* Find the appropriate member bucket for a given member count
*/
function findMemberBucket(members: number): string {
if (members <= 0) return "0";
const memberBuckets = Object.keys(THRESHOLDS.memberBasedThresholds)
.map(Number)
.sort((a, b) => a - b);
// Find highest bucket <= member count
let applicableBucket = "0";
for (const bucket of memberBuckets) {
if (members >= bucket) {
applicableBucket = bucket.toString();
} else {
break;
}
}
return applicableBucket;
}
/**
* Get the appropriate threshold for an operation based on type, member count, and region
*/
export function getThresholdForOperation(
operation: string,
operationType: OperationType,
members: number = 0,
region: string = "us-east",
): number {
// Normalize inputs
const operationLower = operation.toLowerCase();
const regionNormalized = region.toLowerCase().trim();
const regionMultiplier =
THRESHOLDS.regionMultipliers[
regionNormalized as keyof typeof THRESHOLDS.regionMultipliers
] || 1.0;
let baseThreshold = 0;
if (operationType === "network") {
const networkThreshold =
THRESHOLDS.network[operationLower as keyof typeof THRESHOLDS.network];
baseThreshold =
typeof networkThreshold === "number"
? networkThreshold
: THRESHOLDS.network.server_call;
} else if (operationType === "core") {
baseThreshold =
THRESHOLDS.core[operationLower as keyof typeof THRESHOLDS.core] || 0;
} else if (operationType === "group" && members > 0) {
const applicableBucket = findMemberBucket(members);
const memberThresholds =
THRESHOLDS.memberBasedThresholds[
applicableBucket as keyof typeof THRESHOLDS.memberBasedThresholds
];
if (memberThresholds && operationLower in memberThresholds) {
baseThreshold =
memberThresholds[operationLower as keyof typeof memberThresholds];
// console.log(
// `Operation: ${operation}, members: ${members}, bucket: ${applicableBucket}, threshold: ${baseThreshold}`,
// );
} else {
baseThreshold =
THRESHOLDS.core[operationLower as keyof typeof THRESHOLDS.core] || 0;
console.log(
`Operation: ${operation}, members: ${members}, using core threshold: ${baseThreshold}`,
);
}
} else {
baseThreshold =
THRESHOLDS.core[operationLower as keyof typeof THRESHOLDS.core] || 0;
}
return Math.round(baseThreshold * regionMultiplier);
}
/**
* Calculate average of numeric values
*/
export function calculateAverage(values: number[]): number {
if (values.length === 0) return 0;
return values.reduce((sum, val) => sum + val, 0) / values.length;
}
/**
* Group metrics by operation name and member count
*/
export function groupMetricsByOperation(
metrics: [string, MetricData][],
): Map<
string,
{ operationName: string; members: string; operationData: MetricData }
> {
const groups = new Map();
for (const [operation, data] of metrics) {
// Use the operation parsing logic from parseTestName for consistency
const parts = operation.split(":");
const operationPart = parts[0];
const dashMatch = operationPart.match(/^([a-zA-Z]+)-(\d+)$/);
const operationName = dashMatch ? dashMatch[1] : operationPart;
const memberCount = dashMatch ? dashMatch[2] : data.members || "-";
const groupKey = `${operationName}-${memberCount}`;
if (!groups.has(groupKey)) {
groups.set(groupKey, {
operationName,
members: memberCount,
operationData: null,
});
}
groups.get(groupKey).operationData = data;
}
return groups as Map<
string,
{ operationName: string; members: string; operationData: MetricData }
>;
}
/**
* Get country code from geolocation
*/
function getCountryCodeFromGeo(geolocation: string): string {
return (
THRESHOLDS.GEO_TO_COUNTRY_CODE[
geolocation as keyof typeof THRESHOLDS.GEO_TO_COUNTRY_CODE
] || "US"
);
}
// DataDog integration
/**
* Initialize DataDog metrics reporting
*/
export function initDataDog(
testName: string,
envValue: string,
geolocation: string,
apiKey: string,
): boolean {
if (state.isInitialized) {
return true;
}
if (!testName.includes("ts_")) {
return true;
}
if (!apiKey) {
console.warn("⚠️ DATADOG_API_KEY not found. Metrics will not be sent.");
return false;
}
try {
const countryCode = getCountryCodeFromGeo(geolocation);
state.currentGeo = geolocation;
const initConfig = {
apiKey: apiKey,
defaultTags: [
`env:${envValue}`,
`test:${testName}`,
`region:${geolocation}`,
`country_iso_code:${countryCode}`,
],
};
metrics.init(initConfig);
state.isInitialized = true;
return true;
} catch (error) {
console.error("❌ Failed to initialize DataDog metrics:", error);
return false;
}
}
/**
* Send a metric to DataDog and collect for summary
*/
export function sendMetric(
metricName: string,
metricValue: number,
tags: Record<string, string>,
): void {
if (!state.isInitialized) return;
try {
const fullMetricName = `xmtp.sdk.${metricName}`;
const allTags = Object.entries({ ...tags }).map(
([key, value]) => `${key}:${String(value)}`,
);
// Add environment tag if not already present
if (!allTags.some((tag) => tag.startsWith("env:"))) {
allTags.push(`env:${process.env.XMTP_ENV as string}`);
}
// Add version tag if not already present
if (!allTags.some((tag) => tag.startsWith("xv:"))) {
allTags.push(`vm:${process.env.RAILWAY_SERVICE_ID || "unknown"}`);
}
// Create a distinctive operation key that properly includes member count
// Format: operation_name-member_count (e.g., "createGroup-10")
const memberCount = tags.members || "";
const operationKey = tags.operation
? `${tags.operation}${memberCount ? `-${memberCount}` : ""}`
: metricName;
if (!state.collectedMetrics[operationKey]) {
state.collectedMetrics[operationKey] = {
values: [],
threshold: Number(tags.threshold) || 0,
members: memberCount,
};
}
state.collectedMetrics[operationKey].values.push(metricValue);
metrics.gauge(fullMetricName, Math.round(metricValue), allTags);
} catch (error) {
console.error(
`❌ Error sending metric '${metricName}':`,
error instanceof Error ? error.message : String(error),
);
}
}
/**
* Send test results metrics
*/
export function sendTestResults(hasFailures: boolean, testName: string): void {
if (!state.isInitialized) {
console.warn("Datadog metrics not initialized");
return;
}
try {
const metricValue = hasFailures ? 0 : 1;
sendMetric("workflow", metricValue, {
workflow: testName,
metric_type: "workflow",
});
} catch (error) {
console.error("Error reporting to Datadog:", error);
}
}
// Performance tracking
/**
* Send performance metrics for tests
*/
export const sendPerformanceResult = (
expect: any,
workers: WorkerManager,
start: number,
) => {
const testName = expect.getState().currentTestName;
if (testName) {
console.timeEnd(testName as string);
expect(workers.getWorkers()).toBeDefined();
expect(workers.getWorkers().length).toBeGreaterThan(0);
void sendPerformanceMetric(
performance.now() - start,
testName as string,
workers.getVersion(),
false,
);
}
};
/**
* Extract operation details from test name
*/
export function parseTestName(testName: string): ParsedTestName {
const metricNameParts = testName.split(":")[0];
const metricName = metricNameParts.replaceAll(" > ", ".");
const metricDescription = testName.split(":")[1] || "";
const operationParts = metricName.split(".");
const testNameExtracted = operationParts[0];
// Extract operation name and member count
let operationName = "";
let members = "";
if (operationParts[1]) {
// Check formats: "operation-10" and "operation10"
const dashMatch = operationParts[1].match(/^([a-zA-Z]+)-(\d+)$/);
const noSeparatorMatch = operationParts[1].match(/^([a-zA-Z]+)(\d+)$/);
if (dashMatch) {
operationName = dashMatch[1];
members = dashMatch[2];
} else if (noSeparatorMatch) {
operationName = noSeparatorMatch[1];
members = noSeparatorMatch[2];
} else {
operationName = operationParts[1];
}
}
const operationType = parseInt(members) >= 5 ? "group" : "core";
return {
metricName,
metricDescription,
testNameExtracted,
operationType,
operationName,
members,
};
}
/**
* Send detailed performance metrics
*/
export async function sendPerformanceMetric(
metricValue: number,
testName: string,
libXmtpVersion: string,
skipNetworkStats: boolean = false,
): Promise<void> {
if (!state.isInitialized) return;
try {
const {
metricDescription,
testNameExtracted,
operationType,
operationName,
members,
} = parseTestName(testName);
const threshold = getThresholdForOperation(
operationName,
operationType as OperationType,
parseInt(members) || 0,
state.currentGeo,
);
const isSuccess = metricValue <= threshold;
sendMetric("duration", metricValue, {
libxmtp: libXmtpVersion,
operation: operationName,
test: testNameExtracted,
metric_type: "operation",
metric_subtype: operationType,
description: metricDescription,
members: members,
success: isSuccess.toString(),
threshold: threshold.toString(),
region: state.currentGeo,
});
// Network stats handling
if (!skipNetworkStats) {
const networkStats = await getNetworkStats();
const countryCode = getCountryCodeFromGeo(state.currentGeo);
for (const [statName, statValue] of Object.entries(networkStats)) {
const networkMetricValue = Math.round(statValue * 1000);
const networkPhase = statName.toLowerCase().replace(/\s+/g, "_");
const networkThreshold = getThresholdForOperation(
networkPhase,
"network",
parseInt(members) || 0,
state.currentGeo,
);
sendMetric("duration", networkMetricValue, {
libxmtp: libXmtpVersion,
operation: operationName,
test: testNameExtracted,
metric_type: "network",
network_phase: networkPhase,
country_iso_code: countryCode,
members: members,
success: networkMetricValue <= networkThreshold ? "true" : "false",
threshold: networkThreshold.toString(),
region: state.currentGeo,
});
}
}
} catch (error) {
console.error(
`❌ Error sending performance metric for '${testName}':`,
error,
);
}
}
/**
* Send delivery reliability metrics
*/
export function sendDeliveryMetric(
metricValue: number,
sdkVersion: string,
libXmtpVersion: string,
testName: string,
metricSubType: MetricSubType,
metricType: MetricType,
): void {
const threshold = THRESHOLDS.reliability;
const isSuccess = metricValue >= threshold;
sendMetric(metricType, Math.round(metricValue), {
libxmtp: libXmtpVersion,
sdk: sdkVersion,
test: testName,
metric_type: metricType,
metric_subtype: metricSubType,
success: isSuccess.toString(),
threshold: threshold.toString(),
});
}
// Network performance
const execAsync = promisify(exec);
/**
* Measure network performance to an endpoint
*/
export async function getNetworkStats(
endpoint = "https://grpc.dev.xmtp.network:443",
): Promise<NetworkStats> {
const curlCommand = `curl -s -w "\\n{\\"DNS Lookup\\": %{time_namelookup}, \\"TCP Connection\\": %{time_connect}, \\"TLS Handshake\\": %{time_appconnect}, \\"Server Call\\": %{time_starttransfer}}" -o /dev/null --max-time 10 ${endpoint}`;
let stdout: string;
try {
const result = await execAsync(curlCommand);
stdout = result.stdout;
} catch (error: unknown) {
// Even if curl returns an error, we might still have useful stdout
if (error instanceof Error && "stdout" in error) {
stdout = error.stdout as string;
console.warn(
`⚠️ Curl command returned error code ${String(error)}, but stdout is available.`,
);
} else {
console.error(`❌ Curl command failed without stdout:`, error);
throw error; // rethrow if no stdout is available
}
}
// Parse the JSON response
const stats = JSON.parse(stdout.trim()) as NetworkStats;
// Handle the case where Server Call time is 0
if (stats["Server Call"] === 0) {
console.warn(
`Network request to ${endpoint} returned Server Call time of 0.`,
);
// Use a reasonable estimate
stats["Server Call"] = stats["TLS Handshake"] + 0.1;
}
// Calculate processing time
stats["Processing"] = stats["Server Call"] - stats["TLS Handshake"];
// Ensure processing time is not negative
if (stats["Processing"] < 0) {
stats["Processing"] = 0;
}
return stats;
}
// Reporting and summary
/**
* Flush all metrics and generate summary report
*/
export function flushMetrics(): Promise<void> {
return new Promise<void>((resolve) => {
if (!state.isInitialized) {
resolve();
return;
}
void metrics.flush().then(() => {
resolve();
});
});
}