Our great sponsors
-
validator
:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
-
WorkOS
The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.
-
InfluxDB
Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
Let's try to create closer to real world gRPC microservice with tracing and monitoring 👋: Source code u can find here Core tools used what will be used: 🚀 PostgreSQL as database Redis for sessions and caching Jaeger open source, end-to-end distributed tracing Prometheus monitoring and alerting Grafana for to compose observability dashboards with everything from Prometheus
Then we usually have to validate request input, for errors gRPC has packages status and codes I found good practice to parse and log errors in handler layer, here i use ParseGRPCErrStatusCode method, which parse err and returns matched gRPC code. Validator is good solution for validation.
Every app must be covered by tests, I didn't completely cover all code this one, but wrote some test of course. For testing and mocking testify and gomock is very good tools.
Setup postgres and redis Usually production SQL db standard solution for these days is combination of sqlx and pgx. Good Redis Go clients is go-redis and redigo, i used first.
I found this is very good gRPC Middleware repository, but we easy can create our own, for example logger interceptor:
I like to use evans for simple testing gRPC.
In cmd folder let's init all dependencies and start the app. Viper is very good and common choice as complete configuration solution for Go applications. We use here config-local.yml file approach.
Next let's create logger, here i used Uber's Zap under the hood, important here is to create Logger interface for be able to replace logger in the future if it's need.