Skip to content

Commit

Permalink
prometheus HafasCache Stats
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrLevin committed Dec 12, 2024
1 parent 5dc27df commit 0949842
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 19 deletions.
63 changes: 44 additions & 19 deletions app/DataProviders/CachedHafas.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Enum\TravelType;
use App\Helpers\CacheKey;
use App\Helpers\HCK;
use App\Models\Station;
use App\Models\Trip;
use Carbon\Carbon;
Expand All @@ -13,21 +14,30 @@

class CachedHafas extends Hafas implements DataProviderInterface
{

public function fetchHafasTrip(string $tripID, string $lineName): Trip {
$key = CacheKey::getHafasTripKey($tripID, $lineName);

return $this->remember($key, now()->addMinutes(15), function() use ($tripID, $lineName) {
return parent::fetchHafasTrip($tripID, $lineName);
});
return $this->remember(
$key,
now()->addMinutes(15),
function() use ($tripID, $lineName) {
return parent::fetchHafasTrip($tripID, $lineName);
},
HCK::TRIPS_SUCCESS
);
}

public function getStations(string $query, int $results = 10): Collection {
$key = CacheKey::getHafasStationsKey($query);

return $this->remember($key, now()->addMinutes(15), function() use ($query, $results) {
return parent::getStations($query, $results);
});
return $this->remember(
$key,
now()->addMinutes(15),
function() use ($query, $results) {
return parent::getStations($query, $results);
},
HCK::LOCATIONS_SUCCESS
);
}

public function getDepartures(Station $station, Carbon $when, int $duration = 15, TravelType $type = null, bool $localtime = false): Collection {
Expand All @@ -43,9 +53,14 @@ public function getDepartures(Station $station, Carbon $when, int $duration = 15

$key = CacheKey::getHafasDeparturesKey($station->id, $when, $localtime);

$departures = $this->remember($key, now()->addMinutes(15), function() use ($station, $when, $duration, $type, $localtime) {
return parent::getDepartures($station, $when, $duration, $type, $localtime);
});
$departures = $this->remember(
$key,
now()->addMinutes(15),
function() use ($station, $when, $duration, $type, $localtime) {
return parent::getDepartures($station, $when, $duration, $type, $localtime);
},
HCK::DEPARTURES_SUCCESS
);

// filter entries by when and duration
return $departures->filter(function($departure) use ($filterWhen, $duration) {
Expand All @@ -57,29 +72,39 @@ public function getDepartures(Station $station, Carbon $when, int $duration = 15
public function getStationByRilIdentifier(string $rilIdentifier): ?Station {
$key = CacheKey::getHafasByRilIdentifierKey($rilIdentifier);

return $this->remember($key, now()->addMinutes(15), function() use ($rilIdentifier) {
return parent::getStationByRilIdentifier($rilIdentifier);
});
return $this->remember(
$key,
now()->addMinutes(15),
function() use ($rilIdentifier) {
return parent::getStationByRilIdentifier($rilIdentifier);
},
HCK::STATIONS_SUCCESS
);
}

public function getStationsByFuzzyRilIdentifier(string $rilIdentifier): ?Collection {
$key = CacheKey::getHafasStationsFuzzyKey($rilIdentifier);

return $this->remember($key, now()->addMinutes(15), function() use ($rilIdentifier) {
return parent::getStationsByFuzzyRilIdentifier($rilIdentifier);
});
return $this->remember(
$key,
now()->addMinutes(15),
function() use ($rilIdentifier) {
return parent::getStationsByFuzzyRilIdentifier($rilIdentifier);
},
HCK::STATIONS_SUCCESS
);
}

private function remember(string $key, Carbon $expires, callable $callback): mixed {
private function remember(string $key, Carbon $expires, callable $callback, ?string $ident = null): mixed {
if (Cache::has($key)) {
CacheKey::increment('hafas_cache_hit');
CacheKey::increment(CacheKey::getHafasCacheHitKey($ident));
return Cache::get($key);
}

try {
$result = $callback();
CacheKey::increment(CacheKey::getHafasCacheSetKey($ident));
Cache::put($key, $result, $expires);
CacheKey::increment('hafas_cache_set');
return $result;
} catch (Throwable $e) {
Cache::put($key, null, $expires);
Expand Down
12 changes: 12 additions & 0 deletions app/Helpers/CacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,23 @@ class CacheKey
private const string HAFAS_DEPARTURES = '_HafasDepartures_%d_%s_%s';
private const string HAFAFS_STATION_RIL = '_HafasStationRil';
private const string HAFAS_STATIONS_FUZZY = '_HafasStationsFuzzy';
private const string HAFAS_CACHE_HIT = '_HafasCacheHit_%s';
private const string HAFAS_CACHE_SET = '_HafasCacheSet_%s';

// formatting keys
private const string FOR = '%s-for-%s';
private const string FROM_TO = '%s-from-%s-to-%s';

public static function getHafasCacheHitKey(string $key): string {
$key = str_replace('monitoring-counter-', '', $key);
return sprintf(self::HAFAS_CACHE_HIT, $key);
}

public static function getHafasCacheSetKey(string $key): string {
$key = str_replace('monitoring-counter-', '', $key);
return sprintf(self::HAFAS_CACHE_SET, $key);
}

public static function getHafasTripKey(string $tripId, string $lineName): string {
$tripId = sha1($tripId);
return sprintf(self::HAFAS_TRIP, $tripId, $lineName);
Expand Down
1 change: 1 addition & 0 deletions app/Helpers/HCK.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static function getFailures(): array {
return [
self::DEPARTURES_FAILURE => 'Departures',
self::TRIPS_FAILURE => 'Trips',
self::TRIPS_502 => 'Trips502',
self::STOPS_FAILURE => 'Stops',
self::STATIONS_FAILURE => 'Stations',
self::LOCATIONS_FAILURE => 'Locations',
Expand Down
26 changes: 26 additions & 0 deletions app/Providers/PrometheusServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,32 @@ public function register() {
return $this->getHafasByType(HCK::getSuccesses());
});

Prometheus::addGauge("hafas_cache_hits")
->helpText("How many hafas requests have been served from cache?")
->labels(["request_name"])
->value(function() {
$values = [];
foreach (HCK::getSuccesses() as $key => $name) {
$key = CacheKey::getHafasCacheHitKey($key);
$values[$name] = Cache::get($key, 0);
}

return array_map(fn($value, $key) => [$value, [$key]], $values, array_keys($values));
});

Prometheus::addGauge("hafas_cache_sets")
->helpText("How many hafas requests have been stored in cache?")
->labels(["request_name"])
->value(function() {
$values = [];
foreach (HCK::getSuccesses() as $key => $name) {
$key = CacheKey::getHafasCacheSetKey($key);
$values[$name] = Cache::get($key, 0);
}

return array_map(fn($value, $key) => [$value, [$key]], $values, array_keys($values));
});

Prometheus::addGauge("completed_jobs_count")
->helpText("How many jobs are done? Old items from queue monitor table are deleted after 7 days.")
->labels(["job_name", "status", "queue"])
Expand Down

0 comments on commit 0949842

Please sign in to comment.