-
Notifications
You must be signed in to change notification settings - Fork 117
migrate extension propagator cloudtrace #418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
brettmc
merged 12 commits into
open-telemetry:main
from
weslenteche:migrate-extension-propagator-cloudtrace
Aug 8, 2025
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
91f483d
chore: update dependabot configuration [skip ci]
github-actions[bot] 721f9b8
Merge branch 'open-telemetry:main' into main
weslenteche aacb6cc
Merge branch 'open-telemetry:main' into main
weslenteche 62718ec
chore: update dependabot configuration [skip ci]
github-actions[bot] 8c766fd
Merge branch 'open-telemetry:main' into main
weslenteche 2f9b771
chore: update dependabot configuration [skip ci]
github-actions[bot] 10f7969
Merge branch 'open-telemetry:main' into main
weslenteche da0bc1b
migrate cloudtrace propagator
weslenteche ebfbc2f
fix: dependabot
weslenteche 8ab2c92
update readme
weslenteche af73a91
Merge branch 'open-telemetry:main' into migrate-extension-propagator-…
weslenteche 34b3809
applying code review adjustments
weslenteche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| * text=auto | ||
|
|
||
| *.md diff=markdown | ||
| *.php diff=php | ||
|
|
||
| /.gitattributes export-ignore | ||
| /.gitignore export-ignore | ||
| /.php-cs-fixer.php export-ignore | ||
| /phpstan.neon.dist export-ignore | ||
| /phpunit.xml.dist export-ignore | ||
| /psalm.xml.dist export-ignore | ||
| /tests export-ignore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /vendor/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php | ||
| $finder = PhpCsFixer\Finder::create() | ||
| ->exclude('vendor') | ||
| ->exclude('var/cache') | ||
| ->in(__DIR__); | ||
|
|
||
| $config = new PhpCsFixer\Config(); | ||
| return $config->setRules([ | ||
| 'concat_space' => ['spacing' => 'one'], | ||
| 'declare_equal_normalize' => ['space' => 'none'], | ||
| 'is_null' => true, | ||
| 'modernize_types_casting' => true, | ||
| 'ordered_imports' => true, | ||
| 'php_unit_construct' => true, | ||
| 'single_line_comment_style' => true, | ||
| 'yoda_style' => false, | ||
| '@PSR2' => true, | ||
| 'array_syntax' => ['syntax' => 'short'], | ||
| 'blank_line_after_opening_tag' => true, | ||
| 'blank_line_before_statement' => true, | ||
| 'cast_spaces' => true, | ||
| 'declare_strict_types' => true, | ||
| 'type_declaration_spaces' => true, | ||
| 'include' => true, | ||
| 'lowercase_cast' => true, | ||
| 'new_with_parentheses' => true, | ||
| 'no_extra_blank_lines' => true, | ||
| 'no_leading_import_slash' => true, | ||
| 'echo_tag_syntax' => true, | ||
| 'no_unused_imports' => true, | ||
| 'no_useless_else' => true, | ||
| 'no_useless_return' => true, | ||
| 'phpdoc_order' => true, | ||
| 'phpdoc_scalar' => true, | ||
| 'phpdoc_types' => true, | ||
| 'short_scalar_cast' => true, | ||
| 'blank_lines_before_namespace' => true, | ||
| 'single_quote' => true, | ||
| 'trailing_comma_in_multiline' => true, | ||
| ]) | ||
| ->setRiskyAllowed(true) | ||
| ->setFinder($finder); | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| [](https://github.com/opentelemetry-php/extension-propagator-cloudtrace/releases) | ||
| [](https://github.com/open-telemetry/opentelemetry-php/issues) | ||
| [](https://github.com/open-telemetry/opentelemetry-php-contrib/tree/main/src/Propagation/CloudTrace) | ||
| [](https://github.com/opentelemetry-php/extension-propagator-cloudtrace) | ||
| [](https://packagist.org/packages/open-telemetry/extension-propagator-cloudtrace/) | ||
| [](https://packagist.org/packages/open-telemetry/extension-propagator-cloudtrace/) | ||
|
|
||
| This is a read-only subtree split of https://github.com/open-telemetry/opentelemetry-php-contrib. | ||
|
|
||
| # OpenTelemetry CloudTrace Propagator | ||
|
|
||
| CloudTrace is a propagator that supports the specification for the header "x-cloud-trace-context" used for trace context propagation across | ||
| service boundaries. (https://cloud.google.com/trace/docs/setup#force-trace). OpenTelemetry PHP CloudTrace Propagator Extension provides | ||
| option to use it bi-directionally or one-way. One-way does not inject the header for downstream consumption, it only processes the incoming headers | ||
| and returns the correct span context. It only attaches to existing X-Cloud-Trace-Context traces and does not create downstream ones. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```sh | ||
| composer require open-telemetry/extension-propagator-cloudtrace | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| For one-way CloudTrace: | ||
|
|
||
| ``` | ||
| $propagator = CloudTracePropagator::getOneWayInstance(); | ||
| ``` | ||
|
|
||
| For bi-directional CloudTrace: | ||
|
|
||
| ``` | ||
| $propagator = CloudTracePropagator::getInstance(); | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use OpenTelemetry\Extension\Propagator\CloudTrace\CloudTracePropagator; | ||
| use OpenTelemetry\SDK\Registry; | ||
|
|
||
| if (!class_exists(Registry::class)) { | ||
| return; | ||
| } | ||
|
|
||
| Registry::registerTextMapPropagator( | ||
| 'cloudtrace', | ||
| CloudTracePropagator::getInstance() | ||
| ); | ||
|
|
||
| Registry::registerTextMapPropagator( | ||
| 'cloudtrace-oneway', | ||
| CloudTracePropagator::getOneWayInstance() | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| { | ||
| "name": "open-telemetry/extension-propagator-cloudtrace", | ||
| "description": "CloudTraceContext propagator extension for OpenTelemetry PHP.", | ||
| "keywords": ["opentelemetry", "otel", "tracing", "apm", "extension", "propagator", "cloudtrace"], | ||
| "type": "library", | ||
| "readme": "./README.md", | ||
| "license": "Apache-2.0", | ||
| "minimum-stability": "dev", | ||
| "prefer-stable": true, | ||
| "require": { | ||
| "php": "^8.1", | ||
| "open-telemetry/api": "^1.0", | ||
| "open-telemetry/context": "^1.0" | ||
| }, | ||
| "autoload": { | ||
| "psr-4":{ | ||
| "OpenTelemetry\\Extension\\Propagator\\CloudTrace\\": "src/" | ||
| }, | ||
| "files":[ | ||
| "_register.php" | ||
| ] | ||
| }, | ||
| "require-dev": { | ||
| "friendsofphp/php-cs-fixer": "^3", | ||
| "phan/phan": "^5.0", | ||
| "phpstan/phpstan": "^1.1", | ||
| "phpstan/phpstan-phpunit": "^1.0", | ||
| "psalm/plugin-phpunit": "^0.19.2", | ||
| "open-telemetry/sdk": "^1.0", | ||
| "phpunit/phpunit": "^9.5", | ||
| "vimeo/psalm": "^4|^5|^6" | ||
| }, | ||
| "config": { | ||
| "allow-plugins": { | ||
| "php-http/discovery": true, | ||
| "tbachert/spi": true | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| includes: | ||
| - vendor/phpstan/phpstan-phpunit/extension.neon | ||
|
|
||
| parameters: | ||
| tmpDir: var/cache/phpstan | ||
| level: 5 | ||
| paths: | ||
| - src | ||
| - tests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
|
|
||
| <phpunit | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd" | ||
| backupGlobals="false" | ||
| backupStaticAttributes="false" | ||
| cacheResult="false" | ||
| colors="false" | ||
| convertErrorsToExceptions="true" | ||
| convertNoticesToExceptions="true" | ||
| convertWarningsToExceptions="true" | ||
| forceCoversAnnotation="false" | ||
| processIsolation="false" | ||
| stopOnError="false" | ||
| stopOnFailure="false" | ||
| stopOnIncomplete="false" | ||
| stopOnSkipped="false" | ||
| stopOnRisky="false" | ||
| timeoutForSmallTests="1" | ||
| timeoutForMediumTests="10" | ||
| timeoutForLargeTests="60" | ||
| verbose="true"> | ||
|
|
||
| <coverage processUncoveredFiles="true" disableCodeCoverageIgnore="false"> | ||
| <include> | ||
| <directory>src</directory> | ||
| </include> | ||
| </coverage> | ||
|
|
||
| <php> | ||
| <ini name="date.timezone" value="UTC" /> | ||
| <ini name="display_errors" value="On" /> | ||
| <ini name="display_startup_errors" value="On" /> | ||
| <ini name="error_reporting" value="E_ALL" /> | ||
| </php> | ||
|
|
||
| <testsuites> | ||
| <testsuite name="unit"> | ||
| <directory>tests/Unit</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
|
|
||
| </phpunit> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?xml version="1.0"?> | ||
| <psalm | ||
| errorLevel="3" | ||
| cacheDirectory="var/cache/psalm" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xmlns="https://getpsalm.org/schema/config" | ||
| xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"> | ||
| <projectFiles> | ||
| <directory name="src"/> | ||
| <directory name="tests"/> | ||
| </projectFiles> | ||
| <plugins> | ||
| <pluginClass class="Psalm\PhpUnitPlugin\Plugin"/> | ||
| </plugins> | ||
| </psalm> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\Extension\Propagator\CloudTrace; | ||
|
|
||
| use OpenTelemetry\API\Trace\SpanContext; | ||
| use OpenTelemetry\API\Trace\SpanContextInterface; | ||
|
|
||
| /** | ||
| * This format using a human readable string encoding to propagate SpanContext. | ||
| * The current format of the header is `<trace-id>[/<span-id>][;o=<options>]`. | ||
| * The options are a bitmask of options. Currently the only option is the | ||
| * least significant bit which signals whether the request was traced or not | ||
| * (1 = traced, 0 = not traced). | ||
| */ | ||
| final class CloudTraceFormatter | ||
| { | ||
| const CONTEXT_HEADER_FORMAT = '/([0-9a-fA-F]{32})(?:\/(\d+))?(?:;o=(\d+))?/'; | ||
|
|
||
| /** | ||
| * Generate a SpanContext object from the Trace Context header | ||
| */ | ||
| public static function deserialize(string $header) : SpanContextInterface | ||
| { | ||
| $matched = preg_match(self::CONTEXT_HEADER_FORMAT, $header, $matches); | ||
|
|
||
| if (!$matched) { | ||
| return SpanContext::getInvalid(); | ||
| } | ||
| if (!array_key_exists(2, $matches) || empty($matches[2])) { | ||
| return SpanContext::getInvalid(); | ||
| } | ||
| if (!array_key_exists(3, $matches)) { | ||
| return SpanContext::getInvalid(); | ||
| } | ||
|
|
||
| return SpanContext::createFromRemoteParent( | ||
| strtolower($matches[1]), | ||
| Utils::leftZeroPad(Utils::decToHex($matches[2])), | ||
| (int) ($matches[3] === '1') | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Convert a SpanContextInterface to header string | ||
| */ | ||
| public static function serialize(SpanContextInterface $context) : string | ||
| { | ||
| $ret = $context->getTraceId(); | ||
| if ($context->getSpanId()) { | ||
| $ret .= '/' . Utils::hexToDec($context->getSpanId()); | ||
| } | ||
|
|
||
| return $ret . (';o=' . ($context->isSampled() ? '1' : '0')); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.