|
| 1 | +using Microsoft.AspNetCore.Authorization; |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | +using Taskdeck.Application.Services; |
| 4 | + |
| 5 | +namespace Taskdeck.Api.Controllers; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Endpoints for opt-in product telemetry event recording and client configuration. |
| 9 | +/// All telemetry is disabled by default and requires explicit opt-in. |
| 10 | +/// </summary> |
| 11 | +[ApiController] |
| 12 | +[Route("api/telemetry")] |
| 13 | +[Authorize] |
| 14 | +public class TelemetryController : ControllerBase |
| 15 | +{ |
| 16 | + private readonly ITelemetryEventService _telemetryEventService; |
| 17 | + private readonly SentrySettings _sentrySettings; |
| 18 | + private readonly AnalyticsSettings _analyticsSettings; |
| 19 | + private readonly TelemetrySettings _telemetrySettings; |
| 20 | + |
| 21 | + public TelemetryController( |
| 22 | + ITelemetryEventService telemetryEventService, |
| 23 | + SentrySettings sentrySettings, |
| 24 | + AnalyticsSettings analyticsSettings, |
| 25 | + TelemetrySettings telemetrySettings) |
| 26 | + { |
| 27 | + _telemetryEventService = telemetryEventService; |
| 28 | + _sentrySettings = sentrySettings; |
| 29 | + _analyticsSettings = analyticsSettings; |
| 30 | + _telemetrySettings = telemetrySettings; |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Returns client-side telemetry configuration. The frontend uses this to |
| 35 | + /// determine which integrations are available and how to initialize them. |
| 36 | + /// DSNs and script URLs are only returned when the corresponding integration |
| 37 | + /// is enabled. No secrets or API keys are exposed. |
| 38 | + /// </summary> |
| 39 | + [HttpGet("config")] |
| 40 | + [AllowAnonymous] |
| 41 | + public IActionResult GetConfig() |
| 42 | + { |
| 43 | + return Ok(new ClientTelemetryConfigResponse |
| 44 | + { |
| 45 | + Sentry = new SentryClientConfig |
| 46 | + { |
| 47 | + Enabled = _sentrySettings.Enabled, |
| 48 | + Dsn = _sentrySettings.Enabled ? _sentrySettings.Dsn : string.Empty, |
| 49 | + Environment = _sentrySettings.Environment, |
| 50 | + TracesSampleRate = _sentrySettings.TracesSampleRate, |
| 51 | + }, |
| 52 | + Analytics = new AnalyticsClientConfig |
| 53 | + { |
| 54 | + Enabled = _analyticsSettings.Enabled, |
| 55 | + Provider = _analyticsSettings.Enabled ? _analyticsSettings.Provider : string.Empty, |
| 56 | + ScriptUrl = _analyticsSettings.Enabled ? _analyticsSettings.ScriptUrl : string.Empty, |
| 57 | + SiteId = _analyticsSettings.Enabled ? _analyticsSettings.SiteId : string.Empty, |
| 58 | + }, |
| 59 | + Telemetry = new TelemetryClientConfig |
| 60 | + { |
| 61 | + Enabled = _telemetrySettings.Enabled, |
| 62 | + }, |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Records a batch of product telemetry events. Requires authentication. |
| 68 | + /// Events are validated against the taxonomy naming convention and rejected |
| 69 | + /// if telemetry is disabled on the server. |
| 70 | + /// </summary> |
| 71 | + [HttpPost("events")] |
| 72 | + public IActionResult RecordEvents([FromBody] TelemetryBatchRequest? request) |
| 73 | + { |
| 74 | + if (!_telemetryEventService.IsEnabled) |
| 75 | + { |
| 76 | + return Ok(new { recorded = 0, message = "Telemetry is disabled on this server." }); |
| 77 | + } |
| 78 | + |
| 79 | + if (request == null || request.Events == null || request.Events.Count == 0) |
| 80 | + { |
| 81 | + return BadRequest(new { error = "No events provided." }); |
| 82 | + } |
| 83 | + |
| 84 | + var recorded = _telemetryEventService.RecordEvents(request.Events); |
| 85 | + return Ok(new { recorded }); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +public sealed class ClientTelemetryConfigResponse |
| 90 | +{ |
| 91 | + public SentryClientConfig Sentry { get; set; } = new(); |
| 92 | + public AnalyticsClientConfig Analytics { get; set; } = new(); |
| 93 | + public TelemetryClientConfig Telemetry { get; set; } = new(); |
| 94 | +} |
| 95 | + |
| 96 | +public sealed class SentryClientConfig |
| 97 | +{ |
| 98 | + public bool Enabled { get; set; } |
| 99 | + public string Dsn { get; set; } = string.Empty; |
| 100 | + public string Environment { get; set; } = string.Empty; |
| 101 | + public double TracesSampleRate { get; set; } |
| 102 | +} |
| 103 | + |
| 104 | +public sealed class AnalyticsClientConfig |
| 105 | +{ |
| 106 | + public bool Enabled { get; set; } |
| 107 | + public string Provider { get; set; } = string.Empty; |
| 108 | + public string ScriptUrl { get; set; } = string.Empty; |
| 109 | + public string SiteId { get; set; } = string.Empty; |
| 110 | +} |
| 111 | + |
| 112 | +public sealed class TelemetryClientConfig |
| 113 | +{ |
| 114 | + public bool Enabled { get; set; } |
| 115 | +} |
| 116 | + |
| 117 | +public sealed class TelemetryBatchRequest |
| 118 | +{ |
| 119 | + public List<TelemetryEvent> Events { get; set; } = new(); |
| 120 | +} |
0 commit comments