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 custom timestamp to InMemory adapter #54

Open
wants to merge 4 commits into
base: master
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"jimdo/prometheus_client_php": "*"
},
"require": {
"php": "^7.2",
"php": "^7.2|^8.0",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.3",
"symfony/polyfill-apcu": "^1.6"
Expand Down
4 changes: 3 additions & 1 deletion src/Prometheus/Counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public function inc(array $labels = []): void
/**
* @param int $count e.g. 2
* @param array $labels e.g. ['status', 'opcode']
* @param int|null $timestamp
*/
public function incBy($count, array $labels = []): void
public function incBy($count, array $labels = [], int $timestamp = null): void
{
$this->assertLabelsAreDefinedCorrectly($labels);

Expand All @@ -43,6 +44,7 @@ public function incBy($count, array $labels = []): void
'labelValues' => $labels,
'value' => $count,
'command' => Adapter::COMMAND_INCREMENT_INTEGER,
'timestamp' => $timestamp
]
);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Prometheus/Gauge.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ class Gauge extends Collector
/**
* @param double $value e.g. 123
* @param array $labels e.g. ['status', 'opcode']
* @param int|null $timestamp
*/
public function set(float $value, array $labels = []): void
public function set(float $value, array $labels = [], int $timestamp = null): void
{
$this->assertLabelsAreDefinedCorrectly($labels);

Expand All @@ -27,6 +28,7 @@ public function set(float $value, array $labels = []): void
'labelValues' => $labels,
'value' => $value,
'command' => Adapter::COMMAND_SET,
'timestamp' => $timestamp
]
);
}
Expand All @@ -50,8 +52,9 @@ public function inc($labels = []): void
/**
* @param $value
* @param array $labels
* @param int|null $timestamp
*/
public function incBy($value, array $labels = []): void
public function incBy($value, array $labels = [], int $timestamp = null): void
{
$this->assertLabelsAreDefinedCorrectly($labels);

Expand All @@ -64,6 +67,7 @@ public function incBy($value, array $labels = []): void
'labelValues' => $labels,
'value' => $value,
'command' => Adapter::COMMAND_INCREMENT_FLOAT,
'timestamp' => $timestamp
]
);
}
Expand Down
11 changes: 9 additions & 2 deletions src/Prometheus/RenderTextFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function render(array $metrics): string
*/
private function renderSample(MetricFamilySamples $metric, Sample $sample): string
{
$result = null;
$escapedLabels = [];

$labelNames = $metric->getLabelNames();
Expand All @@ -45,9 +46,14 @@ private function renderSample(MetricFamilySamples $metric, Sample $sample): stri
foreach ($labels as $labelName => $labelValue) {
$escapedLabels[] = $labelName . '="' . $this->escapeLabelValue($labelValue) . '"';
}
return $sample->getName() . '{' . implode(',', $escapedLabels) . '} ' . $sample->getValue();
$result = $sample->getName() . '{' . implode(',', $escapedLabels) . '} ' . $sample->getValue();
} else {
$result = $sample->getName() . ' ' . $sample->getValue();
}
return $sample->getName() . ' ' . $sample->getValue();
if ($sample->hasTimestamp()) {
$result .= ' ' . $sample->getTimestamp() * 1000;
}
return $result;
}

/**
Expand All @@ -56,6 +62,7 @@ private function renderSample(MetricFamilySamples $metric, Sample $sample): stri
*/
private function escapeLabelValue($v): string
{
$v = is_array($v) ? $v : strval($v);
$v = str_replace("\\", "\\\\", $v);
$v = str_replace("\n", "\\n", $v);
$v = str_replace("\"", "\\\"", $v);
Expand Down
24 changes: 23 additions & 1 deletion src/Prometheus/Sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class Sample
*/
private $value;

/**
* @var ?int
*/
private $timestamp;

/**
* Sample constructor.
* @param array $data
Expand All @@ -36,6 +41,7 @@ public function __construct(array $data)
$this->labelNames = $data['labelNames'];
$this->labelValues = $data['labelValues'];
$this->value = $data['value'];
$this->timestamp = (int)($data['timestamp'] ?? 0) ?: null;
}

/**
Expand Down Expand Up @@ -67,7 +73,7 @@ public function getLabelValues(): array
*/
public function getValue(): string
{
return (string) $this->value;
return (string)$this->value;
}

/**
Expand All @@ -77,4 +83,20 @@ public function hasLabelNames(): bool
{
return !empty($this->labelNames);
}

/**
* @return int|null
*/
public function getTimestamp(): ?int
{
return $this->timestamp;
}

/**
* @return bool
*/
public function hasTimestamp(): bool
{
return (bool)$this->timestamp;
}
}
3 changes: 3 additions & 0 deletions src/Prometheus/Storage/InMemory.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,13 @@ private function internalCollect(array $metrics): array
foreach ($metric['samples'] as $key => $value) {
$parts = explode(':', $key);
$labelValues = $parts[2];
$timestamp = $parts[3] ?? false;
$data['samples'][] = [
'name' => $metaData['name'],
'labelNames' => [],
'labelValues' => $this->decodeLabelValues($labelValues),
'value' => $value,
'timestamp' => $timestamp
];
}
$this->sortSamples($data['samples']);
Expand Down Expand Up @@ -261,6 +263,7 @@ private function valueKey(array $data): string
$data['type'],
$data['name'],
$this->encodeLabelValues($data['labelValues']),
$data['timestamp'] ?? false,
'value'
]);
}
Expand Down