Skip to content

Commit ae5a7f1

Browse files
authored
Add response duration tracking to webhook message attempts (#1877)
1 parent e7733ee commit ae5a7f1

File tree

6 files changed

+29
-15
lines changed

6 files changed

+29
-15
lines changed

Diff for: server/openapi.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1520,7 +1520,7 @@
15201520
"type": "string"
15211521
},
15221522
"responseDurationMs": {
1523-
"description": "Response duration in milliseconds.\n\nCurrently, this is not actually recorded in the OSS version of Svix, so this field always has a value of `0`.",
1523+
"description": "Response duration in milliseconds.",
15241524
"format": "int64",
15251525
"type": "integer"
15261526
},
@@ -1706,7 +1706,7 @@
17061706
"type": "string"
17071707
},
17081708
"responseDurationMs": {
1709-
"description": "Response duration in milliseconds.\n\nCurrently, this is not actually recorded in the OSS version of Svix, so this field always has a value of `0`.",
1709+
"description": "Response duration in milliseconds.",
17101710
"format": "int64",
17111711
"type": "integer"
17121712
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Remove response_duration_ms column from messageattempt table
2+
ALTER TABLE messageattempt DROP COLUMN response_duration_ms;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Add response_duration_ms column to messageattempt table
2+
ALTER TABLE messageattempt ADD COLUMN response_duration_ms BIGINT NOT NULL DEFAULT 0;

Diff for: server/svix-server/src/db/models/messageattempt.rs

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ pub struct Model {
2525
pub response: String,
2626
pub ended_at: Option<DateTimeWithTimeZone>,
2727
pub trigger_type: MessageAttemptTriggerType,
28+
/// Response duration in milliseconds
29+
pub response_duration_ms: i64,
2830
}
2931

3032
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]

Diff for: server/svix-server/src/v1/endpoints/attempt.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ pub struct MessageAttemptOut {
6666
#[schemars(example = "example_status_code")]
6767
pub response_status_code: i16,
6868
/// Response duration in milliseconds.
69-
///
70-
/// Currently, this is not actually recorded in the OSS version of Svix, so
71-
/// this field always has a value of `0`.
7269
pub response_duration_ms: i64,
7370
pub status: MessageStatus,
7471
pub trigger_type: MessageAttemptTriggerType,
@@ -88,7 +85,7 @@ impl From<messageattempt::Model> for MessageAttemptOut {
8885
url: model.url,
8986
response: model.response,
9087
response_status_code: model.response_status_code,
91-
response_duration_ms: 0,
88+
response_duration_ms: model.response_duration_ms,
9289
status: model.status,
9390
trigger_type: model.trigger_type,
9491
msg_id: model.msg_id,

Diff for: server/svix-server/src/worker.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,15 @@ async fn make_http_call(
352352
url: Set(endp.url.clone()),
353353
ended_at: Set(Some(Utc::now().into())),
354354
trigger_type: Set(msg_task.trigger_type),
355+
response_duration_ms: Set(0), // Default to 0, will be updated after the request
355356
..Default::default()
356357
};
357358

358359
match client.execute(req).await {
359360
Ok(res) => {
361+
// Calculate the duration in milliseconds
362+
let duration_ms = (Utc::now() - created_at).num_milliseconds();
363+
360364
let status_code = res.status().as_u16() as i16;
361365
let status = if res.status().is_success() {
362366
MessageStatus::Success
@@ -386,6 +390,7 @@ async fn make_http_call(
386390
response_status_code: Set(status_code),
387391
response: Set(body),
388392
status: Set(status),
393+
response_duration_ms: Set(duration_ms),
389394
..attempt
390395
};
391396

@@ -397,15 +402,21 @@ async fn make_http_call(
397402
None => Ok(CompletedDispatch::Successful(SuccessfulDispatch(attempt))),
398403
}
399404
}
400-
Err(err) => Ok(CompletedDispatch::Failed(FailedDispatch(
401-
messageattempt::ActiveModel {
402-
response_status_code: Set(0),
403-
response: Set(err.to_string()),
404-
status: Set(MessageStatus::Fail),
405-
..attempt
406-
},
407-
err.into(),
408-
))),
405+
Err(err) => {
406+
// For errors, we still calculate the duration
407+
let duration_ms = (Utc::now() - created_at).num_milliseconds();
408+
409+
Ok(CompletedDispatch::Failed(FailedDispatch(
410+
messageattempt::ActiveModel {
411+
response_status_code: Set(0),
412+
response: Set(err.to_string()),
413+
status: Set(MessageStatus::Fail),
414+
response_duration_ms: Set(duration_ms),
415+
..attempt
416+
},
417+
err.into(),
418+
)))
419+
}
409420
}
410421
}
411422

0 commit comments

Comments
 (0)