Skip to content

Commit 7d5496c

Browse files
committed
👷 replace monitor checks by logs query
1 parent 3f3aac3 commit 7d5496c

File tree

2 files changed

+44
-43
lines changed

2 files changed

+44
-43
lines changed

‎scripts/deploy/check-monitors.ts‎

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,66 @@
55
*/
66
import { printLog, runMain, fetchHandlingError } from '../lib/executionUtils.ts'
77
import { getTelemetryOrgApiKey, getTelemetryOrgApplicationKey } from '../lib/secrets.ts'
8-
import { monitorIdsByDatacenter, siteByDatacenter } from '../lib/datacenter.ts'
9-
10-
interface MonitorStatus {
11-
id: number
12-
name: string
13-
overall_state: string
14-
}
8+
import { siteByDatacenter } from '../lib/datacenter.ts'
9+
import { browserSdkVersion } from '../lib/browserSdkVersion.ts'
1510

1611
const datacenters = process.argv[2].split(',')
1712

1813
runMain(async () => {
1914
for (const datacenter of datacenters) {
20-
if (!monitorIdsByDatacenter[datacenter]) {
21-
printLog(`No monitors configured for datacenter ${datacenter}`)
15+
const site = siteByDatacenter[datacenter]
16+
const apiKey = getTelemetryOrgApiKey(site)
17+
const applicationKey = getTelemetryOrgApplicationKey(site)
18+
19+
if (!apiKey || !applicationKey) {
20+
printLog(`No API key or application key found for ${site}, skipping...`)
2221
continue
2322
}
24-
const monitorIds = monitorIdsByDatacenter[datacenter]
25-
const site = siteByDatacenter[datacenter]
26-
const monitorStatuses = await Promise.all(monitorIds.map((monitorId) => fetchMonitorStatus(site, monitorId)))
27-
for (const monitorStatus of monitorStatuses) {
28-
printLog(`${monitorStatus.overall_state} - ${monitorStatus.name}`)
29-
if (monitorStatus.overall_state !== 'OK') {
30-
throw new Error(
31-
`Monitor ${monitorStatus.name} is in state ${monitorStatus.overall_state}, see ${computeMonitorLink(site, monitorStatus.id)}`
32-
)
33-
}
23+
24+
const errorLogsCount = await queryErrorLogsCount(site, apiKey, applicationKey)
25+
26+
if (errorLogsCount > 0) {
27+
throw new Error(`Errors found in the last 30 minutes,
28+
see ${computeMonitorLink(site)}`)
29+
} else {
30+
printLog(`No errors found in the last 30 minutes for ${datacenter}`)
3431
}
3532
}
3633
})
3734

38-
async function fetchMonitorStatus(site: string, monitorId: number): Promise<MonitorStatus> {
39-
const response = await fetchHandlingError(`https://api.${site}/api/v1/monitor/${monitorId}`, {
40-
method: 'GET',
35+
async function queryErrorLogsCount(site: string, apiKey: string, applicationKey: string): Promise<number> {
36+
const response = await fetchHandlingError(`https://api.${site}/api/v2/logs/events/search`, {
37+
method: 'POST',
4138
headers: {
42-
Accept: 'application/json',
43-
'DD-API-KEY': getTelemetryOrgApiKey(site),
44-
'DD-APPLICATION-KEY': getTelemetryOrgApplicationKey(site),
39+
'Content-Type': 'application/json',
40+
'DD-API-KEY': apiKey,
41+
'DD-APPLICATION-KEY': applicationKey,
4542
},
43+
body: JSON.stringify({
44+
filter: {
45+
from: 'now-30m',
46+
to: 'now',
47+
query: `source:browser status:error version:${browserSdkVersion}`,
48+
},
49+
}),
4650
})
47-
return response.json() as Promise<MonitorStatus>
51+
52+
const data = (await response.json()) as { data: unknown[] }
53+
54+
return data.data.length
4855
}
4956

50-
function computeMonitorLink(site: string, monitorId: number): string {
51-
return `https://${computeTelemetryOrgDomain(site)}/monitors/${monitorId}`
57+
function computeMonitorLink(site: string): string {
58+
const now = Date.now()
59+
const thirtyMinutesAgo = now - 30 * 60 * 1000
60+
61+
const queryParams = new URLSearchParams({
62+
query: `source:browser status:error version:${browserSdkVersion}`,
63+
from_ts: `${thirtyMinutesAgo}`,
64+
to_ts: `${now}`,
65+
})
66+
67+
return `https://${computeTelemetryOrgDomain(site)}/logs?${queryParams.toString()}`
5268
}
5369

5470
function computeTelemetryOrgDomain(site: string): string {

‎scripts/lib/datacenter.ts‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,3 @@ export const siteByDatacenter: Record<string, string> = {
77
ap2: 'ap2.datadoghq.com',
88
prtest00: 'prtest00.datad0g.com',
99
}
10-
11-
/**
12-
* Each datacenter has 3 monitor IDs:
13-
* - Telemetry errors
14-
* - Telemetry errors on specific org
15-
* - Telemetry errors on specific message
16-
*/
17-
export const monitorIdsByDatacenter: Record<string, [number, number, number]> = {
18-
us1: [72055549, 68975047, 110519972],
19-
eu1: [5855803, 5663834, 9896387],
20-
us3: [164368, 160677, 329066],
21-
us5: [22388, 20646, 96049],
22-
ap1: [858, 859, 2757030],
23-
ap2: [1234, 1235, 1236],
24-
}

0 commit comments

Comments
 (0)