I've just pushed the first iteration of a plugin for adding a correlation id to requests. This is to enable us to track requests across multiple services and is available on github and nuget.
Any feedback is greatly appreciated. There are more details in the readme of the repository.
The initial implementation of the plugin added the correlation id to the request in
PreRequestFilter and the response in
GlobalResponseFilter. However, after I saw this post regarding the new
IServiceGateway interface I tried to implement something using this that would allow external calls to have the correlation id appended to them, internal calls are fine as the same
IRequest is used.
In addition to the PreRequest/Response filters I check the IoC container for implementation of
IServiceGatewayFactory then check if it is a
ServiceGatewayFactoryBase, if it is I then use a decorator (source) to add the correlation id to request. The reason for this is that I need to access the
IServiceGateway GetServiceGateway(IRequest request)) to get the correlation id from the current request. I then override
IServiceGateway GetGateway(Type requestType), the only way I could see to add the header to outgoing external calls was to check if the
IServiceGateway is a
ServiceClientBase, and if so add the header to the
ServiceClientBase.Headers collection. Is this the best way to achieve adding the correlation Id to an outgoing request?
It seems that it could be a bit brittle as it relies on an implementation of
ServiceGatewayFactoryBase that internally uses and implementation of
ServiceClientBase (although if these are the general way that external calls are made it may not be too much of a problem).
All HttpWebRequest-based Service Clients inherit from
ServiceClientBase, the only other .NET ServiceClient is the HttpClient-based
JsonHttpClient. But you can instead cast to
IRestClient and use the
void AddHeader(string name, string value) API which all Service Clients implement.
Thanks Demis, not sure how I missed that method!
I've just updated the code locally to cast to
IRestClient and will push update soon.