Skip to content

Commit

Permalink
Merge branch 'master' into fix-http-headers-attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
cdaguerre authored Mar 12, 2024
2 parents 487bb62 + cee36ca commit 03acbd7
Show file tree
Hide file tree
Showing 71 changed files with 591 additions and 150 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: Tests

on: [ push, pull_request ]
on: [push, pull_request]

jobs:
phpstan:
name: PHPStan
strategy:
matrix:
php-version: [ '8.1' ]
php-version: ["8.3"]
fail-fast: false
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -45,7 +45,7 @@ jobs:
name: PHP-CS-Fixer
strategy:
matrix:
php-version: [ '8.1' ]
php-version: ["8.3"]
fail-fast: false
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -83,7 +83,7 @@ jobs:
name: PHPSpec
strategy:
matrix:
php-version: [ '8.1' ]
php-version: ["8.1", "8.3"]
fail-fast: false
runs-on: ubuntu-latest
steps:
Expand Down
6 changes: 5 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

require_once 'vendor/autoload.php';

return CodingStandards\Factory::createPhpCsFixerConfig(__DIR__);
return CodingStandards\Factory::createPhpCsFixerConfig(__DIR__, [
'rules' => [
'nullable_type_declaration' => ['syntax' => 'union']
]
]);
31 changes: 18 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,36 @@
"name": "worldia/instrumentation-bundle",
"type": "symfony-bundle",
"license": "MIT",
"keywords": ["instrumentation", "tracing", "metrics", "open-telemetry", "prometheus"],
"authors": [{
"name": "Worldia developers",
"email": "[email protected]"
}],
"keywords": [
"instrumentation",
"tracing",
"metrics",
"open-telemetry",
"prometheus"
],
"authors": [
{
"name": "Worldia developers",
"email": "[email protected]"
}
],
"require": {
"monolog/monolog": "^2.0",
"nyholm/dsn": "^2.0",
"nyholm/psr7": "^1.5",
"open-telemetry/sdk": "^1.0@dev",
"open-telemetry/api": "^1.0@dev",
"open-telemetry/context": "^1.0@dev",
"open-telemetry/sem-conv": "^1.0@dev",
"open-telemetry/api": ">=1.0.2",
"open-telemetry/sdk": "^1.0",
"promphp/prometheus_client_php": "^2.4",
"psr/http-client": "^1.0",
"symfony/dependency-injection": "*"
},
"require-dev": {
"doctrine/dbal": "^3.0",
"friends-of-phpspec/phpspec-expect": "^4.0",
"open-telemetry/sdk-contrib": "^1.0@dev",
"open-telemetry/transport-grpc": "^1.0@dev",
"open-telemetry/gen-otlp-protobuf": "^1.0@dev",
"open-telemetry/transport-grpc": "^1.0",
"open-telemetry/gen-otlp-protobuf": "^1.0",
"php-http/httplug": "^2.3",
"phpspec/phpspec": "^7.2",
"phpspec/phpspec": "^7.5",
"phpstan/phpstan": "^1.4",
"symfony/framework-bundle": "*",
"symfony/http-client": "*",
Expand Down
12 changes: 6 additions & 6 deletions docs/metrics/default-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
### Requests
Ref. [RequestEventSubscriber](../../src/Metrics/EventSubscriber/RequestEventSubscriber.php).

| Name | Type | Description | Labels |
|--------------------------|-------------|--------------------------------------------------------|--------|
| `requests_handled_total` | `counter` | Total requests handled by this instance | |
| `requests_handling` | `gauge` | Number of requests this instance is currently handling | |
| `response_codes_total` | `counter` | Number of requests per status code | `code` |
| `response_times_seconds` | `histogram` | Distribution of response times in seconds | |
| Name | Type | Description | Labels |
|--------------------------|-------------|--------------------------------------------------------|---------------------|
| `requests_handled_total` | `counter` | Total requests handled by this instance | |
| `requests_handling` | `gauge` | Number of requests this instance is currently handling | |
| `response_codes_total` | `counter` | Number of requests per status code and operation | `code`, `operation` |
| `response_times_seconds` | `histogram` | Distribution of response times in seconds | |

### Messages
Ref. [MessageEventSubscriber](../../src/Metrics/EventSubscriber/MessageEventSubscriber.php).
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
### Installation and configuration

```sh
composer require worldia/instrumentation-bundle
composer require worldia/instrumentation-bundle <your-exporter>
```
You will aso need to install an [exporter implementation](https://packagist.org/packages/open-telemetry/exporter-otlp?query=open-telemetry%2Fexporter-) and `APCu` is required by the prometheus exporter.
```
Add to ```bundles.php```:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class BaggageHeaderProviderSpec extends ObjectBehavior
{
private ?ScopeInterface $scope = null;
private ScopeInterface|null $scope = null;

public function let()
{
Expand Down
2 changes: 1 addition & 1 deletion spec/Baggage/Propagation/Messenger/BaggageStampSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class BaggageStampSpec extends ObjectBehavior
{
private ?ScopeInterface $scope = null;
private ScopeInterface|null $scope = null;

public function let()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private function createConsoleCommandEvent(Command|null $command = null): Consol
return new ConsoleCommandEvent($command, new ArrayInput([]), new NullOutput());
}

private function createConsoleErrorEvent(Command|null $command = null): ConsoleErrorEvent
private function createConsoleErrorEvent(): ConsoleErrorEvent
{
return new ConsoleErrorEvent(new ArrayInput([]), new NullOutput(), new \Exception());
}
Expand Down
69 changes: 69 additions & 0 deletions spec/Tracing/Instrumentation/LogHandler/TracingHandlerSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the worldia/instrumentation-bundle package.
* (c) Worldia <[email protected]>
*/

namespace spec\Instrumentation\Tracing\Instrumentation\LogHandler;

use Instrumentation\Tracing\Instrumentation\MainSpanContextInterface;
use Monolog\Logger;
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\API\Trace\TracerProviderInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class TracingHandlerSpec extends ObjectBehavior
{
public function it_adds_event_from_all_channels(
TracerProviderInterface $tracerProvider,
MainSpanContextInterface $mainSpanContext,
SpanInterface $span
): void {
$this->beConstructedWith($tracerProvider, $mainSpanContext, Logger::INFO, []);

$mainSpanContext->getMainSpan()->willReturn($span);
$span->addEvent(Argument::any())->willReturn($span);

$this->handle(['message' => 'Error from channel "foo"', 'channel' => 'foo', 'level' => Logger::ERROR, 'extra' => [], 'context' => []]);
$this->handle(['message' => 'Error from channel "bar"', 'channel' => 'bar', 'level' => Logger::ERROR, 'extra' => [], 'context' => []]);

$span->addEvent('Error from channel "foo"')->shouldHaveBeenCalled();
$span->addEvent('Error from channel "bar"')->shouldHaveBeenCalled();
}

public function it_adds_event_from_specific_channel_only(
TracerProviderInterface $tracerProvider,
MainSpanContextInterface $mainSpanContext,
SpanInterface $span
): void {
$this->beConstructedWith($tracerProvider, $mainSpanContext, Logger::INFO, ['foo']);

$mainSpanContext->getMainSpan()->willReturn($span);
$span->addEvent(Argument::any())->willReturn($span);

$this->handle(['message' => 'Error from channel "foo"', 'channel' => 'foo', 'level' => Logger::ERROR, 'extra' => [], 'context' => []]);
$this->handle(['message' => 'Error from channel "bar"', 'channel' => 'bar', 'level' => Logger::ERROR, 'extra' => [], 'context' => []]);

$span->addEvent('Error from channel "foo"')->shouldHaveBeenCalled();
$span->addEvent('Error from channel "bar"')->shouldNotHaveBeenCalled();
}

public function it_ignores_event_from_specific_channel(
TracerProviderInterface $tracerProvider,
MainSpanContextInterface $mainSpanContext,
SpanInterface $span
): void {
$this->beConstructedWith($tracerProvider, $mainSpanContext, Logger::INFO, ['!foo']);

$mainSpanContext->getMainSpan()->willReturn($span);
$span->addEvent(Argument::any())->willReturn($span);

$this->handle(['message' => 'Error from channel "foo"', 'channel' => 'foo', 'level' => Logger::ERROR, 'extra' => [], 'context' => []]);
$this->handle(['message' => 'Error from channel "bar"', 'channel' => 'bar', 'level' => Logger::ERROR, 'extra' => [], 'context' => []]);

$span->addEvent('Error from channel "foo"')->shouldNotHaveBeenCalled();
$span->addEvent('Error from channel "bar"')->shouldHaveBeenCalled();
}
}
3 changes: 2 additions & 1 deletion spec/Tracing/Propagation/Messenger/TraceContextStampSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Instrumentation\Tracing\Propagation\Exception\ContextPropagationException;
use OpenTelemetry\API\Trace\NonRecordingSpan;
use OpenTelemetry\API\Trace\SpanContext;
use OpenTelemetry\API\Trace\TraceFlags;
use OpenTelemetry\API\Trace\TraceState;
use PhpSpec\ObjectBehavior;
use Symfony\Component\Messenger\Stamp\StampInterface;
Expand Down Expand Up @@ -48,7 +49,7 @@ public function it_creates_stamp_with_state(): void
$span = new NonRecordingSpan(SpanContext::create(
'b23f37322b169de7bcaf63b9f84b1427',
'3c17130d40834256',
SpanContext::TRACE_FLAG_DEFAULT,
TraceFlags::DEFAULT,
(new TraceState())->with('key', 'value'),
));
$scope = $span->activate();
Expand Down
2 changes: 1 addition & 1 deletion src/Baggage/Baggage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Baggage
/**
* @param string $value
*/
public static function set(string $key, $value, MetadataInterface|string $metadata = null): void
public static function set(string $key, $value, MetadataInterface|string|null $metadata = null): void
{
if (\is_string($metadata)) {
$metadata = new Metadata($metadata);
Expand Down
6 changes: 3 additions & 3 deletions src/Baggage/Propagation/ContextInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

final class ContextInitializer
{
public static function fromRequest(Request $request): ?ScopeInterface
public static function fromRequest(Request $request): ScopeInterface|null
{
if (!$baggage = $request->headers->get(BaggagePropagator::BAGGAGE)) {
return null;
Expand All @@ -26,7 +26,7 @@ public static function fromRequest(Request $request): ?ScopeInterface
return static::activateContext($baggage);
}

public static function fromMessage(Envelope $envelope): ?ScopeInterface
public static function fromMessage(Envelope $envelope): ScopeInterface|null
{
/** @var BaggageStamp|null $stamp */
$stamp = $envelope->last(BaggageStamp::class);
Expand All @@ -38,7 +38,7 @@ public static function fromMessage(Envelope $envelope): ?ScopeInterface
return static::activateContext($stamp->getBaggage());
}

public static function fromW3CHeader(string $header): ?ScopeInterface
public static function fromW3CHeader(string $header): ScopeInterface|null
{
return static::activateContext($header);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class MessengerEventSubscriber implements EventSubscriberInterface
{
private ?ScopeInterface $scope = null;
private ScopeInterface|null $scope = null;

public static function getSubscribedEvents(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class RequestEventSubscriber implements EventSubscriberInterface
{
private ?ScopeInterface $scope = null;
private ScopeInterface|null $scope = null;

public static function getSubscribedEvents(): array
{
Expand Down
4 changes: 1 addition & 3 deletions src/Bridge/GoogleCloud/Logging/Formatter/StdOutFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public function __construct(private string $project)
*
* @see https://cloud.google.com/logging/docs/agent/configuration#process-payload
* @see https://github.com/GoogleCloudPlatform/fluent-plugin-google-cloud/blob/master/lib/fluent/plugin/out_google_cloud.rb
*
* @return mixed
*/
protected function normalize($data, int $depth = 0)
{
Expand Down Expand Up @@ -103,7 +101,7 @@ protected function normalizeException(\Throwable $e, int $depth = 0): array
*
* @param array<mixed> $trace The stack trace returned from Exception::getTrace()
*/
private static function getFunctionNameForReport(array $trace = null): string
private static function getFunctionNameForReport(array|null $trace = null): string
{
if (null === $trace) {
return '<unknown function>';
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/config/metrics/metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Instrumentation\Resources;

use Instrumentation\Metrics;
use Instrumentation\Tracing\Instrumentation\MainSpanContextInterface;
use Prometheus\CollectorRegistry;
use Prometheus\Storage\Adapter;
use Prometheus\Storage\APCng;
Expand Down Expand Up @@ -48,6 +49,7 @@
->args([
service(Metrics\RegistryInterface::class),
param('app.path_blacklist'),
service(MainSpanContextInterface::class)->nullOnInvalid(),
])

->set(Metrics\EventSubscriber\ConsumerEventSubscriber::class)
Expand Down
6 changes: 3 additions & 3 deletions src/DependencyInjection/config/tracing/request.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@

return static function (ContainerConfigurator $container) {
$container->services()
->set(Tracing\Propagation\EventSubscriber\RequestEventSubscriber::class)
->set(Propagation\EventSubscriber\RequestEventSubscriber::class)
->args([
service(Propagation\ForcableIdGenerator::class),
service(Propagation\IncomingTraceHeaderResolverInterface::class)->nullOnInvalid(),
])
->autoconfigure()

->set(Tracing\Instrumentation\EventSubscriber\RequestEventSubscriber::class)
->set(Instrumentation\EventSubscriber\RequestEventSubscriber::class)
->args([
service(TracerProviderInterface::class),
service(Instrumentation\MainSpanContextInterface::class),
Expand All @@ -52,7 +52,7 @@
])
->autoconfigure()

->set(Tracing\Instrumentation\EventSubscriber\AddUserEventSubscriber::class)
->set(Instrumentation\EventSubscriber\AddUserEventSubscriber::class)
->args([
service(Instrumentation\MainSpanContextInterface::class),
service(TokenStorageInterface::class)->nullOnInvalid(),
Expand Down
2 changes: 1 addition & 1 deletion src/Health/Controller/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function getProvidedMetrics(): array
/**
* @param iterable<HealtcheckInterface> $checks
*/
public function __construct(private ResourceInfo $resourceInfo, private iterable $checks, private ?RegistryInterface $registry = null, private ?Profiler $profiler = null)
public function __construct(private ResourceInfo $resourceInfo, private iterable $checks, private RegistryInterface|null $registry = null, private Profiler|null $profiler = null)
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/Health/HealtcheckInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ interface HealtcheckInterface

public function getName(): string;

public function getDescription(): ?string;
public function getDescription(): string|null;

/**
* Contextual information about the status.
* Should be returned when the check is failing to give additional
* information about the reason for the current status.
*/
public function getStatusMessage(): ?string;
public function getStatusMessage(): string|null;

/**
* @return string One of the HealtcheckInterface constants
Expand Down
Loading

0 comments on commit 03acbd7

Please sign in to comment.