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

Macros: Log the object where a macro is not found #996

Merged
merged 1 commit into from
Mar 22, 2024
Merged
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
13 changes: 9 additions & 4 deletions library/Icingadb/Common/Macros.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Icinga\Module\Icingadb\Compat\CompatHost;
use Icinga\Module\Icingadb\Compat\CompatService;
use Icinga\Module\Icingadb\Model\Host;
use ipl\Orm\Model;
use Icinga\Module\Icingadb\Model\Service;
use ipl\Orm\Query;
use ipl\Orm\ResultSet;

Expand All @@ -20,7 +20,7 @@ trait Macros
* Get the given string with macros being resolved
*
* @param string $input The string in which to look for macros
* @param Model|CompatService|CompatHost $object The host or service used to resolve the macros
* @param Host|Service|CompatService|CompatHost $object The host or service used to resolve the macros
*
* @return string
*/
Expand All @@ -42,7 +42,7 @@ public function expandMacros(string $input, $object): string
* Resolve a macro based on the given object
*
* @param string $macro The macro to resolve
* @param Model|CompatService|CompatHost $object The host or service used to resolve the macros
* @param Host|Service|CompatService|CompatHost $object The host or service used to resolve the macros
*
* @return string
*/
Expand Down Expand Up @@ -102,8 +102,13 @@ public function resolveMacro(string $macro, $object): string
$value = $object->$macro;
}
} catch (\Exception $e) {
$objectName = $object->name;
if ($objectType === 'service' && isset($object->host)) {
$objectName = $object->host->name . '!' . $objectName;
}

$value = null;
Logger::debug('Unable to resolve macro "%s". An error occurred: %s', $macro, $e);
Logger::debug('Unable to resolve macro "%s" on object "%s". An error occured: %s', $macro, $objectName, $e);
}

if ($value instanceof Query || $value instanceof ResultSet || is_array($value)) {
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ parameters:
- ipl\Orm\Model
- Icinga\Web\View
- Icinga\Data\ConfigObject
- Icinga\Module\Monitoring\Object\MonitoredObject
Loading