Skip to content

Commit c9022ef

Browse files
authored
Merge pull request #1 from rodmatos/events-api
add support for events api
2 parents 1e57002 + 859ed9a commit c9022ef

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ it's just targetting Deno as a runtime (Typescript, URL imports, fetch, etc).
2222
* For a full example of metrics submission, see `examples/emit-metrics.ts`
2323
* `v1Monitors`: get by id, get all, search by query
2424
* `v1ServiceChecks`: submit 'check run' statuses to Datadog
25+
* `v1Events`: submit events to Datadog
2526
* `v1UsageMetering`: get billable summary, get top custom metrics
2627
* `v2Roles`: list and describe roles & permissions
2728
* `v2Users`: list, search, and describe datadog users

Diff for: mod.ts

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import v1MonitorsApi from "./v1/monitors.ts";
55
import v1ServiceChecksApi from "./v1/service_checks.ts";
66
export {CheckStatus} from './v1/service_checks.ts';
77
import v1UsageMeteringApi from "./v1/usage_metering.ts";
8+
import v1EventsApi from "./v1/events.ts";
89

910
import v2RolesApi from "./v2/roles.ts";
1011
import v2UsersApi from "./v2/users.ts";
@@ -66,6 +67,15 @@ export default class DatadogApi extends ApiClient {
6667
return new v1ServiceChecksApi(this);
6768
}
6869

70+
/**
71+
* The events API allows you to post events to Datadog.
72+
* Event titles are limited to 100 characters.
73+
* Event text are limited to 4000 characters.
74+
*/
75+
get v1Events(): v1EventsApi {
76+
return new v1EventsApi(this);
77+
}
78+
6979
/**
7080
* The usage metering API allows you to get hourly, daily, and monthly usage
7181
* across multiple facets of Datadog.

Diff for: v1/events.ts

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
type TODO = unknown;
2+
3+
// Common API client contract
4+
interface ApiClient {
5+
fetchJson(opts: {
6+
method: "GET" | "POST";
7+
path: string;
8+
query?: URLSearchParams;
9+
body?: unknown;
10+
}): Promise<unknown>;
11+
}
12+
13+
export default class DatadogEventsApi {
14+
#api: ApiClient;
15+
constructor(api: ApiClient) {
16+
this.#api = api;
17+
}
18+
19+
async submit(data: Event): Promise<string> {
20+
const json = await this.#api.fetchJson({
21+
path: `/api/v1/events`,
22+
method: "POST",
23+
body: {
24+
text: data.text,
25+
title: data.title,
26+
aggregation_key: data.aggregation_key,
27+
alert_type: data.alert_type,
28+
date_happened: data.date_happened
29+
? Math.floor(data.date_happened.valueOf() / 1000)
30+
: undefined,
31+
device_name: data.device_name,
32+
host: data.host,
33+
priority: data.priority,
34+
related_event_id: data.related_event_id,
35+
source_type_name: data.source_type_name,
36+
tags: data.tags ?? [],
37+
},
38+
});
39+
return (json as { status: string }).status;
40+
}
41+
}
42+
43+
export interface Event {
44+
/** max length: 4000 **/
45+
text: string;
46+
/** max length: 100 **/
47+
title: string;
48+
aggregation_key?: string;
49+
alert_type?: AlertType;
50+
date_happened?: Date;
51+
device_name?: string;
52+
host?: string;
53+
priority?: "normal" | "low";
54+
related_event_id?: number;
55+
source_type_name?: string;
56+
tags?: Array<string>;
57+
}
58+
59+
export type AlertType =
60+
| "error"
61+
| "warning"
62+
| "info"
63+
| "success"
64+
| "user_update"
65+
| "recommendation"
66+
| "snapshot";

0 commit comments

Comments
 (0)