Skip to content

Commit d8a33eb

Browse files
authored
Add options automator latency as a tag and into the msg (#923)
* Add options automator latency as a tag and into the msg * added a testcase * fields * update latencyy seconds expcted * small change
1 parent 73a26b6 commit d8a33eb

File tree

4 files changed

+105
-9
lines changed

4 files changed

+105
-9
lines changed

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface SentryOptionsResponse {
1919
got_type: string;
2020
expected_type: string;
2121
}[];
22+
latency_seconds?: number;
2223
}
2324

2425
export interface KafkaControlPlaneResponse {

src/webhooks/sentry-options/sentry-options.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Sentry from '@sentry/node';
33
import testAdminPayload from '@test/payloads/sentry-options/test-admin-payload.json';
44
import testBadPayload from '@test/payloads/sentry-options/test-bad-payload.json';
55
import testEmptyPayload from '@test/payloads/sentry-options/test-empty-payload.json';
6+
import testLatencyPayload from '@test/payloads/sentry-options/test-latency-payload.json';
67
import testMegaPayload from '@test/payloads/sentry-options/test-mega-payload.json';
78
import testPartialPayload from '@test/payloads/sentry-options/test-partial-payload.json';
89
import testPayload from '@test/payloads/sentry-options/test-payload.json';
@@ -628,4 +629,63 @@ describe('sentry-options webhook', function () {
628629
},
629630
});
630631
});
632+
633+
it('should include latency_seconds tag when present', async function () {
634+
await sendSentryOptionsUpdatesToDatadog(testLatencyPayload, 1699563828);
635+
expect(datadogApiInstanceSpy).toHaveBeenCalledTimes(2);
636+
637+
// Check the drifted_options message
638+
const driftedMessage = datadogApiInstanceSpy.mock.calls[0][0];
639+
expect(driftedMessage).toEqual({
640+
body: {
641+
dateHappened: 1699563828,
642+
text: '{"change":"drifted_options","option":{"option_name":"drifted_option_1","option_value":"value_1"},"latency_seconds":2.5}',
643+
title: 'Sentry Options Update',
644+
alertType: 'error',
645+
tags: [
646+
'sentry_region:st-test_region',
647+
'source_tool:options-automator',
648+
'source:options-automator',
649+
'source_category:infra-tools',
650+
'option_name:drifted_option_1',
651+
'sentry_user:options-automator',
652+
'latency_seconds:2.5',
653+
],
654+
},
655+
});
656+
657+
const updatedMessage = datadogApiInstanceSpy.mock.calls[1][0];
658+
expect(updatedMessage).toEqual({
659+
body: {
660+
dateHappened: 1699563828,
661+
text: '{"change":"updated_options","option":{"option_name":"updated_option_1","db_value":"db_value_1","value":"new_value_1"},"latency_seconds":2.5}',
662+
title: 'Sentry Options Update',
663+
alertType: 'success',
664+
tags: [
665+
'sentry_region:st-test_region',
666+
'source_tool:options-automator',
667+
'source:options-automator',
668+
'source_category:infra-tools',
669+
'option_name:updated_option_1',
670+
'sentry_user:options-automator',
671+
'latency_seconds:2.5',
672+
],
673+
},
674+
});
675+
});
676+
677+
it('should not include latency_seconds tag when not present', async function () {
678+
await sendSentryOptionsUpdatesToDatadog(testPartialPayload, 1699563828);
679+
expect(datadogApiInstanceSpy).toHaveBeenCalledTimes(2);
680+
681+
const firstMessage = datadogApiInstanceSpy.mock.calls[0][0];
682+
const secondMessage = datadogApiInstanceSpy.mock.calls[1][0];
683+
684+
expect(firstMessage.body.tags).not.toContain(
685+
expect.stringMatching(/^latency_seconds:/)
686+
);
687+
expect(secondMessage.body.tags).not.toContain(
688+
expect.stringMatching(/^latency_seconds:/)
689+
);
690+
});
631691
});

src/webhooks/sentry-options/sentry-options.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,29 +75,44 @@ export async function sendSentryOptionsUpdatesToDatadog(
7575
const region = formatRegionTag(message.region);
7676

7777
for (const optionType in message) {
78-
if (optionType === 'region' || optionType === 'source') continue;
78+
if (
79+
optionType === 'region' ||
80+
optionType === 'source' ||
81+
optionType === 'latency_seconds'
82+
)
83+
continue;
7984
for (const option of message[optionType]) {
8085
const text = {
8186
change: optionType,
8287
option: option,
88+
...(message.latency_seconds !== undefined && {
89+
latency_seconds: message.latency_seconds,
90+
}),
8391
};
8492

8593
const alertType = formatAlertType(optionType);
8694

95+
const tags = [
96+
region,
97+
`source_tool:${message.source}`,
98+
`source:${message.source}`,
99+
`source_category:infra-tools`,
100+
`option_name:${option.option_name}`,
101+
`sentry_user:${message.source}`,
102+
];
103+
104+
// Add latency_seconds as a tag if it exists
105+
if (message.latency_seconds !== undefined) {
106+
tags.push(`latency_seconds:${message.latency_seconds}`);
107+
}
108+
87109
const params: v1.EventCreateRequest = {
88110
title: 'Sentry Options Update',
89111
// TODO(getsentry/eng-pipes#706): Refactor Text Message
90112
text: JSON.stringify(text),
91113
alertType: alertType,
92114
dateHappened: timestamp,
93-
tags: [
94-
region,
95-
`source_tool:${message.source}`,
96-
`source:${message.source}`,
97-
`source_category:infra-tools`,
98-
`option_name:${option.option_name}`,
99-
`sentry_user:${message.source}`,
100-
],
115+
tags: tags,
101116
};
102117
await DATADOG_API_INSTANCE.createEvent({ body: params });
103118
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"region": "test_region",
3+
"source": "options-automator",
4+
"latency_seconds": 2.5,
5+
"drifted_options": [
6+
{"option_name": "drifted_option_1", "option_value": "value_1"}
7+
],
8+
"updated_options": [
9+
{
10+
"option_name": "updated_option_1",
11+
"db_value": "db_value_1",
12+
"value": "new_value_1"
13+
}
14+
],
15+
"set_options": [],
16+
"unset_options": [],
17+
"not_writable_options": [],
18+
"unregistered_options": [],
19+
"invalid_type_options": []
20+
}

0 commit comments

Comments
 (0)