Skip to content
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

Add support for observability. #80

Merged
merged 6 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"php-http/promise": "^1.1.0",
"league/uri": "^6.5",
"doctrine/annotations": "^1.13 || ^2.0",
"open-telemetry/sdk": "^0.0.17",
"open-telemetry/api": "^0.0.17",
"ramsey/uuid": "^3 || ^4"
},
"require-dev": {
Expand Down
8 changes: 8 additions & 0 deletions src/Constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Microsoft\Kiota\Abstractions;

final class Constants
{
public const VERSION = '0.8.1';
}
29 changes: 29 additions & 0 deletions src/ObservabilityOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Microsoft\Kiota\Abstractions;

use OpenTelemetry\API\Common\Instrumentation\Globals;
use OpenTelemetry\API\Trace\TracerInterface;

class ObservabilityOptions implements RequestOption
{
private static ?TracerInterface $tracer = null;
private const OBSERVABILITY_TRACER_NAME = 'microsoft-php-kiota-abstractions';
public const REQUEST_TYPE_KEY = "com.microsoft.kiota.request.type";

/**
* @return TracerInterface
*/
public static function getTracer(): TracerInterface
{
if (self::$tracer === null) {
self::$tracer = Globals::tracerProvider()->getTracer(self::OBSERVABILITY_TRACER_NAME, Constants::VERSION);
}
return self::$tracer;
}

public static function setTracer(TracerInterFace $tracer): void
{
self::$tracer = $tracer;
}
}
37 changes: 36 additions & 1 deletion src/RequestInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class RequestInformation {
private array $requestOptions = [];
/** @var string $binaryContentType */
private static string $binaryContentType = 'application/octet-stream';
/** @var string $contentTypeHeader */
/** @var non-empty-string $contentTypeHeader */
public static string $contentTypeHeader = 'Content-Type';
private static AnnotationReader $annotationReader;

Expand Down Expand Up @@ -152,13 +152,20 @@ public function setStreamContent(StreamInterface $value): void {
* @param Parsable $value the models.
*/
public function setContentFromParsable(RequestAdapter $requestAdapter, string $contentType, Parsable $value): void {
$span = ObservabilityOptions::getTracer()->spanBuilder('setContentFromParsableCollection')
->startSpan();
$scope = $span->activate();
try {
$writer = $requestAdapter->getSerializationWriterFactory()->getSerializationWriter($contentType);
$writer->writeObjectValue(null, $value);
$span->setAttribute(ObservabilityOptions::REQUEST_TYPE_KEY, get_class($value));
$this->headers->add(self::$contentTypeHeader, $contentType);
$this->content = $writer->getSerializedContent();
} catch (Exception $exception) {
throw new RuntimeException('could not serialize payload.', 1, $exception);
Ndiritu marked this conversation as resolved.
Show resolved Hide resolved
} finally {
$scope->detach();
$span->end();
}
}

Expand All @@ -172,13 +179,23 @@ public function setContentFromParsable(RequestAdapter $requestAdapter, string $c
*/
public function setContentFromParsableCollection(RequestAdapter $requestAdapter, string $contentType, array $values): void
{
$span = ObservabilityOptions::getTracer()->spanBuilder('setContentFromParsableCollection')
->startSpan();
$scope = $span->activate();
try {
$writer = $requestAdapter->getSerializationWriterFactory()->getSerializationWriter($contentType);
$writer->writeCollectionOfObjectValues(null, $values);
$span->setAttribute(self::$contentTypeHeader, $contentType);
if (!empty($values)) {
$span->setAttribute(ObservabilityOptions::REQUEST_TYPE_KEY, get_class($values[0]));
}
$this->headers->add(self::$contentTypeHeader, $contentType);
$this->content = $writer->getSerializedContent();
} catch (Exception $exception) {
throw new RuntimeException('could not serialize payload.', 1, $exception);
} finally {
$scope->detach();
$span->end();
}
}

Expand All @@ -191,13 +208,21 @@ public function setContentFromParsableCollection(RequestAdapter $requestAdapter,
* @return void
*/
public function setContentFromScalar(RequestAdapter $requestAdapter, string $contentType, $value): void {
$span = ObservabilityOptions::getTracer()->spanBuilder('setContentFromScalar')
->startSpan();
$scope = $span->activate();
try {
$writer = $requestAdapter->getSerializationWriterFactory()->getSerializationWriter($contentType);
$writer->writeAnyValue(null, $value);
$span->setAttribute(self::$contentTypeHeader, $contentType);
$span->setAttribute(ObservabilityOptions::REQUEST_TYPE_KEY, gettype($value));
$this->headers->add(self::$contentTypeHeader, $contentType);
$this->content = $writer->getSerializedContent();
} catch (Exception $exception) {
throw new RuntimeException('could not serialize payload.', 1, $exception);
} finally {
$scope->detach();
$span->end();
}
}

Expand All @@ -210,13 +235,23 @@ public function setContentFromScalar(RequestAdapter $requestAdapter, string $con
* @return void
*/
public function setContentFromScalarCollection(RequestAdapter $requestAdapter, string $contentType, array $values): void {
$span = ObservabilityOptions::getTracer()->spanBuilder('setContentFromScalarCollection')
->startSpan();
$scope = $span->activate();
try {
$writer = $requestAdapter->getSerializationWriterFactory()->getSerializationWriter($contentType);
$writer->writeCollectionOfPrimitiveValues(null, $values);
$span->setAttribute(self::$contentTypeHeader, $contentType);
if (!empty($values)) {
$span->setAttribute(ObservabilityOptions::REQUEST_TYPE_KEY, gettype($values[0]));
}
$this->headers->add(self::$contentTypeHeader, $contentType);
$this->content = $writer->getSerializedContent();
} catch (Exception $exception) {
throw new RuntimeException('could not serialize payload.', 1, $exception);
} finally {
$scope->detach();
$span->end();
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Serialization/SerializationWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Microsoft\Kiota\Abstractions\Enum;
use Microsoft\Kiota\Abstractions\Types\Date;
use Microsoft\Kiota\Abstractions\Types\Time;
use OpenTelemetry\API\Trace\SpanInterface;
use Psr\Http\Message\StreamInterface;

/** Defines an interface for serialization of objects to a stream. */
Expand Down Expand Up @@ -128,7 +129,7 @@ public function writeCollectionOfPrimitiveValues(?string $key, ?array $value): v
* @param string|null $key The key for the value.
* @param mixed|null $value.
**/
public function writeAnyValue(?string $key, $value): void;
public function writeAnyValue(?string $key, $value, ?SpanInterface $span = null): void;
Ndiritu marked this conversation as resolved.
Show resolved Hide resolved

/**
* Write a binary stream.
Expand Down