function it_collects_and_publishes_the_metrics(MetricCollector $collector, MetricPublisher $publisher, InputInterface $input, OutputInterface $output) { $metrics = [new Metric('first', 1), new Metric('bar', 0.5)]; $collector->collect()->shouldBeCalled()->willReturn($metrics); $publisher->publish($metrics)->shouldBeCalled(); $this->run($input, $output); }
function it_publish_a_failure_metric_and_rethrow_an_exception(OperationRunner $runner, MetricPublisher $publisher, Operation $operation) { $e = new \InvalidArgumentException(); $runner->run($operation)->willThrow($e); $publisher->publish([new Metric('my_namespace.failure', 1, Metric::TYPE_INCREMENT)])->shouldBeCalled(); $this->shouldThrow($e)->during('run', [$operation]); }
function it_publishes_to_every_publisher(MetricPublisher $first, MetricPublisher $second) { $this->beConstructedWith([$first, $second]); $metrics = [new Metric('foo', 'bar')]; $first->publish($metrics)->shouldBeCalled(); $second->publish($metrics)->shouldBeCalled(); $this->publish($metrics); }
/** * @param RequestEnded $event */ public function onRequestEnd(RequestEnded $event) { $request = $event->getRequest(); if (null === ($requestTime = $request->attributes->get('_tolerance_request_time', null))) { $this->logger !== null && $this->logger->debug('The request do not contain the start time'); return; } $requestDurationInSeconds = microtime(true) - (double) $requestTime; $requestDurationInMilliseconds = $requestDurationInSeconds * 1000; $namespace = $this->requestMetricNamespaceResolver->resolve($request); $this->metricPublisher->publish([new Metric($namespace, $requestDurationInMilliseconds, Metric::TYPE_TIMING), new Metric($namespace, null, Metric::TYPE_INCREMENT)]); }
function it_runs_the_publish_via_a_callback_operation(MetricPublisher $decoratedPublisher, OperationRunner $operationRunner) { $operationRunner->run(Argument::that(function ($operation) { if (!$operation instanceof Callback) { return false; } $operation->call(); return true; }))->shouldBeCalled(); $metrics = [new Metric('name', 'foo')]; $decoratedPublisher->publish($metrics)->shouldBeCalled(); $this->publish($metrics); }
/** * {@inheritdoc} */ public function run(Operation $operation) { try { $result = $this->decoratedOperationRunner->run($operation); $this->metricPublisher->publish([new Metric($this->namespace . '.success', 1, Metric::TYPE_INCREMENT)]); return $result; } catch (\Exception $e) { // Will be re-thrown later } catch (\Throwable $e) { // Will be re-thrown later } $this->metricPublisher->publish([new Metric($this->namespace . '.failure', 1, Metric::TYPE_INCREMENT)]); throw $e; }
function it_publishes_the_timing_and_increment_metrics(MetricPublisher $metricPublisher, RequestMetricNamespaceResolver $requestMetricNamespaceResolver) { $request = new Request([], [], ['_tolerance_request_time' => 123456.789]); $requestMetricNamespaceResolver->resolve($request)->willReturn('the_namespace'); $metricPublisher->publish(Argument::that(function ($metrics) { if (!is_array($metrics)) { return false; } elseif (count($metrics) != 2) { return false; } elseif ($metrics[0]->getName() != 'the_namespace') { return false; } return true; }))->shouldBeCalled(); $this->onRequestEnd(new RequestEnded($request)); }
/** * {@inheritdoc} */ public function publish(array $metrics) { $this->publisher->publish($metrics); $this->publishedMetrics = array_merge($this->publishedMetrics, $metrics); }
/** * {@inheritdoc} */ public function publish(array $metrics) { return $this->operationRunner->run(new Callback(function () use($metrics) { return $this->decoratedPublisher->publish($metrics); })); }