Skip to content

Commit

Permalink
parse url once, during init
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-garcia committed Dec 13, 2023
1 parent e072400 commit f5efe1c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
11 changes: 4 additions & 7 deletions src/Sentry/Http/SpotlightHttpTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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") } }
Expand Down
7 changes: 5 additions & 2 deletions src/Sentry/Internal/SdkComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public async Task SendEnvelopeAsync_SpotlightRequestFailed_InnerTransportFailure
DiagnosticLogger = logger
},
new HttpClient(httpHandler),
new Uri("http://localhost:8969/stream"),
Substitute.For<ISystemClock>());

var envelope = Envelope.FromEvent(new SentryEvent());
Expand Down

0 comments on commit f5efe1c

Please sign in to comment.