44
55namespace OpenTelemetry \Instrumentation \AwsSdk ;
66
7+ use Aws \Middleware ;
8+ use Aws \ResultInterface ;
79use OpenTelemetry \API \Common \Instrumentation \InstrumentationInterface ;
810use OpenTelemetry \API \Common \Instrumentation \InstrumentationTrait ;
911use OpenTelemetry \API \Trace \SpanKind ;
1012use OpenTelemetry \API \Trace \TracerInterface ;
1113use OpenTelemetry \API \Trace \TracerProviderInterface ;
14+
15+ use OpenTelemetry \Context \Context ;
1216use OpenTelemetry \Context \Propagation \TextMapPropagatorInterface ;
1317
1418/**
@@ -23,6 +27,7 @@ class AwsSdkInstrumentation implements InstrumentationInterface
2327 public const SPAN_KIND = SpanKind::KIND_CLIENT ;
2428 private TextMapPropagatorInterface $ propagator ;
2529 private TracerProviderInterface $ tracerProvider ;
30+ private $ clients = [] ;
2631
2732 public function getName (): string
2833 {
@@ -69,52 +74,56 @@ public function getTracer(): TracerInterface
6974 return $ this ->tracerProvider ->getTracer ('io.opentelemetry.contrib.php ' );
7075 }
7176
72- public function activate (): bool
77+ public function getContext ():Context
7378 {
74- AwsGlobal::setInstrumentation ($ this );
79+ return Context::getRoot ();
80+ }
7581
82+ public function instrumentClients ($ clientsArray ) : void
83+ {
84+ $ this ->clients = $ clientsArray ;
85+ }
86+
87+ public function activate (): bool
88+ {
7689 try {
77- runkit7_method_copy ('Aws\AwsClient ' , '__call_copy ' , 'Aws\AwsClient ' , '__call ' );
78- runkit7_method_copy ('Aws\AwsClient ' , 'executeAsync_copy ' , 'Aws\AwsClient ' , 'executeAsync ' );
79-
80- runkit7_method_redefine (
81- 'Aws\AwsClient ' ,
82- '__call ' ,
83- '$name, $args ' ,
84- '
85-
86- $tracer = \OpenTelemetry\Instrumentation\AwsSdk\AwsGlobal::getInstrumentation()->getTracer();
90+ $ middleware = Middleware::tap (function ($ cmd , $ req ) {
91+ $ tracer = $ this ->getTracer ();
92+ $ propagator = $ this ->getPropagator ();
93+
8794 $ carrier = [];
88-
89- $this->span = $tracer->spanBuilder($this->getApi()->getServiceName() . "." . $name) ->setSpanKind(\OpenTelemetry\Instrumentation\AwsSdk\ AwsSdkInstrumentation::SPAN_KIND)->startSpan();
95+
96+ $ this ->span = $ tracer ->spanBuilder ($ this ->clientName . ' . ' . $ cmd -> getName ()) ->setSpanKind (AwsSdkInstrumentation::SPAN_KIND )->startSpan ();
9097 $ this ->scope = $ this ->span ->activate ();
91-
92- $propagator = \OpenTelemetry\Instrumentation\AwsSdk\AwsGlobal::getInstrumentation()->getPropagator();
93- $propagator->inject($carrier);
94-
98+
99+ $ propagator ->inject ($ carrier , null , $ this ->getContext ());
100+
101+ $ this ->span ->setAttributes ([
102+ 'rpc.method ' => $ cmd ->getName (),
103+ 'rpc.service ' => $ this ->clientName ,
104+ 'rpc.system ' => 'aws-api ' ,
105+ 'aws.region ' => $ this ->region ,
106+ ]);
107+ });
108+
109+ $ end_middleware = Middleware::mapResult (function (ResultInterface $ result ) {
95110 $ this ->span ->setAttributes ([
96- "rpc.method" => $name,
97- "rpc.service" => $this->getApi()->getServiceName(),
98- "rpc.system" => "aws-api",
99- "aws.region" => $this->getRegion()
111+ 'http.status_code ' => $ result ['@metadata ' ]['statusCode ' ],
100112 ]);
101-
102- return $this->__call_copy($name, $args);
103-
104-
105- ' ,
106- );
107-
108- runkit7_method_redefine (
109- 'Aws\AwsClient ' ,
110- 'executeAsync ' ,
111- '$command ' ,
112- '
113- $this->scope->detach();
114- $this->span->end();
115- return $this->executeAsync_copy($command);
116- ' ,
117- );
113+
114+ $ this ->span ->end ();
115+ $ this ->scope ->detach ();
116+
117+ return $ result ;
118+ });
119+
120+ foreach ($ this ->clients as $ client ) {
121+ $ this ->clientName = $ client ->getApi ()->getServiceName ();
122+ $ this ->region = $ client ->getRegion ();
123+
124+ $ client ->getHandlerList ()->prependInit ($ middleware , 'instrumentation ' );
125+ $ client ->getHandlerList ()->appendSign ($ end_middleware , 'end_instrumentation ' );
126+ }
118127 } catch (\Throwable $ e ) {
119128 return false ;
120129 }
0 commit comments