Files
michaelschiemer/src/Framework/Metrics/Metric.php
Michael Schiemer fc3d7e6357 feat(Production): Complete production deployment infrastructure
- Add comprehensive health check system with multiple endpoints
- Add Prometheus metrics endpoint
- Add production logging configurations (5 strategies)
- Add complete deployment documentation suite:
  * QUICKSTART.md - 30-minute deployment guide
  * DEPLOYMENT_CHECKLIST.md - Printable verification checklist
  * DEPLOYMENT_WORKFLOW.md - Complete deployment lifecycle
  * PRODUCTION_DEPLOYMENT.md - Comprehensive technical reference
  * production-logging.md - Logging configuration guide
  * ANSIBLE_DEPLOYMENT.md - Infrastructure as Code automation
  * README.md - Navigation hub
  * DEPLOYMENT_SUMMARY.md - Executive summary
- Add deployment scripts and automation
- Add DEPLOYMENT_PLAN.md - Concrete plan for immediate deployment
- Update README with production-ready features

All production infrastructure is now complete and ready for deployment.
2025-10-25 19:18:37 +02:00

120 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Framework\Metrics;
use App\Framework\Core\ValueObjects\Timestamp;
/**
* Represents a single metric with its metadata and value
*/
final readonly class Metric
{
/**
* @param array<string, string> $labels
*/
public function __construct(
public string $name,
public float $value,
public MetricType $type,
public ?string $help = null,
public array $labels = [],
public ?string $unit = null,
public ?Timestamp $timestamp = null
) {
}
public function withLabel(string $key, string $value): self
{
return new self(
$this->name,
$this->value,
$this->type,
$this->help,
array_merge($this->labels, [$key => $value]),
$this->unit,
$this->timestamp
);
}
public function withValue(float $value): self
{
return new self(
$this->name,
$value,
$this->type,
$this->help,
$this->labels,
$this->unit,
$this->timestamp
);
}
public function withTimestamp(Timestamp $timestamp): self
{
return new self(
$this->name,
$this->value,
$this->type,
$this->help,
$this->labels,
$this->unit,
$timestamp
);
}
/**
* Get formatted label string for Prometheus format
*/
public function getFormattedLabels(): string
{
if (empty($this->labels)) {
return '';
}
// Sort labels alphabetically for Prometheus format consistency
$sortedLabels = $this->labels;
ksort($sortedLabels);
$parts = [];
foreach ($sortedLabels as $key => $value) {
// Escape special characters in label values
$escapedValue = str_replace(['\\', '"', "\n"], ['\\\\', '\\"', '\\n'], $value);
$parts[] = sprintf('%s="%s"', $key, $escapedValue);
}
return '{' . implode(',', $parts) . '}';
}
/**
* Convert metric to array representation
*/
public function toArray(): array
{
$data = [
'name' => $this->name,
'value' => $this->value,
'type' => $this->type->value,
];
if ($this->help !== null) {
$data['help'] = $this->help;
}
if (! empty($this->labels)) {
$data['labels'] = $this->labels;
}
if ($this->unit !== null) {
$data['unit'] = $this->unit;
}
if ($this->timestamp !== null) {
$data['timestamp'] = $this->timestamp->toIso8601();
}
return $data;
}
}