Skip to content

Commit

Permalink
Added TwitchChat metrics (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
csharpfritz committed Mar 19, 2024
1 parent 011bf8f commit cc7b50c
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"description": "",
"gridPos": {
"h": 11,
"w": 2,
"x": 7,
"w": 3,
"x": 0,
"y": 0
},
"id": 3,
Expand All @@ -53,6 +53,81 @@
"pluginVersion": "10.3.1",
"type": "text"
},
{
"datasource": {
"type": "prometheus",
"uid": "prometheus"
},
"description": "Top 5 Twitch Chatters",
"fieldConfig": {
"defaults": {
"color": {
"mode": "continuous-GrYlRd"
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"unitScale": true
},
"overrides": []
},
"gridPos": {
"h": 11,
"w": 8,
"x": 3,
"y": 0
},
"id": 4,
"options": {
"displayMode": "gradient",
"maxVizHeight": 80,
"minVizHeight": 50,
"minVizWidth": 8,
"namePlacement": "top",
"orientation": "horizontal",
"reduceOptions": {
"calcs": [
"lastNotNull"
],
"fields": "",
"values": false
},
"showUnfilled": false,
"sizing": "manual",
"valueMode": "color"
},
"pluginVersion": "10.3.1",
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "prometheus"
},
"editorMode": "code",
"exemplar": false,
"expr": "topk(5, sum by (author) (sort_desc(max_over_time(twitchchat_messages_by_author_message_total[$__range]))))",
"hide": false,
"instant": true,
"legendFormat": "__auto",
"range": false,
"refId": "B"
}
],
"title": "Top 5 Twitch Chatters",
"transformations": [],
"type": "bargauge"
},
{
"datasource": {
"type": "prometheus",
Expand Down Expand Up @@ -85,7 +160,7 @@
"gridPos": {
"h": 11,
"w": 8,
"x": 9,
"x": 11,
"y": 0
},
"id": 1,
Expand Down Expand Up @@ -218,7 +293,7 @@
"gridPos": {
"h": 15,
"w": 10,
"x": 7,
"x": 0,
"y": 11
},
"id": 2,
Expand Down Expand Up @@ -247,30 +322,30 @@
},
"editorMode": "code",
"exemplar": false,
"expr": "round(increase(mastodon_messages_received_message_total[$__rate_interval]))",
"instant": false,
"expr": "round(increase(twitchchat_messages_received_message_total[$__rate_interval]))",
"instant": true,
"legendFormat": "__auto",
"range": true,
"range": false,
"refId": "A"
}
],
"title": "Posts Received",
"title": "TwitchChat Messages Received",
"transformations": [],
"type": "timeseries"
}
],
"refresh": "auto",
"refresh": "5s",
"schemaVersion": 39,
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "now-1h",
"from": "now-6h",
"to": "now"
},
"timepicker": {},
"timezone": "utc",
"timezone": "",
"title": "TagzApp Dashboard",
"uid": "fb737849-bd1f-45e3-9b20-3d87d4cc3e20",
"version": 1,
Expand Down
1 change: 1 addition & 0 deletions src/TagzApp.Blazor/OpenTelemetryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static IServiceCollection AddOpenTelemetryObservability(this IServiceColl
builder
.SetResourceBuilder(resourceBuilder)
.AddMeter("mastodon-metrics")
.AddMeter("twitchchat-metrics")
.AddProcessInstrumentation()
.AddRuntimeInstrumentation()
.AddHttpClientInstrumentation()
Expand Down
1 change: 1 addition & 0 deletions src/TagzApp.Providers.TwitchChat/StartTwitchChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public async Task<IServiceCollection> RegisterServices(IServiceCollection servic

_TwitchChatConfiguration = await ConfigureTagzAppFactory.Current.GetConfigurationById<TwitchChatConfiguration>(ConfigurationKey);

services.AddSingleton<TwitchChatInstrumentation>();
services.AddSingleton(_TwitchChatConfiguration ?? TwitchChatConfiguration.Empty);
services.AddHttpClient<ISocialMediaProvider, TwitchChatProvider, HttpClientOptions>(new());
services.AddSingleton<ISocialMediaProvider, TwitchChatProvider>();
Expand Down
20 changes: 20 additions & 0 deletions src/TagzApp.Providers.TwitchChat/TwitchChatInstrumentation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Diagnostics.Metrics;

namespace TagzApp.Providers.TwitchChat;

public class TwitchChatInstrumentation
{
private Counter<int> MessagesReceivedCounter { get; }
private Counter<int> MessagesByAuthorCounter { get; }

public TwitchChatInstrumentation(IMeterFactory meterFactory)
{
var meter = meterFactory.Create("twitchchat-metrics");

MessagesReceivedCounter = meter.CreateCounter<int>("twitchchat-messages-received", "message", "Counter for TwitchChat Messages Received");
MessagesByAuthorCounter = meter.CreateCounter<int>("twitchchat-messages-by-author", "message", "Counter for TwitchChat Messages Received by Author");
}

public void AddMessages(int count) => MessagesReceivedCounter.Add(count);
public void AddMessages(string author) => MessagesByAuthorCounter.Add(1, new KeyValuePair<string, object?>("author", author));
}
14 changes: 13 additions & 1 deletion src/TagzApp.Providers.TwitchChat/TwitchChatProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ public class TwitchChatProvider : ISocialMediaProvider, IDisposable
private readonly TwitchChatConfiguration _Settings;
private readonly ILogger<TwitchChatProvider> _Logger;
private readonly TwitchProfileRepository _ProfileRepository;
private readonly TwitchChatInstrumentation _Instrumentation;

public TwitchChatProvider(ILogger<TwitchChatProvider> logger, IConfiguration configuration, HttpClient client)
public TwitchChatProvider(ILogger<TwitchChatProvider> logger, IConfiguration configuration, HttpClient client, TwitchChatInstrumentation instrumentation)
{
_Settings = ConfigureTagzAppFactory.Current.GetConfigurationById<TwitchChatConfiguration>(Id).GetAwaiter().GetResult();
_Logger = logger;
_ProfileRepository = new TwitchProfileRepository(configuration, client);
Enabled = _Settings.Enabled;
_Instrumentation = instrumentation;

if (!string.IsNullOrWhiteSpace(_Settings.Description))
{
Expand Down Expand Up @@ -152,6 +154,16 @@ public Task<IEnumerable<Content>> GetContentForHashtag(Hashtag tag, DateTimeOffs
_Status = SocialMediaStatus.Healthy;
_StatusMessage = "OK";

_Instrumentation.AddMessages(messages?.Count() ?? 0);

foreach (var username in messages?.Select(x => x.Author?.UserName)!)
{
if (!string.IsNullOrEmpty(username))
{
_Instrumentation.AddMessages(username);
}
}

messages.ForEach(m => m.HashtagSought = tag.Text);

return Task.FromResult(messages.AsEnumerable());
Expand Down

0 comments on commit cc7b50c

Please sign in to comment.