Skip to content

Commit ef514b1

Browse files
Catalog Telemetry logging added for all feed upload flows (#581)
* Telemetry logging added for all feed upload flows * PR comments resolved * Static errors resolved * feed id moved to extras
1 parent bfecfc2 commit ef514b1

File tree

10 files changed

+241
-60
lines changed

10 files changed

+241
-60
lines changed

app/code/Meta/BusinessExtension/Helper/FBEHelper.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class FBEHelper
4040
private const URL_TYPE_WEB = 'web';
4141

4242
public const PERSIST_META_LOG_IMMEDIATELY = 'persist_meta_log_immediately';
43+
public const PERSIST_META_TELEMETRY_LOGS = 'persist_meta_telemetry_logs';
4344

4445
/**
4546
* @var GraphAPIConfig
@@ -309,6 +310,14 @@ public function logException(Throwable $e, array $context = [])
309310
$this->logExceptionDetails($exceptionCode, $errorMessage, $exceptionTrace, $context);
310311
}
311312

313+
/**
314+
* Log exception details
315+
*
316+
* @param int $code
317+
* @param string $message
318+
* @param string $traceAsString
319+
* @param array $context
320+
*/
312321
public function logExceptionDetails($code, $message, $traceAsString, array $context = [])
313322
{
314323
// If the log type is not set just log the error message and trace.
@@ -367,6 +376,18 @@ public function logExceptionDetailsImmediatelyToMeta($code, $message, $traceAsSt
367376
$this->logExceptionDetails($code, $message, $traceAsString, $context);
368377
}
369378

379+
/**
380+
* Log telemetry and persist with Meta
381+
*
382+
* @param string $message
383+
* @param array $context
384+
*/
385+
public function logTelemetryToMeta(string $message, array $context = [])
386+
{
387+
$context['log_type'] = self::PERSIST_META_TELEMETRY_LOGS;
388+
$this->log($message, $context);
389+
}
390+
370391
/**
371392
* Log pixel event
372393
*
@@ -474,4 +495,23 @@ public function checkAdminEndpointPermission()
474495
}
475496
}
476497

498+
/**
499+
* Return unique trace id for telemetry flows
500+
*
501+
* @return string
502+
*/
503+
public function genUniqueTraceID(): string
504+
{
505+
return uniqid("magento_");
506+
}
507+
508+
/**
509+
* Return current time in milliseconds
510+
*
511+
* @return int
512+
*/
513+
public function getCurrentTimeInMS(): int
514+
{
515+
return (int)(microtime(true) * 1000);
516+
}
477517
}

app/code/Meta/BusinessExtension/Logger/Handler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected function write(array $record): void
6868
$logTypeIsSet = isset($record['context']['log_type']);
6969
if ($logTypeIsSet && $record['context']['log_type'] === FBEHelper::PERSIST_META_LOG_IMMEDIATELY) {
7070
$this->publisher->publish('persist.meta.log.immediately', json_encode($record['context']));
71-
} elseif ($logTypeIsSet && $record['context']['log_type'] === 'persist_meta_telemetry_logs') {
71+
} elseif ($logTypeIsSet && $record['context']['log_type'] === FBEHelper::PERSIST_META_TELEMETRY_LOGS) {
7272
$this->publisher->publish('persist.meta.telemetry.logs', json_encode($record['context']));
7373
return;
7474
}

app/code/Meta/BusinessExtension/Model/PersistMetaTelemetryLogsHandler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
namespace Meta\BusinessExtension\Model;
2222

23+
use GuzzleHttp\Exception\GuzzleException;
24+
use Meta\BusinessExtension\Helper\FBEHelper;
2325
use Meta\BusinessExtension\Helper\GraphAPIAdapter;
2426
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
2527

@@ -54,6 +56,7 @@ public function __construct(
5456
* Consumer handler to persist telemetry logs from message queue to Meta
5557
*
5658
* @param string $messages
59+
* @throws GuzzleException
5760
*/
5861
public function persistMetaTelemetryLogs(string $messages)
5962
{
@@ -68,7 +71,7 @@ public function persistMetaTelemetryLogs(string $messages)
6871
}
6972
}
7073
$telemetryContext = [];
71-
$telemetryContext['event'] = 'persist_meta_telemetry_logs';
74+
$telemetryContext['event'] = FBEHelper::PERSIST_META_TELEMETRY_LOGS;
7275
$telemetryContext['extra_data'] = [];
7376
$telemetryContext['extra_data']['telemetry_logs'] = $messages;
7477
$this->graphApiAdapter->persistLogToMeta($telemetryContext, $accessToken);

app/code/Meta/BusinessExtension/Plugin/LoggingPluginBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private function getTelemetryLogData(string $log_prefix, $subject, string $step)
194194
$storeId = $this->getMaybeStoreID();
195195
return [
196196
'flow_name' => $log_prefix . ': ' . $this->getClassnameForLogging($subject),
197-
'log_type' => 'persist_meta_telemetry_logs',
197+
'log_type' => FBEHelper::PERSIST_META_TELEMETRY_LOGS,
198198
'store_id' => $storeId,
199199
'flow_step' => $step,
200200
];

app/code/Meta/Catalog/Controller/Adminhtml/Ajax/ProductFeedUpload.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use Meta\Catalog\Model\Product\Feed\Uploader;
2828
use Magento\Backend\App\Action\Context;
2929
use Magento\Framework\Controller\Result\JsonFactory;
30-
use Magento\Framework\Exception\NoSuchEntityException;
3130

3231
class ProductFeedUpload extends AbstractAjax
3332
{
@@ -72,7 +71,7 @@ public function __construct(
7271
* Execute for json
7372
*
7473
* @return array
75-
* @throws NoSuchEntityException
74+
* @throws \Throwable
7675
*/
7776
public function executeForJson()
7877
{
@@ -100,8 +99,11 @@ public function executeForJson()
10099
return $response;
101100
}
102101

102+
$traceId = $this->fbeHelper->genUniqueTraceID();
103+
$flowName = 'force_product_feed_upload';
104+
103105
try {
104-
$feedPushResponse = $this->uploader->uploadFullCatalog($storeId);
106+
$feedPushResponse = $this->uploader->uploadFullCatalog($storeId, $flowName, $traceId);
105107
$response['success'] = true;
106108
$response['feed_push_response'] = $feedPushResponse;
107109
} catch (Exception $e) {

app/code/Meta/Catalog/Cron/UploadProductFeed.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Exception;
2424
use Meta\Catalog\Model\Product\Feed\Uploader;
2525
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
26-
use Magento\Framework\Exception\LocalizedException;
2726
use Meta\BusinessExtension\Helper\FBEHelper;
2827

2928
class UploadProductFeed
@@ -63,12 +62,14 @@ public function __construct(
6362
*
6463
* @param int $storeId
6564
* @return $this
66-
* @throws LocalizedException
65+
* @throws \Throwable
6766
*/
6867
private function uploadForStore($storeId)
6968
{
7069
if ($this->isFeedUploadEnabled($storeId)) {
71-
$this->uploader->uploadFullCatalog($storeId);
70+
$traceId = $this->fbeHelper->genUniqueTraceID();
71+
$flowName = 'daily_product_feed_upload';
72+
$this->uploader->uploadFullCatalog($storeId, $flowName, $traceId);
7273
}
7374

7475
return $this;

app/code/Meta/Catalog/Helper/CatalogSyncHelper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,15 @@ public function __construct(
7272
* Syncs all products and categories to Meta Catalog
7373
*
7474
* @param int $storeId
75+
* @param string $flowName
76+
* @param string $traceId
7577
* @return void
7678
*/
77-
public function syncFullCatalog(int $storeId)
79+
public function syncFullCatalog(int $storeId, string $flowName, string $traceId)
7880
{
7981
try {
8082
if ($this->systemConfig->isCatalogSyncEnabled($storeId)) {
81-
$this->uploader->uploadFullCatalog($storeId);
83+
$this->uploader->uploadFullCatalog($storeId, $flowName, $traceId);
8284
$this->categoryCollection->pushAllCategoriesToFbCollections($storeId);
8385
}
8486
} catch (\Throwable $e) {

0 commit comments

Comments
 (0)