Flink Metrics Reporter Source Code Analysis with Practical PushGateway Integration
This article provides a comprehensive guide to Flink Metrics Reporter, demonstrating practical integration with Prometheus PushGateway for metric collection and visualization via Grafana, followed by a detailed source code analysis of the PrometheusPushGatewayReporter implementation in Flink 1.18.
Reading font
Flink sends runtime metrics to external systems via Metrics Reporters. Supported reporters include Prometheus and PrometheusPushGateway. Flink's plugin design allows adding a JAR to the plugins directory and configuring flink-conf.yaml for automatic loading at startup. Custom reporters can be built by implementing org.apache.flink.metrics.reporter.MetricReporter and Scheduled interfaces.
Practical Example: Flink 1.18 to Prometheus PushGateway
The example pushes metrics from Flink 1.18 to Prometheus PushGateway, with Prometheus scraping PushGateway and Grafana visualizing the data.
Environment Setup
KubeSphere (bundles Prometheus Operator and exporters) installed via
https://kubesphere.io/zh/docs/v3.4/quick-start/minimal-kubesphere-on-k8s/.
Prometheus PushGateway deployed as a Kubernetes Deployment and Service (NodePort 32039) in the kubesphere-monitoring-system namespace. The article provides exact manifests for Deployment, Service, and ServiceMonitor.
ServiceMonitor resource enables Prometheus Operator to automatically discover and scrape PushGateway. After applying, PushGateway appears as a target in Prometheus.
Flink Configuration
Built-in plugin JARs are located under /flink-1.18.0/plugins. The following configuration is added to flink-conf.yaml:
# Specify factory class
metrics.reporter.promgateway.factory.class: org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporterFactory
# PushGateway host
metrics.reporter.promgateway.host: localhost
# PushGateway port
metrics.reporter.promgateway.port: 32039
# Job name for PushGateway
metrics.reporter.promgateway.jobName: flink-metrics
# Add random suffix to job name to avoid conflicts
metrics.reporter.promgateway.randomJobNameSuffix: true
# Delete metrics on shutdown
metrics.reporter.promgateway.deleteOnShutdown: false
# Push interval
metrics.reporter.promgateway.interval: 10 SECONDSStarting the cluster with ./bin/start-cluster.sh loads the plugin, visible in logs. The PushGateway UI then shows received metrics. Grafana dashboards can be imported from https://grafana.com/grafana/dashboards/?search=flink and linked to the Prometheus data source for visualization.
Source Code Analysis (Flink 1.18, release-1.18 branch)
The Flink repository is cloned from GitHub and the release-1.18 branch is checked out. The flink-metrics-prometheus module contains the PushGateway reporter classes:
PrometheusPushGatewayReporterOptions : maps configuration keys from flink-conf.yaml.
PrometheusPushGatewayReporterFactory : loaded via SPI at startup, initializes the reporter.
PrometheusPushGatewayReporter : performs the actual metric reporting.
The factory creates a ScheduledExecutorService that periodically triggers the reporter's report() method. Scheduling is driven by org.apache.flink.runtime.metrics.MetricRegistryImpl, which initializes a timer running a ReporterTask that calls report() on each registered reporter.
PrometheusPushGatewayReporter extends AbstractPrometheusReporter , which defines how Flink metric types are translated to Prometheus formats:
GAUGE : single value that can go up or down (e.g., queue length, connection count).
COUNTER : monotonically increasing value (e.g., request count, error count).
METER : measures event rate per second (throughput).
HISTOGRAM : measures data distribution via buckets.
The article includes screenshots of the source code for these classes, showing implementation details.
Conclusion
The official Flink metrics documentation (
https://nightlies.apache.org/flink/flink-docs-master/docs/ops/metrics/) provides deeper metric definitions. The piece covered both practical deployment and core source code flow of the PushGateway reporter.
For questions about attribution, corrections or content rights, please contactand we will review it promptly.