Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EnableTelemetry flag #1771

Merged
merged 1 commit into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Stripe.net/Infrastructure/Public/StripeConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public static class StripeConfiguration

private static string clientId;

private static bool enableTelemetry = true;

private static int maxNetworkRetries;

private static IStripeClient stripeClient;
Expand Down Expand Up @@ -129,6 +131,24 @@ public static int MaxNetworkRetries
}
}

/// <summary>
/// Gets or sets the flag enabling request latency telemetry. Enabled by default.
/// </summary>
public static bool EnableTelemetry
{
get => enableTelemetry;

set
{
if (value != enableTelemetry)
{
StripeClient = null;
}

enableTelemetry = value;
}
}

/// <summary>
/// Gets or sets a custom <see cref="StripeClient"/> for sending requests to Stripe's
/// API. You can use this to use a custom message handler, set proxy parameters, etc.
Expand Down Expand Up @@ -239,7 +259,8 @@ private static StripeClient BuildDefaultStripeClient()
var httpClient = new SystemNetHttpClient(
httpClient: null,
maxNetworkRetries: MaxNetworkRetries,
appInfo: AppInfo);
appInfo: AppInfo,
enableTelemetry: EnableTelemetry);
return new StripeClient(ApiKey, ClientId, httpClient: httpClient);
}
}
Expand Down
19 changes: 16 additions & 3 deletions src/Stripe.net/Infrastructure/Public/SystemNetHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class SystemNetHttpClient : IHttpClient

private readonly AppInfo appInfo;

private readonly bool enableTelemetry;

private readonly RequestTelemetry requestTelemetry = new RequestTelemetry();

private readonly object randLock = new object();
Expand All @@ -50,10 +52,14 @@ public class SystemNetHttpClient : IHttpClient
/// Information about the "app" which this integration belongs to. This should be reserved
/// for plugins that wish to identify themselves with Stripe.
/// </param>
/// <param name="enableTelemetry">
/// Whether to enable request latency telemetry or not.
/// </param>
public SystemNetHttpClient(
System.Net.Http.HttpClient httpClient = null,
int maxNetworkRetries = 0,
AppInfo appInfo = null)
AppInfo appInfo = null,
bool enableTelemetry = true)
{
#if NET45
// With .NET Framework 4.5, it's necessary to manually enable support for TLS 1.2.
Expand All @@ -64,6 +70,7 @@ public SystemNetHttpClient(
this.httpClient = httpClient ?? BuildDefaultSystemNetHttpClient();
this.maxNetworkRetries = maxNetworkRetries;
this.appInfo = appInfo;
this.enableTelemetry = enableTelemetry;

this.stripeClientUserAgentString = this.BuildStripeClientUserAgentString();
this.userAgentString = this.BuildUserAgentString();
Expand Down Expand Up @@ -118,7 +125,10 @@ public async Task<StripeResponse> MakeRequestAsync(
HttpResponseMessage response = null;
int retry = 0;

this.requestTelemetry.MaybeAddTelemetryHeader(request.StripeHeaders);
if (this.enableTelemetry)
{
this.requestTelemetry.MaybeAddTelemetryHeader(request.StripeHeaders);
}

while (true)
{
Expand Down Expand Up @@ -165,7 +175,10 @@ public async Task<StripeResponse> MakeRequestAsync(
throw requestException;
}

this.requestTelemetry.MaybeEnqueueMetrics(response, duration);
if (this.enableTelemetry)
{
this.requestTelemetry.MaybeEnqueueMetrics(response, duration);
}

var reader = new StreamReader(
await response.Content.ReadAsStreamAsync().ConfigureAwait(false));
Expand Down
28 changes: 28 additions & 0 deletions src/StripeTests/Functional/TelemetryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,34 @@ public async Task TelemetryWorksWithConcurrentRequests()
ItExpr.IsAny<CancellationToken>());
}

[Fact]
public void NoTelemetryWhenDisabled()
{
var mockHandler = new Mock<HttpClientHandler> { CallBase = true };
var httpClient = new SystemNetHttpClient(
new System.Net.Http.HttpClient(mockHandler.Object),
enableTelemetry: false);
var stripeClient = new StripeClient("sk_test_123", httpClient: httpClient);

mockHandler.Reset();
var fakeServer = FakeServer.ForMockHandler(mockHandler);
fakeServer.Delay = TimeSpan.FromMilliseconds(20);

var service = new BalanceService(stripeClient);
service.Get();
fakeServer.Delay = TimeSpan.FromMilliseconds(40);
service.Get();
service.Get();

mockHandler.Protected()
.Verify(
"SendAsync",
Times.Exactly(3),
ItExpr.Is<HttpRequestMessage>(m =>
!m.Headers.Contains("X-Stripe-Client-Telemetry")),
ItExpr.IsAny<CancellationToken>());
}

private static bool TelemetryHeaderMatcher(
HttpHeaders headers,
Func<string, bool> requestIdMatcher,
Expand Down