From f5efe1c5428b039eeef9b2652a4d956268bfc8f1 Mon Sep 17 00:00:00 2001 From: Bruno Garcia Date: Wed, 13 Dec 2023 09:45:42 -0500 Subject: [PATCH] parse url once, during init --- src/Sentry/Http/SpotlightHttpTransport.cs | 11 ++++------- src/Sentry/Internal/SdkComposer.cs | 7 +++++-- .../Internals/Http/SpotlightTransportTests.cs | 1 + 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Sentry/Http/SpotlightHttpTransport.cs b/src/Sentry/Http/SpotlightHttpTransport.cs index ad06ddf811..097847531f 100644 --- a/src/Sentry/Http/SpotlightHttpTransport.cs +++ b/src/Sentry/Http/SpotlightHttpTransport.cs @@ -10,27 +10,24 @@ internal class SpotlightHttpTransport : HttpTransport private readonly ITransport _inner; private readonly SentryOptions _options; private readonly HttpClient _httpClient; + private readonly Uri _spotlightUrl; private readonly ISystemClock _clock; - public SpotlightHttpTransport(ITransport inner, SentryOptions options, HttpClient httpClient, ISystemClock clock) + public SpotlightHttpTransport(ITransport inner, SentryOptions options, HttpClient httpClient, Uri spotlightUrl, ISystemClock clock) : base(options, httpClient) { _options = options; _httpClient = httpClient; + _spotlightUrl = spotlightUrl; _inner = inner; _clock = clock; } protected internal override HttpRequestMessage CreateRequest(Envelope envelope) { - if (!Uri.TryCreate(_options.SpotlightUrl, UriKind.Absolute, out var spotlightUrl)) - { - throw new InvalidOperationException("Invalid option for SpotlightUrl: " + _options.SpotlightUrl); - } - return new HttpRequestMessage { - RequestUri = spotlightUrl, + RequestUri = _spotlightUrl, Method = HttpMethod.Post, Content = new EnvelopeHttpContent(envelope, _options.DiagnosticLogger, _clock) { Headers = { ContentType = MediaTypeHeaderValue.Parse("application/x-sentry-envelope") } } diff --git a/src/Sentry/Internal/SdkComposer.cs b/src/Sentry/Internal/SdkComposer.cs index 9b6c7c1921..66d158d3f5 100644 --- a/src/Sentry/Internal/SdkComposer.cs +++ b/src/Sentry/Internal/SdkComposer.cs @@ -46,8 +46,11 @@ You can set a different environment via SENTRY_ENVIRONMENT env var or programati { _options.LogInfo("Connecting to Spotlight at {0}", _options.SpotlightUrl); } - - transport = new SpotlightHttpTransport(transport, _options, _options.GetHttpClient(), SystemClock.Clock); + if (!Uri.TryCreate(_options.SpotlightUrl, UriKind.Absolute, out var spotlightUrl)) + { + throw new InvalidOperationException("Invalid option for SpotlightUrl: " + _options.SpotlightUrl); + } + transport = new SpotlightHttpTransport(transport, _options, _options.GetHttpClient(), spotlightUrl, SystemClock.Clock); } // Always persist the transport on the options, so other places can pick it up where necessary. diff --git a/test/Sentry.Tests/Internals/Http/SpotlightTransportTests.cs b/test/Sentry.Tests/Internals/Http/SpotlightTransportTests.cs index 722caef016..0669280b4b 100644 --- a/test/Sentry.Tests/Internals/Http/SpotlightTransportTests.cs +++ b/test/Sentry.Tests/Internals/Http/SpotlightTransportTests.cs @@ -28,6 +28,7 @@ public async Task SendEnvelopeAsync_SpotlightRequestFailed_InnerTransportFailure DiagnosticLogger = logger }, new HttpClient(httpHandler), + new Uri("http://localhost:8969/stream"), Substitute.For()); var envelope = Envelope.FromEvent(new SentryEvent());