Skip to content

Commit

Permalink
Merge pull request #1 from spiral/feature/improve-sources
Browse files Browse the repository at this point in the history
Improve sources
  • Loading branch information
SerafimArts authored Feb 18, 2021
2 parents e5204aa + ea61f03 commit 256c121
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 130 deletions.
73 changes: 44 additions & 29 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
{
"name": "spiral/roadrunner-metrics",
"type": "server",
"description": "RoadRunner: Prometheus metrics RPC",
"license": "MIT",
"authors": [
{
"name": "Anton Titov / Wolfy-J",
"email": "[email protected]"
"name": "spiral/roadrunner-metrics",
"type": "library",
"description": "RoadRunner: Prometheus metrics RPC",
"license": "MIT",
"authors": [
{
"name": "Anton Titov (wolfy-j)",
"email": "[email protected]"
},
{
"name": "RoadRunner Community",
"homepage": "https://github.com/spiral/roadrunner/graphs/contributors"
}
],
"require": {
"php": ">=7.4",
"symfony/polyfill-php80": "^1.22",
"spiral/goridge": "^3.0"
},
{
"name": "RoadRunner Community",
"homepage": "https://github.com/spiral/roadrunner/graphs/contributors"
}
],
"require": {
"php": ">=7.4",
"spiral/roadrunner": ">=2.0"
},
"require-dev": {
"phpstan/phpstan": "~0.12",
"phpunit/phpunit": "~8.0"
},
"scripts": {
"analyze": "phpstan analyze -c ./phpstan.neon.dist --no-progress --ansi"
},
"autoload": {
"psr-4": {
"Spiral\\RoadRunner\\Metrics\\": "src/"
}
}
"autoload": {
"psr-4": {
"Spiral\\RoadRunner\\Metrics\\": "src"
}
},
"require-dev": {
"jetbrains/phpstorm-attributes": "^1.0",
"vimeo/psalm": "^4.4",
"phpunit/phpunit": "~8.0"
},
"scripts": {
"analyze": "psalm"
},
"extra": {
"branch-alias": {
"dev-master": "2.1.x-dev"
}
},
"suggest": {
"spiral/roadrunner-cli": "Provides RoadRunner installation and management CLI tools"
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
15 changes: 15 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
resolveFromConfigFile="true"
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" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
161 changes: 104 additions & 57 deletions src/Collector.php
Original file line number Diff line number Diff line change
@@ -1,130 +1,177 @@
<?php

/**
* High-performance PHP process supervisor and load balancer written in Go. Http core.
* This file is part of RoadRunner package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Spiral\RoadRunner\Metrics;

final class Collector implements \JsonSerializable
use JetBrains\PhpStorm\ExpectedValues;
use JetBrains\PhpStorm\Pure;
use JsonSerializable;

/**
* @psalm-import-type CollectorType from CollectorInterface
* @psalm-import-type ArrayFormatType from CollectorInterface
*/
final class Collector implements CollectorInterface, JsonSerializable
{
public const HISTOGRAM = 'histogram';
public const GAUGE = 'gauge';
public const COUNTER = 'counter';
/**
* @var CollectorType
*/
#[ExpectedValues(valuesFromClass: self::class)]
private string $type;

/**
* @var string
*/
private string $namespace = '';

/**
* @var string
*/
private string $subsystem = '';
private string $type;

/**
* @var string
*/
private string $help = '';

/**
* @var array<array-key, string>
*/
private array $labels = [];

/**
* @var array<array-key, float>
*/
private array $buckets = [];

/**
* @param string $type
* @param CollectorType $type
*/
private function __construct(string $type)
{
private function __construct(
#[ExpectedValues(valuesFromClass: self::class)]
string $type
) {
$this->type = $type;
}

/**
* @param string $namespace
* @return $this
* {@inheritDoc}
*/
public function withNamespace(string $namespace): self
{
$c = clone $this;
$c->namespace = $namespace;

return $c;
#[Pure]
public function withNamespace(
string $namespace
): self {
$self = clone $this;
$self->namespace = $namespace;

return $self;
}

/**
* @param string $subsystem
* @return $this
* {@inheritDoc}
*/
public function withSubsystem(string $subsystem): self
{
$c = clone $this;
$c->subsystem = $subsystem;

return $c;
#[Pure]
public function withSubsystem(
string $subsystem
): self {
$self = clone $this;
$self->subsystem = $subsystem;

return $self;
}

/**
* @param string $help
* @return $this
* {@inheritDoc}
*/
public function withHelp(string $help): self
{
$c = clone $this;
$c->help = $help;

return $c;
#[Pure]
public function withHelp(
string $help
): self {
$self = clone $this;
$self->help = $help;

return $self;
}

/**
* @param string ...$label
* @return $this
* {@inheritDoc}
*/
public function withLabels(string ...$label): self
{
$c = clone $this;
$c->labels = $label;

return $c;
#[Pure]
public function withLabels(
string ...$label
): self {
$self = clone $this;
$self->labels = $label;

return $self;
}

/**
* @return array
* {@inheritDoc}
*/
public function jsonSerialize(): array
#[Pure]
public function toArray(): array
{
return [
'namespace' => $this->namespace,
'subsystem' => $this->subsystem,
'type' => $this->type,
'help' => $this->help,
'labels' => $this->labels,
'buckets' => $this->buckets,
'type' => $this->type,
'help' => $this->help,
'labels' => $this->labels,
'buckets' => $this->buckets,
];
}

/**
* @return ArrayFormatType
*/
#[Pure]
public function jsonSerialize(): array
{
return $this->toArray();
}

/**
* New histogram metric.
*
* @param float ...$bucket
* @return static
*/
#[Pure]
public static function histogram(float ...$bucket): self
{
$c = new self(self::HISTOGRAM);
$c->buckets = $bucket;
$self = new self(self::TYPE_HISTOGRAM);
/** @psalm-suppress ImpurePropertyAssignment */
$self->buckets = $bucket;

return $c;
return $self;
}

/**
* New gauge metric.
*
* @return static
*/
#[Pure]
public static function gauge(): self
{
$c = new self(self::GAUGE);

return $c;
return new self(self::TYPE_GAUGE);
}

/**
* New counter metric.
*
* @return static
*/
#[Pure]
public static function counter(): self
{
$c = new self(self::COUNTER);

return $c;
return new self(self::TYPE_COUNTER);
}
}
}
Loading

0 comments on commit 256c121

Please sign in to comment.