Skip to content

Commit bbc665f

Browse files
authored
feat: Update contrib for the experimental response propagator interface (#436)
* Revise contrib for response propagator * Bumped composer.json * Updated Yii composer.json * Fixed instrumentation integration tests * Fixed Propagation tests * Fixed style issue for Symfony * Fixed semconv issue of MongoDB * Bumped sem-conv version in MongoDB to 1.36
1 parent b81b43f commit bbc665f

File tree

5 files changed

+49
-38
lines changed

5 files changed

+49
-38
lines changed

_register.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use OpenTelemetry\Contrib\Propagation\TraceResponse\TraceResponsePropagator;
6+
use OpenTelemetry\SDK\Registry;
7+
8+
if (!class_exists(Registry::class)) {
9+
return;
10+
}
11+
12+
Registry::registerResponsePropagator(
13+
'traceresponse',
14+
TraceResponsePropagator::getInstance()
15+
);

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,23 @@
1010
"prefer-stable": true,
1111
"require": {
1212
"php": "^8.1",
13-
"open-telemetry/context": "^1.0"
13+
"open-telemetry/context": "^1.4"
1414
},
1515
"autoload": {
1616
"psr-4": {
1717
"OpenTelemetry\\Contrib\\Propagation\\TraceResponse\\": "src/"
18-
}
18+
},
19+
"files": [
20+
"_register.php"
21+
]
1922
},
2023
"require-dev": {
2124
"friendsofphp/php-cs-fixer": "^3",
2225
"phan/phan": "^5.0",
2326
"phpstan/phpstan": "^1.1",
2427
"phpstan/phpstan-phpunit": "^1.0",
2528
"psalm/plugin-phpunit": "^0.19.2",
26-
"open-telemetry/sdk": "^1.0",
29+
"open-telemetry/sdk": "^1.8",
2730
"phpunit/phpunit": "^9.5",
2831
"vimeo/psalm": "6.4.0",
2932
"symfony/http-client": "^5.4|^6.0",

src/ResponsePropagator.php

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

src/TraceResponsePropagator.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,38 @@
99
use OpenTelemetry\Context\ContextInterface;
1010
use OpenTelemetry\Context\Propagation\ArrayAccessGetterSetter;
1111
use OpenTelemetry\Context\Propagation\PropagationSetterInterface;
12+
use OpenTelemetry\Context\Propagation\ResponsePropagatorInterface;
1213

1314
/**
1415
* Provides a ResponsePropagator for the Trace Context HTTP Response Headers Format
1516
*
1617
* @see https://w3c.github.io/trace-context/#trace-context-http-response-headers-format
1718
*/
18-
final class TraceResponsePropagator implements ResponsePropagator
19+
final class TraceResponsePropagator implements ResponsePropagatorInterface
1920
{
2021
const IS_SAMPLED = '01';
2122
const NOT_SAMPLED = '00';
2223
const SUPPORTED_VERSION = '00';
2324
const TRACERESPONSE = 'traceresponse';
2425

26+
private static ?self $instance = null;
27+
2528
public function fields(): array
2629
{
2730
return [
2831
self::TRACERESPONSE,
2932
];
3033
}
3134

35+
public static function getInstance(): self
36+
{
37+
if (null === self::$instance) {
38+
self::$instance = new self();
39+
}
40+
41+
return self::$instance;
42+
}
43+
3244
public function inject(&$carrier, ?PropagationSetterInterface $setter = null, ?ContextInterface $context = null): void
3345
{
3446
$setter = $setter ?? ArrayAccessGetterSetter::getInstance();

tests/Unit/PropagatorTest.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use OpenTelemetry\API\Trace\TraceState;
1212
use OpenTelemetry\Context\Context;
1313
use OpenTelemetry\Context\ContextInterface;
14-
use OpenTelemetry\Contrib\Propagation\TraceResponse\TraceResponsePropagator as Propagator;
14+
use OpenTelemetry\Contrib\Propagation\TraceResponse\TraceResponsePropagator;
1515
use OpenTelemetry\SDK\Trace\Span;
1616
use PHPUnit\Framework\TestCase;
1717

@@ -21,15 +21,20 @@ class PropagatorTest extends TestCase
2121
private const SPAN_ID = '53995c3f42cd8ad8';
2222
private const TRACERESPONSE_HEADER_SAMPLED = '00-5759e988bd862e3fe1be46a994272793-53995c3f42cd8ad8-01';
2323
private const TRACERESPONSE_HEADER_NOT_SAMPLED = '00-5759e988bd862e3fe1be46a994272793-53995c3f42cd8ad8-00';
24+
private TraceResponsePropagator $propagator;
2425

26+
#[\Override]
27+
protected function setUp(): void
28+
{
29+
$this->propagator = TraceResponsePropagator::getInstance();
30+
}
2531
/**
2632
* @test
2733
* fields() should return an array of fields with AWS X-Ray Trace ID Header
2834
*/
2935
public function test_fields()
3036
{
31-
$propagator = new Propagator();
32-
$this->assertSame($propagator->fields(), [Propagator::TRACERESPONSE]);
37+
$this->assertSame($this->propagator->fields(), [TraceResponsePropagator::TRACERESPONSE]);
3338
}
3439

3540
/**
@@ -40,7 +45,7 @@ public function test_fields()
4045
public function test_inject_valid_sampled_trace_id()
4146
{
4247
$carrier = [];
43-
(new Propagator())->inject(
48+
$this->propagator->inject(
4449
$carrier,
4550
null,
4651
$this->withSpanContext(
@@ -50,7 +55,7 @@ public function test_inject_valid_sampled_trace_id()
5055
);
5156

5257
$this->assertSame(
53-
[Propagator::TRACERESPONSE => self::TRACERESPONSE_HEADER_SAMPLED],
58+
[TraceResponsePropagator::TRACERESPONSE => self::TRACERESPONSE_HEADER_SAMPLED],
5459
$carrier
5560
);
5661
}
@@ -62,7 +67,7 @@ public function test_inject_valid_sampled_trace_id()
6267
public function test_inject_valid_not_sampled_trace_id()
6368
{
6469
$carrier = [];
65-
(new Propagator())->inject(
70+
$this->propagator->inject(
6671
$carrier,
6772
null,
6873
$this->withSpanContext(
@@ -72,7 +77,7 @@ public function test_inject_valid_not_sampled_trace_id()
7277
);
7378

7479
$this->assertSame(
75-
[Propagator::TRACERESPONSE => self::TRACERESPONSE_HEADER_NOT_SAMPLED],
80+
[TraceResponsePropagator::TRACERESPONSE => self::TRACERESPONSE_HEADER_NOT_SAMPLED],
7681
$carrier
7782
);
7883
}
@@ -84,7 +89,7 @@ public function test_inject_valid_not_sampled_trace_id()
8489
public function test_inject_trace_id_with_trace_state()
8590
{
8691
$carrier = [];
87-
(new Propagator())->inject(
92+
$this->propagator->inject(
8893
$carrier,
8994
null,
9095
$this->withSpanContext(
@@ -94,7 +99,7 @@ public function test_inject_trace_id_with_trace_state()
9499
);
95100

96101
$this->assertSame(
97-
[Propagator::TRACERESPONSE => self::TRACERESPONSE_HEADER_SAMPLED],
102+
[TraceResponsePropagator::TRACERESPONSE => self::TRACERESPONSE_HEADER_SAMPLED],
98103
$carrier
99104
);
100105
}
@@ -106,7 +111,7 @@ public function test_inject_trace_id_with_trace_state()
106111
public function test_inject_trace_id_with_invalid_span_context()
107112
{
108113
$carrier = [];
109-
(new Propagator())->inject(
114+
$this->propagator->inject(
110115
$carrier,
111116
null,
112117
$this->withSpanContext(

0 commit comments

Comments
 (0)