Enabled Instrumentations with OpenTelemetry

This commit is contained in:
Sumit Ghosh 2021-04-07 18:05:46 +05:30
parent 69cc6cb129
commit 18e39fd980
24 changed files with 985 additions and 484 deletions

View File

@ -1,28 +1,33 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry;
using OpenTelemetry.Resources; using OpenTelemetry.Resources;
using OpenTelemetry.Trace; using OpenTelemetry.Trace;
using StackExchange.Redis;
using System; using System;
static class OpenTelemetryExtensions namespace Basket.API.Extensions
{ {
public static IServiceCollection AddOpenTelemetry(this IServiceCollection services) static class OpenTelemetryExtensions
{
public static void AddOpenTelemetry(ConnectionMultiplexer connectionMultiplexer)
{ {
var exportType = Environment.GetEnvironmentVariable("OTEL_USE_EXPORTER")?.ToLower(); var exportType = Environment.GetEnvironmentVariable("OTEL_USE_EXPORTER")?.ToLower();
if (exportType == null) if (exportType == null)
{ {
return services; return;
} }
return services.AddOpenTelemetryTracing((serviceProvider, tracerProviderBuilder) => var tracerProviderBuilder = Sdk.CreateTracerProviderBuilder();
{
// Configure resource // Configure resource
tracerProviderBuilder tracerProviderBuilder
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("WebMVC")); .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("Basket.API"));
// Configure instrumentation // Configure instrumentation
tracerProviderBuilder tracerProviderBuilder
.AddAspNetCoreInstrumentation() .AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation(); .AddHttpClientInstrumentation()
.AddRedisInstrumentation(connectionMultiplexer);
// Configure exporter // Configure exporter
switch (exportType) switch (exportType)
@ -57,6 +62,8 @@ static class OpenTelemetryExtensions
tracerProviderBuilder.AddConsoleExporter(); tracerProviderBuilder.AddConsoleExporter();
break; break;
} }
});
tracerProviderBuilder.Build();
}
} }
} }

View File

@ -1,66 +0,0 @@
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using StackExchange.Redis;
using System;
static class OpenTelemetryExtensions
{
public static void AddOpenTelemetry(ConnectionMultiplexer connectionMultiplexer)
{
var exportType = Environment.GetEnvironmentVariable("OTEL_USE_EXPORTER")?.ToLower();
if (exportType == null)
{
return;
}
var tracerProviderBuilder = Sdk.CreateTracerProviderBuilder();
// Configure resource
tracerProviderBuilder
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("Basket.API"));
// Configure instrumentation
tracerProviderBuilder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddRedisInstrumentation(connectionMultiplexer);
// Configure exporter
switch (exportType)
{
case "jaeger":
tracerProviderBuilder.AddJaegerExporter(options =>
{
var agentHost = Environment.GetEnvironmentVariable("OTEL_EXPORTER_JAEGER_AGENTHOST");
options.AgentHost = agentHost;
});
break;
case "otlp":
tracerProviderBuilder.AddOtlpExporter(options =>
{
var endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT")
?? Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT");
options.Endpoint = new Uri(endpoint);
var headers = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_TRACES_HEADERS")
?? Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS");
options.Headers = headers;
});
break;
case "zipkin":
tracerProviderBuilder.AddZipkinExporter(options =>
{
var endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_ZIPKIN_ENDPOINT");
options.Endpoint = new Uri(endpoint);
});
break;
default:
tracerProviderBuilder.AddConsoleExporter();
break;
}
tracerProviderBuilder.Build();
}
}

View File

@ -1,5 +1,6 @@
using Autofac; using Autofac;
using Autofac.Extensions.DependencyInjection; using Autofac.Extensions.DependencyInjection;
using Basket.API.Extensions;
using Basket.API.Infrastructure.Filters; using Basket.API.Infrastructure.Filters;
using Basket.API.IntegrationEvents.EventHandling; using Basket.API.IntegrationEvents.EventHandling;
using Basket.API.IntegrationEvents.Events; using Basket.API.IntegrationEvents.Events;
@ -188,6 +189,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory, ConnectionMultiplexer connectionMultiplexer) public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory, ConnectionMultiplexer connectionMultiplexer)
{ {
OpenTelemetryExtensions.AddOpenTelemetry(connectionMultiplexer); OpenTelemetryExtensions.AddOpenTelemetry(connectionMultiplexer);
//loggerFactory.AddAzureWebAppDiagnostics(); //loggerFactory.AddAzureWebAppDiagnostics();
//loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace); //loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace);

View File

@ -68,6 +68,19 @@
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" /> <PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc2" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc2" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc2" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.0.0-rc2" />
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.0.0-rc2" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusRabbitMQ\EventBusRabbitMQ.csproj" /> <ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusRabbitMQ\EventBusRabbitMQ.csproj" />
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusServiceBus\EventBusServiceBus.csproj" /> <ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusServiceBus\EventBusServiceBus.csproj" />

View File

@ -0,0 +1,68 @@
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System;
namespace Catalog.API.Extensions
{
static class OpenTelemetryExtensions
{
public static IServiceCollection AddOpenTelemetry(this IServiceCollection services)
{
var exportType = Environment.GetEnvironmentVariable("OTEL_USE_EXPORTER")?.ToLower();
if (exportType == null)
{
return services;
}
return services.AddOpenTelemetryTracing((serviceProvider, tracerProviderBuilder) =>
{
// Configure resource
tracerProviderBuilder
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("Catalog.API"));
// Configure instrumentation
tracerProviderBuilder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddGrpcClientInstrumentation()
.AddSqlClientInstrumentation();
// Configure exporter
switch (exportType)
{
case "jaeger":
tracerProviderBuilder.AddJaegerExporter(options =>
{
var agentHost = Environment.GetEnvironmentVariable("OTEL_EXPORTER_JAEGER_AGENTHOST");
options.AgentHost = agentHost;
});
break;
case "otlp":
tracerProviderBuilder.AddOtlpExporter(options =>
{
var endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT")
?? Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT");
options.Endpoint = new Uri(endpoint);
var headers = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_TRACES_HEADERS")
?? Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS");
options.Headers = headers;
});
break;
case "zipkin":
tracerProviderBuilder.AddZipkinExporter(options =>
{
var endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_ZIPKIN_ENDPOINT");
options.Endpoint = new Uri(endpoint);
});
break;
default:
tracerProviderBuilder.AddConsoleExporter();
break;
}
});
}
}
}

View File

@ -31,6 +31,7 @@ using System;
using System.Data.Common; using System.Data.Common;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using Catalog.API.Extensions;
namespace Microsoft.eShopOnContainers.Services.Catalog.API namespace Microsoft.eShopOnContainers.Services.Catalog.API
{ {
@ -53,7 +54,8 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API
.AddIntegrationServices(Configuration) .AddIntegrationServices(Configuration)
.AddEventBus(Configuration) .AddEventBus(Configuration)
.AddSwagger(Configuration) .AddSwagger(Configuration)
.AddCustomHealthCheck(Configuration); .AddCustomHealthCheck(Configuration)
.AddOpenTelemetry();
var container = new ContainerBuilder(); var container = new ContainerBuilder();
container.Populate(services); container.Populate(services);

View File

@ -0,0 +1,70 @@
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Identity.API.Extensions
{
static class OpenTelemetryExtensions
{
public static IServiceCollection AddOpenTelemetry(this IServiceCollection services)
{
var exportType = Environment.GetEnvironmentVariable("OTEL_USE_EXPORTER")?.ToLower();
if (exportType == null)
{
return services;
}
return services.AddOpenTelemetryTracing((serviceProvider, tracerProviderBuilder) =>
{
// Configure resource
tracerProviderBuilder
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("Identity.API"));
// Configure instrumentation
tracerProviderBuilder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddSqlClientInstrumentation();
// Configure exporter
switch (exportType)
{
case "jaeger":
tracerProviderBuilder.AddJaegerExporter(options =>
{
var agentHost = Environment.GetEnvironmentVariable("OTEL_EXPORTER_JAEGER_AGENTHOST");
options.AgentHost = agentHost;
});
break;
case "otlp":
tracerProviderBuilder.AddOtlpExporter(options =>
{
var endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT")
?? Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT");
options.Endpoint = new Uri(endpoint);
var headers = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_TRACES_HEADERS")
?? Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS");
options.Headers = headers;
});
break;
case "zipkin":
tracerProviderBuilder.AddZipkinExporter(options =>
{
var endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_ZIPKIN_ENDPOINT");
options.Endpoint = new Uri(endpoint);
});
break;
default:
tracerProviderBuilder.AddConsoleExporter();
break;
}
});
}
}
}

View File

@ -52,6 +52,18 @@
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" /> <PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc2" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc2" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc2" />
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.0.0-rc2" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="Certificate\idsrv3test.pfx" /> <EmbeddedResource Include="Certificate\idsrv3test.pfx" />
</ItemGroup> </ItemGroup>
@ -62,8 +74,4 @@
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Extensions\" />
</ItemGroup>
</Project> </Project>

View File

@ -1,6 +1,7 @@
using Autofac; using Autofac;
using Autofac.Extensions.DependencyInjection; using Autofac.Extensions.DependencyInjection;
using HealthChecks.UI.Client; using HealthChecks.UI.Client;
using Identity.API.Extensions;
using IdentityServer4.Services; using IdentityServer4.Services;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.DataProtection;
@ -38,6 +39,8 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API
{ {
RegisterAppInsights(services); RegisterAppInsights(services);
services.AddOpenTelemetry();
// Add framework services. // Add framework services.
services.AddDbContext<ApplicationDbContext>(options => services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["ConnectionString"], options.UseSqlServer(Configuration["ConnectionString"],

View File

@ -0,0 +1,70 @@
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Ordering.API.Extensions
{
static class OpenTelemetryExtensions
{
public static IServiceCollection AddOpenTelemetry(this IServiceCollection services)
{
var exportType = Environment.GetEnvironmentVariable("OTEL_USE_EXPORTER")?.ToLower();
if (exportType == null)
{
return services;
}
return services.AddOpenTelemetryTracing((serviceProvider, tracerProviderBuilder) =>
{
// Configure resource
tracerProviderBuilder
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("Order.API"));
// Configure instrumentation
tracerProviderBuilder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddSqlClientInstrumentation();
// Configure exporter
switch (exportType)
{
case "jaeger":
tracerProviderBuilder.AddJaegerExporter(options =>
{
var agentHost = Environment.GetEnvironmentVariable("OTEL_EXPORTER_JAEGER_AGENTHOST");
options.AgentHost = agentHost;
});
break;
case "otlp":
tracerProviderBuilder.AddOtlpExporter(options =>
{
var endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT")
?? Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT");
options.Endpoint = new Uri(endpoint);
var headers = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_TRACES_HEADERS")
?? Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS");
options.Headers = headers;
});
break;
case "zipkin":
tracerProviderBuilder.AddZipkinExporter(options =>
{
var endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_ZIPKIN_ENDPOINT");
options.Endpoint = new Uri(endpoint);
});
break;
default:
tracerProviderBuilder.AddConsoleExporter();
break;
}
});
}
}
}

View File

@ -69,6 +69,18 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc2" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc2" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc2" />
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.0.0-rc2" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="Setup\*"> <None Update="Setup\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

View File

@ -37,6 +37,7 @@
using System.IdentityModel.Tokens.Jwt; using System.IdentityModel.Tokens.Jwt;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using global::Ordering.API.Extensions;
public class Startup public class Startup
{ {
@ -49,12 +50,14 @@
public virtual IServiceProvider ConfigureServices(IServiceCollection services) public virtual IServiceProvider ConfigureServices(IServiceCollection services)
{ {
services services
.AddGrpc(options => .AddGrpc(options =>
{ {
options.EnableDetailedErrors = true; options.EnableDetailedErrors = true;
}) })
.Services .Services
.AddOpenTelemetry()
.AddApplicationInsights(Configuration) .AddApplicationInsights(Configuration)
.AddCustomMvc() .AddCustomMvc()
.AddHealthChecks(Configuration) .AddHealthChecks(Configuration)
@ -64,8 +67,8 @@
.AddCustomConfiguration(Configuration) .AddCustomConfiguration(Configuration)
.AddEventBus(Configuration) .AddEventBus(Configuration)
.AddCustomAuthentication(Configuration); .AddCustomAuthentication(Configuration);
//configure autofac
//configure autofac
var container = new ContainerBuilder(); var container = new ContainerBuilder();
container.Populate(services); container.Populate(services);

View File

@ -0,0 +1,69 @@
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Payment.API.Extensions
{
static class OpenTelemetryExtensions
{
public static IServiceCollection AddOpenTelemetry(this IServiceCollection services)
{
var exportType = Environment.GetEnvironmentVariable("OTEL_USE_EXPORTER")?.ToLower();
if (exportType == null)
{
return services;
}
return services.AddOpenTelemetryTracing((serviceProvider, tracerProviderBuilder) =>
{
// Configure resource
tracerProviderBuilder
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("Payment.API"));
// Configure instrumentation
tracerProviderBuilder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation();
// Configure exporter
switch (exportType)
{
case "jaeger":
tracerProviderBuilder.AddJaegerExporter(options =>
{
var agentHost = Environment.GetEnvironmentVariable("OTEL_EXPORTER_JAEGER_AGENTHOST");
options.AgentHost = agentHost;
});
break;
case "otlp":
tracerProviderBuilder.AddOtlpExporter(options =>
{
var endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT")
?? Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT");
options.Endpoint = new Uri(endpoint);
var headers = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_TRACES_HEADERS")
?? Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS");
options.Headers = headers;
});
break;
case "zipkin":
tracerProviderBuilder.AddZipkinExporter(options =>
{
var endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_ZIPKIN_ENDPOINT");
options.Endpoint = new Uri(endpoint);
});
break;
default:
tracerProviderBuilder.AddConsoleExporter();
break;
}
});
}
}
}

View File

@ -28,6 +28,17 @@
<PackageReference Include="Serilog.Sinks.Seq" Version="4.1.0-dev-00166" /> <PackageReference Include="Serilog.Sinks.Seq" Version="4.1.0-dev-00166" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc2" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc2" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc2" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusRabbitMQ\EventBusRabbitMQ.csproj" /> <ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusRabbitMQ\EventBusRabbitMQ.csproj" />
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusServiceBus\EventBusServiceBus.csproj" /> <ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusServiceBus\EventBusServiceBus.csproj" />

View File

@ -12,6 +12,7 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Payment.API.Extensions;
using Payment.API.IntegrationEvents.EventHandling; using Payment.API.IntegrationEvents.EventHandling;
using Payment.API.IntegrationEvents.Events; using Payment.API.IntegrationEvents.Events;
using RabbitMQ.Client; using RabbitMQ.Client;
@ -31,6 +32,7 @@ namespace Payment.API
// This method gets called by the runtime. Use this method to add services to the container. // This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services) public IServiceProvider ConfigureServices(IServiceCollection services)
{ {
services.AddOpenTelemetry();
services.AddCustomHealthCheck(Configuration); services.AddCustomHealthCheck(Configuration);
services.Configure<PaymentSettings>(Configuration); services.Configure<PaymentSettings>(Configuration);

View File

@ -0,0 +1,65 @@
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System;
namespace Microsoft.eShopOnContainers.WebMVC.Extensions
{
static class OpenTelemetryExtensions
{
public static IServiceCollection AddOpenTelemetry(this IServiceCollection services)
{
var exportType = Environment.GetEnvironmentVariable("OTEL_USE_EXPORTER")?.ToLower();
if (exportType == null)
{
return services;
}
return services.AddOpenTelemetryTracing((serviceProvider, tracerProviderBuilder) =>
{
// Configure resource
tracerProviderBuilder
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("WebMVC"));
// Configure instrumentation
tracerProviderBuilder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation();
// Configure exporter
switch (exportType)
{
case "jaeger":
tracerProviderBuilder.AddJaegerExporter(options =>
{
var agentHost = Environment.GetEnvironmentVariable("OTEL_EXPORTER_JAEGER_AGENTHOST");
options.AgentHost = agentHost;
});
break;
case "otlp":
tracerProviderBuilder.AddOtlpExporter(options =>
{
var endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT")
?? Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT");
options.Endpoint = new Uri(endpoint);
var headers = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_TRACES_HEADERS")
?? Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS");
options.Headers = headers;
});
break;
case "zipkin":
tracerProviderBuilder.AddZipkinExporter(options =>
{
var endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_ZIPKIN_ENDPOINT");
options.Endpoint = new Uri(endpoint);
});
break;
default:
tracerProviderBuilder.AddConsoleExporter();
break;
}
});
}
}
}

View File

@ -7,6 +7,7 @@ using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.eShopOnContainers.WebMVC.Extensions;
using Microsoft.eShopOnContainers.WebMVC.Services; using Microsoft.eShopOnContainers.WebMVC.Services;
using Microsoft.eShopOnContainers.WebMVC.ViewModels; using Microsoft.eShopOnContainers.WebMVC.ViewModels;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;

View File

@ -0,0 +1,69 @@
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebSPA.Extensions
{
static class OpenTelemetryExtensions
{
public static IServiceCollection AddOpenTelemetry(this IServiceCollection services)
{
var exportType = Environment.GetEnvironmentVariable("OTEL_USE_EXPORTER")?.ToLower();
if (exportType == null)
{
return services;
}
return services.AddOpenTelemetryTracing((serviceProvider, tracerProviderBuilder) =>
{
// Configure resource
tracerProviderBuilder
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("WebSPA"));
// Configure instrumentation
tracerProviderBuilder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation();
// Configure exporter
switch (exportType)
{
case "jaeger":
tracerProviderBuilder.AddJaegerExporter(options =>
{
var agentHost = Environment.GetEnvironmentVariable("OTEL_EXPORTER_JAEGER_AGENTHOST");
options.AgentHost = agentHost;
});
break;
case "otlp":
tracerProviderBuilder.AddOtlpExporter(options =>
{
var endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT")
?? Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT");
options.Endpoint = new Uri(endpoint);
var headers = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_TRACES_HEADERS")
?? Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS");
options.Headers = headers;
});
break;
case "zipkin":
tracerProviderBuilder.AddZipkinExporter(options =>
{
var endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_ZIPKIN_ENDPOINT");
options.Endpoint = new Uri(endpoint);
});
break;
default:
tracerProviderBuilder.AddConsoleExporter();
break;
}
});
}
}
}

View File

@ -13,6 +13,7 @@ using Microsoft.Extensions.Logging;
using StackExchange.Redis; using StackExchange.Redis;
using System; using System;
using System.IO; using System.IO;
using WebSPA.Extensions;
using WebSPA.Infrastructure; using WebSPA.Infrastructure;
namespace eShopConContainers.WebSPA namespace eShopConContainers.WebSPA
@ -38,6 +39,7 @@ namespace eShopConContainers.WebSPA
{ {
RegisterAppInsights(services); RegisterAppInsights(services);
services.AddOpenTelemetry();
services.AddHealthChecks() services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy()) .AddCheck("self", () => HealthCheckResult.Healthy())
.AddUrlGroup(new Uri(Configuration["IdentityUrlHC"]), name: "identityapi-check", tags: new string[] { "identityapi" }); .AddUrlGroup(new Uri(Configuration["IdentityUrlHC"]), name: "identityapi-check", tags: new string[] { "identityapi" });

View File

@ -100,6 +100,17 @@
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0-dev-00834" /> <PackageReference Include="Serilog.Sinks.Console" Version="4.0.0-dev-00834" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc2" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc2" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc2" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="wwwroot\assets\" /> <Folder Include="wwwroot\assets\" />
</ItemGroup> </ItemGroup>
@ -165,7 +176,9 @@
</ItemGroup> </ItemGroup>
<ProjectExtensions> <ProjectExtensions>
<VisualStudio><UserProperties package_1json__JSONSchema="http://json.schemastore.org/project-1.0.0-beta4" /></VisualStudio> <VisualStudio>
<UserProperties package_1json__JSONSchema="http://json.schemastore.org/project-1.0.0-beta4" />
</VisualStudio>
</ProjectExtensions> </ProjectExtensions>
</Project> </Project>

View File

@ -6,10 +6,31 @@ version: '3.4'
# docker-compose -f docker-compose.yml -f docker-compose.override.yml -f docker-compose.opentelemetry.console.yml up # docker-compose -f docker-compose.yml -f docker-compose.override.yml -f docker-compose.opentelemetry.console.yml up
services: services:
identity-api:
environment:
- OTEL_USE_EXPORTER=console
basket-api: basket-api:
environment: environment:
- OTEL_USE_EXPORTER=console - OTEL_USE_EXPORTER=console
catalog-api:
environment:
- OTEL_USE_EXPORTER=console
ordering-api:
environment:
- OTEL_USE_EXPORTER=console
payment-api:
environment:
- OTEL_USE_EXPORTER=console
webmvc: webmvc:
environment: environment:
- OTEL_USE_EXPORTER=console - OTEL_USE_EXPORTER=console
webspa:
environment:
- OTEL_USE_EXPORTER=console

View File

@ -18,12 +18,37 @@ services:
- "14250:14250" - "14250:14250"
- "9411:9411" - "9411:9411"
identity-api:
environment:
- OTEL_USE_EXPORTER=jaeger
- OTEL_EXPORTER_JAEGER_AGENTHOST=${OTEL_EXPORTER_JAEGER_AGENTHOST:-jaeger-all-in-one}
basket-api: basket-api:
environment: environment:
- OTEL_USE_EXPORTER=jaeger - OTEL_USE_EXPORTER=jaeger
- OTEL_EXPORTER_JAEGER_AGENTHOST=${OTEL_EXPORTER_JAEGER_AGENTHOST:-jaeger-all-in-one} - OTEL_EXPORTER_JAEGER_AGENTHOST=${OTEL_EXPORTER_JAEGER_AGENTHOST:-jaeger-all-in-one}
catalog-api:
environment:
- OTEL_USE_EXPORTER=jaeger
- OTEL_EXPORTER_JAEGER_AGENTHOST=${OTEL_EXPORTER_JAEGER_AGENTHOST:-jaeger-all-in-one}
ordering-api:
environment:
- OTEL_USE_EXPORTER=jaeger
- OTEL_EXPORTER_JAEGER_AGENTHOST=${OTEL_EXPORTER_JAEGER_AGENTHOST:-jaeger-all-in-one}
payment-api:
environment:
- OTEL_USE_EXPORTER=jaeger
- OTEL_EXPORTER_JAEGER_AGENTHOST=${OTEL_EXPORTER_JAEGER_AGENTHOST:-jaeger-all-in-one}
webmvc: webmvc:
environment: environment:
- OTEL_USE_EXPORTER=jaeger - OTEL_USE_EXPORTER=jaeger
- OTEL_EXPORTER_JAEGER_AGENTHOST=${OTEL_EXPORTER_JAEGER_AGENTHOST:-jaeger-all-in-one} - OTEL_EXPORTER_JAEGER_AGENTHOST=${OTEL_EXPORTER_JAEGER_AGENTHOST:-jaeger-all-in-one}
webspa:
environment:
- OTEL_USE_EXPORTER=jaeger
- OTEL_EXPORTER_JAEGER_AGENTHOST=${OTEL_EXPORTER_JAEGER_AGENTHOST:-jaeger-all-in-one}

View File

@ -20,6 +20,12 @@ services:
- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=${OTEL_EXPORTER_OTLP_TRACES_ENDPOINT:-http://otel-collector:4317} - OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=${OTEL_EXPORTER_OTLP_TRACES_ENDPOINT:-http://otel-collector:4317}
- OTEL_EXPORTER_OTLP_HEADERS=${OTEL_EXPORTER_OTLP_HEADERS} - OTEL_EXPORTER_OTLP_HEADERS=${OTEL_EXPORTER_OTLP_HEADERS}
catalog-api:
environment:
- OTEL_USE_EXPORTER=otlp
- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=${OTEL_EXPORTER_OTLP_TRACES_ENDPOINT:-http://otel-collector:4317}
- OTEL_EXPORTER_OTLP_HEADERS=${OTEL_EXPORTER_OTLP_HEADERS}
webmvc: webmvc:
environment: environment:
- OTEL_USE_EXPORTER=otlp - OTEL_USE_EXPORTER=otlp

View File

@ -11,12 +11,37 @@ services:
ports: ports:
- "9411:9411" - "9411:9411"
identity-api:
environment:
- OTEL_USE_EXPORTER=zipkin
- OTEL_EXPORTER_ZIPKIN_ENDPOINT=${OTEL_EXPORTER_ZIPKIN_ENDPOINT:-http://zipkin:9411/api/v2/spans}
basket-api: basket-api:
environment: environment:
- OTEL_USE_EXPORTER=zipkin - OTEL_USE_EXPORTER=zipkin
- OTEL_EXPORTER_ZIPKIN_ENDPOINT=${OTEL_EXPORTER_ZIPKIN_ENDPOINT:-http://zipkin:9411/api/v2/spans} - OTEL_EXPORTER_ZIPKIN_ENDPOINT=${OTEL_EXPORTER_ZIPKIN_ENDPOINT:-http://zipkin:9411/api/v2/spans}
catalog-api:
environment:
- OTEL_USE_EXPORTER=zipkin
- OTEL_EXPORTER_ZIPKIN_ENDPOINT=${OTEL_EXPORTER_ZIPKIN_ENDPOINT:-http://zipkin:9411/api/v2/spans}
ordering-api:
environment:
- OTEL_USE_EXPORTER=zipkin
- OTEL_EXPORTER_ZIPKIN_ENDPOINT=${OTEL_EXPORTER_ZIPKIN_ENDPOINT:-http://zipkin:9411/api/v2/spans}
payment-api:
environment:
- OTEL_USE_EXPORTER=zipkin
- OTEL_EXPORTER_ZIPKIN_ENDPOINT=${OTEL_EXPORTER_ZIPKIN_ENDPOINT:-http://zipkin:9411/api/v2/spans}
webmvc: webmvc:
environment: environment:
- OTEL_USE_EXPORTER=zipkin - OTEL_USE_EXPORTER=zipkin
- OTEL_EXPORTER_ZIPKIN_ENDPOINT=${OTEL_EXPORTER_ZIPKIN_ENDPOINT:-http://zipkin:9411/api/v2/spans} - OTEL_EXPORTER_ZIPKIN_ENDPOINT=${OTEL_EXPORTER_ZIPKIN_ENDPOINT:-http://zipkin:9411/api/v2/spans}
webspa:
environment:
- OTEL_USE_EXPORTER=zipkin
- OTEL_EXPORTER_ZIPKIN_ENDPOINT=${OTEL_EXPORTER_ZIPKIN_ENDPOINT:-http://zipkin:9411/api/v2/spans}