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

#44 Metrics init draft #45

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 20 additions & 0 deletions src/Prometheus/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ protected function assertLabelsAreDefinedCorrectly(array $labels): void
}
}

/**
* @param array[] $labelValuesSet
*/
protected function assertValidInitLabelsValuesSet(array $labelValuesSet = []): void
{
$initLabelsKeys = array_keys($labelValuesSet);

$forgottenLabels = array_diff($this->getLabelNames(), $initLabelsKeys);
if (count($forgottenLabels) > 0) {
throw new InvalidArgumentException("Missing label values for: " . implode(',', $forgottenLabels));
}

$unnecessaryLabels = array_diff($initLabelsKeys, $this->getLabelNames());
if (count($unnecessaryLabels) > 0) {
throw new InvalidArgumentException(
"Some labels values are not mentioned on metric creation:" . implode(',', $unnecessaryLabels)
);
}
}

/**
* @param string $metricName
*/
Expand Down
18 changes: 18 additions & 0 deletions src/Prometheus/Counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,22 @@ public function incBy($count, array $labels = []): void
]
);
}

/**
* @param array[] $labelValuesSet
*/
public function init(array $labelValuesSet = []): void
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using such methods we can leave it to user to decide if he needs init or not, so there will be no changes in behaviour for regular usage

{
$this->assertValidInitLabelsValuesSet($labelValuesSet);

$this->storageAdapter->initCounter(
[
'name' => $this->getName(),
'help' => $this->getHelp(),
'type' => $this->getType(),
'labelNames' => $this->getLabelNames(),
'labelValuesSet' => $labelValuesSet,
]
);
}
}
18 changes: 18 additions & 0 deletions src/Prometheus/Gauge.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,22 @@ public function decBy($value, array $labels = []): void
{
$this->incBy(-$value, $labels);
}

/**
* @param array[] $labelValuesSet
*/
public function init(array $labelValuesSet = []): void
{
$this->assertValidInitLabelsValuesSet($labelValuesSet);

$this->storageAdapter->initGauge(
[
'name' => $this->getName(),
'help' => $this->getHelp(),
'type' => $this->getType(),
'labelNames' => $this->getLabelNames(),
'labelValuesSet' => $labelValuesSet,
]
);
}
}
19 changes: 19 additions & 0 deletions src/Prometheus/Histogram.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ public function observe(float $value, array $labels = []): void
);
}

/**
* @param array[] $labelValuesSet
*/
public function init(array $labelValuesSet = []): void
{
$this->assertValidInitLabelsValuesSet($labelValuesSet);

$this->storageAdapter->initHistogram(
[
'name' => $this->getName(),
'help' => $this->getHelp(),
'type' => $this->getType(),
'labelNames' => $this->getLabelNames(),
'labelValuesSet' => $labelValuesSet,
'buckets' => $this->buckets,
]
);
}

/**
* @return string
*/
Expand Down
15 changes: 15 additions & 0 deletions src/Prometheus/Storage/APC.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ public function updateCounter(array $data): void
}
}

public function initHistogram(array $data): void
{
throw new \RuntimeException('APC driver currently doesn\'t support this feature');
}

public function initGauge(array $data): void
{
throw new \RuntimeException('APC driver currently doesn\'t support this feature');
}

public function initCounter(array $data): void
{
throw new \RuntimeException('APC driver currently doesn\'t support this feature');
}

/**
* @deprecated use replacement method wipeStorage from Adapter interface
*
Expand Down
15 changes: 15 additions & 0 deletions src/Prometheus/Storage/APCng.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@ public function updateCounter(array $data): void
}
}

public function initHistogram(array $data): void
{
throw new \RuntimeException('APCng driver currently doesn\'t support this feature');
}

public function initGauge(array $data): void
{
throw new \RuntimeException('APCng driver currently doesn\'t support this feature');
}

public function initCounter(array $data): void
{
throw new \RuntimeException('APCng driver currently doesn\'t support this feature');
}

/**
* @param array<string> $metaData
* @param string $labels
Expand Down
27 changes: 23 additions & 4 deletions src/Prometheus/Storage/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ interface Adapter
const COMMAND_INCREMENT_FLOAT = 2;
const COMMAND_SET = 3;

/**
* Removes all previously stored metrics from underlying storage
*
* @throws StorageException
* @return void
*/
public function wipeStorage(): void;

/**
* @return MetricFamilySamples[]
*/
Expand Down Expand Up @@ -43,10 +51,21 @@ public function updateGauge(array $data): void;
public function updateCounter(array $data): void;

/**
* Removes all previously stored metrics from underlying storage
*
* @throws StorageException
* @param mixed[] $data
* @return void
*/
public function wipeStorage(): void;
public function initHistogram(array $data): void;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding new methods to this interface is a breaking change, we should avoid doing it or maybe release this as a new version afterward (lesson learned from #43)



/**
* @param mixed[] $data
* @return void
*/
public function initGauge(array $data): void;

/**
* @param mixed[] $data
* @return void
*/
public function initCounter(array $data): void;
}
15 changes: 15 additions & 0 deletions src/Prometheus/Storage/InMemory.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,21 @@ public function updateCounter(array $data): void
}
}

public function initHistogram(array $data): void
{
throw new \RuntimeException('InMemory driver currently doesn\'t support this feature');
}

public function initGauge(array $data): void
{
throw new \RuntimeException('InMemory driver currently doesn\'t support this feature');
}

public function initCounter(array $data): void
{
throw new \RuntimeException('InMemory driver currently doesn\'t support this feature');
}

/**
* @param mixed[] $data
* @param string|int $bucket
Expand Down
Loading