Skip to content

Commit

Permalink
Add opentelemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
ZOXEXIVO committed Dec 12, 2022
1 parent ca83e79 commit ab2f96c
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Backend/Geen.Data/Geen.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
<PackageReference Include="MongoDB.Driver" Version="2.18.0" />
<PackageReference Include="StackExchange.Redis" Version="2.6.70" />
<PackageReference Include="StackExchange.Redis" Version="2.6.80" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/Backend/Geen.Data/Registration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reflection;
using Geen.Core.Services.Interfaces;
using Geen.Data.Caches;
using Geen.Data.Services;
using Geen.Data.Settings;
using Geen.Data.Storages.Mongo;
using Geen.Data.Storages.Redis;
Expand Down
1 change: 1 addition & 0 deletions src/Backend/Geen.Data/Repositories/MentionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Geen.Core.Models.Likes;
using Geen.Data.Entities;
using Geen.Data.Extensions;
using Geen.Data.Services;
using Geen.Data.Storages.Mongo;
using Geen.Data.Utils;
using Mapster;
Expand Down
2 changes: 2 additions & 0 deletions src/Backend/Geen.Data/Services/IdentityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Geen.Data.Storages.Mongo;
using MongoDB.Driver;

namespace Geen.Data.Services;

public class IdentityService
{
private readonly MongoContext _context;
Expand Down
23 changes: 17 additions & 6 deletions src/Backend/Geen.Data/Settings/GeenSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,36 @@
{
public class GeenSettings
{
public virtual DatabaseSettings Database { get; set; }
public virtual Authentication Authentication { get; set; }
public virtual Prerender Prerender { get; set; }
public virtual DatabaseSettingsSettings Database { get; set; }
public virtual AuthenticationSettings Authentication { get; set; }
public virtual PrerenderSettings Prerender { get; set; }
public virtual TracingSettings Tracing { get; set; }
}

public class DatabaseSettings
public class DatabaseSettingsSettings
{
public string MongoUrl { get; set; }
public string RedisUrl { get; set; }
}

public class Authentication
public class AuthenticationSettings
{
public string Login { get; set; }
public string Password { get; set; }
}

public class Prerender
public class PrerenderSettings
{
public string StaticPath { get; set; }
}

public class TracingSettings
{
public JaegerSettings Jaeger { get; set; }

public class JaegerSettings
{
public string Endpoint { get; set; }
}
}
}
7 changes: 3 additions & 4 deletions src/Backend/Geen.Data/Storages/Mongo/MongoContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Reflection;
using Geen.Data.Entities.Attributes;
using Microsoft.Extensions.Logging;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Driver;
using MongoDB.Driver.Core.Configuration;
Expand All @@ -14,7 +13,7 @@ namespace Geen.Data.Storages.Mongo
{
public class MongoContext : IDisposable
{
private readonly ConcurrentDictionary<Type, string> _collectionNameCache = new ConcurrentDictionary<Type, string>();
private readonly ConcurrentDictionary<Type, string> _collectionNameCache = new();

private readonly MongoUrl _mongoUrl;
private readonly Lazy<MongoClient> _mongoClient;
Expand Down Expand Up @@ -65,12 +64,12 @@ private IMongoCollection<TEntity> GetCollection<TEntity>()

#region Logging

private static readonly JsonWriterSettings JsonWriterSettings = new JsonWriterSettings
private static readonly JsonWriterSettings JsonWriterSettings = new()
{
Indent = true
};

private readonly HashSet<string> _ignoringCommands = new HashSet<string>
private readonly HashSet<string> _ignoringCommands = new()
{
"isMaster",
"buildInfo",
Expand Down
4 changes: 3 additions & 1 deletion src/Backend/Geen.Web/Application/Registration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Geen.Web.Application
{
public static class ServiceRegistration
{
public static void RegisterInternalServices(this IServiceCollection services, IConfiguration configuration)
public static GeenSettings RegisterInternalServices(this IServiceCollection services, IConfiguration configuration)
{
var settings = services.RegisterSettings(configuration);

Expand All @@ -35,6 +35,8 @@ public static void RegisterInternalServices(this IServiceCollection services, IC
services.AddTransient<AuthenticationService>();

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

return settings;
}

private static GeenSettings RegisterSettings(this IServiceCollection services, IConfiguration configuration)
Expand Down
6 changes: 6 additions & 0 deletions src/Backend/Geen.Web/Geen.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.3.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc9.9" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9.9" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.0.0-rc9.9" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc9.9" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.1.0-beta.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.4.0" />
Expand Down
27 changes: 22 additions & 5 deletions src/Backend/Geen.Web/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.IO.Compression;
using Geen.Core.Domains.Mentions.Queries;
using Geen.Core.Domains.Replies.Queries;
using Geen.Web.Application;
using Geen.Web.Application.Prerender;
using Microsoft.AspNetCore.Builder;
Expand All @@ -10,6 +8,11 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

namespace Geen.Web
{
Expand All @@ -24,7 +27,7 @@ public Startup(IConfiguration configuration)

public void ConfigureServices(IServiceCollection services)
{
services.RegisterInternalServices(Configuration);
var settings = services.RegisterInternalServices(Configuration);

services.Configure<BrotliCompressionProviderOptions>(
options => options.Level = CompressionLevel.Fastest);
Expand All @@ -50,6 +53,21 @@ public void ConfigureServices(IServiceCollection services)
};
});

services.AddOpenTelemetryTracing(builder =>
{
builder.SetSampler(new AlwaysOnSampler())
.ConfigureResource(r => r.AddService(
serviceName: "Geen.Web",
serviceInstanceId: Environment.MachineName))
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation();

builder.AddJaegerExporter(exporter =>
{
exporter.Endpoint = new Uri(settings.Tracing.Jaeger.Endpoint);
});
});

services.AddCors(options =>
{
options.AddPolicy("AllowAnyOrigin",
Expand All @@ -60,15 +78,14 @@ public void ConfigureServices(IServiceCollection services)
});

services.AddControllers();
#if DEBUG

services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "GeenApi", Version = "v1"
});
});
#endif
}

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
Expand Down
7 changes: 6 additions & 1 deletion src/Backend/Geen.Web/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
},
"Prerender": {
"StaticPath": "E:\\test"
},
"Tracing": {
"Jaeger": {
"Endpoint": "http://localhost:14268"
}
}
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Error"
}
}
}
}
9 changes: 7 additions & 2 deletions src/Backend/Geen.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
},
"Prerender": {
"StaticPath": "/html/geen"
}
},
"Tracing": {
"Jaeger": {
"Endpoint": "http://172.17.0.1:14268"
}
}
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Error"
}
}
}
}

0 comments on commit ab2f96c

Please sign in to comment.