Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CrowdParlay.Social.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CrowdParlay.Social.Infrastr
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CrowdParlay.Social.UnitTests", "tests\CrowdParlay.Social.UnitTests\CrowdParlay.Social.UnitTests.csproj", "{8F95AF80-A278-49DB-97EB-F8DB96F083D5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CrowdParlay.Social.Aspects", "src\CrowdParlay.Social.Aspects\CrowdParlay.Social.Aspects.csproj", "{4DBC1701-9141-4AC2-80C1-CA9050FF7F0B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -52,6 +54,10 @@ Global
{8F95AF80-A278-49DB-97EB-F8DB96F083D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8F95AF80-A278-49DB-97EB-F8DB96F083D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8F95AF80-A278-49DB-97EB-F8DB96F083D5}.Release|Any CPU.Build.0 = Release|Any CPU
{4DBC1701-9141-4AC2-80C1-CA9050FF7F0B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4DBC1701-9141-4AC2-80C1-CA9050FF7F0B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4DBC1701-9141-4AC2-80C1-CA9050FF7F0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4DBC1701-9141-4AC2-80C1-CA9050FF7F0B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{1C0DE505-4898-4FB4-A5AA-C01630770F79} = {340CE817-E3FB-4753-94AA-E5BFB13E6F6A}
Expand All @@ -61,5 +67,6 @@ Global
{640B084C-59F5-448D-845A-7D6A8E1A7F99} = {340CE817-E3FB-4753-94AA-E5BFB13E6F6A}
{47776236-58AD-449A-B3BF-6ED960C3D12C} = {340CE817-E3FB-4753-94AA-E5BFB13E6F6A}
{8F95AF80-A278-49DB-97EB-F8DB96F083D5} = {00C5EC71-2662-4897-8C51-8C1355111519}
{4DBC1701-9141-4AC2-80C1-CA9050FF7F0B} = {340CE817-E3FB-4753-94AA-E5BFB13E6F6A}
EndGlobalSection
EndGlobal
6 changes: 1 addition & 5 deletions src/CrowdParlay.Social.Api/CrowdParlay.Social.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Elastic.Apm.NetCoreAll" Version="1.32.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.5" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.StackExchangeRedis" Version="9.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.1.0"/>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.5" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Common" Version="9.0.5" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.9.0-beta.1" />
<PackageReference Include="Serilog.Enrichers.OpenTelemetry" Version="1.0.1" />
<PackageReference Include="Serilog.Sinks.OpenTelemetry" Version="4.2.0" />
<PackageReference Include="SignalRSwaggerGen" Version="4.8.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.2" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace CrowdParlay.Social.Api.Extensions;

public static class ConfigureAuthenticationExtensions
partial class ServiceCollectionExtensions
{
public static IServiceCollection ConfigureAuthentication(this IServiceCollection services, IConfiguration configuration)
private static IServiceCollection ConfigureAuthentication(this IServiceCollection services, IConfiguration configuration)
{
var dataProtectionRedisConnectionString = configuration["DATA_PROTECTION_REDIS_CONNECTION_STRING"]!;
var dataProtectionRedisMultiplexer = ConnectionMultiplexer.Connect(dataProtectionRedisConnectionString);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace CrowdParlay.Social.Api.Extensions;

public static class ConfigureCorsExtensions
partial class ServiceCollectionExtensions
{
public static IServiceCollection ConfigureCors(this IServiceCollection services, IConfiguration configuration)
private static IServiceCollection ConfigureCors(this IServiceCollection services, IConfiguration configuration)
{
var corsOrigins =
configuration["CORS_ORIGINS"]?.Split(';')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace CrowdParlay.Social.Api.Extensions;

public static class ConfigureEndpointsExtensions
partial class ServiceCollectionExtensions
{
public static IServiceCollection ConfigureEndpoints(this IServiceCollection services)
internal static IServiceCollection ConfigureEndpoints(this IServiceCollection services)
{
var mvcBuilder = services.AddControllers(options =>
{
Expand Down
3 changes: 1 addition & 2 deletions src/CrowdParlay.Social.Api/Extensions/ConfigureServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace CrowdParlay.Social.Api.Extensions;

public static class ConfigureApiExtensions
public static partial class ServiceCollectionExtensions
{
public static IServiceCollection AddApi(this IServiceCollection services, IConfiguration configuration)
{
Expand All @@ -13,7 +13,6 @@ public static IServiceCollection AddApi(this IServiceCollection services, IConfi
.ConfigureAuthentication(configuration)
.ConfigureCors(configuration)
.ConfigureSignalR(configuration)
.ConfigureTelemetry(configuration)
.AddExceptionHandler<GlobalExceptionHandler>()
.AddProblemDetails()
.AddAuthorization();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace CrowdParlay.Social.Api.Extensions;

public static class ConfigureSignalRExtensions
partial class ServiceCollectionExtensions
{
public static IServiceCollection ConfigureSignalR(this IServiceCollection services, IConfiguration configuration)
private static IServiceCollection ConfigureSignalR(this IServiceCollection services, IConfiguration configuration)
{
var redisConnectionString =
configuration["REDIS_CONNECTION_STRING"]
Expand Down

This file was deleted.

13 changes: 12 additions & 1 deletion src/CrowdParlay.Social.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using CrowdParlay.Social.Infrastructure.Communication;
using Serilog;
using Serilog.Enrichers.OpenTelemetry;
using Serilog.Sinks.OpenTelemetry;

namespace CrowdParlay.Social.Api;

Expand All @@ -8,5 +11,13 @@ public class Program

private static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(builder => builder.UseStartup<Startup>())
.UseSerilog();
.UseSerilog((context, logger) => logger
.ReadFrom.Configuration(context.Configuration)
.Enrich.WithOpenTelemetrySpanId()
.Enrich.WithOpenTelemetryTraceId()
.WriteTo.OpenTelemetry(sink =>
{
sink.Endpoint = context.Configuration.GetRequiredSection("OpenTelemetry").Get<OpenTelemetrySettings>()?.OtlpEndpoint;
sink.Protocol = OtlpProtocol.HttpProtobuf;
}));
}
17 changes: 4 additions & 13 deletions src/CrowdParlay.Social.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
using CrowdParlay.Social.Api.Extensions;
using CrowdParlay.Social.Api.Hubs;
using CrowdParlay.Social.Application;
using CrowdParlay.Social.Infrastructure.Communication;
using CrowdParlay.Social.Infrastructure.Persistence;
using Elastic.Apm.NetCoreAll;
using CrowdParlay.Social.Application.Extensions;
using CrowdParlay.Social.Infrastructure.Communication.Extensions;
using CrowdParlay.Social.Infrastructure.Persistence.Extensions;
using Microsoft.AspNetCore.Http.Connections;
using Serilog;
using Serilog.Enrichers.OpenTelemetry;

namespace CrowdParlay.Social.Api;

public class Startup(IConfiguration configuration)
{
public void Configure(IApplicationBuilder app, IWebHostEnvironment environment)
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Enrich.WithOpenTelemetrySpanId()
.Enrich.WithOpenTelemetryTraceId()
.CreateLogger();

app.UseAllElasticApm(configuration);
app.UseExceptionHandler();
app.UseSerilogRequestLogging();
app.UseHealthChecks("/healthz");
Expand All @@ -39,6 +30,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment environment)
public void ConfigureServices(IServiceCollection services) => services
.AddApi(configuration)
.AddApplication()
.AddPersistence(configuration)
.AddPersistence()
.AddCommunication(configuration);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CrowdParlay.Social.Aspects\CrowdParlay.Social.Aspects.csproj" />
<ProjectReference Include="..\CrowdParlay.Social.Domain\CrowdParlay.Social.Domain.csproj" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/CrowdParlay.Social.Application/DTOs/UserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace CrowdParlay.Social.Application.DTOs;
public class UserDto
{
public required Guid Id { get; set; }
public required string Username { get; set; }
public required string DisplayName { get; set; }
public required string? Username { get; set; }
public required string? DisplayName { get; set; }
public required string? AvatarUrl { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;

namespace CrowdParlay.Social.Application;
namespace CrowdParlay.Social.Application.Extensions;

public static class ConfigureServicesExtensions
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddApplication(this IServiceCollection services) => services
.AddScoped<IDiscussionsService, DiscussionsService>()
Expand Down
34 changes: 19 additions & 15 deletions src/CrowdParlay.Social.Application/Services/CommentsService.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using System.Diagnostics;
using CrowdParlay.Social.Application.Abstractions;
using CrowdParlay.Social.Application.DTOs;
using CrowdParlay.Social.Aspects;
using CrowdParlay.Social.Domain.Abstractions;
using CrowdParlay.Social.Domain.DTOs;
using CrowdParlay.Social.Domain.Entities;
using Mapster;
using Metalama.Framework.Code;

// ReSharper disable ExplicitCallerInfoArgument

namespace CrowdParlay.Social.Application.Services;

[TraceMethods(Accessibility.Public, Accessibility.Private)]
public class CommentsService(
IUnitOfWorkFactory unitOfWorkFactory,
ICommentsRepository commentsRepository,
Expand All @@ -16,6 +21,7 @@ public class CommentsService(
: ICommentsService
{
private readonly ISubjectsService _subjectsService = new SubjectsService(commentsRepository);
private readonly ActivitySource _activitySource = new(typeof(CommentsService).FullName!);

public async Task<CommentResponse> GetByIdAsync(string commentId, Guid? viewerId)
{
Expand Down Expand Up @@ -47,18 +53,18 @@ public async Task<CommentResponse> ReplyToCommentAsync(string commentId, Guid au

private async Task<CommentResponse> CreateAsync(string subjectId, Guid authorId, string content)
{
var source = new ActivitySource(nameof(CommentsService));
using var activity = source.CreateActivity("Create comment", ActivityKind.Server);

Comment comment;
using (var unitOfWork = await unitOfWorkFactory.CreateAsync())
using (_activitySource.StartActivity("Save and retrieve entity"))
{
var commentId = await unitOfWork.CommentsRepository.CreateAsync(subjectId, authorId, content);
comment = await unitOfWork.CommentsRepository.GetByIdAsync(commentId, authorId);
await unitOfWork.CommitAsync();
using (var unitOfWork = await unitOfWorkFactory.CreateAsync())
{
var commentId = await unitOfWork.CommentsRepository.CreateAsync(subjectId, authorId, content);
comment = await unitOfWork.CommentsRepository.GetByIdAsync(commentId, authorId);
await unitOfWork.CommitAsync();
}
}

using (source.CreateActivity("Update dependant metadata", ActivityKind.Server))
using (_activitySource.StartActivity("Update ancestors"))
{
var ancestors = await commentsRepository.GetAncestorsAsync(comment.Id, null);
await commentsRepository.IncludeCommentInAncestorsMetadataAsync(ancestors, authorId);
Expand All @@ -78,11 +84,8 @@ public async Task SetReactionsAsync(string commentId, Guid authorId, ISet<string

public async Task DeleteAsync(string commentId)
{
var source = new ActivitySource(nameof(CommentsService));
using var activity = source.CreateActivity("Delete comment", ActivityKind.Server);

var comment = await commentsRepository.GetByIdAsync(commentId, null);
using (source.CreateActivity("Update dependant metadata", ActivityKind.Server))
using (_activitySource.StartActivity("Update ancestors", ActivityKind.Server))
{
var ancestors = await commentsRepository.GetAncestorsAsync(commentId, null);
await commentsRepository.ExcludeCommentFromAncestorsMetadataAsync(ancestors);
Expand All @@ -91,12 +94,13 @@ public async Task DeleteAsync(string commentId)
await discussionsRepository.ExcludeCommentFromMetadataAsync(discussionId);
}

await commentsRepository.DeleteAsync(commentId);
using (_activitySource.StartActivity("Delete entity", ActivityKind.Server))
await commentsRepository.DeleteAsync(commentId);
}

private async Task<CommentResponse> EnrichAsync(Comment comment) => (await EnrichAsync([comment])).First();
private async Task<CommentResponse> EnrichAsync([TraceIgnore] Comment comment) => (await EnrichAsync([comment])).First();

private async Task<IEnumerable<CommentResponse>> EnrichAsync(IReadOnlyList<Comment> comments)
private async Task<IEnumerable<CommentResponse>> EnrichAsync([TraceIgnore] IReadOnlyList<Comment> comments)
{
var authorIds = comments
.SelectMany(comment => comment.LastCommentsAuthorIds.Append(comment.AuthorId))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using CrowdParlay.Social.Application.Abstractions;
using CrowdParlay.Social.Application.DTOs;
using CrowdParlay.Social.Application.Exceptions;
using CrowdParlay.Social.Aspects;
using CrowdParlay.Social.Domain.Abstractions;
using CrowdParlay.Social.Domain.DTOs;
using CrowdParlay.Social.Domain.Entities;
using Mapster;
using Metalama.Framework.Code;

namespace CrowdParlay.Social.Application.Services;

[TraceMethods(Accessibility.Public, Accessibility.Private)]
public class DiscussionsService(
IUnitOfWorkFactory unitOfWorkFactory,
IDiscussionsRepository discussionsRepository,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using CrowdParlay.Social.Application.Abstractions;
using CrowdParlay.Social.Application.Exceptions;
using CrowdParlay.Social.Aspects;
using CrowdParlay.Social.Domain.Abstractions;
using CrowdParlay.Social.Domain.ValueObjects;
using Metalama.Framework.Code;

namespace CrowdParlay.Social.Application.Services;

[TraceMethods(Accessibility.Public, Accessibility.Private)]
public class SubjectsService(ISubjectsRepository subjectsRepository) : ISubjectsService
{
public async Task<ISet<string>> GetReactionsAsync(string subjectId, Guid authorId) =>
Expand Down
13 changes: 13 additions & 0 deletions src/CrowdParlay.Social.Aspects/CrowdParlay.Social.Aspects.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Metalama.Framework" Version="2025.1.9" />
</ItemGroup>

</Project>
18 changes: 18 additions & 0 deletions src/CrowdParlay.Social.Aspects/MethodTracingOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Metalama.Framework.Aspects;

namespace CrowdParlay.Social.Aspects;

[CompileTime]
internal class MethodTracingOptions
{
public string? ActivityName;
public TraceKind? TraceKind;
public readonly HashSet<string> IgnoredParameters = new();

public void Fallback(MethodTracingOptions other)
{
ActivityName ??= other.ActivityName;
TraceKind ??= other.TraceKind;
IgnoredParameters.UnionWith(other.IgnoredParameters);
}
}
Loading
Loading