We're looking at adding either zipkin (or the emerging distributed systems tracing spec at http://opentracing.io) headers to our http calls so we can see wtf is going on in our distributed problemsystem.
Is there a way to hook onto the http client in the guts of the SDK here so we can publish information to our collector before requests start and after they finish, to implement the client-end of traces? Obviously, AWS doesn't support either zipkin (or opentracing.io) headers/annotations (and it would be fantastic if it started to!), but this would at least allow us to see the client-calls to the external AWS APIs within our monitoring.
Yes, there are a couple of ways to do this.
var client = new AmazonS3Client();
client.BeforeRequestEvent += OnBeforeRequestEvent;
client.AfterResponseEvent += OnAfterResponseEvent;
private void OnAfterResponseEvent(object sender, ResponseEventArgs e)
{
var responseEvent = e as WebServiceResponseEventArgs;
// Log response code
Console.WriteLine(responseEvent.Response.HttpStatusCode);
}
private void OnBeforeRequestEvent(object sender, RequestEventArgs e)
{
var requestEvent = e as WebServiceRequestEventArgs;
requestEvent.Headers.Add("MyHeaderKey", "MyHeaderValue");
}
Here are the steps to use this approach
a. Create a custom handler by implementing the IPipelineHandler interface or extending the PipelineHandler class . You can refer to handler implementations here and here .
b. Add the handler to the client. To do this you need to subclass from a client and override the _CustomizeRuntimePipeline_ method, see an example here. The _AddHandlerBefore_, _AddHandlerAfter_ and _ReplaceHandler_ methods can be used to modify the handlers in the runtime pipeline.
public class MyS3Client : AmazonS3Client
{
// Add constructors
// Modify the runtime pipeline
protected override void CustomizeRuntimePipeline(RuntimePipeline pipeline)
{
base.CustomizeRuntimePipeline(pipeline);
pipeline.AddHandlerAfter<Amazon.Runtime.Internal.Marshaller>(new MyCustomHandler());
}
}
Closing as answered; feel free to reopen if more guidance is needed.
Most helpful comment
Yes, there are a couple of ways to do this.
Here are the steps to use this approach
a. Create a custom handler by implementing the IPipelineHandler interface or extending the PipelineHandler class . You can refer to handler implementations here and here .
b. Add the handler to the client. To do this you need to subclass from a client and override the _CustomizeRuntimePipeline_ method, see an example here. The _AddHandlerBefore_, _AddHandlerAfter_ and _ReplaceHandler_ methods can be used to modify the handlers in the runtime pipeline.