Backend Development 10 min read

Master Go Microservices: gRPC, TLS, Tracing & Prometheus Monitoring

This article shares practical Go microservice building experiences, covering gRPC-based communication, TLS security, request tracing, and comprehensive monitoring with Prometheus, including metric selection, alerting, and log management using Logrus and Graylog, to help reduce coupling and improve system observability.

UCloud Tech
UCloud Tech
UCloud Tech
Master Go Microservices: gRPC, TLS, Tracing & Prometheus Monitoring

Introduction

In large‑scale systems, microservice architecture splits a system into many loosely coupled modules, each providing specific functionality. Developers aim to minimize inter‑module coupling while addressing deployment and operation challenges.

Development with gRPC

To define clear interfaces and reduce communication overhead, the team adopted Google’s gRPC framework instead of traditional HTTP RPC. gRPC uses Protocol Buffers (proto3) for interface definition, enabling compile‑time checks and eliminating runtime mismatches.

gRPC also brings performance benefits: it runs over HTTP/2, reuses TCP connections, and allows header‑based control fields for routing and canary releases.

Using context for request lifetimes

In Go’s gRPC implementation, the first argument of each RPC is a

context.Context

. The context is propagated via HTTP/2 headers, allowing timeout and cancellation handling.

ctx := context.Background() // blank context
ctx, cancel = context.WithTimeout(ctx, 5*time.Second)
defer cancel()
grpc.CallServiceX(ctx, arg1)

This code sets a 5‑second deadline; if the remote call does not return within that period, the caller aborts with an error.

TLS for access control

gRPC integrates TLS certificates, enabling fine‑grained permission control. The team built a self‑signed two‑level certificate system: a root certificate held by a privileged service A, and per‑client certificates issued to services that need to call A, allowing authentication, logging, and traffic shaping.

Tracing requests

gRPC’s built‑in tracing records detailed logs for the most recent ten requests and aggregates statistics for all calls. By injecting trace IDs into the context, the system can retrieve end‑to‑end logs from Graylog.

Monitoring

The team follows Google SRE’s “four golden signals”: latency, traffic, errors, and saturation. They monitor latency percentiles, request rates, error codes, and CPU/memory usage.

For monitoring solutions, they evaluated a custom in‑house system versus the open‑source Prometheus. Given ~100 containers on 30 VMs and the need for flexible metric queries, Prometheus was chosen.

Prometheus scrapes metrics from services registered in Consul, stores them locally, and provides a Web UI for PromQL queries. Grafana visualizes the data, and Alertmanager handles alerts via phone, email, or other channels.

Logging

Log format choice impacts downstream processing. The team uses Logrus, supporting plain text and JSON formats. Example logs:

time="2015-03-26T01:27:38-04:00" level=debug g="Started observing beach" animal=walrus number=8
time="2015-03-26T01:27:38-04:00" level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10
{"animal":"walrus","level":"info","msg":"A group of walrus emerges from the ocean","size":10,"time":"2014-03-10 19:57:38.562264131 -0400 EDT"}

For end‑to‑end visibility, a global request ID is generated at the entry point, propagated via gRPC context, and logs are aggregated in Graylog, enabling full‑trace retrieval by ID.

Conclusion

Building microservices with Go involves best practices in deployment, scheduling, service discovery, and consistency using Docker, Kubernetes, Consul, etcd, and the techniques described above.

monitoringmicroservicesGogrpcloggingPrometheus
UCloud Tech
Written by

UCloud Tech

UCloud is a leading neutral cloud provider in China, developing its own IaaS, PaaS, AI service platform, and big data exchange platform, and delivering comprehensive industry solutions for public, private, hybrid, and dedicated clouds.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.