如何通过Grafana监控基于spring-cloud-gcp-starter-pubsub的Google PubSub订阅指标?
Monitoring Spring Cloud GCP Pub/Sub with Grafana: Acknowledged Messages & Latency
Absolutely, there are reliable, battle-tested ways to track your Pub/Sub subscription's acknowledged message count and message latency in Grafana. Let’s break down the most practical approaches I’ve implemented or seen work well for teams using Spring Cloud GCP Pub/Sub:
1. Leverage Spring Boot Actuator + Micrometer (Simplest Native Integration)
This is my go-to for Spring-based services since it’s baked into the ecosystem:
- Add Dependencies: Include
spring-boot-starter-actuatorandmicrometer-registry-prometheusin yourpom.xmlorbuild.gradle—these let you expose metrics and make them scrapeable by Prometheus. - Enable Pub/Sub Metrics: In your
application.ymlorapplication.properties, turn on Pub/Sub's built-in metrics:spring: cloud: gcp: pubsub: metrics: enabled: true management: endpoints: web: exposure: include: metrics,prometheus metrics: tags: application: your-service-name - Key Metrics to Use:
- Acknowledged Messages: Use
pubsub_subscription_acknowledged_messages_total(a counter that tracks all successfully acknowledged messages per subscription). - Message Latency: Use
pubsub_subscription_message_age_seconds(a gauge that shows the time between a message being published and being acknowledged, in seconds).
- Acknowledged Messages: Use
- Connect to Grafana: Set up Prometheus as a data source in Grafana, then build panels—use a Stat panel for total acknowledged messages, and a Time Series panel to visualize latency trends over time.
2. Use Google Cloud Monitoring (Native GCP Integration)
If your service runs on GCP, this is a zero-code option for out-of-the-box metrics:
- GCP’s Built-in Pub/Sub Metrics: GCP automatically tracks metrics like:
Acknowledged messages(count of messages successfully acknowledged)Message latency(time from message publication to acknowledgment)
- Connect Grafana to GCP Monitoring: Add the Google Cloud Monitoring data source in Grafana, authenticate with a service account that has the
Monitoring Viewerrole, then select the Pub/Sub metrics for your subscription directly in the panel builder. - Pro Tip: You can filter metrics by subscription name, region, or project to narrow down to exactly what you need.
3. Custom Metrics (For Granular Control)
If you need more specific tracking (e.g., latency per message type, or custom acknowledgment logic), roll your own with Micrometer:
- Track Acknowledged Messages: Create a Counter and increment it every time you successfully process a message:
@Autowired private MeterRegistry meterRegistry; private Counter acknowledgedMessagesCounter; @PostConstruct public void initMetrics() { acknowledgedMessagesCounter = Counter.builder("pubsub.custom.acknowledged.messages") .tag("subscription", "your-subscription-name") .description("Total custom acknowledged messages") .register(meterRegistry); } // In your message listener method public void handleMessage(PubsubMessage message) { // Process message logic acknowledgedMessagesCounter.increment(); } - Track Custom Latency: Extract the message's publish time (either from PubSub's
publish_timeattribute or a custom attribute you add when sending), calculate the latency, and record it with a Timer:private Timer messageLatencyTimer; @PostConstruct public void initMetrics() { messageLatencyTimer = Timer.builder("pubsub.custom.message.latency") .tag("subscription", "your-subscription-name") .description("Latency from publish to acknowledgment") .register(meterRegistry); } public void handleMessage(PubsubMessage message) { long publishTime = Long.parseLong(message.getAttributes().get("publish_time")); long latencyMs = System.currentTimeMillis() - publishTime; messageLatencyTimer.record(latencyMs, TimeUnit.MILLISECONDS); // Process message logic } - Expose & Visualize: These custom metrics will be included in the Prometheus endpoint—just add them to your Grafana panels like any other metric.
Quick Notes to Avoid Pitfalls
- Make sure your Spring Cloud GCP version is 2.0 or higher (earlier versions don’t have full Micrometer integration for Pub/Sub).
- For latency metrics, clarify whether you need "time to receive" or "time to acknowledge"—the built-in
message_age_secondstracks the latter, which is usually what teams care about for processing SLAs. - When using GCP Monitoring, ensure your service account has the correct IAM permissions to read metrics.
内容的提问来源于stack exchange,提问作者Mahmoud Azmy
相关产品推荐
相关产品推荐

