Implement telemetry system with data collection and reporting
Signed-off-by: Dmitrii <computer@yawaflua.tech> Took 20 minutes
This commit is contained in:
@@ -20,6 +20,7 @@ namespace SpMega.Backend.Controllers.v1;
|
||||
|
||||
public record GetSessionIdBody(string userName, Guid userUUID);
|
||||
public record ValidateSessionBody(string sessionId, Guid userUUID);
|
||||
|
||||
[Route("api/v1/auth")]
|
||||
[ApiController]
|
||||
public class AuthController(AppDbContext dbContext, TokenService tokenService, ILogger<AuthController> logger) : ControllerBase
|
||||
@@ -67,7 +68,7 @@ public class AuthController(AppDbContext dbContext, TokenService tokenService, I
|
||||
|
||||
var resp = await httpClient.SendAsync(request);
|
||||
if (resp.StatusCode != HttpStatusCode.OK) throw new Exception("Mojang response is not OK");
|
||||
Console.WriteLine(await resp.Content.ReadAsStringAsync());
|
||||
|
||||
var dto = await resp.Content.ReadFromJsonAsync<MojangDto>();
|
||||
if (dto == null || dto.Name != session.UserName || Guid.Parse(dto.Id) != session.UserId) throw new Exception("Session expired, or dto is not acceptable.");
|
||||
var token = tokenService.GenerateAccessToken(session.UserName, body.userUUID);
|
||||
@@ -75,6 +76,9 @@ public class AuthController(AppDbContext dbContext, TokenService tokenService, I
|
||||
if (user != null)
|
||||
{
|
||||
user.Token = token;
|
||||
user.Username = session.UserName;
|
||||
user.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
dbContext.Update(user);
|
||||
}
|
||||
else
|
||||
@@ -84,6 +88,7 @@ public class AuthController(AppDbContext dbContext, TokenService tokenService, I
|
||||
Id = body.userUUID,
|
||||
Username = session.UserName,
|
||||
Token = token,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
};
|
||||
await dbContext.AddAsync(user);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
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]
|
||||
[Authorize]
|
||||
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.Internal);
|
||||
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 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user