| | | 1 | | using System.ComponentModel.DataAnnotations; |
| | | 2 | | using System.Security.Claims; |
| | | 3 | | using Microsoft.AspNetCore.Authorization; |
| | | 4 | | using Microsoft.AspNetCore.Mvc; |
| | | 5 | | using Microsoft.AspNetCore.RateLimiting; |
| | | 6 | | using Microsoft.Extensions.Options; |
| | | 7 | | using OzonOrdersWeb.Build; |
| | | 8 | | |
| | | 9 | | namespace OzonOrdersWeb.Observability; |
| | | 10 | | |
| | | 11 | | [ApiController] |
| | | 12 | | [AllowAnonymous] |
| | | 13 | | [IgnoreAntiforgeryToken] |
| | | 14 | | [EnableRateLimiting("client-diagnostics")] |
| | | 15 | | [Route("api/diagnostics/client-event")] |
| | 35 | 16 | | public sealed class ClientTelemetryController( |
| | 35 | 17 | | ILogger<ClientTelemetryController> logger, |
| | 35 | 18 | | SensitiveDataRedactor redactor, |
| | 35 | 19 | | IOptions<ObservabilityOptions> options) : ControllerBase |
| | | 20 | | { |
| | 35 | 21 | | private readonly ObservabilityOptions _options = options.Value; |
| | | 22 | | |
| | | 23 | | [HttpGet] |
| | 1 | 24 | | public IActionResult Get() => NoContent(); |
| | | 25 | | |
| | | 26 | | [HttpPost] |
| | | 27 | | public IActionResult Post([FromBody] ClientTelemetryEvent input) |
| | | 28 | | { |
| | 34 | 29 | | var origin = Request.Headers.Origin.ToString(); |
| | 34 | 30 | | if (!Uri.TryCreate(origin, UriKind.Absolute, out var originUri) |
| | 34 | 31 | | || !string.Equals(originUri.Authority, Request.Host.Value, StringComparison.OrdinalIgnoreCase)) |
| | | 32 | | { |
| | | 33 | | // Не даём внешнему источнику превращать endpoint в генератор GitHub Issues. |
| | 0 | 34 | | return NoContent(); |
| | | 35 | | } |
| | | 36 | | |
| | 34 | 37 | | if (string.Equals(input.Kind, "ResourceLoadError", StringComparison.OrdinalIgnoreCase) |
| | 34 | 38 | | && (input.Message.Contains("IMG", StringComparison.OrdinalIgnoreCase) |
| | 34 | 39 | | || input.Message.Contains("VIDEO", StringComparison.OrdinalIgnoreCase) |
| | 34 | 40 | | || input.Message.Contains("AUDIO", StringComparison.OrdinalIgnoreCase))) |
| | | 41 | | { |
| | 1 | 42 | | return NoContent(); |
| | | 43 | | } |
| | | 44 | | |
| | 33 | 45 | | var page = redactor.Redact(input.PageUrl, _options.MaxPayloadCharacters); |
| | 33 | 46 | | var target = redactor.Redact(input.TargetUrl, _options.MaxPayloadCharacters); |
| | 33 | 47 | | var message = redactor.Redact(input.Message, _options.MaxPayloadCharacters); |
| | 33 | 48 | | var stack = redactor.Redact(input.Stack, _options.MaxPayloadCharacters); |
| | | 49 | | |
| | 33 | 50 | | using var scope = logger.BeginScope(new Dictionary<string, object?> |
| | 33 | 51 | | { |
| | 33 | 52 | | ["EventKind"] = string.IsNullOrWhiteSpace(input.Kind) ? "FrontendError" : input.Kind, |
| | 33 | 53 | | ["EventCategory"] = "frontend", |
| | 33 | 54 | | ["PageUrl"] = page, |
| | 33 | 55 | | ["RequestUri"] = target, |
| | 33 | 56 | | ["StatusCode"] = input.StatusCode, |
| | 33 | 57 | | ["ElapsedMilliseconds"] = input.ElapsedMilliseconds, |
| | 33 | 58 | | ["FrontendStack"] = stack, |
| | 33 | 59 | | ["Browser"] = redactor.Redact(Request.Headers.UserAgent.ToString(), 1_024), |
| | 33 | 60 | | ["FrontendVersion"] = redactor.Redact(input.FrontendVersion, 256), |
| | 33 | 61 | | ["ApplicationVersion"] = AppBuildInfo.CommitHash, |
| | 33 | 62 | | ["ApplicationBuildDate"] = AppBuildInfo.CommitDate, |
| | 33 | 63 | | ["UserAction"] = redactor.Redact(input.UserAction, 1_024), |
| | 33 | 64 | | ["TraceId"] = string.IsNullOrWhiteSpace(input.TraceId) ? HttpContext.TraceIdentifier : input.TraceId, |
| | 33 | 65 | | ["UserId"] = User.FindFirstValue(ClaimTypes.NameIdentifier), |
| | 33 | 66 | | ["RemoteIp"] = HttpContext.Connection.RemoteIpAddress?.ToString() |
| | 33 | 67 | | }); |
| | | 68 | | |
| | 33 | 69 | | if (FrontendOutageNoiseClassifier.IsServerOutageNoise(input)) |
| | | 70 | | { |
| | | 71 | | // Недоступность самого Studio2 (деплой, 502 от Nginx, обрыв соединения) дефектом |
| | | 72 | | // фронтенда не является: в Seq запись остаётся, в errors-*.log — нет, и наблюдатель |
| | | 73 | | // логов не заводит по ней issue на каждый упавший запрос. |
| | 27 | 74 | | logger.LogInformation("Недоступность сервера в клиенте {Kind} на {Page}: {Message}", input.Kind, page, messa |
| | 27 | 75 | | return Accepted(); |
| | | 76 | | } |
| | | 77 | | |
| | 6 | 78 | | logger.LogWarning("Ошибка frontend {Kind} на {Page}: {Message}", input.Kind, page, message); |
| | 6 | 79 | | return Accepted(); |
| | 33 | 80 | | } |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | public sealed class ClientTelemetryEvent |
| | | 84 | | { |
| | | 85 | | [Required, StringLength(100)] |
| | | 86 | | public string Kind { get; init; } = "FrontendError"; |
| | | 87 | | |
| | | 88 | | [Required, StringLength(16_384)] |
| | | 89 | | public string Message { get; init; } = string.Empty; |
| | | 90 | | |
| | | 91 | | [StringLength(16_384)] |
| | | 92 | | public string? Stack { get; init; } |
| | | 93 | | |
| | | 94 | | [StringLength(4_096)] |
| | | 95 | | public string? PageUrl { get; init; } |
| | | 96 | | |
| | | 97 | | [StringLength(4_096)] |
| | | 98 | | public string? TargetUrl { get; init; } |
| | | 99 | | |
| | | 100 | | [StringLength(256)] |
| | | 101 | | public string? TraceId { get; init; } |
| | | 102 | | |
| | | 103 | | public int? StatusCode { get; init; } |
| | | 104 | | public long? ElapsedMilliseconds { get; init; } |
| | | 105 | | |
| | | 106 | | [StringLength(256)] |
| | | 107 | | public string? FrontendVersion { get; init; } |
| | | 108 | | |
| | | 109 | | [StringLength(1_024)] |
| | | 110 | | public string? UserAction { get; init; } |
| | | 111 | | } |