vendor/sentry/sentry/src/Event.php line 203

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry;
  4. use Sentry\Context\OsContext;
  5. use Sentry\Context\RuntimeContext;
  6. use Sentry\Profiling\Profile;
  7. use Sentry\Tracing\Span;
  8. /**
  9. * This is the base class for classes containing event data.
  10. *
  11. * @author Stefano Arlandini <sarlandini@alice.it>
  12. */
  13. final class Event
  14. {
  15. public const DEFAULT_ENVIRONMENT = 'production';
  16. /**
  17. * @var EventId The ID
  18. */
  19. private $id;
  20. /**
  21. * @var float|null The date and time of when this event was generated
  22. */
  23. private $timestamp;
  24. /**
  25. * This property is used if it's a Transaction event together with $timestamp it's the duration of the transaction.
  26. *
  27. * @var float|null The date and time of when this event was generated
  28. */
  29. private $startTimestamp;
  30. /**
  31. * @var Severity|null The severity of this event
  32. */
  33. private $level;
  34. /**
  35. * @var string|null The name of the logger which created the record
  36. */
  37. private $logger;
  38. /**
  39. * @var string|null the name of the transaction (or culprit) which caused this exception
  40. */
  41. private $transaction;
  42. /**
  43. * @var CheckIn|null The check in data
  44. */
  45. private $checkIn;
  46. /**
  47. * @var string|null The name of the server (e.g. the host name)
  48. */
  49. private $serverName;
  50. /**
  51. * @var string|null The release of the program
  52. */
  53. private $release;
  54. /**
  55. * @var string|null The error message
  56. */
  57. private $message;
  58. /**
  59. * @var string|null The formatted error message
  60. */
  61. private $messageFormatted;
  62. /**
  63. * @var string[] The parameters to use to format the message
  64. */
  65. private $messageParams = [];
  66. /**
  67. * @var string|null The environment where this event generated (e.g. production)
  68. */
  69. private $environment;
  70. /**
  71. * @var array<string, string> A list of relevant modules and their versions
  72. */
  73. private $modules = [];
  74. /**
  75. * @var array<string, mixed> The request data
  76. */
  77. private $request = [];
  78. /**
  79. * @var array<string, string> A list of tags associated to this event
  80. */
  81. private $tags = [];
  82. /**
  83. * @var OsContext|null The server OS context data
  84. */
  85. private $osContext;
  86. /**
  87. * @var RuntimeContext|null The runtime context data
  88. */
  89. private $runtimeContext;
  90. /**
  91. * @var UserDataBag|null The user context data
  92. */
  93. private $user;
  94. /**
  95. * @var array<string, array<string, mixed>> An arbitrary mapping of additional contexts associated to this event
  96. */
  97. private $contexts = [];
  98. /**
  99. * @var array<string, mixed> An arbitrary mapping of additional metadata
  100. */
  101. private $extra = [];
  102. /**
  103. * @var string[] An array of strings used to dictate the deduplication of this event
  104. */
  105. private $fingerprint = [];
  106. /**
  107. * @var Breadcrumb[] The associated breadcrumbs
  108. */
  109. private $breadcrumbs = [];
  110. /**
  111. * @var Span[] The array of spans if it's a transaction
  112. */
  113. private $spans = [];
  114. /**
  115. * @var ExceptionDataBag[] The exceptions
  116. */
  117. private $exceptions = [];
  118. /**
  119. * @var Stacktrace|null The stacktrace that generated this event
  120. */
  121. private $stacktrace;
  122. /**
  123. * A place to stash data which is needed at some point in the SDK's
  124. * event processing pipeline but which shouldn't get sent to Sentry.
  125. *
  126. * @var array<string, mixed>
  127. */
  128. private $sdkMetadata = [];
  129. /**
  130. * @var string The Sentry SDK identifier
  131. */
  132. private $sdkIdentifier = Client::SDK_IDENTIFIER;
  133. /**
  134. * @var string The Sentry SDK version
  135. */
  136. private $sdkVersion = Client::SDK_VERSION;
  137. /**
  138. * @var EventType The type of the Event
  139. */
  140. private $type;
  141. /**
  142. * @var Profile|null The profile data
  143. */
  144. private $profile;
  145. private function __construct(?EventId $eventId, EventType $eventType)
  146. {
  147. $this->id = $eventId ?? EventId::generate();
  148. $this->timestamp = microtime(true);
  149. $this->type = $eventType;
  150. }
  151. /**
  152. * Creates a new event.
  153. *
  154. * @param EventId|null $eventId The ID of the event
  155. */
  156. public static function createEvent(?EventId $eventId = null): self
  157. {
  158. return new self($eventId, EventType::event());
  159. }
  160. /**
  161. * Creates a new transaction event.
  162. *
  163. * @param EventId|null $eventId The ID of the event
  164. */
  165. public static function createTransaction(EventId $eventId = null): self
  166. {
  167. return new self($eventId, EventType::transaction());
  168. }
  169. public static function createCheckIn(?EventId $eventId = null): self
  170. {
  171. return new self($eventId, EventType::checkIn());
  172. }
  173. /**
  174. * Gets the ID of this event.
  175. */
  176. public function getId(): EventId
  177. {
  178. return $this->id;
  179. }
  180. /**
  181. * Gets the identifier of the SDK package that generated this event.
  182. *
  183. * @internal
  184. */
  185. public function getSdkIdentifier(): string
  186. {
  187. return $this->sdkIdentifier;
  188. }
  189. /**
  190. * Sets the identifier of the SDK package that generated this event.
  191. *
  192. * @internal
  193. */
  194. public function setSdkIdentifier(string $sdkIdentifier): void
  195. {
  196. $this->sdkIdentifier = $sdkIdentifier;
  197. }
  198. /**
  199. * Gets the version of the SDK package that generated this Event.
  200. *
  201. * @internal
  202. */
  203. public function getSdkVersion(): string
  204. {
  205. return $this->sdkVersion;
  206. }
  207. /**
  208. * Sets the version of the SDK package that generated this Event.
  209. *
  210. * @internal
  211. */
  212. public function setSdkVersion(string $sdkVersion): void
  213. {
  214. $this->sdkVersion = $sdkVersion;
  215. }
  216. /**
  217. * Gets the timestamp of when this event was generated.
  218. *
  219. * @return float
  220. */
  221. public function getTimestamp(): ?float
  222. {
  223. return $this->timestamp;
  224. }
  225. /**
  226. * Sets the timestamp of when the Event was created.
  227. */
  228. public function setTimestamp(?float $timestamp): void
  229. {
  230. $this->timestamp = $timestamp;
  231. }
  232. /**
  233. * Gets the severity of this event.
  234. */
  235. public function getLevel(): ?Severity
  236. {
  237. return $this->level;
  238. }
  239. /**
  240. * Sets the severity of this event.
  241. *
  242. * @param Severity|null $level The severity
  243. */
  244. public function setLevel(?Severity $level): void
  245. {
  246. $this->level = $level;
  247. }
  248. /**
  249. * Gets the name of the logger which created the event.
  250. */
  251. public function getLogger(): ?string
  252. {
  253. return $this->logger;
  254. }
  255. /**
  256. * Sets the name of the logger which created the event.
  257. *
  258. * @param string|null $logger The logger name
  259. */
  260. public function setLogger(?string $logger): void
  261. {
  262. $this->logger = $logger;
  263. }
  264. /**
  265. * Gets the name of the transaction (or culprit) which caused this
  266. * exception.
  267. */
  268. public function getTransaction(): ?string
  269. {
  270. return $this->transaction;
  271. }
  272. /**
  273. * Sets the name of the transaction (or culprit) which caused this
  274. * exception.
  275. *
  276. * @param string|null $transaction The transaction name
  277. */
  278. public function setTransaction(?string $transaction): void
  279. {
  280. $this->transaction = $transaction;
  281. }
  282. public function setCheckIn(?CheckIn $checkIn): void
  283. {
  284. $this->checkIn = $checkIn;
  285. }
  286. public function getCheckIn(): ?CheckIn
  287. {
  288. return $this->checkIn;
  289. }
  290. /**
  291. * Gets the name of the server.
  292. */
  293. public function getServerName(): ?string
  294. {
  295. return $this->serverName;
  296. }
  297. /**
  298. * Sets the name of the server.
  299. *
  300. * @param string|null $serverName The server name
  301. */
  302. public function setServerName(?string $serverName): void
  303. {
  304. $this->serverName = $serverName;
  305. }
  306. /**
  307. * Gets the release of the program.
  308. */
  309. public function getRelease(): ?string
  310. {
  311. return $this->release;
  312. }
  313. /**
  314. * Sets the release of the program.
  315. *
  316. * @param string|null $release The release
  317. */
  318. public function setRelease(?string $release): void
  319. {
  320. $this->release = $release;
  321. }
  322. /**
  323. * Gets the error message.
  324. */
  325. public function getMessage(): ?string
  326. {
  327. return $this->message;
  328. }
  329. /**
  330. * Gets the formatted message.
  331. */
  332. public function getMessageFormatted(): ?string
  333. {
  334. return $this->messageFormatted;
  335. }
  336. /**
  337. * Gets the parameters to use to format the message.
  338. *
  339. * @return string[]
  340. */
  341. public function getMessageParams(): array
  342. {
  343. return $this->messageParams;
  344. }
  345. /**
  346. * Sets the error message.
  347. *
  348. * @param string $message The message
  349. * @param string[] $params The parameters to use to format the message
  350. * @param string|null $formatted The formatted message
  351. */
  352. public function setMessage(string $message, array $params = [], ?string $formatted = null): void
  353. {
  354. $this->message = $message;
  355. $this->messageParams = $params;
  356. $this->messageFormatted = $formatted;
  357. }
  358. /**
  359. * Gets a list of relevant modules and their versions.
  360. *
  361. * @return array<string, string>
  362. */
  363. public function getModules(): array
  364. {
  365. return $this->modules;
  366. }
  367. /**
  368. * Sets a list of relevant modules and their versions.
  369. *
  370. * @param array<string, string> $modules
  371. */
  372. public function setModules(array $modules): void
  373. {
  374. $this->modules = $modules;
  375. }
  376. /**
  377. * Gets the request data.
  378. *
  379. * @return array<string, mixed>
  380. */
  381. public function getRequest(): array
  382. {
  383. return $this->request;
  384. }
  385. /**
  386. * Sets the request data.
  387. *
  388. * @param array<string, mixed> $request The request data
  389. */
  390. public function setRequest(array $request): void
  391. {
  392. $this->request = $request;
  393. }
  394. /**
  395. * Gets an arbitrary mapping of additional contexts.
  396. *
  397. * @return array<string, array<string, mixed>>
  398. */
  399. public function getContexts(): array
  400. {
  401. return $this->contexts;
  402. }
  403. /**
  404. * Sets data to the context by a given name.
  405. *
  406. * @param string $name The name that uniquely identifies the context
  407. * @param array<string, mixed> $data The data of the context
  408. */
  409. public function setContext(string $name, array $data): self
  410. {
  411. if (!empty($data)) {
  412. $this->contexts[$name] = $data;
  413. }
  414. return $this;
  415. }
  416. /**
  417. * Gets an arbitrary mapping of additional metadata.
  418. *
  419. * @return array<string, mixed>
  420. */
  421. public function getExtra(): array
  422. {
  423. return $this->extra;
  424. }
  425. /**
  426. * Sets an arbitrary mapping of additional metadata.
  427. *
  428. * @param array<string, mixed> $extra The context object
  429. */
  430. public function setExtra(array $extra): void
  431. {
  432. $this->extra = $extra;
  433. }
  434. /**
  435. * Gets a list of tags associated to this event.
  436. *
  437. * @return array<string, string>
  438. */
  439. public function getTags(): array
  440. {
  441. return $this->tags;
  442. }
  443. /**
  444. * Sets a list of tags associated to this event.
  445. *
  446. * @param array<string, string> $tags The tags to set
  447. */
  448. public function setTags(array $tags): void
  449. {
  450. $this->tags = $tags;
  451. }
  452. /**
  453. * Sets or updates a tag in this event.
  454. *
  455. * @param string $key The key that uniquely identifies the tag
  456. * @param string $value The value
  457. */
  458. public function setTag(string $key, string $value): void
  459. {
  460. $this->tags[$key] = $value;
  461. }
  462. /**
  463. * Removes a given tag from the event.
  464. *
  465. * @param string $key The key that uniquely identifies the tag
  466. */
  467. public function removeTag(string $key): void
  468. {
  469. unset($this->tags[$key]);
  470. }
  471. /**
  472. * Gets the user context.
  473. */
  474. public function getUser(): ?UserDataBag
  475. {
  476. return $this->user;
  477. }
  478. /**
  479. * Sets the user context.
  480. *
  481. * @param UserDataBag|null $user The context object
  482. */
  483. public function setUser(?UserDataBag $user): void
  484. {
  485. $this->user = $user;
  486. }
  487. /**
  488. * Gets the server OS context.
  489. */
  490. public function getOsContext(): ?OsContext
  491. {
  492. return $this->osContext;
  493. }
  494. /**
  495. * Sets the server OS context.
  496. *
  497. * @param OsContext|null $osContext The context object
  498. */
  499. public function setOsContext(?OsContext $osContext): void
  500. {
  501. $this->osContext = $osContext;
  502. }
  503. /**
  504. * Gets the runtime context data.
  505. */
  506. public function getRuntimeContext(): ?RuntimeContext
  507. {
  508. return $this->runtimeContext;
  509. }
  510. /**
  511. * Sets the runtime context data.
  512. *
  513. * @param RuntimeContext|null $runtimeContext The context object
  514. */
  515. public function setRuntimeContext(?RuntimeContext $runtimeContext): void
  516. {
  517. $this->runtimeContext = $runtimeContext;
  518. }
  519. /**
  520. * Gets an array of strings used to dictate the deduplication of this
  521. * event.
  522. *
  523. * @return string[]
  524. */
  525. public function getFingerprint(): array
  526. {
  527. return $this->fingerprint;
  528. }
  529. /**
  530. * Sets an array of strings used to dictate the deduplication of this
  531. * event.
  532. *
  533. * @param string[] $fingerprint The strings
  534. */
  535. public function setFingerprint(array $fingerprint): void
  536. {
  537. $this->fingerprint = $fingerprint;
  538. }
  539. /**
  540. * Gets the environment in which this event was generated.
  541. */
  542. public function getEnvironment(): ?string
  543. {
  544. return $this->environment;
  545. }
  546. /**
  547. * Sets the environment in which this event was generated.
  548. *
  549. * @param string|null $environment The name of the environment
  550. */
  551. public function setEnvironment(?string $environment): void
  552. {
  553. $this->environment = $environment;
  554. }
  555. /**
  556. * Gets the breadcrumbs.
  557. *
  558. * @return Breadcrumb[]
  559. */
  560. public function getBreadcrumbs(): array
  561. {
  562. return $this->breadcrumbs;
  563. }
  564. /**
  565. * Set new breadcrumbs to the event.
  566. *
  567. * @param Breadcrumb[] $breadcrumbs The breadcrumb array
  568. */
  569. public function setBreadcrumb(array $breadcrumbs): void
  570. {
  571. $this->breadcrumbs = $breadcrumbs;
  572. }
  573. /**
  574. * Gets the exception.
  575. *
  576. * @return ExceptionDataBag[]
  577. */
  578. public function getExceptions(): array
  579. {
  580. return $this->exceptions;
  581. }
  582. /**
  583. * Sets the exceptions.
  584. *
  585. * @param ExceptionDataBag[] $exceptions The exceptions
  586. */
  587. public function setExceptions(array $exceptions): void
  588. {
  589. foreach ($exceptions as $exception) {
  590. if (!$exception instanceof ExceptionDataBag) {
  591. throw new \UnexpectedValueException(sprintf('Expected an instance of the "%s" class. Got: "%s".', ExceptionDataBag::class, get_debug_type($exception)));
  592. }
  593. }
  594. $this->exceptions = $exceptions;
  595. }
  596. /**
  597. * Gets the stacktrace that generated this event.
  598. */
  599. public function getStacktrace(): ?Stacktrace
  600. {
  601. return $this->stacktrace;
  602. }
  603. /**
  604. * Sets the stacktrace that generated this event.
  605. *
  606. * @param Stacktrace|null $stacktrace The stacktrace instance
  607. */
  608. public function setStacktrace(?Stacktrace $stacktrace): void
  609. {
  610. $this->stacktrace = $stacktrace;
  611. }
  612. public function getType(): EventType
  613. {
  614. return $this->type;
  615. }
  616. /**
  617. * Sets the SDK metadata with the given name.
  618. *
  619. * @param string $name The name that uniquely identifies the SDK metadata
  620. * @param mixed $data The data of the SDK metadata
  621. */
  622. public function setSdkMetadata(string $name, $data): void
  623. {
  624. $this->sdkMetadata[$name] = $data;
  625. }
  626. /**
  627. * Gets the SDK metadata.
  628. *
  629. * @return mixed
  630. *
  631. * @psalm-template T of string|null
  632. *
  633. * @psalm-param T $name
  634. *
  635. * @psalm-return (T is string ? mixed : array<string, mixed>|null)
  636. */
  637. public function getSdkMetadata(?string $name = null)
  638. {
  639. if (null !== $name) {
  640. return $this->sdkMetadata[$name] ?? null;
  641. }
  642. return $this->sdkMetadata;
  643. }
  644. /**
  645. * Gets a timestamp representing when the measuring of a transaction started.
  646. */
  647. public function getStartTimestamp(): ?float
  648. {
  649. return $this->startTimestamp;
  650. }
  651. /**
  652. * Sets a timestamp representing when the measuring of a transaction started.
  653. *
  654. * @param float|null $startTimestamp The start time of the measurement
  655. */
  656. public function setStartTimestamp(?float $startTimestamp): void
  657. {
  658. $this->startTimestamp = $startTimestamp;
  659. }
  660. /**
  661. * A list of timed application events that have a start and end time.
  662. *
  663. * @return Span[]
  664. */
  665. public function getSpans(): array
  666. {
  667. return $this->spans;
  668. }
  669. /**
  670. * Sets a list of timed application events that have a start and end time.
  671. *
  672. * @param Span[] $spans The list of spans
  673. */
  674. public function setSpans(array $spans): void
  675. {
  676. $this->spans = $spans;
  677. }
  678. public function setProfile(?Profile $profile): void
  679. {
  680. $this->profile = $profile;
  681. }
  682. public function getProfile(): ?Profile
  683. {
  684. return $this->profile;
  685. }
  686. public function getTraceId(): ?string
  687. {
  688. $traceId = $this->getContexts()['trace']['trace_id'];
  689. if (\is_string($traceId) && !empty($traceId)) {
  690. return $traceId;
  691. }
  692. return null;
  693. }
  694. }