Skip to content

Commit

Permalink
instrument Profiler by adding explicit fb user id setter in TCPS
Browse files Browse the repository at this point in the history
Summary: Instrument at Profiler - first introduce function in ThriftContextPropState (TCPS) that allows setting with an explicit integer value (as opposed to a ViewerContext)

Reviewed By: aknott

Differential Revision: D69280868

fbshipit-source-id: 5bca187ce90ec5ca11156544f0ed45ae2f1d8c94
  • Loading branch information
Mahmudul Rapi authored and facebook-github-bot committed Feb 7, 2025
1 parent 9ea0974 commit 098ca74
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 13 deletions.
45 changes: 32 additions & 13 deletions thrift/lib/hack/src/ThriftContextPropState.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,37 @@ public static function initFromString(?string $s)[defaults]: void {
}
}

// update FB user id from explicit value
public static function updateFBUserId(?int $fb_user_id, string $src): bool {
// don't overwrite if TCPS already has a valid fb user id
$tcps_fb_user_id = self::get()->getFBUserId();
if (self::coerceId($tcps_fb_user_id) is nonnull) {
return false;
}
if (
!JustKnobs::eval(
'meta_cp/www:enable_user_id_ctx_prop',
/*hashval=*/null,
/*switchval=*/$src,
)
) {
return false;
}

$fb_user_id = self::coerceId($fb_user_id);

$ods = CategorizedOBC::typedGet(ODSCategoryID::ODS_CONTEXTPROP);
if ($fb_user_id is nonnull) {
self::get()->setFBUserId($fb_user_id);
$ods->bumpKey('contextprop.set_fb_user_id'.$src);
return true;
}

$ods->bumpKey('contextprop.missing_fb_user_id'.$src);
return false;
}

// update user id from ViewerContext
public static function updateUserIdFromVC(
?IViewerContextBase $vc,
string $src,
Expand Down Expand Up @@ -143,21 +174,9 @@ private static function updateFBUserIdFromVC(
): bool {
$ods = CategorizedOBC::typedGet(ODSCategoryID::ODS_CONTEXTPROP);
$ods->bumpKey('contextprop.fb_vc'.$src);
// don't overwrite if TCPS already has a valid fb user id
$tcps_fb_user_id = self::get()->getFBUserId();
if (self::coerceId($tcps_fb_user_id) is nonnull) {
return false;
}

$fb_user_id = self::coerceId($vc->getUserID());
if ($fb_user_id is nonnull) {
$ods->bumpKey('contextprop.set_fb_user_id'.$src);
self::get()->setFBUserId($fb_user_id);
return true;
}

$ods->bumpKey('contextprop.missing_fb_user_id'.$src);
return false;
return self::updateFBUserId($fb_user_id, $src);
}

private static function updateIGUserIdFromVC(
Expand Down
47 changes: 47 additions & 0 deletions thrift/lib/hack/src/__tests__/ThriftContextPropStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,53 @@ public function testInitialization()[defaults]: void {
expect($tcps->getUserIds()?->ig_user_id)->toEqual(456);
}

public async function testUpdatedWithExplicitFBUserId(
)[defaults]: Awaitable<void> {
MockJustKnobs::setBool('meta_cp/www:enable_user_id_ctx_prop', true);
$tfm = ThriftFrameworkMetadata::withDefaultValues();
$tfm->baggage = ContextProp\Baggage::withDefaultValues();
$tfm->baggage->user_ids = ContextProp\UserIds::fromShape(
shape('fb_user_id' => null, 'ig_user_id' => 456),
);

$buf = new TMemoryBuffer();
$prot = new TCompactProtocolAccelerated($buf);
$tfm->write($prot);
$s = $buf->getBuffer();
$e = Base64::encode($s);

ThriftContextPropState::initFromString($e);
// expect these to be no-op if TFM already has user ids
ThriftContextPropState::updateFBUserId(1, "test");

$tcps = ThriftContextPropState::get();
expect($tcps->getUserIds()?->fb_user_id)->toEqual(1);
expect($tcps->getUserIds()?->ig_user_id)->toEqual(456);
}

public async function testUpdatedWithExplicitFBUserIdNoOverwrite(
)[defaults]: Awaitable<void> {
MockJustKnobs::setBool('meta_cp/www:enable_user_id_ctx_prop', true);
$tfm = ThriftFrameworkMetadata::withDefaultValues();
$tfm->baggage = ContextProp\Baggage::withDefaultValues();
$tfm->baggage->user_ids = ContextProp\UserIds::fromShape(
shape('fb_user_id' => 123, 'ig_user_id' => null),
);

$buf = new TMemoryBuffer();
$prot = new TCompactProtocolAccelerated($buf);
$tfm->write($prot);
$s = $buf->getBuffer();
$e = Base64::encode($s);

ThriftContextPropState::initFromString($e);
// expect these to be no-op if TFM already has user ids
ThriftContextPropState::updateFBUserId(456, "test");

$tcps = ThriftContextPropState::get();
expect($tcps->getUserIds()?->fb_user_id)->toEqual(123);
}

public function testGen()[defaults]: void {
ThriftContextPropState::initFromString(null);
$tcps = ThriftContextPropState::get();
Expand Down

0 comments on commit 098ca74

Please sign in to comment.