private static void SetEnv(Scope scope, HttpContext context, SentryAspNetCoreOptions options) { scope.Request.Method = context.Request.Method; // Logging integration, if enabled, sets the following tag which ends up as duplicate // to Request.Url. Prefer the interface value and remove tag. var host = context.Request.Host.Host; if (context.Request.Host.Port != null) { host += $":{context.Request.Host.Port}"; } scope.Request.Url = $"{context.Request.Scheme}://{host}{context.Request.Path}"; scope.UnsetTag("RequestPath"); scope.Request.QueryString = context.Request.QueryString.ToString(); foreach (var requestHeader in context.Request.Headers) { if (!options.SendDefaultPii // Don't add headers which might contain PII && (requestHeader.Key == HeaderNames.Cookie || requestHeader.Key == HeaderNames.Authorization)) { continue; } scope.Request.Headers[requestHeader.Key] = requestHeader.Value; } // TODO: Hide these 'Env' behind some extension method as // these might be reported in a non CGI, old-school way if (options.SendDefaultPii && context.Connection.RemoteIpAddress?.ToString() is { } ipAddress) { scope.Request.Env["REMOTE_ADDR"] = ipAddress; } scope.Request.Env["SERVER_NAME"] = Environment.MachineName; scope.Request.Env["SERVER_PORT"] = context.Connection.LocalPort.ToString(); if (context.Response.Headers.TryGetValue("Server", out var server)) { scope.Request.Env["SERVER_SOFTWARE"] = server; } }
public async Task Transaction_TransactionNameProviderSetSet_TransactionNameSet() { // Arrange Transaction transaction = null; var expectedName = "My custom name"; var sentryClient = Substitute.For <ISentryClient>(); sentryClient.When(x => x.CaptureTransaction(Arg.Any <Transaction>())) .Do(callback => transaction = callback.Arg <Transaction>()); var options = new SentryAspNetCoreOptions { Dsn = DsnSamples.ValidDsnWithoutSecret, TracesSampleRate = 1 }; var hub = new Hub(options, sentryClient); var server = new TestServer(new WebHostBuilder() .UseSentry(aspNewOptions => aspNewOptions.TransactionNameProvider = _ => expectedName) .ConfigureServices(services => { services.RemoveAll(typeof(Func <IHub>)); services.AddSingleton <Func <IHub> >(() => hub); }).Configure(app => app.UseSentryTracing())); var client = server.CreateClient(); // Act try { await client.GetStringAsync("/person/13.bmp"); } // Expected error. catch (HttpRequestException ex) when(ex.Message.Contains("404")) { } // Assert transaction.Should().NotBeNull(); transaction?.Name.Should().Be($"GET {expectedName}"); }
public Fixture() { var loggingOptions = new SentryLoggingOptions { InitializeSdk = false, }; var aspnetOptions = new SentryAspNetCoreOptions(); aspnetOptions.Apply(loggingOptions); loggingOptions.InitializeSdk = false; var hub = new Hub(new SentryOptions { Dsn = DsnSamples.Valid }); hub.BindClient(Client); Hub = hub; var provider = new SentryLoggerProvider(hub, Clock, loggingOptions); _disposable = provider; SentryLogger = provider.CreateLogger(nameof(SentryLogger)); HttpContext.Features.Returns(FeatureCollection); }