|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * This file is part of friendsofhyperf/components. |
| 6 | + * |
| 7 | + * @link https://github.com/friendsofhyperf/components |
| 8 | + * @document https://github.com/friendsofhyperf/components/blob/main/README.md |
| 9 | + |
| 10 | + */ |
| 11 | + |
| 12 | +namespace FriendsOfHyperf\Sentry; |
| 13 | + |
| 14 | +use FriendsOfHyperf\Sentry\Util\Carrier; |
| 15 | +use Hyperf\Context\Context; |
| 16 | +use Sentry\Tracing\SpanContext; |
| 17 | + |
| 18 | +class SentryContext |
| 19 | +{ |
| 20 | + public const CTX_CRON_CHECKIN_ID = 'sentry.ctx.crons.checkin_id'; |
| 21 | + |
| 22 | + public const CTX_DISABLE_COROUTINE_TRACING = 'sentry.ctx.coroutine.disabled'; |
| 23 | + |
| 24 | + public const CTX_CARRIER = 'sentry.ctx.carrier'; |
| 25 | + |
| 26 | + public const CTX_ELASTICSEARCH_SPAN_DATA = 'sentry.ctx.elasticsearch.span.data'; |
| 27 | + |
| 28 | + public const CTX_DB_SERVER_ADDRESS = 'sentry.ctx.db.server.address'; |
| 29 | + |
| 30 | + public const CTX_DB_SERVER_PORT = 'sentry.ctx.db.server.port'; |
| 31 | + |
| 32 | + public const CTX_REDIS_SERVER_ADDRESS = 'sentry.ctx.redis.server.address'; |
| 33 | + |
| 34 | + public const CTX_REDIS_SERVER_PORT = 'sentry.ctx.redis.server.port'; |
| 35 | + |
| 36 | + public const CTX_RPC_SERVER_ADDRESS = 'sentry.ctx.rpc.server.address'; |
| 37 | + |
| 38 | + public const CTX_RPC_SERVER_PORT = 'sentry.ctx.rpc.server.port'; |
| 39 | + |
| 40 | + public const CTX_RPC_SPAN_CONTEXT = 'sentry.ctx.rpc.span.context'; |
| 41 | + |
| 42 | + public static function disableTracing(): void |
| 43 | + { |
| 44 | + Context::set(self::CTX_DISABLE_COROUTINE_TRACING, true); |
| 45 | + } |
| 46 | + |
| 47 | + public static function enableTracing(): void |
| 48 | + { |
| 49 | + Context::set(self::CTX_DISABLE_COROUTINE_TRACING, false); |
| 50 | + } |
| 51 | + |
| 52 | + public static function isTracingDisabled(): bool |
| 53 | + { |
| 54 | + return (bool) Context::get(self::CTX_DISABLE_COROUTINE_TRACING, false); |
| 55 | + } |
| 56 | + |
| 57 | + public static function setCronCheckInId(string $checkInId): void |
| 58 | + { |
| 59 | + Context::set(self::CTX_CRON_CHECKIN_ID, $checkInId); |
| 60 | + } |
| 61 | + |
| 62 | + public static function getCronCheckInId(): ?string |
| 63 | + { |
| 64 | + return Context::get(self::CTX_CRON_CHECKIN_ID); |
| 65 | + } |
| 66 | + |
| 67 | + public static function setCarrier(Carrier $carrier): void |
| 68 | + { |
| 69 | + Context::set(self::CTX_CARRIER, $carrier); |
| 70 | + } |
| 71 | + |
| 72 | + public static function getCarrier(?int $coroutineId = null): ?Carrier |
| 73 | + { |
| 74 | + return Context::get(self::CTX_CARRIER, coroutineId: $coroutineId); |
| 75 | + } |
| 76 | + |
| 77 | + public static function setRedisServerAddress(string $address): void |
| 78 | + { |
| 79 | + Context::set(self::CTX_REDIS_SERVER_ADDRESS, $address); |
| 80 | + } |
| 81 | + |
| 82 | + public static function getRedisServerAddress(): ?string |
| 83 | + { |
| 84 | + return Context::get(self::CTX_REDIS_SERVER_ADDRESS); |
| 85 | + } |
| 86 | + |
| 87 | + public static function setRedisServerPort(int $port): void |
| 88 | + { |
| 89 | + Context::set(self::CTX_REDIS_SERVER_PORT, $port); |
| 90 | + } |
| 91 | + |
| 92 | + public static function getRedisServerPort(): ?int |
| 93 | + { |
| 94 | + return Context::get(self::CTX_REDIS_SERVER_PORT); |
| 95 | + } |
| 96 | + |
| 97 | + public static function setRpcServerAddress(string $address): void |
| 98 | + { |
| 99 | + Context::set(self::CTX_RPC_SERVER_ADDRESS, $address); |
| 100 | + } |
| 101 | + |
| 102 | + public static function getRpcServerAddress(): ?string |
| 103 | + { |
| 104 | + return Context::get(self::CTX_RPC_SERVER_ADDRESS); |
| 105 | + } |
| 106 | + |
| 107 | + public static function setRpcServerPort(int $port): void |
| 108 | + { |
| 109 | + Context::set(self::CTX_RPC_SERVER_PORT, $port); |
| 110 | + } |
| 111 | + |
| 112 | + public static function getRpcServerPort(): ?int |
| 113 | + { |
| 114 | + return Context::get(self::CTX_RPC_SERVER_PORT); |
| 115 | + } |
| 116 | + |
| 117 | + public static function setDbServerAddress(string $address): void |
| 118 | + { |
| 119 | + Context::set(self::CTX_DB_SERVER_ADDRESS, $address); |
| 120 | + } |
| 121 | + |
| 122 | + public static function getDbServerAddress(): ?string |
| 123 | + { |
| 124 | + return Context::get(self::CTX_DB_SERVER_ADDRESS); |
| 125 | + } |
| 126 | + |
| 127 | + public static function setDbServerPort(int $port): void |
| 128 | + { |
| 129 | + Context::set(self::CTX_DB_SERVER_PORT, $port); |
| 130 | + } |
| 131 | + |
| 132 | + public static function getDbServerPort(): ?int |
| 133 | + { |
| 134 | + return Context::get(self::CTX_DB_SERVER_PORT); |
| 135 | + } |
| 136 | + |
| 137 | + public static function setElasticsearchSpanData(array $data): void |
| 138 | + { |
| 139 | + Context::set(self::CTX_ELASTICSEARCH_SPAN_DATA, $data); |
| 140 | + } |
| 141 | + |
| 142 | + public static function getElasticsearchSpanData(): ?array |
| 143 | + { |
| 144 | + return Context::get(self::CTX_ELASTICSEARCH_SPAN_DATA); |
| 145 | + } |
| 146 | + |
| 147 | + public static function setRpcSpanContext(SpanContext $spanContext): void |
| 148 | + { |
| 149 | + Context::set(self::CTX_RPC_SPAN_CONTEXT, $spanContext); |
| 150 | + } |
| 151 | + |
| 152 | + public static function getRpcSpanContext(): ?SpanContext |
| 153 | + { |
| 154 | + return Context::get(self::CTX_RPC_SPAN_CONTEXT); |
| 155 | + } |
| 156 | + |
| 157 | + public static function destroyRpcSpanContext(): void |
| 158 | + { |
| 159 | + Context::destroy(self::CTX_RPC_SPAN_CONTEXT); |
| 160 | + } |
| 161 | +} |
0 commit comments