Extending the App¶
Contributions and extensions are welcome. Please open an issue first to discuss the proposed change before submitting a PR.
Adding Custom Metrics¶
To add a new Prometheus metric:
-
Define the metric in
nautobot_graphql_observability/metrics.py: -
Import and record it in the appropriate method of
PrometheusMiddlewareinnautobot_graphql_observability/middleware.py. -
If the metric should be optional, add a new boolean setting to
NautobotAppGraphqlObservabilityConfig.default_settingsin__init__.pyand gate the recording behind a config check in the middleware.
Adding New Labels to Existing Metrics¶
Adding labels to existing metrics is a breaking change for Prometheus (it creates a new time series). If you need additional labels:
- Consider creating a new metric instead.
- If modifying an existing metric, update the label list in
metrics.pyand all.labels()calls inmiddleware.py. - Update tests to include the new label values.
Customizing Histogram Buckets¶
The default histogram buckets are defined in metrics.py. To customize them for your deployment, you can fork the metric definitions. A future enhancement may allow bucket configuration via PLUGINS_CONFIG.
Extending the Logging Middleware¶
The GraphQLQueryLoggingMiddleware in nautobot_graphql_observability/logging_middleware.py can be extended to add custom fields to log entries. The middleware uses Python's standard logging module with the logger name nautobot_graphql_observability.graphql_query_log.
To add custom log fields, subclass GraphQLQueryLoggingMiddleware and override _log_query():
from nautobot_graphql_observability.logging_middleware import GraphQLQueryLoggingMiddleware
class CustomLoggingMiddleware(GraphQLQueryLoggingMiddleware):
@staticmethod
def _log_query(config, operation_type, operation_name, user, start_time, info, error=None):
# Call parent to emit the standard log entry
GraphQLQueryLoggingMiddleware._log_query(
config, operation_type, operation_name, user, start_time, info, error
)
# Add custom logic here