Skip to content

Commit 416e095

Browse files
committed
# Conflicts: # src/Instrumentation/Doctrine/composer.json
2 parents 829157c + c37dbac commit 416e095

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+6968
-72
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: OSSF Scorecard
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
schedule:
8+
- cron: "19 6 * * 1" # once a week
9+
workflow_dispatch:
10+
11+
permissions: read-all
12+
13+
jobs:
14+
analysis:
15+
runs-on: ubuntu-latest
16+
permissions:
17+
# Needed for Code scanning upload
18+
security-events: write
19+
# Needed for GitHub OIDC token if publish_results is true
20+
id-token: write
21+
steps:
22+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
23+
with:
24+
persist-credentials: false
25+
26+
- uses: ossf/scorecard-action@f49aabe0b5af0936a0987cfb85d86b75731b0186 # v2.4.1
27+
with:
28+
results_file: results.sarif
29+
results_format: sarif
30+
publish_results: true
31+
32+
# Upload the results as artifacts (optional). Commenting out will disable
33+
# uploads of run results in SARIF format to the repository Actions tab.
34+
# https://docs.github.com/en/actions/advanced-guides/storing-workflow-data-as-artifacts
35+
- name: "Upload artifact"
36+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
37+
with:
38+
name: SARIF file
39+
path: results.sarif
40+
retention-days: 5
41+
42+
# Upload the results to GitHub's code scanning dashboard (optional).
43+
# Commenting out will disable upload of results to your repo's Code Scanning dashboard
44+
- name: "Upload to code-scanning"
45+
uses: github/codeql-action/upload-sarif@5f8171a638ada777af81d42b55959a643bb29017 # v3.28.12
46+
with:
47+
sarif_file: results.sarif

.github/workflows/php.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ jobs:
5454
'Sampler/RuleBased',
5555
'Shims/OpenTracing',
5656
'Symfony',
57+
'Utils/Test'
5758
]
5859
exclude:
5960

.gitsplit.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ splits:
7878
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-sampler-rulebased.git"
7979
- prefix: "src/Shims/OpenTracing"
8080
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-shim-opentracing.git"
81+
- prefix: "src/Utils/Test"
82+
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-utils-test.git"
8183
# List of references to split (defined as regexp)
8284
origins:
8385
- ^main$

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959
"src/ResourceDetectors/Container/_register.php"
6060
]
6161
},
62+
"autoload-dev": {
63+
"psr-4": {
64+
"OpenTelemetry\\TestUtils\\": "src/",
65+
"OpenTelemetry\\TestUtils\\Tests\\": "tests/"
66+
}
67+
},
6268
"replace": {
6369
"open-telemetry/contrib-aws": "self.version",
6470
"open-telemetry/contrib-sdk-bundle": "self.version",
@@ -81,7 +87,8 @@
8187
"open-telemetry/opentelemetry-logger-monolog": "self.version",
8288
"open-telemetry/detector-container": "self.version",
8389
"open-telemetry/symfony-sdk-bundle": "self.version",
84-
"open-telemetry/opentracing-shim": "self.version"
90+
"open-telemetry/opentracing-shim": "self.version",
91+
"open-telemetry/test-utils": "self.version"
8592
},
8693
"config": {
8794
"sort-packages": true,

docker-compose.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
services:
22
php:
3+
image: opentelemetry-php:local-${PHP_VERSION}-cli
34
build:
45
context: ./docker
56
dockerfile: Dockerfile
67
args:
7-
- PHP_VERSION
8+
- PHP_VERSION:${PHP_VERSION}
89
volumes:
910
- ./:/usr/src/myapp
1011
user: "${PHP_USER}:root"

src/Aws/phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
<testsuite name="unit">
4040
<directory>tests/Unit</directory>
4141
</testsuite>
42+
<testsuite name="integration">
43+
<directory>tests/Integration</directory>
44+
</testsuite>
4245
</testsuites>
4346

4447
</phpunit>

src/Aws/src/AwsSdkInstrumentation.php

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
use Aws\ResultInterface;
99
use OpenTelemetry\API\Instrumentation\InstrumentationInterface;
1010
use OpenTelemetry\API\Instrumentation\InstrumentationTrait;
11-
use OpenTelemetry\API\Trace\SpanInterface;
1211
use OpenTelemetry\API\Trace\SpanKind;
1312
use OpenTelemetry\API\Trace\TracerInterface;
1413
use OpenTelemetry\API\Trace\TracerProviderInterface;
1514
use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface;
16-
use OpenTelemetry\Context\ScopeInterface;
1715

1816
/**
1917
* @experimental
@@ -25,13 +23,12 @@ class AwsSdkInstrumentation implements InstrumentationInterface
2523
public const NAME = 'AWS SDK Instrumentation';
2624
public const VERSION = '0.0.1';
2725
public const SPAN_KIND = SpanKind::KIND_CLIENT;
28-
private TextMapPropagatorInterface $propagator;
29-
private TracerProviderInterface $tracerProvider;
30-
private $clients = [] ;
31-
private string $clientName;
32-
private string $region;
33-
private SpanInterface $span;
34-
private ScopeInterface $scope;
26+
27+
private array $clients = [];
28+
29+
private array $instrumentedClients = [];
30+
31+
private array $spanStorage = [];
3532

3633
public function getName(): string
3734
{
@@ -79,61 +76,70 @@ public function getTracer(): TracerInterface
7976
}
8077

8178
/** @psalm-api */
82-
public function instrumentClients($clientsArray) : void
79+
public function instrumentClients($clientsArray): void
8380
{
8481
$this->clients = $clientsArray;
8582
}
8683

87-
/** @psalm-suppress ArgumentTypeCoercion */
8884
public function activate(): bool
8985
{
9086
try {
91-
$middleware = Middleware::tap(function ($cmd, $_req) {
92-
$tracer = $this->getTracer();
93-
$propagator = $this->getPropagator();
94-
95-
$carrier = [];
96-
/** @phan-suppress-next-line PhanTypeMismatchArgument */
97-
$this->span = $tracer->spanBuilder($this->clientName)->setSpanKind(AwsSdkInstrumentation::SPAN_KIND)->startSpan();
98-
$this->scope = $this->span->activate();
99-
100-
$propagator->inject($carrier);
101-
102-
/** @psalm-suppress PossiblyInvalidArgument */
103-
$this->span->setAttributes([
104-
'rpc.method' => $cmd->getName(),
105-
'rpc.service' => $this->clientName,
106-
'rpc.system' => 'aws-api',
107-
'aws.region' => $this->region,
108-
]);
109-
});
110-
111-
/** @psalm-suppress PossiblyInvalidArgument */
112-
$end_middleware = Middleware::mapResult(function (ResultInterface $result) {
113-
/**
114-
* Some AWS SDK Funtions, such as S3Client->getObjectUrl() do not actually perform on the wire comms
115-
* with AWS Servers, and therefore do not return with a populated AWS\Result object with valid @metadata
116-
* Check for the presence of @metadata before extracting status code as these calls are still
117-
* instrumented.
118-
*/
119-
if (isset($result['@metadata'])) {
120-
$this->span->setAttributes([
121-
'http.status_code' => $result['@metadata']['statusCode'], //@phan-suppress-current-line PhanTypeMismatchDimFetch
122-
]);
87+
foreach ($this->clients as $client) {
88+
$hash = spl_object_hash($client);
89+
if (isset($this->instrumentedClients[$hash])) {
90+
continue;
12391
}
12492

125-
$this->span->end();
126-
$this->scope->detach();
93+
$clientName = $client->getApi()->getServiceName();
94+
$region = $client->getRegion();
12795

128-
return $result;
129-
});
96+
$client->getHandlerList()->prependInit(Middleware::tap(function ($cmd, $_req) use ($clientName, $region, $hash) {
97+
$tracer = $this->getTracer();
98+
$propagator = $this->getPropagator();
13099

131-
foreach ($this->clients as $client) {
132-
$this->clientName = $client->getApi()->getServiceName();
133-
$this->region = $client->getRegion();
100+
$carrier = [];
101+
/** @phan-suppress-next-line PhanTypeMismatchArgument */
102+
$span = $tracer->spanBuilder($clientName)->setSpanKind(AwsSdkInstrumentation::SPAN_KIND)->startSpan();
103+
$scope = $span->activate();
104+
$this->spanStorage[$hash] = [$span, $scope];
134105

135-
$client->getHandlerList()->prependInit($middleware, 'instrumentation');
136-
$client->getHandlerList()->appendSign($end_middleware, 'end_instrumentation');
106+
$propagator->inject($carrier);
107+
108+
/** @psalm-suppress PossiblyInvalidArgument */
109+
$span->setAttributes([
110+
'rpc.method' => $cmd->getName(),
111+
'rpc.service' => $clientName,
112+
'rpc.system' => 'aws-api',
113+
'aws.region' => $region,
114+
]);
115+
}), 'instrumentation');
116+
117+
$client->getHandlerList()->appendSign(Middleware::mapResult(function (ResultInterface $result) use ($hash) {
118+
if (empty($this->spanStorage[$hash])) {
119+
return $result;
120+
}
121+
[$span, $scope] = $this->spanStorage[$hash];
122+
unset($this->spanStorage[$hash]);
123+
124+
/*
125+
* Some AWS SDK Functions, such as S3Client->getObjectUrl() do not actually perform on the wire comms
126+
* with AWS Servers, and therefore do not return with a populated AWS\Result object with valid @metadata
127+
* Check for the presence of @metadata before extracting status code as these calls are still
128+
* instrumented.
129+
*/
130+
if (isset($result['@metadata'])) {
131+
$span->setAttributes([
132+
'http.status_code' => $result['@metadata']['statusCode'], // @phan-suppress-current-line PhanTypeMismatchDimFetch
133+
]);
134+
}
135+
136+
$span->end();
137+
$scope->detach();
138+
139+
return $result;
140+
}), 'end_instrumentation');
141+
142+
$this->instrumentedClients[$hash] = 1;
137143
}
138144
} catch (\Throwable $e) {
139145
return false;

0 commit comments

Comments
 (0)