Skip to content

Commit

Permalink
Opentelemetry integration (#249)
Browse files Browse the repository at this point in the history
* Opentelemetry draft

* add Metrics

* contract metrics

* enable metrics

* fix

* Working opentelemetry tracing

* Renames

* Tracing updates

* Fix sample

* Updates

* removed comment

* removed recordException option

* Fix appsettings secrets

* Fixes

* Removed comment

* Versions

Co-authored-by: Lucian Ghinet <[email protected]>
  • Loading branch information
fraliv13 and lghinet authored Dec 16, 2022
1 parent 4830fed commit a67c3dd
Show file tree
Hide file tree
Showing 54 changed files with 1,367 additions and 993 deletions.
4 changes: 0 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ root = true
# Use 4 spaces as indentation
[*]
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.cs]
# IDE0073: File header
dotnet_diagnostic.IDE0073.severity = warning
file_header_template = Copyright (c) TotalSoft.\nThis source code is licensed under the MIT license.

[{*.csproj, *.fsproj, *.props, *.xml}]
indent_size = 2
4 changes: 2 additions & 2 deletions NBB.sln
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NBB.EventStore.InMemory.Tes
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NBB.Application.DataContracts.Schema", "src\Application\NBB.Application.DataContracts.Schema\NBB.Application.DataContracts.Schema.csproj", "{B7F02268-98F8-4C73-9B3A-41250835F3FD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NBB.Messaging.OpenTracing", "src\Messaging\NBB.Messaging.OpenTracing\NBB.Messaging.OpenTracing.csproj", "{B5C4D462-E7AF-4146-9618-75FF242E148C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NBB.Messaging.OpenTelemetry", "src\Messaging\NBB.Messaging.OpenTelemetry\NBB.Messaging.OpenTelemetry.csproj", "{B5C4D462-E7AF-4146-9618-75FF242E148C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{F57779EB-9107-427D-A65C-5AA262C348E1}"
ProjectSection(SolutionItems) = preProject
Expand Down Expand Up @@ -311,7 +311,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProcessManagerSample", "sam
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{EF374BA2-61FC-41FB-8229-5187DCBAA63A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NBB.Tools.Serilog.OpenTracingSink", "src\Tools\Serilog\NBB.Tools.Serilog.OpenTracingSink\NBB.Tools.Serilog.OpenTracingSink.csproj", "{A5826A9E-7E96-4A52-8A06-2B6DC9855954}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NBB.Tools.Serilog.OpenTelemetryTracingSink", "src\Tools\Serilog\NBB.Tools.Serilog.OpenTelemetryTracingSink\NBB.Tools.Serilog.OpenTelemetryTracingSink.csproj", "{A5826A9E-7E96-4A52-8A06-2B6DC9855954}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NBB.EventStore.AdoNet.MultiTenancy", "src\EventStore\NBB.EventStore.AdoNet.Multitenancy\NBB.EventStore.AdoNet.MultiTenancy.csproj", "{2FEDB78D-E355-4449-A9D3-1C3079E38FD9}"
EndProject
Expand Down
13 changes: 10 additions & 3 deletions dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,24 @@
<MicrosoftExtensionsPackagesVersion>7.0.0</MicrosoftExtensionsPackagesVersion>
<MicrosoftEntityFrameworkPackagesVersion>7.0.0</MicrosoftEntityFrameworkPackagesVersion>
<MicrosoftCSharpPackageVersion>4.7.0</MicrosoftCSharpPackageVersion>
<OpenTelemetryCorePackageVersion>1.4.0-rc.1</OpenTelemetryCorePackageVersion>
<OpenTelemetryExtensionsHostingPackageVersion>1.4.0-rc.1</OpenTelemetryExtensionsHostingPackageVersion>
<OpenTelemetryExtensionsPropagatorsPackageVersion>1.4.0-rc.1</OpenTelemetryExtensionsPropagatorsPackageVersion>
<OpenTelemetryInstrumentationAspNetCorePackageVersion>1.0.0-rc9.10</OpenTelemetryInstrumentationAspNetCorePackageVersion>
<OpenTelemetryInstrumentationEFCorePackageVersion>1.0.0-beta.3</OpenTelemetryInstrumentationEFCorePackageVersion>
<OpenTelemetryInstrumentationHttpPackageVersion>1.0.0-rc9.10</OpenTelemetryInstrumentationHttpPackageVersion>
<OpenTelemetryInstrumentationRuntimePackageVersion>1.1.0-beta.2</OpenTelemetryInstrumentationRuntimePackageVersion>
<OpenTelemetryExporterPrometheusHttpListenerPackageVersion>1.4.0-rc.1</OpenTelemetryExporterPrometheusHttpListenerPackageVersion>
<OpenTelemetryExporterPrometheusAspnetCorePackageVersion>1.4.0-rc.1</OpenTelemetryExporterPrometheusAspnetCorePackageVersion>
<PollyVersion>7.2.2</PollyVersion>
<SerilogPackageVersion>2.10.0</SerilogPackageVersion>
<SerilogExtensionsLoggingPackageVersion>3.1.0</SerilogExtensionsLoggingPackageVersion>
<SerilogSinksConsole>4.0.1</SerilogSinksConsole>
<SerilogSinksMSSqlServerPackageVersion>5.6.1</SerilogSinksMSSqlServerPackageVersion>
<SerilogAspNetCorePackageVersion>4.1.0</SerilogAspNetCorePackageVersion>
<AutoMapperPackageVersion>12.0.0</AutoMapperPackageVersion>
<OpenTracingPackageVersion>0.12.1</OpenTracingPackageVersion>
<AutoMapperPackageVersion>12.0.0</AutoMapperPackageVersion>
<NJsonSchemaPackageVersion>10.6.6</NJsonSchemaPackageVersion>
<AutoFixturePackageVersion>4.17.0</AutoFixturePackageVersion>
<OpenTracingContribPackageVersion>0.8.0</OpenTracingContribPackageVersion>
<JaegerPackageVersion>1.0.3</JaegerPackageVersion>
<SystemIdentityModelTokensJwtPackageVersion>6.15.0</SystemIdentityModelTokensJwtPackageVersion>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,74 +1,78 @@
// Copyright (c) TotalSoft.
// This source code is licensed under the MIT license.

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using NBB.Contracts.PublishedLanguage;
using NBB.Contracts.ReadModel;
using NBB.Messaging.Abstractions;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace NBB.Contracts.Api.Controllers
{
[Route("api/[controller]")]
public class ContractsController : Controller
{
private readonly IQueryable<ContractReadModel> _contractReadModelQuery;
private readonly IMessageBusPublisher _messageBusPublisher;

public ContractsController(IMessageBusPublisher messageBusPublisher, IQueryable<ContractReadModel> contractReadModelQuery)
{
_messageBusPublisher = messageBusPublisher;
_contractReadModelQuery = contractReadModelQuery;
}


// GET api/contracts
[HttpGet]
public async Task<IActionResult> Get()
{
var query = await _contractReadModelQuery.ToListAsync();
return Ok(query.ToList());
}

// GET api/contracts/7327223E-22EA-48DC-BC44-FFF6AB3B9489
[HttpGet("{id}")]
public async Task<IActionResult> Get(Guid id)
{
//var contract = await _contractReadModelRepository.GetFirstOrDefaultAsync(x=> x.ContractId == id, "ContractLines");
var contract = await _contractReadModelQuery
.Include(x=> x.ContractLines)
.SingleOrDefaultAsync(x => x.ContractId == id, CancellationToken.None);

if (contract != null)
return Ok(contract);

return NotFound();
}

// POST api/contracts
[HttpPost]
public Task Post([FromBody]CreateContract command, CancellationToken cancellationToken)
{
return _messageBusPublisher.PublishAsync(command, cancellationToken);
}

// POST api/contracts/7327223E-22EA-48DC-BC44-FFF6AB3B9489/lines
[HttpPost("{id}/lines")]
public Task Post([FromBody]AddContractLine command, CancellationToken cancellationToken)
{
return _messageBusPublisher.PublishAsync(command, cancellationToken);
}

// POST api/contracts/7327223E-22EA-48DC-BC44-FFF6AB3B9489/validate
[HttpPost("{id}/validate")]
public Task Post([FromBody]ValidateContract command, CancellationToken cancellationToken)
{
return _messageBusPublisher.PublishAsync(command, cancellationToken);
}

}
}
using NBB.Contracts.ReadModel;
using NBB.Messaging.Abstractions;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace NBB.Contracts.Api.Controllers
{
[Route("api/[controller]")]
public class ContractsController : Controller
{
private readonly IQueryable<ContractReadModel> _contractReadModelQuery;
private readonly ILogger<ContractsController> _logger;
private readonly IMessageBusPublisher _messageBusPublisher;

public ContractsController(IMessageBusPublisher messageBusPublisher, IQueryable<ContractReadModel> contractReadModelQuery, ILogger<ContractsController> logger)
{
_messageBusPublisher = messageBusPublisher;
_contractReadModelQuery = contractReadModelQuery;
_logger = logger;
}


// GET api/contracts
[HttpGet]
public async Task<IActionResult> Get()
{
var query = await _contractReadModelQuery.ToListAsync();
return Ok(query.ToList());
}

// GET api/contracts/7327223E-22EA-48DC-BC44-FFF6AB3B9489
[HttpGet("{id}")]
public async Task<IActionResult> Get(Guid id)
{
//var contract = await _contractReadModelRepository.GetFirstOrDefaultAsync(x=> x.ContractId == id, "ContractLines");
var contract = await _contractReadModelQuery
.Include(x=> x.ContractLines)
.SingleOrDefaultAsync(x => x.ContractId == id, CancellationToken.None);

if (contract != null)
return Ok(contract);

return NotFound();
}

// POST api/contracts
[HttpPost]
public Task Post([FromBody]CreateContract command, CancellationToken cancellationToken)
{
return _messageBusPublisher.PublishAsync(command, cancellationToken);
}

// POST api/contracts/7327223E-22EA-48DC-BC44-FFF6AB3B9489/lines
[HttpPost("{id}/lines")]
public Task Post([FromBody]AddContractLine command, CancellationToken cancellationToken)
{
return _messageBusPublisher.PublishAsync(command, cancellationToken);
}

// POST api/contracts/7327223E-22EA-48DC-BC44-FFF6AB3B9489/validate
[HttpPost("{id}/validate")]
public Task Post([FromBody]ValidateContract command, CancellationToken cancellationToken)
{
_logger.LogInformation("Validating contract");
return _messageBusPublisher.PublishAsync(command, cancellationToken);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ COPY ["src/Messaging/NBB.Messaging.Nats/NBB.Messaging.Nats.csproj", "src/Messagi
COPY ["src/Correlation/NBB.Correlation.AspNet/NBB.Correlation.AspNet.csproj", "src/Correlation/NBB.Correlation.AspNet/"]
COPY ["samples/MicroServices/NBB.Contracts/NBB.Contracts.PublishedLanguage/NBB.Contracts.PublishedLanguage.csproj", "samples/MicroServices/NBB.Contracts/NBB.Contracts.PublishedLanguage/"]
COPY ["src/Messaging/NBB.Messaging.BackwardCompatibility/NBB.Messaging.BackwardCompatibility.csproj", "src/Messaging/NBB.Messaging.BackwardCompatibility/"]
COPY ["src/Messaging/NBB.Messaging.OpenTracing/NBB.Messaging.OpenTracing.csproj", "src/Messaging/NBB.Messaging.OpenTracing/"]
COPY ["src/Messaging/NBB.Messaging.OpenTelemetry/NBB.Messaging.OpenTelemetry.csproj", "src/Messaging/NBB.Messaging.OpenTelemetry/"]
COPY ["samples/MicroServices/NBB.Contracts/NBB.Contracts.ReadModel.Data/NBB.Contracts.ReadModel.Data.csproj", "samples/MicroServices/NBB.Contracts/NBB.Contracts.ReadModel.Data/"]
COPY ["samples/MicroServices/NBB.Contracts/NBB.Contracts.ReadModel/NBB.Contracts.ReadModel.csproj", "samples/MicroServices/NBB.Contracts/NBB.Contracts.ReadModel/"]
COPY ["src/Data/NBB.Data.EntityFramework/NBB.Data.EntityFramework.csproj", "src/Data/NBB.Data.EntityFramework/"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,29 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="$(OpenTelemetryCorePackageVersion)" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="$(OpenTelemetryCorePackageVersion)" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="$(OpenTelemetryExporterPrometheusAspnetCorePackageVersion)" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="$(OpenTelemetryExtensionsHostingPackageVersion)" />
<PackageReference Include="OpenTelemetry.Extensions.Propagators" Version="$(OpenTelemetryExtensionsPropagatorsPackageVersion)" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="$(OpenTelemetryInstrumentationAspNetCorePackageVersion)" />
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="$(OpenTelemetryInstrumentationEFCorePackageVersion)" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="$(OpenTelemetryInstrumentationHttpPackageVersion)" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="$(OpenTelemetryInstrumentationRuntimePackageVersion)" />
<PackageReference Include="Serilog.AspNetCore" Version="$(SerilogAspNetCorePackageVersion)" />
<PackageReference Include="Serilog.Extensions.Logging" Version="$(SerilogExtensionsLoggingPackageVersion)" />
<PackageReference Include="Serilog.Sinks.Console" Version="$(SerilogSinksConsole)" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Jaeger" Version="$(JaegerPackageVersion)" />
<PackageReference Include="Jaeger.Senders.Thrift" Version="$(JaegerPackageVersion)" />
<!--<PackageReference Include="OpenTracing.Contrib.NetCore" Version="$(OpenTracingContribPackageVersion)" />-->
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\src\Correlation\NBB.Correlation.AspNet\NBB.Correlation.AspNet.csproj" />
<ProjectReference Include="..\..\..\..\src\Correlation\NBB.Correlation.Serilog\NBB.Correlation.Serilog.csproj" />
<ProjectReference Include="..\..\..\..\src\Messaging\NBB.Messaging.BackwardCompatibility\NBB.Messaging.BackwardCompatibility.csproj" />
<ProjectReference Include="..\..\..\..\src\Messaging\NBB.Messaging.Nats\NBB.Messaging.Nats.csproj" />
<ProjectReference Include="..\..\..\..\src\Messaging\NBB.Messaging.OpenTracing\NBB.Messaging.OpenTracing.csproj" />
<ProjectReference Include="..\..\..\..\src\Messaging\NBB.Messaging.Rusi\NBB.Messaging.Rusi.csproj" />
<ProjectReference Include="..\..\..\..\src\Messaging\NBB.Messaging.OpenTelemetry\NBB.Messaging.OpenTelemetry.csproj" />
<ProjectReference Include="..\..\..\..\src\Messaging\NBB.Messaging.Rusi\NBB.Messaging.Rusi.csproj" />
<ProjectReference Include="..\..\..\..\src\Tools\Serilog\NBB.Tools.Serilog.OpenTelemetryTracingSink\NBB.Tools.Serilog.OpenTelemetryTracingSink.csproj" />
<ProjectReference Include="..\NBB.Contracts.PublishedLanguage\NBB.Contracts.PublishedLanguage.csproj" />
<ProjectReference Include="..\NBB.Contracts.ReadModel.Data\NBB.Contracts.ReadModel.Data.csproj" />
</ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions samples/MicroServices/NBB.Contracts/NBB.Contracts.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using NBB.Correlation.Serilog;
using NBB.Tools.Serilog.OpenTelemetryTracingSink;
using Serilog;

namespace NBB.Contracts.Api
{
Expand All @@ -15,6 +18,15 @@ public static void Main(string[] args)

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog((context, services, logConfig) =>
{
logConfig
.ReadFrom.Configuration(context.Configuration)
.Enrich.FromLogContext()
.Enrich.With<CorrelationLogEventEnricher>()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} {TenantCode:u}] {Message:lj}{NewLine}{Exception}")
.WriteTo.OpenTelemetryTracing();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
Expand Down
Loading

0 comments on commit a67c3dd

Please sign in to comment.