Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(personhog): label merge calls by type and saga outcomes by stage (#117405)

Nick Best505c3ba7148d
diff --git a/rust/personhog-identity/src/lifecycle/engine.rs b/rust/personhog-identity/src/lifecycle/engine.rs
index 65ae814c2ac3..5befd3720a81 100644
--- a/rust/personhog-identity/src/lifecycle/engine.rs
+++ b/rust/personhog-identity/src/lifecycle/engine.rs
@@ -428,7 +428,10 @@ impl Engine {
// reports its full lifetime, not the last drive's.
common_metrics::histogram(
OP_DURATION_MS,
- &[("op_type".to_string(), row.op_type.clone())],
+ &[
+ ("op_type".to_string(), row.op_type.clone()),
+ ("final_step".to_string(), row.step.clone()),
+ ],
(completed_at - row.created_at).num_milliseconds() as f64,
);
}
diff --git a/rust/personhog-identity/src/lifecycle/merge.rs b/rust/personhog-identity/src/lifecycle/merge.rs
index 425e37f03ea4..01d826c304b6 100644
--- a/rust/personhog-identity/src/lifecycle/merge.rs
+++ b/rust/personhog-identity/src/lifecycle/merge.rs
@@ -258,11 +258,15 @@ fn record_transition(from: &str, to: &str) {
/// Count settled per-source outcomes. Shared by the saga's terminal record
/// and the entrance's inline settlement, so the counter covers every
-/// requested source — not just the ones that entered the saga.
-pub(crate) fn record_outcome_count(outcome: &str, count: u64) {
+/// requested source — not just the ones that entered the saga. `stage` is
+/// `inline` or the step the op settled from.
+pub(crate) fn record_outcome_count(stage: &'static str, outcome: &str, count: u64) {
common_metrics::inc(
OUTCOMES_TOTAL,
- &[("outcome".to_string(), outcome.to_string())],
+ &[
+ ("stage".to_string(), stage.to_string()),
+ ("outcome".to_string(), outcome.to_string()),
+ ],
count,
);
}
@@ -290,7 +294,7 @@ pub(crate) fn record_conflict(reason: &str, count: u64) {
}
}
-fn record_outcomes(outcome: &Value) {
+fn record_outcomes(stage: &'static str, outcome: &Value) {
let Ok(parsed) = serde_json::from_value::<MergeOutcome>(outcome.clone()) else {
return;
};
@@ -305,7 +309,7 @@ fn record_outcomes(outcome: &Value) {
] {
let count = parsed.results.iter().filter(|r| r.outcome == label).count();
if count > 0 {
- record_outcome_count(label, count as u64);
+ record_outcome_count(stage, label, count as u64);
}
}
}
@@ -995,7 +999,7 @@ async fn abort_in_claim_tx(
tx.commit().await?;
record_conflicts(conflicts);
record_transition(MergeStep::Started.as_str(), STEP_ABORTED);
- record_outcomes(&outcome);
+ record_outcomes(MergeStep::Started.as_str(), &outcome);
Ok(())
}
@@ -1124,7 +1128,7 @@ impl MergeDriver {
}
tx.commit().await?;
record_transition(MergeStep::Claimed.as_str(), STEP_ABORTED);
- record_outcomes(&outcome);
+ record_outcomes(MergeStep::Claimed.as_str(), &outcome);
return Ok(());
}
@@ -1221,7 +1225,7 @@ impl MergeDriver {
)],
1,
);
- record_outcomes(&outcome);
+ record_outcomes(from_step.as_str(), &outcome);
// Releases only after the completing CAS proves we still owned the
// op; ghosts left by a crash here are healed lazily by the leader.
self.release_fences(op, &pairs).await?;
@@ -1942,7 +1946,7 @@ impl MergeDriver {
}
tx.commit().await?;
record_transition(MergeStep::Flipped.as_str(), STEP_COMPLETED);
- record_outcomes(&outcome);
+ record_outcomes(MergeStep::Flipped.as_str(), &outcome);
Ok(())
}
}
diff --git a/rust/personhog-identity/src/service/merge.rs b/rust/personhog-identity/src/service/merge.rs
index 2169d453b6d5..5b54db98f392 100644
--- a/rust/personhog-identity/src/service/merge.rs
+++ b/rust/personhog-identity/src/service/merge.rs
@@ -21,7 +21,7 @@ use personhog_proto::personhog::identity::v1::{
use personhog_proto::personhog::types::v1::{Person as ProtoPerson, UpdatePersonPropertiesRequest};
use crate::leader::{PersonStateReader, PropertyWriter};
-use crate::lifecycle::engine::OpRow;
+use crate::lifecycle::engine::{OpRow, STEP_COMPLETED};
use crate::lifecycle::merge::{
record_conflict, record_outcome_count, MergeOpExecutor, MergeOutcome, MergeRequest,
MergeSourceEntry, MergeSourceRecord, OP_TYPE_MERGE, OUTCOME_ERROR, OUTCOME_MERGED,
@@ -39,6 +39,127 @@ const OUTCOME_ATTACHED: &str = "attached";
const MERGE_SOURCES_PER_CALL: &str = "personhog_identity_merge_sources_per_call";
const MERGE_PHASE_DURATION: &str = "personhog_identity_merge_phase_duration_ms";
+const MERGE_CALLS_TOTAL: &str = "personhog_identity_merge_calls_total";
+const MERGE_CALL_DURATION: &str = "personhog_identity_merge_call_duration_ms";
+
+const PATH_UNCLASSIFIED: &str = "unclassified";
+const PATH_INLINE: &str = "inline";
+const PATH_SAGA: &str = "saga";
+const PATH_RESUMED: &str = "resumed";
+const RESULT_SETTLED: &str = "settled";
+const RESULT_COMPLETED: &str = "completed";
+const RESULT_ABORTED: &str = "aborted";
+const RESULT_ABORTED_CLAIM: &str = "aborted_claim";
+const RESULT_ERROR: &str = "error";
+const RESULT_CANCELLED: &str = "cancelled";
+
+/// `Adopted`: establishment attached the target distinct id to a source's person.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum TargetOrigin {
+ Existing,
+ Born,
+ Adopted,
+}
+
+impl TargetOrigin {
+ fn as_str(self) -> &'static str {
+ match self {
+ TargetOrigin::Existing => "existing",
+ TargetOrigin::Born => "born",
+ TargetOrigin::Adopted => "adopted",
+ }
+ }
+}
+
+/// Labels of the per-call merge metrics; the values are a dashboard contract.
+struct MergeShape {
+ path: &'static str,
+ target: &'static str,
+ sources: &'static str,
+ mode: &'static str,
+ result: &'static str,
+}
+
+/// Records the call once, on `finish` or on drop. A caller deadline or
+/// disconnect drops the handler future mid-call, and those calls are the
+/// slow ones, so they are recorded as `cancelled` rather than lost.
+struct MergeCall {
+ shape: MergeShape,
+ start: Instant,
+ recorded: bool,
+}
+
+impl MergeCall {
+ fn start(request: &MergePersonsRequest) -> Self {
+ Self {
+ shape: MergeShape {
+ path: PATH_UNCLASSIFIED,
+ target: "unresolved",
+ sources: if request.sources.len() > 1 {
+ "multi"
+ } else {
+ "single"
+ },
+ mode: if request.allow_identified_sources {
+ "dangerous"
+ } else {
+ "identify"
+ },
+ result: RESULT_ERROR,
+ },
+ start: Instant::now(),
+ recorded: false,
+ }
+ }
+
+ fn finish(mut self, ok: bool) {
+ if !ok {
+ self.shape.result = RESULT_ERROR;
+ }
+ self.record();
+ }
+
+ fn record(&mut self) {
+ if self.recorded {
+ return;
+ }
+ self.recorded = true;
+ let shape = &self.shape;
+ let labels = [
+ ("path", shape.path),
+ ("target", shape.target),
+ ("sources", shape.sources),
+ ("mode", shape.mode),
+ ("result", shape.result),
+ ]
+ .map(|(key, value)| (key.to_string(), value.to_string()));
+ common_metrics::inc(MERGE_CALLS_TOTAL, &labels, 1);
+ common_metrics::histogram(
+ MERGE_CALL_DURATION,
+ &labels,
+ self.start.elapsed().as_secs_f64() * 1000.0,
+ );
+ }
+}
+
+impl Drop for MergeCall {
+ fn drop(&mut self) {
+ if !self.recorded {
+ self.shape.result = RESULT_CANCELLED;
+ self.record();
+ }
+ }
+}
+
+fn saga_result(row: &OpRow) -> &'static str {
+ if row.step == STEP_COMPLETED {
+ RESULT_COMPLETED
+ } else if MergeOpExecutor::is_claim_abort(row) {
+ RESULT_ABORTED_CLAIM
+ } else {
+ RESULT_ABORTED
+ }
+}
const ESTABLISH_IDENTIFIED_READS_TOTAL: &str =
"personhog_identity_merge_establish_identified_reads_total";
@@ -81,6 +202,19 @@ impl MergeEntrance {
pub async fn handle(
&self,
request: MergePersonsRequest,
+ ) -> Result<MergePersonsResponse, Status> {
+ let mut call = MergeCall::start(&request);
+ let response = self.run(request, &mut call.shape).await;
+ call.finish(response.is_ok());
+ response
+ }
+
+ // See `handle` for why result_large_err is allowed.
+ #[allow(clippy::result_large_err)]
+ async fn run(
+ &self,
+ request: MergePersonsRequest,
+ shape: &mut MergeShape,
) -> Result<MergePersonsResponse, Status> {
let (op_id, move_limit) = validate_merge_persons(&request)?;
common_metrics::histogram(MERGE_SOURCES_PER_CALL, &[], request.sources.len() as f64);
@@ -134,10 +268,24 @@ impl MergeEntrance {
"op_id_reused",
));
}
+ shape.path = PATH_RESUMED;
+ // The frozen request records a birth but not an adoption, so an
+ // adopted target reports as existing on this path.
+ shape.target = if row
+ .request
+ .get("target_born")
+ .and_then(Value::as_bool)
+ .unwrap_or(false)
+ {
+ TargetOrigin::Born.as_str()
+ } else {
+ TargetOrigin::Existing.as_str()
+ };
let frozen = row.request.clone();
let start = Instant::now();
let row = self.ops.execute(op_id, row.team_id, &frozen).await?;
record_phase("saga", start);
+ shape.result = saga_result(&row);
let start = Instant::now();
let delivered = self.deliver_aborted_writes(&request, &row).await?;
record_phase("deliver", start);
@@ -173,8 +321,8 @@ impl MergeEntrance {
record_phase("resolve", start);
let target_key = (request.team_id, request.target_distinct_id.clone());
- let (target_person, target_was_born) = match resolved.get(&target_key) {
- Some(target) => (target.clone(), false),
+ let (target_person, target_origin) = match resolved.get(&target_key) {
+ Some(target) => (target.clone(), TargetOrigin::Existing),
None => {
let start = Instant::now();
let established = self.establish_target(&request, &resolved).await?;
@@ -182,6 +330,8 @@ impl MergeEntrance {
established
}
};
+ shape.target = target_origin.as_str();
+ let target_was_born = target_origin == TargetOrigin::Born;
let mut attach: Vec<String> = Vec::new();
let mut saga_sources: Vec<MergeSourceEntry> = Vec::new();
@@ -250,10 +400,12 @@ impl MergeEntrance {
*inline_counts.entry(outcome.as_str()).or_default() += 1;
}
for (outcome, count) in inline_counts {
- record_outcome_count(outcome, count);
+ record_outcome_count(PATH_INLINE, outcome, count);
}
if saga_sources.is_empty() {
+ shape.path = PATH_INLINE;
+ shape.result = RESULT_SETTLED;
// Nothing to destroy, so no op row. Inline settlement is
// idempotent: a retry re-executes against the current world,
// the same at-least-once semantics as event redelivery — and
@@ -319,9 +471,11 @@ impl MergeEntrance {
frozen["inline_results"] = serde_json::to_value(&inline_results)
.map_err(|e| Status::internal(format!("failed to freeze inline results: {e}")))?;
+ shape.path = PATH_SAGA;
let start = Instant::now();
let row = self.ops.execute(op_id, request.team_id, &frozen).await?;
record_phase("saga", start);
+ shape.result = saga_result(&row);
let start = Instant::now();
let delivered = self.deliver_aborted_writes(&request, &row).await?;
record_phase("deliver", start);
@@ -440,7 +594,7 @@ impl MergeEntrance {
&self,
request: &MergePersonsRequest,
resolved: &HashMap<(i64, String), Person>,
- ) -> Result<(Person, bool), Status> {
+ ) -> Result<(Person, TargetOrigin), Status> {
let target_did = &request.target_distinct_id;
// Sources decide in request order, as one-at-a-time identifies would:
@@ -482,12 +636,15 @@ impl MergeEntrance {
.await
.map_err(|e| Status::internal(format!("target attach failed: {e}")))?;
return match attached.get(target_did) {
- Some(AttachOutcome::Attached { .. }) => Ok((survivor.clone(), false)),
+ Some(AttachOutcome::Attached { .. }) => {
+ Ok((survivor.clone(), TargetOrigin::Adopted))
+ }
// The target distinct id got mapped concurrently; that
// mapping wins and its person is the survivor.
- Some(AttachOutcome::AlreadyMapped { .. }) => {
- Ok((self.resolve_target_after_race(request).await?, false))
- }
+ Some(AttachOutcome::AlreadyMapped { .. }) => Ok((
+ self.resolve_target_after_race(request).await?,
+ TargetOrigin::Existing,
+ )),
// The survivor row vanished under the attach (a racing
// lifecycle op committed). Nothing durable happened for
// this op, so the retry re-classifies against the settled
@@ -525,11 +682,19 @@ impl MergeEntrance {
.map_err(|e| Status::internal(format!("target creation failed: {e}")))?;
match outcomes.into_iter().next() {
// A rival's insert winning the row makes this call not the creator.
- Some(StubOutcome::Committed { person, created }) => Ok((person, created)),
- // The race winner's person keeps its own establishment's creator.
- Some(StubOutcome::LostRace) | None => {
- Ok((self.resolve_target_after_race(request).await?, false))
+ Some(StubOutcome::Committed { person, created }) => {
+ let origin = if created {
+ TargetOrigin::Born
+ } else {
+ TargetOrigin::Existing
+ };
+ Ok((person, origin))
}
+ // The race winner's person keeps its own establishment's creator.
+ Some(StubOutcome::LostRace) | None => Ok((
+ self.resolve_target_after_race(request).await?,
+ TargetOrigin::Existing,
+ )),
}
}