Golang web service framework with DI, Telemetry and more
I have my own take on this: <a href="https:&#x2F;&#x2F;github.com&#x2F;jrockway&#x2F;opinionated-server" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jrockway&#x2F;opinionated-server</a><p>Some things we do differently, and some things we do the same.  We give each RPC handler a logger, so you know where logs are coming from (and the trace ID associated with every message).  We collect detailed prometheus metrics for every RPC, and send everything we know to Jaeger.  We provide clients for outgoing RPCs that log and trace in the same way as incoming requests.<p>We pick concrete types for logging&#x2F;tracing&#x2F;metrics, which makes it easier to figure out what code is actually running.  Having used the abstraction layers, I find that when they don&#x27;t do what you want, it can take a long time to figure out why.  If you have to switch from Prometheus to Statsd or something, the performance and configuration characteristics of your app change, so you&#x27;ll probably want to manually revisit every metric.  It&#x27;s a straightforward refactoring that you will probably never do more than once, so I don&#x27;t think it&#x27;s worth making it &quot;easy&quot; out of the box.<p>Finally, we let you configure your app with either flags or environment variables.  Generally, I like flags for running things while developing (go run main.go --foo=bar) and environment variables for production.  Why pick one or the other when you can do both!<p>Extensive care has been taken to be able to log everything, including all payloads for bidirectional streams.  (HTTP logging is a bit of a disaster; check out the code you have to write to be able to hook every type of http.ResponseWriter without the consumer noticing: <a href="https:&#x2F;&#x2F;github.com&#x2F;prometheus&#x2F;client_golang&#x2F;blob&#x2F;master&#x2F;prometheus&#x2F;promhttp&#x2F;delegator.go#L117" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;prometheus&#x2F;client_golang&#x2F;blob&#x2F;master&#x2F;prom...</a>.)<p>Some features that are missing or sub-optimal:<p>* A long chain of interceptors for every request is very expensive.  They should all be condensed into one wrapper that does &quot;everything&quot; explicitly.  I don&#x27;t think it&#x27;s worth the debugging complexity to be able to compose a bunch of middleware at runtime; just write a function that wraps every request that does the stuff you want.  If you want more stuff, type it in right there.  Easy to read, easy for the computer to execute.<p>* There should be fine-grained control over logging at runtime.  I think you really want to be able to pass in configuration for every sub-logger, rather than a global log level.  For example, you may want to adjust log sampling rate at runtime for a particular component, or upgrade a particular component from INFO to DEBUG for temporary investigation.  (Without these controls, people stop writing log statements because they&#x27;re too spammy, and then you have absolutely nothing to help you with ongoing issues.)<p>* There is absolutely no support for mTLS, which is a crime in this day and age.  This is probably the next thing that I&#x27;ll add; you should be able to watch a volume for changes in TLS key&#x2F;cert&#x2F;ca material (so you can rotate keys frequently and without restarting), and require that incoming requests have a certificate trusted by the loaded CA.  Outgoing requests should also be marked as &quot;external&quot; or &quot;internal&quot;, and use the correct client cert for internal requests.  (I somewhat promise to add this soon, because it&#x27;s sorely needed.)<p>* I don&#x27;t think applications should support any non-production features for development, like a different logging configuration.  opinionated-server apps take a --pretty_logs argument that configures it to print plain text instead of JSON, but I don&#x27;t use it anymore; I just pipe the logs through jlog (`go install github.com&#x2F;jrockway&#x2F;json-logs&#x2F;cmd&#x2F;jlog`) and the results are much prettier than what zap would do itself.  And it&#x27;s the same tool I use every day to look at production logs, so there are no surprises.<p>Ultimately, I wrote this because I like small pieces of software that do one thing, and found myself writing the same boilerplate over and over.  Overall, this has been a success.  I have written small utilities (github.com&#x2F;jrockway&#x2F;alertmanager-status, github.com&#x2F;jrockway&#x2F;nodedns, github.com&#x2F;jrockway&#x2F;alertmanager-discord, which I forked from someone else and converted to use opinionated-server), and larger applications (github.com&#x2F;jrockway&#x2F;jsso2, github.com&#x2F;jrockway&#x2F;ekglue), and overall not having to write the same monitoring&#x2F;logging code over and over has proven quite productive.  I have not achieved my goal of &quot;1 hour to make a production application&quot; quite yet, but opinionated-server is certainly helping.