Implement "hop count" header via EnvoyFilter

I need to create a “hop count” header for each request between my services inside my cluster running with istio.

After some research, I found the EnvoyFilter and the ability to “run some code” on each request.

So I was thinking, is possible to code something like:

  • Before response, get my custom-header-hop-count from request, increment it, check if the “max-hop-count” is not reached (if reached, throw error) and return the incremented hop-count to be verified in the next request if necessary.

Is this possible? Can someone provide an example?