Skip to content

Commit

Permalink
Merge pull request #40 from spacecowboy-app/143/add-appversion-metric/2
Browse files Browse the repository at this point in the history
Add labels to the session counter metric
  • Loading branch information
rolfmichelsen committed Aug 20, 2023
2 parents 8551fe6 + c164cc7 commit 2a08d72
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions service/src/Controllers/SessionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ namespace Spacecowboy.Service.Controllers
[ApiController]
public class SessionController : ControllerBase
{
private static readonly Counter sessionsCreated = Metrics.CreateCounter("spacecowboy_sessions_total", "Accumulated number of session creatied");
private static readonly Counter sessionsCreated = Metrics.CreateCounter("spacecowboy_sessions_total", "Accumulated number of session creatied",
new CounterConfiguration
{
LabelNames = new[] { "client_name", "client_version" }
});
private static readonly Gauge sessionsCurrent = Metrics.CreateGauge("spacecowboy_sessions_current", "Number of active sessions");
private static readonly Counter participantsTotal = Metrics.CreateCounter("spacecowboy_participants_total", "Accumulated number of participants across all sessions created",
new CounterConfiguration
Expand Down Expand Up @@ -218,7 +222,8 @@ public async Task<ActionResult<SessionResponse>> CreateSession(string sessionId)
{
await repository.AddSessionAsync(new Session(sessionId));
log.LogInformation("Created session {SessionId}", sessionId);
sessionsCreated.Inc();
var clientInfo = new ClientInfo(HttpContext?.Request?.Headers?.UserAgent);
sessionsCreated.WithLabels(clientInfo.Name, clientInfo.Version).Inc();
sessionsCurrent.Inc();
return Created("", new SessionResponse(await repository.GetSessionAsync(sessionId)));
}
Expand Down Expand Up @@ -604,4 +609,30 @@ public async Task<ActionResult> CleanupSessions()
private ActionResult VotingCompleted(string sessionId, Guid participantId, Guid cardId) =>
Conflict(new ConflictErrorDetails("Vote rejected because voting is finished") { SessionId = sessionId, ParticipantId = participantId, CardId = cardId });
}


public struct ClientInfo
{
public string Name;
public string Version;

public ClientInfo(string userAgent)
{
if (userAgent == null) {
Name = "";
Version = "";
}
else {
var separatorPos = userAgent.LastIndexOf('/');
if (separatorPos < 1) {
Name = userAgent;
Version = "";
}
else {
Name = userAgent.Substring(0, separatorPos);
Version = userAgent.Substring(separatorPos + 1);
}
}
}
}
}

0 comments on commit 2a08d72

Please sign in to comment.