- Implement query optimization (reduced output, parameter skipping) to minimize Zabbix API traffic. - Add indirect dependency handling: deviceType implies tags and state implies items. - Move schema extensions to samples/extensions/ to clarify their role as samples. - Enhance DistanceTrackerDevice with String time fields to support optional date portions. - Ensure allDevices strictly filters by deviceType and populates the field in results. - Refactor runAllRegressionTests mutation to use internal unique names and improve stability. - Fix unnecessary Zabbix API calls for item preprocessing during template and host imports. - Update documentation including cookbook recipes, test specifications, and optimization guides. - Add extensive unit, integration, and regression tests covering all implemented changes. - Update docker-compose.yml to mount the samples/ directory as a volume. - Update IntelliJ .idea run configurations to reflect the new sample extension paths.
3.2 KiB
⚡ Query Optimization
This document describes how the Zabbix GraphQL API optimizes queries to reduce data fetching from Zabbix, improving performance and reducing network load.
🚀 Overview
The optimization works by analyzing the requested GraphQL fields and only fetching the necessary data from the Zabbix API. This is achieved through:
- Output Reduction: Dynamically setting the
outputparameter in Zabbix requests. - Parameter Skipping: Automatically removing optional Zabbix parameters (like
selectTagsorselectItems) if the corresponding GraphQL fields are not requested.
🏗️ Implementation Details
1. ZabbixRequest Enhancement
The base ZabbixRequest class handles the core optimization logic:
optimizeZabbixParams(params, output): This method modifies the Zabbix parameters. It filters theoutputarray to match the requested fields and removes "skippable" parameters based on rules.skippableZabbixParams: A map that defines dependencies between Zabbix parameters and GraphQL fields.- Example:
this.skippableZabbixParams.set("selectTags", "tags")meansselectTagswill be removed iftagsis not in the requested output.
- Example:
2. Parameter Mapping
The GraphqlParamsToNeededZabbixOutput class provides static methods to map GraphQL query arguments and the selection set (GraphQLResolveInfo) to a list of needed Zabbix fields.
3. Resolver Integration
Resolvers use the mapper to determine the required output and pass it to the datasource:
const output = GraphqlParamsToNeededZabbixOutput.mapAllHosts(info);
return await new ZabbixQueryHostsRequestWithItemsAndInventory(...)
.executeRequestThrowError(dataSources.zabbixAPI, new ParsedArgs(args), output);
4. Indirect Dependencies
Some GraphQL fields are not directly returned by Zabbix but are computed from other data. The optimization logic ensures these dependencies are handled:
state: Requesting thestatefield on aDevicerequires Zabbixitems. The mapper automatically addsitemsto the requested output ifstateis present.deviceType: RequestingdeviceTyperequires Zabbixtags(orinheritedTags). This is needed because thedeviceTypeis resolved from a Zabbix tag and will be empty otherwise. The optimization logic ensures thatselectTagsandselectInheritedTagsare not skipped whendeviceTypeis requested.
🛠️ Configuration
Optimization rules are defined in the constructor of specialized ZabbixRequest classes.
📋 Supported Optimizations
- Hosts & Devices:
selectParentTemplatesskipped ifparentTemplatesnot requested.selectTagsandselectInheritedTagsskipped iftags(ordeviceType) not requested.selectHostGroupsskipped ifhostgroupsnot requested.selectItemsskipped ifitems(orstate) not requested.selectInventoryskipped ifinventorynot requested.
- Templates:
selectItemsskipped ifitemsnot requested.
✅ Verification
You can verify that optimization is working by:
- Enabling
debuglog level (LOG_LEVEL=debug). - Observing the Zabbix request bodies in the logs.
- Checking that the
outputarray is minimized andselect*parameters are omitted when not needed.