Hello Encore enthusiast!
I want to save information about calls to our system with some additional information regarding how the call was processed (there are multiple services that can fail/succeed) and I want to save this information to enable better insights for our users.
I planned to use the Pub/Sub feature for this but after testing a little I see that it adds quite some time to responses, therefore I wanted to ask if there is any good way to save these metrics without delaying the response to the caller? Is just inserting it to the psql DB my best choice?
All the best,
Hey Max, great questions. Like most things it depends on your use case.
You can use Pub/Sub but fire off the publishing in a goroutine so it doesn’t block the API response, and add logging to catch publishing errors. That would be my suggested approach.
You can store it in the database which may be faster if you’re already doing a bunch of database operations, but over time may lead to poor performance if you’re constantly inserting large volumes. It depends on what you want to do with the data and how you intend to store it.
Thanks for the suggestion!
Would the context not interrupt the pub/sub from the goroutine if I do it at the end of a call (more or less just before responding/returning)? Or will it persist after the return of the endpoint/api?
Not sure how much data I would want to insert although at first it would simply be small updates.
It would if you use the request context, but there is nothing special about that context and you can use whatever context you want.
In this case you should create a separate context based on context.Background (with a timeout) and use that to publish.
Max, I’d suggest avoiding re-inventing the wheel if possible! That’s the encore philosophy after all, right? A time-series database is the way to go here. I’d suggest thinking about something like Grafana Cloud, and then a middleware which captures the request stats.
It’s a little bit of work to get the infrastructure in place but it’ll save you tons of time on the analysis to have the data captured in a standard format.
It would be cool to be able to combine the automatic traces from Encore with external tracing as well - getting OpenTelemetry out from Encore into external systems, and possibly the other way around as well, would be optimal.
Yes, totally agree! We plan to support exactly that in the future.