Skip to content

Commit fed09f1

Browse files
committed
Removed OpenTracing code
Resolves #1
1 parent 2935af4 commit fed09f1

10 files changed

Lines changed: 32 additions & 134 deletions

File tree

Nefarius.DSharpPlus.CommandsNext.Extensions.Hosting/DiscordServiceCollectionExtensions.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Nefarius.DSharpPlus.CommandsNext.Extensions.Hosting.Util;
88
using Nefarius.DSharpPlus.Extensions.Hosting;
99
using Nefarius.DSharpPlus.Extensions.Hosting.Util;
10-
using OpenTracing;
1110

1211
namespace Nefarius.DSharpPlus.CommandsNext.Extensions.Hosting
1312
{
@@ -56,12 +55,6 @@ public static IServiceCollection AddDiscordCommandsNext(
5655

5756
ext.CommandExecuted += async delegate(CommandsNextExtension sender, CommandExecutionEventArgs args)
5857
{
59-
using var workScope = provider.GetRequiredService<ITracer>()
60-
.BuildSpan(nameof(ext.CommandExecuted))
61-
.IgnoreActiveSpan()
62-
.StartActive(true);
63-
workScope.Span.SetTag("Command.Name", args.Command.Name);
64-
6558
using var scope = provider.CreateScope();
6659

6760
foreach (var eventsSubscriber in scope.GetDiscordCommandsNextEventsSubscriber())
@@ -70,12 +63,6 @@ public static IServiceCollection AddDiscordCommandsNext(
7063

7164
ext.CommandErrored += async delegate(CommandsNextExtension sender, CommandErrorEventArgs args)
7265
{
73-
using var workScope = provider.GetRequiredService<ITracer>()
74-
.BuildSpan(nameof(ext.CommandErrored))
75-
.IgnoreActiveSpan()
76-
.StartActive(true);
77-
workScope.Span.SetTag("Command.Name", args.Command.Name);
78-
7966
using var scope = provider.CreateScope();
8067

8168
foreach (var eventsSubscriber in scope.GetDiscordCommandsNextEventsSubscriber())

Nefarius.DSharpPlus.Extensions.Hosting/DiscordHostedService.cs

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,42 @@
33
using System.Threading.Tasks;
44
using Microsoft.Extensions.Hosting;
55
using Microsoft.Extensions.Logging;
6-
using OpenTracing;
76

87
namespace Nefarius.DSharpPlus.Extensions.Hosting
98
{
10-
/// <summary>
11-
/// Brings a <see cref="IDiscordClientService" /> online.
12-
/// </summary>
13-
[SuppressMessage("ReSharper", "UnusedMember.Global")]
14-
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")]
15-
public class DiscordHostedService : IHostedService
16-
{
17-
private readonly IDiscordClientService _discordClient;
9+
/// <summary>
10+
/// Brings a <see cref="IDiscordClientService" /> online.
11+
/// </summary>
12+
[SuppressMessage("ReSharper", "UnusedMember.Global")]
13+
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")]
14+
public class DiscordHostedService : IHostedService
15+
{
16+
private readonly IDiscordClientService _discordClient;
1817

19-
private readonly ILogger<DiscordHostedService> _logger;
18+
private readonly ILogger<DiscordHostedService> _logger;
2019

21-
private readonly ITracer _tracer;
20+
public DiscordHostedService(
21+
IDiscordClientService discordClient,
22+
ILogger<DiscordHostedService> logger)
23+
{
24+
_discordClient = discordClient;
25+
_logger = logger;
26+
}
2227

23-
public DiscordHostedService(
24-
IDiscordClientService discordClient,
25-
ITracer tracer,
26-
ILogger<DiscordHostedService> logger)
27-
{
28-
_discordClient = discordClient;
29-
_tracer = tracer;
30-
_logger = logger;
31-
}
28+
/// <inheritdoc />
29+
public async Task StartAsync(CancellationToken cancellationToken)
30+
{
31+
((DiscordService)_discordClient).Initialize();
3232

33-
/// <inheritdoc />
34-
public async Task StartAsync(CancellationToken cancellationToken)
35-
{
36-
((DiscordService) _discordClient).Initialize();
33+
_logger.LogInformation("Connecting to Discord API...");
34+
await _discordClient.Client.ConnectAsync();
35+
_logger.LogInformation("Connected");
36+
}
3737

38-
using (_tracer.BuildSpan(nameof(_discordClient.Client.ConnectAsync)).StartActive(true))
39-
{
40-
_logger.LogInformation("Connecting to Discord API...");
41-
await _discordClient.Client.ConnectAsync();
42-
_logger.LogInformation("Connected");
43-
}
44-
}
45-
46-
/// <inheritdoc />
47-
public async Task StopAsync(CancellationToken cancellationToken)
48-
{
49-
await _discordClient.Client.DisconnectAsync();
50-
}
51-
}
38+
/// <inheritdoc />
39+
public async Task StopAsync(CancellationToken cancellationToken)
40+
{
41+
await _discordClient.Client.DisconnectAsync();
42+
}
43+
}
5244
}

Nefarius.DSharpPlus.Extensions.Hosting/DiscordService.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.Extensions.Logging;
77
using Microsoft.Extensions.Options;
8-
using OpenTracing;
98

109
namespace Nefarius.DSharpPlus.Extensions.Hosting
1110
{
@@ -35,19 +34,15 @@ public partial class DiscordService : IDiscordClientService
3534

3635
private readonly IServiceProvider _serviceProvider;
3736

38-
private readonly ITracer _tracer;
39-
4037
public DiscordService(
4138
IServiceProvider serviceProvider,
4239
ILoggerFactory logFactory,
4340
ILogger<DiscordService> logger,
44-
ITracer tracer,
4541
IOptions<DiscordConfiguration> discordOptions
4642
)
4743
{
4844
_serviceProvider = serviceProvider;
4945
_logger = logger;
50-
_tracer = tracer;
5146
_discordOptions = discordOptions;
5247
_logFactory = logFactory;
5348
}

Nefarius.DSharpPlus.Extensions.Hosting/DiscordServiceCollectionExtensions.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
using DSharpPlus;
44
using Microsoft.Extensions.DependencyInjection;
55
using Microsoft.Extensions.DependencyInjection.Extensions;
6-
using OpenTracing;
7-
using OpenTracing.Mock;
86

97
namespace Nefarius.DSharpPlus.Extensions.Hosting
108
{
@@ -34,8 +32,6 @@ public static IServiceCollection AddDiscord(
3432
{
3533
services.Configure(configure);
3634

37-
services.TryAddSingleton<ITracer>(provider => new MockTracer());
38-
3935
services.TryAddSingleton<IDiscordClientService, DiscordService>();
4036

4137
if (!autoRegisterSubscribers)

Nefarius.DSharpPlus.Extensions.Hosting/Generators/DiscordServiceEventsHookGenerator.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public void Execute(GeneratorExecutionContext context)
3030
using Microsoft.Extensions.Options;
3131
using Nefarius.DSharpPlus.Extensions.Hosting.Util;
3232
using Nefarius.DSharpPlus.Extensions.Hosting.Events;
33-
using OpenTracing;
3433
3534
namespace Nefarius.DSharpPlus.Extensions.Hosting
3635
{
@@ -55,11 +54,6 @@ internal void HookEvents()
5554
sourceBuilder.Append($@"
5655
Client.{name} += async delegate ({senderType} sender, {argsType} args)
5756
{{
58-
using var workScope = _tracer
59-
.BuildSpan(nameof(Client.{name}))
60-
.IgnoreActiveSpan()
61-
.StartActive(true);
62-
6357
using var scope = _serviceProvider.CreateScope();
6458
6559
var subscribers = scope.ServiceProvider

Nefarius.DSharpPlus.Extensions.Hosting/Generators/DiscordServiceIntentsBuilderGenerator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public void Execute(GeneratorExecutionContext context)
7070
using Microsoft.Extensions.Options;
7171
using Nefarius.DSharpPlus.Extensions.Hosting.Util;
7272
using Nefarius.DSharpPlus.Extensions.Hosting.Events;
73-
using OpenTracing;
7473
7574
namespace Nefarius.DSharpPlus.Extensions.Hosting
7675
{

Nefarius.DSharpPlus.Extensions.Hosting/Nefarius.DSharpPlus.Extensions.Hosting.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
3131
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
3232
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
33-
<PackageReference Include="OpenTracing" Version="0.12.1" />
3433
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
3534
<PrivateAssets>all</PrivateAssets>
3635
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

Nefarius.DSharpPlus.SlashCommands.Extensions.Hosting/DiscordServiceCollectionExtensions.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Nefarius.DSharpPlus.SlashCommands.Extensions.Hosting.Attributes;
99
using Nefarius.DSharpPlus.SlashCommands.Extensions.Hosting.Events;
1010
using Nefarius.DSharpPlus.SlashCommands.Extensions.Hosting.Util;
11-
using OpenTracing;
1211

1312
namespace Nefarius.DSharpPlus.SlashCommands.Extensions.Hosting
1413
{
@@ -57,12 +56,6 @@ public static IServiceCollection AddDiscordSlashCommands(
5756

5857
ext.ContextMenuErrored += async delegate(SlashCommandsExtension sender, ContextMenuErrorEventArgs args)
5958
{
60-
using var workScope = provider.GetRequiredService<ITracer>()
61-
.BuildSpan(nameof(ext.ContextMenuErrored))
62-
.IgnoreActiveSpan()
63-
.StartActive(true);
64-
workScope.Span.SetTag("Context.CommandName", args.Context.CommandName);
65-
6659
using var scope = provider.CreateScope();
6760

6861
foreach (var eventsSubscriber in scope.GetDiscordSlashCommandsEventsSubscriber())
@@ -72,12 +65,6 @@ public static IServiceCollection AddDiscordSlashCommands(
7265
ext.ContextMenuExecuted +=
7366
async delegate(SlashCommandsExtension sender, ContextMenuExecutedEventArgs args)
7467
{
75-
using var workScope = provider.GetRequiredService<ITracer>()
76-
.BuildSpan(nameof(ext.ContextMenuExecuted))
77-
.IgnoreActiveSpan()
78-
.StartActive(true);
79-
workScope.Span.SetTag("Context.CommandName", args.Context.CommandName);
80-
8168
using var scope = provider.CreateScope();
8269

8370
foreach (var eventsSubscriber in scope.GetDiscordSlashCommandsEventsSubscriber())
@@ -87,12 +74,6 @@ async delegate(SlashCommandsExtension sender, ContextMenuExecutedEventArgs args)
8774
ext.SlashCommandErrored +=
8875
async delegate(SlashCommandsExtension sender, SlashCommandErrorEventArgs args)
8976
{
90-
using var workScope = provider.GetRequiredService<ITracer>()
91-
.BuildSpan(nameof(ext.SlashCommandErrored))
92-
.IgnoreActiveSpan()
93-
.StartActive(true);
94-
workScope.Span.SetTag("Context.CommandName", args.Context.CommandName);
95-
9677
using var scope = provider.CreateScope();
9778

9879
foreach (var eventsSubscriber in scope.GetDiscordSlashCommandsEventsSubscriber())
@@ -102,12 +83,6 @@ async delegate(SlashCommandsExtension sender, SlashCommandErrorEventArgs args)
10283
ext.SlashCommandExecuted +=
10384
async delegate(SlashCommandsExtension sender, SlashCommandExecutedEventArgs args)
10485
{
105-
using var workScope = provider.GetRequiredService<ITracer>()
106-
.BuildSpan(nameof(ext.SlashCommandExecuted))
107-
.IgnoreActiveSpan()
108-
.StartActive(true);
109-
workScope.Span.SetTag("Context.CommandName", args.Context.CommandName);
110-
11186
using var scope = provider.CreateScope();
11287

11388
foreach (var eventsSubscriber in scope.GetDiscordSlashCommandsEventsSubscriber())

README.md

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -95,36 +95,6 @@ services.AddDiscordHostedService();
9595

9696
That's pretty much it! When you launch your worker with a valid bot token you should see your bot come online in an instant, congratulations! ✨
9797

98-
### OpenTracing (Optional)
99-
100-
You probably wonder what's the deal with the tracing dependency. I've taken liberty to implement [OpenTracing](https://github.com/opentracing/opentracing-csharp) within all event subscribers, so if your bot struggles with performance, you can easily analyse it with the addition of e.g. [Jaeger Tracing](https://github.com/jaegertracing/jaeger-client-csharp). If you don't know what that means or don't care about tracing at all, just skip this section. To utilise tracing, you can use this snippet to get you up and running (although I highly recommend you check out the Jaeger docs beforehand anyway):
101-
102-
```csharp
103-
// Adds the Jaeger Tracer.
104-
services.AddSingleton<ITracer>(serviceProvider =>
105-
{
106-
var serviceName = "MyBot";
107-
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
108-
109-
// This is necessary to pick the correct sender, otherwise a NoopSender is used!
110-
Configuration.SenderConfiguration.DefaultSenderResolver = new SenderResolver(loggerFactory)
111-
.RegisterSenderFactory<ThriftSenderFactory>();
112-
113-
// This will log to a default localhost installation of Jaeger.
114-
var tracer = new Tracer.Builder(serviceName)
115-
.WithLoggerFactory(loggerFactory)
116-
.WithSampler(new ConstSampler(true))
117-
.Build();
118-
119-
// Allows code that can't use DI to also access the tracer.
120-
GlobalTracer.Register(tracer);
121-
122-
return tracer;
123-
});
124-
```
125-
126-
Make sure you call this **before** `AddDiscord` or the Mock Tracer will be used by default which will not collect or publish any metrics.
127-
12898
### Handling Discord Events
12999

130100
Now to the actual convenience feature of this library! Creating one (or more) class(es) that handle events, like when a guild came online or a message got created. Let's wire one up that gets general guild and member change events:
@@ -144,17 +114,14 @@ internal class BotModuleForGuildAndMemberEvents :
144114
{
145115
private readonly ILogger<BotModuleForGuildAndMemberEvents> _logger;
146116

147-
private readonly ITracer _tracer;
148-
149117
/// <summary>
150118
/// Optional constructor for Dependency Injection.
151119
/// Parameters get populated automatically with your services.
152120
/// </summary>
153121
/// <param name="logger">The logger service instance.</param>
154122
/// <param name="tracer">The tracer service instance.</param>
155123
public BotModuleForGuildAndMemberEvents(
156-
ILogger<BotModuleForGuildAndMemberEvents> logger,
157-
ITracer tracer
124+
ILogger<BotModuleForGuildAndMemberEvents> logger
158125
)
159126
{
160127
//
@@ -164,7 +131,6 @@ internal class BotModuleForGuildAndMemberEvents :
164131
// You can inject scoped services like database contexts as well!
165132
//
166133
_logger = logger;
167-
_tracer = tracer;
168134
}
169135

170136
public Task DiscordOnGuildAvailable(DiscordClient sender, GuildCreateEventArgs args)

WorkerExample/SubscriberExample.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using DSharpPlus.EventArgs;
55
using Microsoft.Extensions.Logging;
66
using Nefarius.DSharpPlus.Extensions.Hosting.Events;
7-
using OpenTracing;
87

98
namespace WorkerExample
109
{
@@ -19,16 +18,13 @@ internal class BotModuleForGuildAndMemberEvents :
1918
{
2019
private readonly ILogger<BotModuleForGuildAndMemberEvents> _logger;
2120

22-
private readonly ITracer _tracer;
23-
2421
/// <summary>
2522
/// Optional constructor for Dependency Injection. parameters get populated automatically with you services.
2623
/// </summary>
2724
/// <param name="logger">The logger service instance.</param>
2825
/// <param name="tracer">The tracer service instance.</param>
2926
public BotModuleForGuildAndMemberEvents(
30-
ILogger<BotModuleForGuildAndMemberEvents> logger,
31-
ITracer tracer
27+
ILogger<BotModuleForGuildAndMemberEvents> logger
3228
)
3329
{
3430
//
@@ -38,7 +34,6 @@ ITracer tracer
3834
// You can inject scoped services like database contexts as well!
3935
//
4036
_logger = logger;
41-
_tracer = tracer;
4237
}
4338

4439
public Task DiscordOnGuildAvailable(DiscordClient sender, GuildCreateEventArgs args)

0 commit comments

Comments
 (0)