77use DateInterval ;
88use DateTimeInterface ;
99use Illuminate \Contracts \Queue \Queue as QueueContract ;
10+ use OpenTelemetry \API \Instrumentation \AutoInstrumentation \HookManager ;
11+ use OpenTelemetry \API \Logs \LoggerInterface ;
12+ use OpenTelemetry \API \Metrics \MeterInterface ;
1013use OpenTelemetry \API \Trace \SpanKind ;
14+ use OpenTelemetry \API \Trace \TracerInterface ;
1115use OpenTelemetry \Context \Context ;
1216use OpenTelemetry \Contrib \Instrumentation \Laravel \Hooks \Illuminate \Queue \AttributesBuilder ;
13- use OpenTelemetry \Contrib \Instrumentation \Laravel \Hooks \LaravelHook ;
14- use OpenTelemetry \Contrib \Instrumentation \Laravel \Hooks \LaravelHookTrait ;
17+ use OpenTelemetry \Contrib \Instrumentation \Laravel \Hooks \Hook ;
1518use OpenTelemetry \Contrib \Instrumentation \Laravel \Hooks \PostHookTrait ;
16- use function OpenTelemetry \Instrumentation \hook ;
19+ use OpenTelemetry \Contrib \ Instrumentation \Laravel \ LaravelConfiguration ;
1720use OpenTelemetry \SemConv \TraceAttributes ;
1821use OpenTelemetry \SemConv \TraceAttributeValues ;
1922use Throwable ;
2023
21- class Queue implements LaravelHook
24+ class Queue implements Hook
2225{
2326 use AttributesBuilder;
24- use LaravelHookTrait;
2527 use PostHookTrait;
2628
27- public function instrument (): void
28- {
29- $ this ->hookBulk ();
30- $ this ->hookLater ();
31- $ this ->hookPushRaw ();
29+ public function instrument (
30+ HookManager $ hookManager ,
31+ LaravelConfiguration $ configuration ,
32+ LoggerInterface $ logger ,
33+ MeterInterface $ meter ,
34+ TracerInterface $ tracer ,
35+ ): void {
36+ $ this ->hookBulk ($ hookManager , $ tracer );
37+ $ this ->hookLater ($ hookManager , $ tracer );
38+ $ this ->hookPushRaw ($ hookManager , $ tracer );
3239 }
3340
34- protected function hookBulk (): bool
41+ protected function hookBulk (HookManager $ hookManager , TracerInterface $ tracer ): void
3542 {
36- return hook (
43+ $ hookManager -> hook (
3744 QueueContract::class,
3845 'bulk ' ,
39- pre : function (QueueContract $ queue , array $ params , string $ class , string $ function , ?string $ filename , ?int $ lineno ) {
46+ preHook : function (QueueContract $ queue , array $ params , string $ class , string $ function , ?string $ filename , ?int $ lineno) use ( $ tracer ) {
4047 $ attributes = array_merge ([
4148 TraceAttributes::CODE_FUNCTION => $ function ,
4249 TraceAttributes::CODE_NAMESPACE => $ class ,
@@ -46,8 +53,7 @@ protected function hookBulk(): bool
4653 ], $ this ->contextualMessageSystemAttributes ($ queue , []));
4754
4855 /** @psalm-suppress ArgumentTypeCoercion */
49- $ span = $ this ->instrumentation
50- ->tracer ()
56+ $ span = $ tracer
5157 ->spanBuilder (vsprintf ('%s %s ' , [
5258 /** @phan-suppress-next-line PhanUndeclaredMethod */
5359 method_exists ($ queue , 'getQueue ' ) ? $ queue ->getQueue ($ params [2 ] ?? null ) : $ queue ->getConnectionName (),
@@ -61,18 +67,18 @@ protected function hookBulk(): bool
6167
6268 return $ params ;
6369 },
64- post : function (QueueContract $ queue , array $ params , $ returnValue , ?Throwable $ exception ) {
70+ postHook : function (QueueContract $ queue , array $ params , $ returnValue , ?Throwable $ exception ) {
6571 $ this ->endSpan ($ exception );
6672 },
6773 );
6874 }
6975
70- protected function hookLater (): bool
76+ protected function hookLater (HookManager $ hookManager , TracerInterface $ tracer ): void
7177 {
72- return hook (
78+ $ hookManager -> hook (
7379 QueueContract::class,
7480 'later ' ,
75- pre : function (QueueContract $ queue , array $ params , string $ class , string $ function , ?string $ filename , ?int $ lineno ) {
81+ preHook : function (QueueContract $ queue , array $ params , string $ class , string $ function , ?string $ filename , ?int $ lineno) use ( $ tracer ) {
7682 $ estimateDeliveryTimestamp = match (true ) {
7783 is_int ($ params [0 ]) => (new \DateTimeImmutable ())->add (new DateInterval ("PT {$ params [0 ]}S " ))->getTimestamp (),
7884 $ params [0 ] instanceof DateInterval => (new \DateTimeImmutable ())->add ($ params [0 ])->getTimestamp (),
@@ -89,8 +95,7 @@ protected function hookLater(): bool
8995 ];
9096
9197 /** @psalm-suppress ArgumentTypeCoercion */
92- $ span = $ this ->instrumentation
93- ->tracer ()
98+ $ span = $ tracer
9499 ->spanBuilder (vsprintf ('%s %s ' , [
95100 /** @phan-suppress-next-line PhanUndeclaredMethod */
96101 method_exists ($ queue , 'getQueue ' ) ? $ queue ->getQueue ($ params [2 ] ?? null ) : $ queue ->getConnectionName (),
@@ -104,25 +109,24 @@ protected function hookLater(): bool
104109
105110 return $ params ;
106111 },
107- post : function (QueueContract $ queue , array $ params , $ returnValue , ?Throwable $ exception ) {
112+ postHook : function (QueueContract $ queue , array $ params , $ returnValue , ?Throwable $ exception ) {
108113 $ this ->endSpan ($ exception );
109114 },
110115 );
111116 }
112117
113- protected function hookPushRaw (): bool
118+ protected function hookPushRaw (HookManager $ hookManager , TracerInterface $ tracer ): void
114119 {
115- return hook (
120+ $ hookManager -> hook (
116121 QueueContract::class,
117122 'pushRaw ' ,
118- pre : function (QueueContract $ queue , array $ params , string $ class , string $ function , ?string $ filename , ?int $ lineno ) {
123+ preHook : function (QueueContract $ queue , array $ params , string $ class , string $ function , ?string $ filename , ?int $ lineno) use ( $ tracer ) {
119124 /** @phan-suppress-next-line PhanParamTooFewUnpack */
120125 $ attributes = $ this ->buildMessageAttributes ($ queue , ...$ params );
121126
122127 $ parent = Context::getCurrent ();
123128 /** @psalm-suppress ArgumentTypeCoercion */
124- $ span = $ this ->instrumentation
125- ->tracer ()
129+ $ span = $ tracer
126130 ->spanBuilder (vsprintf ('%s %s ' , [
127131 $ attributes [TraceAttributes::MESSAGING_DESTINATION_NAME ],
128132 TraceAttributeValues::MESSAGING_OPERATION_CREATE ,
@@ -135,7 +139,7 @@ protected function hookPushRaw(): bool
135139
136140 return $ params ;
137141 },
138- post : function (QueueContract $ queue , array $ params , $ returnValue , ?Throwable $ exception ) {
142+ postHook : function (QueueContract $ queue , array $ params , $ returnValue , ?Throwable $ exception ) {
139143 $ this ->endSpan ($ exception );
140144 },
141145 );
0 commit comments