Scraping Methods
Prometheus is a monitoring system that pulls metrics from specific targets and stores them as time series data. Scraping is the process by which Prometheus periodically collects metrics from target services. Apinizer Gateway’s metrics are provided via port 9091, and Prometheus can pull these metrics in two different ways:Constant Scraping
In the fixed scraping method, services to be monitored are defined under static_configs with predetermined fixed IP addresses or DNS names. Prometheus queries these services at regular intervals. This method is useful when service addresses do not change or can be defined manually.
Dynamic Scraping
In the dynamic scraping method, Prometheus automatically discovers services through a service discovery mechanism such as Kubernetes. kubernetes_sd_config or similar configurations enable Prometheus to automatically discover services without the need to update Prometheus configuration as services change. This method provides great advantages in microservice architectures and constantly changing infrastructures.
Fixed (Constant) Scraping Configuration
For Apinizer Gateway to publish metrics via port 9091, theMETRICS_ENABLED=TRUE parameter must be defined as an environment variable to the relevant container.
Service Creation
apinizer-prometheus-svc.yaml file:
Prometheus ConfigMap Configuration
For Apinizer Gateway to provide metrics to Prometheus from port 9091, Prometheus’s scraping configuration must be done via ConfigMap. Constant scraping configuration is performed by defining Prometheus’s worker-prometheus-service named service in static_configs at the specified address.Dynamic (Dynamic) Scraping Configuration
The dynamic scraping method enables Prometheus to automatically discover pods within Kubernetes. Thanks to this method, there is no need to make manual scraping configuration for each new pod. Prometheus automatically pulls metrics using specific annotations added to pods.For Gateway metrics to be collected by Prometheus, the
METRICS_ENABLED=TRUE variable must be added by selecting Worker edit deployment option in the Deployments & Services section on the Gateway Environments page.If Kubernetes Management is Not Done with Apinizer
To enable Prometheus to collect metrics from Worker pods, relevant annotations must be added to thespec.template.metadata.annotations section of the relevant Deployment manifest. In addition, for Apinizer Gateway to publish metrics via port 9091, the METRICS_ENABLED=TRUE parameter must be defined as an environment variable to the relevant container.
This way, the metrics service will be exported via port 9091 and Prometheus will automatically discover and scrape metrics from the Apinizer Gateway pod running on port 9091.
Prometheus Scraping Configuration
Dynamic scraping must be enabled usingkubernetes_sd_configs in Prometheus’s ConfigMap configuration to discover annotations in pods.
The ConfigMap example below enables Prometheus to dynamically discover Kubernetes pods. This configuration enables Prometheus to automatically discover Kubernetes pods and collect metrics only from pods with the
prometheus.io/scrape: "true" annotation. Thus, metrics can be collected by Prometheus by applying dynamic scraping to Apinizer Gateway without the need for manual target definition.Prometheus Installation
Persistent Storage Configuration
Since Prometheus’s metrics will be stored on a node in the Kubernetes cluster, PersistentVolume (PV) and PersistentVolumeClaim (PVC) definitions must be made. This configuration ensures that Prometheus preserves its data in case of shutdown or restart.ServiceAccount and RBAC Configuration
Prometheus must have the necessary permissions to discover pods and collect their metrics. For this, the following ServiceAccount, ClusterRole and ClusterRoleBinding definitions must be made:When Prometheus is deployed on Kubernetes, it creates a Kubernetes service named prometheus-service and of type NodePort. This service is necessary for accessing Prometheus from outside Kubernetes. However, you can adapt this service according to the structure you use for Ingress or connection method in your organization.

