-
Couldn't load subscription status.
- Fork 212
Add Instana Propagator: Instana Propagator Extension #1582
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
Closed
HeenaBansal20
wants to merge
6
commits into
open-telemetry:main
from
HeenaBansal20:feature/add-instana-propagator
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aa1ba39
Add Instana Propagator
HeenaBansal20 0cae628
Fixing Style CI check
HeenaBansal20 8b5e91e
Handled only sampled header in carrier
HeenaBansal20 7acb45d
Fixed the testcase when x-instana-l is only defined
HeenaBansal20 86f0a77
Addressed review comments and added new testcase for the same usecase
HeenaBansal20 7df2b96
Removed case sensitive comparision of HEADERS as it's already taken i…
HeenaBansal20 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
175 changes: 175 additions & 0 deletions
175
src/Extension/Propagator/Instana/InstanaMultiPropagator.php
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,175 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\Extension\Propagator\Instana; | ||
|
|
||
| use OpenTelemetry\API\Trace\NonRecordingSpan; | ||
| use OpenTelemetry\API\Trace\Span; | ||
| use OpenTelemetry\API\Trace\SpanContext; | ||
| use OpenTelemetry\API\Trace\SpanContextInterface; | ||
| use OpenTelemetry\API\Trace\SpanContextValidator; | ||
| use OpenTelemetry\API\Trace\TraceFlags; | ||
| use OpenTelemetry\Context\Context; | ||
| use OpenTelemetry\Context\ContextInterface; | ||
| use OpenTelemetry\Context\Propagation\ArrayAccessGetterSetter; | ||
| use OpenTelemetry\Context\Propagation\PropagationGetterInterface; | ||
| use OpenTelemetry\Context\Propagation\PropagationSetterInterface; | ||
| use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface; | ||
|
|
||
| /** | ||
| * InstanaPropagator is a propagator that supports the specification for multiple | ||
| * "instana" http headers used for trace context propagation across service | ||
| * boundaries. | ||
| */ | ||
| final class InstanaMultiPropagator implements TextMapPropagatorInterface | ||
| { | ||
| /** | ||
| * The X-INSTANA-T header is required and is encoded as 32 lower-hex characters. | ||
| * For example, a 128-bit TraceId header might look like: X-Instana-T: 463ac35c9f6413ad48485a3953bb6124 . | ||
| * | ||
| */ | ||
| private const INSTANA_TRACE_ID_HEADER = 'X-INSTANA-T'; | ||
|
|
||
| /** | ||
| * The X-Instana-S header must be present on a child span and absent on the root span. | ||
| * It is encoded as 16 lower-hex characters. | ||
| * For example, a ParentSpanId header might look like: X-Instana-S: 0020000000000001 | ||
| * | ||
| */ | ||
| private const INSTANA_SPAN_ID_HEADER = 'X-INSTANA-S'; | ||
|
|
||
| /** | ||
| * An accept sampling decision is encoded as X-INSTANA-L: 1 and a deny as X-INSTANA-L: 0. | ||
| * Absent means defer the decision to the receiver of this header. | ||
| * For example, a Sampled header might look like: X-Instana-L: 1 | ||
| * | ||
| */ | ||
| private const INSTANA_LEVEL_HEADER = 'X-INSTANA-L'; | ||
|
|
||
| private const IS_SAMPLED = '1'; | ||
| private const VALID_SAMPLED = [self::IS_SAMPLED, 'true']; | ||
| private const IS_NOT_SAMPLED = '0'; | ||
| private const VALID_NON_SAMPLED = [self::IS_NOT_SAMPLED, 'false']; | ||
|
|
||
| private const FIELDS = [ | ||
| self::INSTANA_TRACE_ID_HEADER, | ||
| self::INSTANA_SPAN_ID_HEADER, | ||
| self::INSTANA_LEVEL_HEADER, | ||
| ]; | ||
|
|
||
| private static ?self $instance = null; | ||
|
|
||
| public static function getInstance(): self | ||
| { | ||
| if (null === self::$instance) { | ||
| self::$instance = new self(); | ||
| } | ||
|
|
||
| return self::$instance; | ||
| } | ||
|
|
||
| /** {@inheritdoc} */ | ||
| public function fields(): array | ||
| { | ||
| return self::FIELDS; | ||
| } | ||
|
|
||
| /** {@inheritdoc} */ | ||
| public function inject(&$carrier, ?PropagationSetterInterface $setter = null, ?ContextInterface $context = null): void | ||
| { | ||
| $setter ??= ArrayAccessGetterSetter::getInstance(); | ||
| $context ??= Context::getCurrent(); | ||
| $spanContext = Span::fromContext($context)->getContext(); | ||
|
|
||
| if (!$spanContext->isValid()) { | ||
| return; | ||
| } | ||
|
|
||
| // Inject multiple Instana headers | ||
| $setter->set($carrier, self::INSTANA_TRACE_ID_HEADER, $spanContext->getTraceId()); | ||
| $setter->set($carrier, self::INSTANA_SPAN_ID_HEADER, $spanContext->getSpanId()); | ||
| $setter->set($carrier, self::INSTANA_LEVEL_HEADER, $spanContext->isSampled() ? self::IS_SAMPLED : self::IS_NOT_SAMPLED); | ||
| } | ||
|
|
||
| public function extract($carrier, ?PropagationGetterInterface $getter = null, ?ContextInterface $context = null): ContextInterface | ||
| { | ||
| $getter ??= ArrayAccessGetterSetter::getInstance(); | ||
| $context ??= Context::getCurrent(); | ||
|
|
||
| $traceId = self::readHeader($carrier, $getter, self::INSTANA_TRACE_ID_HEADER); | ||
| $spanId = self::readHeader($carrier, $getter, self::INSTANA_SPAN_ID_HEADER); | ||
| $level = self::getSampledValue($carrier, $getter); | ||
|
|
||
| $spanContext = self::extractImpl($carrier, $getter); | ||
|
|
||
| if (($traceId === '' && $spanId === '') && $level !== null) { | ||
| return (new NonRecordingSpan($spanContext)) | ||
| ->storeInContext($context); | ||
|
|
||
| } elseif (!$spanContext->isValid()) { | ||
| return $context; | ||
| } | ||
|
|
||
| return $context->withContextValue(Span::wrap($spanContext)); | ||
|
|
||
| } | ||
|
|
||
| private static function readHeader($carrier, PropagationGetterInterface $getter, string $key): string | ||
| { | ||
| // By convention, X-INSTANA-* headers are sent all-upper-case. For http and http/2, Node.js normalizes all headers to | ||
| // all-lower-case, that's why we first read via the lower case variant of the header name. For other protocols, no | ||
| // such normalization happens, so we also need to read via the original all-upper-case variant. | ||
| $header = $getter->get($carrier, strtolower($key)) ?: $getter->get($carrier, $key); | ||
|
|
||
| // Return the header or an empty string if not found | ||
| return $header ?: ''; | ||
| } | ||
| private static function getSampledValue($carrier, PropagationGetterInterface $getter): ?int | ||
| { | ||
| $value = $getter->get($carrier, self::INSTANA_LEVEL_HEADER); | ||
|
|
||
| if ($value === null) { | ||
| return null; | ||
| } | ||
|
|
||
| if (in_array(strtolower($value), self::VALID_SAMPLED)) { | ||
| return (int) self::IS_SAMPLED; | ||
| } | ||
| if (in_array(strtolower($value), self::VALID_NON_SAMPLED)) { | ||
| return (int) self::IS_NOT_SAMPLED; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private static function extractImpl($carrier, PropagationGetterInterface $getter): SpanContextInterface | ||
| { | ||
| $traceId = self::readHeader($carrier, $getter, self::INSTANA_TRACE_ID_HEADER); | ||
| $spanId = self::readHeader($carrier, $getter, self::INSTANA_SPAN_ID_HEADER); | ||
| $level = self::getSampledValue($carrier, $getter); | ||
|
|
||
| if ($traceId && strlen($traceId) < 32) { | ||
| $traceId = str_pad($traceId, 32, '0', STR_PAD_LEFT); | ||
| } | ||
|
|
||
| if ($spanId && strlen($spanId) < 16) { | ||
| $spanId = str_pad($spanId, 16, '0', STR_PAD_LEFT); | ||
| } | ||
|
|
||
| if ((SpanContextValidator::isValidTraceId($traceId) || SpanContextValidator::isValidSpanId($spanId))) { | ||
| return SpanContext::createFromRemoteParent( | ||
| $traceId, | ||
| $spanId, | ||
| $level ? TraceFlags::SAMPLED : TraceFlags::DEFAULT | ||
| ); | ||
| } | ||
HeenaBansal20 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return SpanContext::createFromRemoteParent( | ||
| SpanContextValidator::INVALID_TRACE, | ||
| SpanContextValidator::INVALID_SPAN, | ||
| $level ? TraceFlags::SAMPLED : TraceFlags::DEFAULT | ||
| ); | ||
|
|
||
| } | ||
| } | ||
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,60 @@ | ||
| OpenTelemetry Instana Propagator | ||
|
|
||
| [![NPM Published Version][npm-img]][npm-url] | ||
| [![Apache License][license-image]][license-image] | ||
|
|
||
|
|
||
| The OpenTelemetry Propagator for Instana provides HTTP header propagation for systems that are using IBM Observability by Instana. | ||
| This propagator translates the Instana trace correlation headers (`X-INSTANA-T/X-INSTANA-S/X-INSTANA-L`) into the OpenTelemetry `SpanContext`, and vice versa. | ||
| It does not handle `TraceState`. | ||
|
|
||
|
|
||
| ## Installation | ||
|
|
||
| ```sh | ||
| composer require @instana/opentelemetry-php-propagator-instana | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
|
|
||
|
|
||
| ## Propagator Details | ||
|
|
||
| There are three headers that the propagator handles: `X-INSTANA-T` (the trace ID), `X-INSTANA-S` (the parent span ID), and `X-INSTANA-L` (the sampling level). | ||
|
|
||
| Example header triplet: | ||
|
|
||
| * `X-INSTANA-T: 80f198ee56343ba864fe8b2a57d3eff7`, | ||
| * `X-INSTANA-S: e457b5a2e4d86bd1`, | ||
| * `X-INSTANA-L: 1`. | ||
|
|
||
| A short summary for each of the headers is provided below. More details are available at <https://www.ibm.com/docs/en/obi/current?topic=monitoring-traces#tracing-headers>. | ||
|
|
||
| ### X-INSTANA-T -- trace ID | ||
|
|
||
| * A string of either 16 or 32 characters from the alphabet `0-9a-f`, representing either a 64 bit or 128 bit ID. | ||
| * This header corresponds to the [OpenTelemetry TraceId](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/overview.md#spancontext). | ||
| * If the propagator receives an X-INSTANA-T header value that is shorter than 32 characters when _extracting_ headers into the OpenTelemetry span context, it will left-pad the string with the character "0" to length 32. | ||
| * No length transformation is applied when _injecting_ the span context into headers. | ||
|
|
||
| ### X-INSTANA-S -- parent span ID | ||
|
|
||
| * Format: A string of 16 characters from the alphabet `0-9a-f`, representing a 64 bit ID. | ||
| * This header corresponds to the [OpenTelemetry SpanId](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/overview.md#spancontext). | ||
|
|
||
| ### X-INSTANA-L - sampling level | ||
|
|
||
| * The only two valid values are `1` and `0`. | ||
| * A level of `1` means that this request is to be sampled, a level of `0` means that the request should not be sampled. | ||
| * This header corresponds to the sampling bit of the [OpenTelemetry TraceFlags](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/overview.md#spancontext). | ||
|
|
||
| ## Useful links | ||
|
|
||
| * For more information on Instana, visit <https://www.instana.com/> and [Instana' documentation](https://www.ibm.com/docs/en/obi/current). | ||
| * For more information on OpenTelemetry, visit: <https://opentelemetry.io/> | ||
|
|
||
|
|
||
| ## License | ||
|
|
||
| Apache 2.0 - See [LICENSE][license-url] for more information. |
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,13 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| use OpenTelemetry\Extension\Propagator\Instana\InstanaMultiPropagator; | ||
| use OpenTelemetry\SDK\Registry; | ||
|
|
||
| if (!class_exists(Registry::class)) { | ||
| return; | ||
| } | ||
| Registry::registerTextMapPropagator( | ||
| 'instana', | ||
| InstanaMultiPropagator::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,37 @@ | ||
| { | ||
| "name": "instana/opentelemetry-php-propagator-instana", | ||
| "description": "Instana propagator extension for OpenTelemetry PHP.", | ||
| "keywords": ["opentelemetry", "otel", "tracing", "apm", "extension", "propagator", "instana"], | ||
| "type": "library", | ||
| "support": { | ||
| "issues": "https://github.com/open-telemetry/opentelemetry-php/issues", | ||
| "source": "https://github.com/open-telemetry/opentelemetry-php", | ||
| "docs": "https://opentelemetry.io/docs/php", | ||
| "chat": "https://app.slack.com/client/T08PSQ7BQ/C01NFPCV44V" | ||
| }, | ||
| "license": "Apache-2.0", | ||
| "authors": [ | ||
| { | ||
| "name": "opentelemetry-php contributors", | ||
| "homepage": "https://github.com/open-telemetry/opentelemetry-php/graphs/contributors" | ||
| } | ||
| ], | ||
| "require": { | ||
| "php": "^8.1", | ||
| "open-telemetry/api": "^1.0", | ||
| "open-telemetry/context": "^1.0" | ||
| }, | ||
| "autoload": { | ||
| "psr-4": { | ||
| "OpenTelemetry\\Extension\\Propagator\\Instana\\": "." | ||
| }, | ||
| "files": [ | ||
| "_register.php" | ||
| ] | ||
| }, | ||
| "extra": { | ||
| "branch-alias": { | ||
| "dev-main": "1.0.x-dev" | ||
| } | ||
| } | ||
| } |
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.