Shopify / pitchfork

Other
677 stars 22 forks source link

Getting stable worker id for instrumentation purposes #145

Open errm opened 2 hours ago

errm commented 2 hours ago

I maintain a small library for doing instrumentation with prometheus (https://github.com/errm/promenade)

A feature of this library is that we label metrics exported from each worker of a multi process server like pitchfork or unicorn with a stable id. On a sever where workers can be restarted (or reforked) it's not ideal to just use the pid for this label, as it causes us some implimentation problems, and increases the cardinality on prometheus...

In unicorn we parsed $0 to get a stable id for each worker, like $0.match(/worker\[([^\]]+)\]/)

I wanted to do something similar for pitchfork, but it seems that because you are using Process.setproctitle("foo") rather than $0 = "foo" I won't be able to do this (because Process.setproctitle by design doesn't change $0).

On linux I could just read /proc/self/cmdline, but I would rather not, because it isn't very portable (and I would like things to also work on OSX development machines).

Is it possible that pitchfork could just assign to $0? Or perhaps provide some other stable interface to give the current worker a stable identity?

casperisfine commented 2 hours ago

Can't you use after_worker_fork callbacks for that? They recieve the Worker instance, so you can assign worker.nr to your instrumentation code.

I'd rather not set $0, and even if I did, I'd likely break your integration whenever I feel like changing how the proctitle is rendered.