Skip to content
This repository has been archived by the owner on Apr 28, 2022. It is now read-only.

Commit

Permalink
Improve HttpSender flexibility (#184)
Browse files Browse the repository at this point in the history
- WithHttpHandler
- WithCertificates
- WithUserAgent

Signed-off-by: Kraemer, Benjamin <[email protected]>
  • Loading branch information
Falco20019 authored Jun 26, 2020
1 parent 4e97b9a commit 69d02ca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ namespace Jaeger.Thrift.Senders.Internal
// ReSharper disable once InconsistentNaming
public class THttpTransport : TTransport
{
private readonly X509Certificate[] _certificates;
private readonly Uri _uri;

private int _connectTimeout = 30000; // Timeouts in milliseconds
Expand All @@ -40,30 +39,30 @@ public class THttpTransport : TTransport
private MemoryStream _outputStream = new MemoryStream();
private bool _isDisposed;

public THttpTransport(Uri uri, IDictionary<string, string> customRequestHeaders = null, string userAgent = null, IDictionary<string, object> customProperties = null)
: this(uri, Enumerable.Empty<X509Certificate>(), customRequestHeaders, userAgent, customProperties)
{
}

public THttpTransport(Uri uri, IEnumerable<X509Certificate> certificates,
IDictionary<string, string> customRequestHeaders, string userAgent = null,
public THttpTransport(Uri uri, IDictionary<string, string> customRequestHeaders,
HttpClientHandler handler = null, IEnumerable<X509Certificate> certificates = null,
string userAgent = null,
IDictionary<string, object> customProperties = null)
{
_uri = uri;
_certificates = (certificates ?? Enumerable.Empty<X509Certificate>()).ToArray();

if (!string.IsNullOrEmpty(userAgent))
{
UserAgent = userAgent;
}

CustomProperties = customProperties ?? new Dictionary<string, object>();

// due to current bug with performance of Dispose in netcore https://github.com/dotnet/corefx/issues/8809
// this can be switched to default way (create client->use->dispose per flush) later
_httpClient = CreateClient(customRequestHeaders);

handler ??= new HttpClientHandler();
certificates ??= Enumerable.Empty<X509Certificate>();
_httpClient = CreateClient(handler, certificates, customRequestHeaders);
}

// According to RFC 2616 section 3.8, the "User-Agent" header may not carry a version number
public readonly string UserAgent = "Thrift netstd THttpClient";
public string UserAgent { get; } = "Thrift netstd THttpClient";

public override bool IsOpen => true;

Expand Down Expand Up @@ -141,10 +140,13 @@ public override async Task WriteAsync(byte[] buffer, int offset, int length, Can
await _outputStream.WriteAsync(buffer, offset, length, cancellationToken);
}

private HttpClient CreateClient(IDictionary<string, string> customRequestHeaders)
private HttpClient CreateClient(HttpClientHandler handler, IEnumerable<X509Certificate> certificates, IDictionary<string, string> customRequestHeaders)
{
var handler = new HttpClientHandler();
handler.ClientCertificates.AddRange(_certificates);
if (certificates != null)
{
handler.ClientCertificates.AddRange(certificates.ToArray());
}

handler.AutomaticDecompression = System.Net.DecompressionMethods.Deflate | System.Net.DecompressionMethods.GZip;

var httpClient = new HttpClient(handler);
Expand Down
25 changes: 24 additions & 1 deletion src/Senders/Jaeger.Senders.Thrift/HttpSender.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -51,7 +53,7 @@ private HttpSender(Builder builder)
{ "ot-ignore", true }
};

_transport = new THttpTransport(collectorUri, customHeaders, customProperties: customProperties);
_transport = new THttpTransport(collectorUri, customHeaders, builder.HttpHandler, builder.Certificates, builder.UserAgent, customProperties);
_protocol = ProtocolFactory.GetProtocol(_transport);
}

Expand Down Expand Up @@ -91,6 +93,9 @@ public sealed class Builder
internal string Endpoint { get; }
internal int MaxPacketSize { get; private set; } = OneMbInBytes;
internal AuthenticationHeaderValue AuthenticationHeaderValue { get; private set; }
public HttpClientHandler HttpHandler { get; private set; }
public IEnumerable<X509Certificate> Certificates { get; private set; }
public string UserAgent { get; private set; }

public Builder(string endpoint)
{
Expand All @@ -117,6 +122,24 @@ public Builder WithAuth(string authToken)
return this;
}

public Builder WithHttpHandler(HttpClientHandler httpHandler)
{
HttpHandler = httpHandler;
return this;
}

public Builder WithCertificates(IEnumerable<X509Certificate> certificates)
{
Certificates = certificates;
return this;
}

public Builder WithUserAgent(string userAgent)
{
UserAgent = userAgent;
return this;
}

public HttpSender Build()
{
return new HttpSender(this);
Expand Down

0 comments on commit 69d02ca

Please sign in to comment.