Files
SPMega/backend/SpMega.Backend/Controllers/v1/TelemetryController.cs
T
Dmitrii f19a5d3586
Java CI / build (push) Successful in 50s
.NET+Docker CI/CD / Unit and Integration tests (push) Successful in 33s
.NET+Docker CI/CD / Push Docker image to ghcr.io (push) Failing after 30s
Add webhook support for card transactions and notifications.
Some optimizations and fixes

Signed-off-by: Dmitrii <computer@yawaflua.tech>

Took 2 hours 4 minutes
2026-07-12 06:27:28 +03:00

47 lines
1.7 KiB
C#

using System.Diagnostics;
using System.Text.Json;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SpMega.Backend.Persistent.Models.DTO;
using SpMega.Backend.Persistent.Models.Users;
namespace SpMega.Backend.Controllers.v1;
[ApiController]
[Route("api/v1/telemetry")]
public class TelemetryController : ControllerBase
{
private static readonly ActivitySource Source = new("SpMega.ModTelemetry");
[HttpPost]
[AllowAnonymous]
public IActionResult Post([FromBody] ModTelemetryBatchDto batch)
{
if (batch?.Events == null || batch.Events.Count == 0)
{
return Ok(new { received = 0 });
}
var user = HttpContext.Items["@me"] as User;
var userId = user?.Id.ToString() ?? User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value ?? "anonymous";
var sessionId = !string.IsNullOrEmpty(batch.SessionId) ? batch.SessionId : Guid.NewGuid().ToString("N");
foreach (var e in batch.Events)
{
using var activity = Source.StartActivity("mod.telemetry.event", ActivityKind.Client);
if (activity is null) continue;
activity.SetTag("user.id", userId);
activity.SetTag("session.id", sessionId);
activity.SetTag("client.version", batch.ClientVersion ?? string.Empty);
activity.SetTag("event.type", e.EventType);
activity.SetTag("event.timestamp", e.Timestamp.ToString("O"));
activity.SetTag("event.payload", JsonSerializer.Serialize(e.Payload));
activity.SetTag("batch.sent_at", batch.SentAt.ToString("O"));
}
return Ok(new { received = batch.Events.Count });
}
}