Skip to content

Commit

Permalink
Cleanup to move to a common instrumentation class (#428)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Shawn Vause and github-actions[bot] authored Mar 26, 2024
1 parent f18b2c6 commit e71e184
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@
},
"editorMode": "code",
"exemplar": false,
"expr": "topk(5, sum by (author) (sort_desc(max_over_time(twitchchat_messages_by_author_message_total[$__range]))))",
"expr": "topk(5, sum by (author) (sort_desc(max_over_time(messages_received_message_total{provider=\"twitchchat\"}[$__range]))))",
"hide": false,
"instant": true,
"legendFormat": "__auto",
"range": false,
"refId": "B"
"refId": "A"
}
],
"title": "Top 5 Twitch Chatters",
Expand Down Expand Up @@ -191,7 +191,7 @@
},
"editorMode": "code",
"exemplar": false,
"expr": "topk(5, sum by (author) (sort_desc(max_over_time(mastodon_messages_by_author_message_total[$__range]))))",
"expr": "topk(5, sum by (author) (sort_desc(max_over_time(messages_received_message_total[$__range]))))",
"hide": false,
"instant": true,
"legendFormat": "__auto",
Expand Down Expand Up @@ -322,7 +322,7 @@
},
"editorMode": "code",
"exemplar": false,
"expr": "round(increase(twitchchat_messages_received_message_total[$__rate_interval]))",
"expr": "round(increase(messages_received_message_total{provider=\"twitchchat\"}[$__rate_interval]))",
"instant": true,
"legendFormat": "__auto",
"range": false,
Expand All @@ -341,7 +341,7 @@
"list": []
},
"time": {
"from": "now-6h",
"from": "now-1h",
"to": "now"
},
"timepicker": {},
Expand All @@ -350,4 +350,4 @@
"uid": "fb737849-bd1f-45e3-9b20-3d87d4cc3e20",
"version": 1,
"weekStart": ""
}
}
6 changes: 4 additions & 2 deletions src/TagzApp.Blazor/OpenTelemetryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System.Diagnostics.Metrics;
using TagzApp.Common.Telemetry;

namespace TagzApp.Blazor;

Expand Down Expand Up @@ -44,8 +45,7 @@ public static IServiceCollection AddOpenTelemetryObservability(this IServiceColl
builder
.SetResourceBuilder(resourceBuilder)
.AddMeter("mastodon-metrics")
.AddMeter("twitchchat-metrics")
.AddMeter("tagzapp-provider-metrics")
.AddProcessInstrumentation()
.AddRuntimeInstrumentation()
.AddHttpClientInstrumentation()
Expand All @@ -55,6 +55,8 @@ public static IServiceCollection AddOpenTelemetryObservability(this IServiceColl
builder.SetupMetricsExporter(configuration);
});

services.AddSingleton<ProviderInstrumentation>();

return services;
}

Expand Down
20 changes: 20 additions & 0 deletions src/TagzApp.Common/Telemetry/ProviderInstrumentation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Diagnostics.Metrics;

namespace TagzApp.Common.Telemetry;

public class ProviderInstrumentation
{
public Counter<int> MessagesReceivedCounter { get; set; }

public ProviderInstrumentation(IMeterFactory meterFactory)
{
var meter = meterFactory.Create("tagzapp-provider-metrics");

MessagesReceivedCounter = meter.CreateCounter<int>("messages-received", "message", "Counter for messages received");
}

public void AddMessage(string provider, string author) =>
MessagesReceivedCounter.Add(1,
new KeyValuePair<string, object?>(nameof(provider), provider),
new KeyValuePair<string, object?>(nameof(author), author));
}
20 changes: 0 additions & 20 deletions src/TagzApp.Providers.Mastodon/MastodonInstrumentation.cs

This file was deleted.

15 changes: 9 additions & 6 deletions src/TagzApp.Providers.Mastodon/MastodonProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.Logging;
using System.Net.Http.Json;
using System.Web;
using TagzApp.Common.Telemetry;
using TagzApp.Providers.Mastodon.Configuration;

namespace TagzApp.Providers.Mastodon;
Expand All @@ -10,12 +11,12 @@ public class MastodonProvider : ISocialMediaProvider, IHasNewestId

private readonly HttpClient _HttpClient;
private readonly ILogger _Logger;
private readonly MastodonInstrumentation _Instrumentation;
private readonly ProviderInstrumentation? _Instrumentation;
private SocialMediaStatus _Status = SocialMediaStatus.Unhealthy;
private string _StatusMessage = "Not started";

public MastodonProvider(IHttpClientFactory httpClientFactory, ILogger<MastodonProvider> logger,
MastodonConfiguration configuration, MastodonInstrumentation instrumentation)
MastodonConfiguration configuration, ProviderInstrumentation? instrumentation = null)
{
_HttpClient = httpClientFactory.CreateClient(nameof(MastodonProvider));
_Logger = logger;
Expand Down Expand Up @@ -84,12 +85,14 @@ public async Task<IEnumerable<Content>> GetContentForHashtag(Hashtag tag, DateTi

NewestId = messages!.OrderByDescending(m => m.id).First().id;

_Instrumentation.AddMessages(messages?.Length ?? 0);
foreach (var username in messages?.Select(x => x.account?.username)!)
if (_Instrumentation is not null)
{
if (!string.IsNullOrEmpty(username))
foreach (var username in messages?.Select(x => x.account?.username)!)
{
_Instrumentation.AddMessages(username);
if (!string.IsNullOrEmpty(username))
{
_Instrumentation.AddMessage("mastodon", username);
}
}
}

Expand Down
1 change: 0 additions & 1 deletion src/TagzApp.Providers.Mastodon/StartMastodon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public async Task<IServiceCollection> RegisterServices(IServiceCollection servic

_MastodonConfiguration = await ConfigureTagzAppFactory.Current.GetConfigurationById<MastodonConfiguration>(MastodonConfiguration.AppSettingsSection);

services.AddSingleton<MastodonInstrumentation>();
services.AddSingleton(_MastodonConfiguration ?? new());
services.AddHttpClient<ISocialMediaProvider, MastodonProvider, MastodonConfiguration>(_MastodonConfiguration ?? new MastodonConfiguration());
services.AddTransient<ISocialMediaProvider, MastodonProvider>();
Expand Down
1 change: 0 additions & 1 deletion src/TagzApp.Providers.TwitchChat/StartTwitchChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ 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: 0 additions & 20 deletions src/TagzApp.Providers.TwitchChat/TwitchChatInstrumentation.cs

This file was deleted.

17 changes: 10 additions & 7 deletions src/TagzApp.Providers.TwitchChat/TwitchChatProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Options;
using System.Collections.Concurrent;
using System.Web;
using TagzApp.Common.Telemetry;

namespace TagzApp.Providers.TwitchChat;

Expand All @@ -25,9 +26,9 @@ public class TwitchChatProvider : ISocialMediaProvider, IDisposable
private readonly TwitchChatConfiguration _Settings;
private readonly ILogger<TwitchChatProvider> _Logger;
private readonly TwitchProfileRepository _ProfileRepository;
private readonly TwitchChatInstrumentation _Instrumentation;
private readonly ProviderInstrumentation? _Instrumentation;

public TwitchChatProvider(ILogger<TwitchChatProvider> logger, IConfiguration configuration, HttpClient client, TwitchChatInstrumentation instrumentation)
public TwitchChatProvider(ILogger<TwitchChatProvider> logger, IConfiguration configuration, HttpClient client, ProviderInstrumentation? instrumentation = null)
{
_Settings = ConfigureTagzAppFactory.Current.GetConfigurationById<TwitchChatConfiguration>(Id).GetAwaiter().GetResult();
_Logger = logger;
Expand Down Expand Up @@ -154,13 +155,15 @@ 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 (_Instrumentation is not null)
{
if (!string.IsNullOrEmpty(username))
foreach (var username in messages?.Select(x => x.Author?.UserName)!)
{
_Instrumentation.AddMessages(username);
if (!string.IsNullOrEmpty(username))
{
_Instrumentation.AddMessage("twitchchat", username);
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using Microsoft.Extensions.Logging.Abstractions;
using System.Diagnostics.Metrics;
using TagzApp.Common.Telemetry;
using TagzApp.Providers.Mastodon;
using TagzApp.Providers.Mastodon.Configuration;
using IHttpClientFactory = System.Net.Http.IHttpClientFactory;
Expand All @@ -17,7 +18,7 @@ public class WhenFetchingMessages

private MastodonProvider _Sut;
private IHttpClientFactory _HttpClientFactory;
private MastodonInstrumentation _Instrumentation;
private ProviderInstrumentation _Instrumentation;

public WhenFetchingMessages()
{
Expand All @@ -27,7 +28,7 @@ public WhenFetchingMessages()
};

_HttpClientFactory = new StubHttpClientFactory(client);
_Instrumentation = new MastodonInstrumentation(new StubMeterFactory());
_Instrumentation = new ProviderInstrumentation(new StubMeterFactory());

_Sut = new MastodonProvider(_HttpClientFactory, NullLogger<MastodonProvider>.Instance, new MastodonConfiguration(), _Instrumentation);
}
Expand Down

0 comments on commit e71e184

Please sign in to comment.