TraceContext.php 984 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace Modules\Common\Support\Trace;
  3. class TraceContext
  4. {
  5. private $traceId;
  6. private $spanId = 0;
  7. private $parentSpanId = 0;
  8. public function __construct($traceId = null, $parentSpanId = 0, $spanId = 0)
  9. {
  10. $this->traceId = $traceId ?? uniqid();
  11. $this->parentSpanId = $parentSpanId;
  12. $this->spanId = $spanId;
  13. }
  14. public static function newFromParent($traceInfo) {
  15. $traceContext = new self($traceInfo['traceId'], $traceInfo['spanId']);
  16. return $traceContext;
  17. }
  18. public function getTraceInfo() {
  19. return [
  20. 'traceId' => $this->getTraceId(),
  21. 'parentSpanId' => $this->getParentSpanId(),
  22. 'spanId' => $this->getSpanId()
  23. ];
  24. }
  25. public function getParentSpanId() {
  26. return $this->parentSpanId;
  27. }
  28. public function getSpanId() {
  29. return ++$this->spanId;
  30. }
  31. public function getTraceId() {
  32. return $this->traceId;
  33. }
  34. }