Aws-sdk-net: Possible to supply our own http wrapper?

Created on 6 Jul 2016  路  2Comments  路  Source: aws/aws-sdk-net

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.

guidance

Most helpful comment

Yes, there are a couple of ways to do this.

  • Use events on the client class
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");
}

  • Add custom handlers to the runtime pipeline used by the client. The client's request response processing is done by a set of handlers, the runtime pipeline is a ordered list of handlers. This option allows for all kinds of customization but it needs an understanding the internals and ordering of handlers in the runtime pipeline. You can see how a client uses a set of handlers and its ordering here.

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());
    }
}

All 2 comments

Yes, there are a couple of ways to do this.

  • Use events on the client class
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");
}

  • Add custom handlers to the runtime pipeline used by the client. The client's request response processing is done by a set of handlers, the runtime pipeline is a ordered list of handlers. This option allows for all kinds of customization but it needs an understanding the internals and ordering of handlers in the runtime pipeline. You can see how a client uses a set of handlers and its ordering here.

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.

Was this page helpful?
0 / 5 - 0 ratings