Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Open Telemetry #122

Merged
merged 1 commit into from
Feb 9, 2023
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
12 changes: 12 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@
<PackageVersion Include="EasyCaching.CSRedis" Version="1.8.0" />
<PackageVersion Include="FluentValidation" Version="11.4.0" />
<PackageVersion Include="HotChocolate.Abstractions" Version="12.16.0" />
<PackageVersion Include="HotChocolate.Diagnostics" Version="12.16.0" />
<PackageVersion Include="NKZSoft.FluentValidation.Options" Version="1.0.1" />
<PackageVersion Include="Npgsql.OpenTelemetry" Version="7.0.1" />
<PackageVersion Include="OpenTelemetry" Version="1.4.0-rc.3" />
<PackageVersion Include="OpenTelemetry.Api" Version="1.4.0-rc.3" />
<PackageVersion Include="OpenTelemetry.Exporter.Console" Version="1.4.0-rc.3" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.4.0-rc.3" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.4.0-rc.3" />
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9.12" />
<PackageVersion Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta.4" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc9.12" />
<PackageVersion Include="OpenTelemetry.Instrumentation.MassTransit" Version="1.0.0-beta.3" />
<PackageVersion Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.0.0-rc9.12" />
<PackageVersion Include="System.ServiceModel.Primitives" Version="4.10.0" />
<PackageVersion Include="AspNetCore.HealthChecks.UI.Client" Version="6.0.5" />
<PackageVersion Include="AspNetCore.HealthChecks.Rabbitmq" Version="6.0.2" />
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,5 @@ dotnet new nkz-template -o My.NewApp1 --EnableGraphQL false --EnableGrpc false -
[EasyCaching](https://github.com/dotnetcore/EasyCaching): EasyCaching is an open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easily!

[Helm](https://github.com/helm/helm): Helm is a tool for managing Charts. Charts are packages of pre-configured Kubernetes resources.

[OpenTelemetry .NET](https://github.com/open-telemetry/opentelemetry-dotnet): The .NET OpenTelemetry client.
11 changes: 11 additions & 0 deletions deployment/docker/docker-compose-infrastructure.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,16 @@ services:
timeout: 30s
retries: 15

jaeger:
image: jaegertracing/all-in-one:1.41
environment:
- COLLECTOR_OTLP_ENABLED=true
- LOG_LEVEL=debug
ports:
- "0.0.0.0:16686:16686"
- "0.0.0.0:4317:4317"
networks:
- dev-network

networks:
dev-network:
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace NKZSoft.Template.MessageBrokers.RabbitMq.Extensions;

using OpenTelemetry.Trace;

public static class TracerProviderBuilderExtension
{
/// <summary>
/// Enables the incoming requests automatic data collection for the MassTransit.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="configuration">The <see cref="IConfiguration"/> containing settings to be used.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddMassTransitOpenTelemetry(this TracerProviderBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
return builder.AddSource("MassTransit");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
<PackageReference Include="NKZSoft.Service.Configuration.MassTransit.RabbitMq" />
<PackageReference Include="OpenTelemetry.Api" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace NKZSoft.Template.Persistence.PostgreSQL.Extensions;

public static class TracerProviderBuilderExtension
{
/// <summary>
/// Enables the incoming requests automatic data collection for the PostgreSQL persistence level.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="configuration">The <see cref="IConfiguration"/> containing settings to be used.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddNgpSqlPersistenceOpenTelemetry(this TracerProviderBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
builder.AddEntityFrameworkCoreInstrumentation(o => o.SetDbStatementForText = true)
.AddNpgsql();
return builder;
}
}
2 changes: 2 additions & 0 deletions src/NKZSoft.Template.Persistence.PostgreSQL/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@
global using NKZSoft.Template.Common;
global using NKZSoft.Template.Domain.AggregatesModel.ToDoAggregates.Entities;
global using NKZSoft.Template.Domain.Common;
global using Npgsql;
global using OpenTelemetry.Trace;
global using Serilog;
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<PackageReference Include="NKZSoft.FluentValidation.Options" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" />
<PackageReference Include="Npgsql.OpenTelemetry" />
<PackageReference Include="OpenTelemetry.Api" />
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" />
<PackageReference Include="Scrutor" />
<PackageReference Include="Serilog" />
<PackageReference Include="Serilog.Extensions.Logging" />
Expand Down Expand Up @@ -49,7 +52,7 @@
<InternalsVisibleTo Include="NKZSoft.Template.Presentation.SignalR.Tests" />
<!--#endif-->
</ItemGroup>

<ItemGroup>
<Folder Include="Migrations" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public static IServiceCollection AddGraphQLPresentation(this IServiceCollection
.AddFiltering()
.AddSorting()
.AddMutationType<Mutation>()
.AddMutationConventions();
.AddMutationConventions()
.AddInstrumentation();

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PackageReference Include="HotChocolate.AspNetCore" />
<PackageReference Include="HotChocolate.Data" />
<PackageReference Include="HotChocolate.Data.EntityFramework" />
<PackageReference Include="HotChocolate.Diagnostics" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace NKZSoft.Template.Presentation.Grpc.Extensions;

public static class TracerProviderBuilderExtension
{
/// <summary>
/// Enables the incoming requests automatic data collection for the grpc presentation level.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="configuration">The <see cref="IConfiguration"/> containing settings to be used.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddGrpcOpenTelemetry(
this TracerProviderBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
builder.AddGrpcClientInstrumentation();
return builder;
}
}
1 change: 1 addition & 0 deletions src/NKZSoft.Template.Presentation.Grpc/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
global using NKZSoft.Template.Application.TodoItems.Models;
global using NKZSoft.Template.Application.TodoItems.Queries.Get;
global using NKZSoft.Template.Application.TodoItems.Queries.GetPage;
global using OpenTelemetry.Trace;

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

<ItemGroup>
<PackageReference Include="Google.Protobuf" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" />
<PackageReference Include="protobuf-net" />
<PackageReference Include="protobuf-net.Grpc.AspNetCore" />
<PackageReference Include="protobuf-net.Grpc.AspNetCore.Reflection" />
Expand All @@ -20,4 +21,8 @@
<PackageReference Include="NKZSoft.Service.Configuration.Grpc" />
</ItemGroup>

<ItemGroup>
<PackageVersion Update="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.0.0-rc9.11" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace NKZSoft.Template.Presentation.Rest.Extensions;

public static class TracerProviderBuilderExtension
{
/// <summary>
/// Enables the incoming requests automatic data collection for the rest presentation level.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="configuration">The <see cref="IConfiguration"/> containing settings to be used.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddRestOpenTelemetry(
this TracerProviderBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
return builder.AddHttpClientInstrumentation();
}
}
1 change: 1 addition & 0 deletions src/NKZSoft.Template.Presentation.Rest/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
global using NKZSoft.Template.Application.TodoItems.Queries.GetPage;
global using NKZSoft.Template.Common;
global using NKZSoft.Service.Configuration.Swagger;
global using OpenTelemetry.Trace;
global using Serilog;


Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<PackageReference Include="FluentValidation.AspNetCore" />
<PackageReference Include="NKZSoft.Service.Configuration.Logger" />
<PackageReference Include="NKZSoft.Service.Configuration.Swagger" />
<PackageReference Include="OpenTelemetry.Api" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" />
<PackageReference Include="OpenTelemetry" />
<PackageReference Include="OpenTelemetry.Exporter.Console" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
<PackageReference Include="Scrutor" />
</ItemGroup>

Expand Down
24 changes: 24 additions & 0 deletions src/NKZSoft.Template.Presentation.Starter/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System.Reflection;
using EFCoreSecondLevelCacheInterceptor;
using NKZSoft.Template.EFCore.Caching.Redis.Extensions;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -35,6 +39,26 @@
.AddMessageBroker(configuration)
.AddHealthChecks();

builder.Services.AddOpenTelemetry()
.ConfigureResource(builder => builder
.AddService(
serviceName: Assembly.GetExecutingAssembly().GetName().Name!,
serviceVersion: Assembly.GetExecutingAssembly().GetName().Version?.ToString(),
serviceInstanceId: Environment.MachineName))
.WithTracing(builder => builder
.AddAspNetCoreInstrumentation(options =>
{
options.EnableGrpcAspNetCoreSupport = true;
options.RecordException = true;
})
.AddRestOpenTelemetry()
.AddNgpSqlPersistenceOpenTelemetry()
.AddMassTransitOpenTelemetry()
.AddOtlpExporter()
.AddConsoleExporter()
)
.StartWithHost();

var app = builder.Build();

using (var scope = app.Services.CreateScope())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"Serilog": {
"MinimumLevel": {
"Default": "Verbose",
"Default": "Debug",
"Override": {
"System.Net.Http": "Verbose"
"System.Net.Http": "Debug"
}
},
"EnableDbLogging": true
Expand Down