Skip to content

Commit e992a00

Browse files
committed
Remove runkit7 dependency and update activate method implementation
1 parent 04ad944 commit e992a00

File tree

4 files changed

+51
-62
lines changed

4 files changed

+51
-62
lines changed

.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
php-version: ${{ matrix.php-version }}
2626
coverage: xdebug
2727
tools: php-cs-fixer
28-
extensions: ast, grpc, runkit7
28+
extensions: ast, grpc
2929

3030
- name: Validate composer.json and composer.lock
3131
run: composer validate

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"ext-json": "*",
1717
"open-telemetry/opentelemetry": "^0.0.15",
1818
"php-http/discovery": "^1.14",
19+
"aws/aws-sdk-php": "^3.232",
1920
"php-http/message": "^1.12"
2021
},
2122
"replace": {
@@ -61,8 +62,7 @@
6162
"suggest": {
6263
"symfony/config": "Needed to use otel-sdk-bundle",
6364
"symfony/dependency-injection": "Needed to use otel-sdk-bundle",
64-
"symfony/options-resolver": "Needed to use otel-sdk-bundle",
65-
"runkit7": "Needed to use AWS SDK Instrumentation"
65+
"symfony/options-resolver": "Needed to use otel-sdk-bundle"
6666
},
6767
"scripts": {
6868
"post-install-cmd": [

src/Instrumentation/AwsSdk/AwsGlobal.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Instrumentation/AwsSdk/AwsSdkInstrumentation.php

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
namespace OpenTelemetry\Instrumentation\AwsSdk;
66

7+
use Aws\Middleware;
8+
use Aws\ResultInterface;
79
use OpenTelemetry\API\Common\Instrumentation\InstrumentationInterface;
810
use OpenTelemetry\API\Common\Instrumentation\InstrumentationTrait;
911
use OpenTelemetry\API\Trace\SpanKind;
1012
use OpenTelemetry\API\Trace\TracerInterface;
1113
use OpenTelemetry\API\Trace\TracerProviderInterface;
14+
15+
use OpenTelemetry\Context\Context;
1216
use 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

Comments
 (0)