-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example: add manage-monitors.ts for monitor service operations
- Loading branch information
Showing
1 changed file
with
77 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { monitors } from '@axiomhq/js'; | ||
|
||
const client = new monitors.Service({ token: process.env.AXIOM_TOKEN || '' }); | ||
const datasetName = 'test-axiom-js-monitors'; | ||
|
||
async function createMonitor() { | ||
console.log('Creating monitor...'); | ||
const response = await client.create({ | ||
name: 'Example CPU Monitor', | ||
type: 'Threshold', | ||
description: 'Monitor high CPU usage', | ||
aplQuery: `['${datasetName}'] | summarize count() by bin_auto(_time)`, | ||
operator: 'Above', | ||
threshold: 90, | ||
alertOnNoData: true, | ||
notifyByGroup: false, | ||
notifierIDs: [], | ||
intervalMinutes: 5, | ||
rangeMinutes: 10, | ||
}); | ||
console.log(`Created monitor with ID: ${response.id}`); | ||
return response; | ||
} | ||
|
||
async function listMonitors() { | ||
console.log('\nListing all monitors:'); | ||
const monitors = await client.list(); | ||
for (const monitor of monitors) { | ||
console.log(`- ${monitor.name} (${monitor.id}): ${monitor.description}`); | ||
} | ||
return monitors; | ||
} | ||
|
||
async function getMonitor(id: string) { | ||
console.log(`\nGetting monitor ${id}...`); | ||
const response = await client.get(id); | ||
console.log(`Retrieved monitor: ${JSON.stringify(response, null, 2)}`); | ||
return response; | ||
} | ||
|
||
async function updateMonitor(id: string) { | ||
console.log('\nUpdating monitor...'); | ||
const response = await client.update(id, { | ||
name: 'Updated CPU Monitor', | ||
type: 'Threshold', | ||
description: 'Monitor high CPU usage', | ||
aplQuery: `['${datasetName}'] | summarize count() by bin_auto(_time)`, | ||
operator: 'Above', | ||
threshold: 90, | ||
alertOnNoData: true, | ||
notifyByGroup: false, | ||
notifierIDs: [], | ||
intervalMinutes: 5, | ||
|
||
rangeMinutes: 10, | ||
}); | ||
console.log(`Updated monitor name to: ${response.name}`); | ||
return response; | ||
} | ||
|
||
async function deleteMonitor(id: string) { | ||
console.log('\nDeleting monitor...'); | ||
const response = await client.delete(id); | ||
console.log('Monitor deleted successfully'); | ||
return response; | ||
} | ||
|
||
async function main() { | ||
// Create and manage a monitor | ||
const monitor = await createMonitor(); | ||
await listMonitors(); | ||
await getMonitor(monitor.id); | ||
await updateMonitor(monitor.id); | ||
await deleteMonitor(monitor.id); | ||
} | ||
|
||
main().catch(console.error); |