From 1fdfa6f1288efc2cdadd7e2befec7a0a42dc7a43 Mon Sep 17 00:00:00 2001 From: Guanjun Date: Wed, 24 Aug 2022 19:48:55 +0800 Subject: [PATCH] add otel support --- deploy/otel/otel-collector-config.yaml | 23 +++++++++++++++++++ .../Basket/Basket.API/Basket.API.csproj | 9 ++++++-- src/Services/Basket/Basket.API/Startup.cs | 17 ++++++++++++++ .../Basket/Basket.API/appsettings.json | 4 ++++ .../Catalog/Catalog.API/Catalog.API.csproj | 7 +++++- src/Services/Catalog/Catalog.API/Startup.cs | 16 +++++++++++++ .../Catalog/Catalog.API/appsettings.json | 5 +++- .../Ordering/Ordering.API/Ordering.API.csproj | 7 +++++- src/Services/Ordering/Ordering.API/Startup.cs | 16 +++++++++++++ .../Ordering/Ordering.API/appsettings.json | 4 ++++ src/Web/WebMVC/Startup.cs | 16 +++++++++++++ src/Web/WebMVC/WebMVC.csproj | 6 +++++ src/Web/WebMVC/appsettings.json | 6 ++++- src/docker-compose.otel.yml | 19 +++++++++++++++ 14 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 deploy/otel/otel-collector-config.yaml create mode 100644 src/docker-compose.otel.yml diff --git a/deploy/otel/otel-collector-config.yaml b/deploy/otel/otel-collector-config.yaml new file mode 100644 index 000000000..082d53b1b --- /dev/null +++ b/deploy/otel/otel-collector-config.yaml @@ -0,0 +1,23 @@ + +# Data sources: traces, metrics, logs +receivers: + otlp: + protocols: + grpc: + http: + +processors: + batch: + +service: + pipelines: + traces: + receivers: [otlp] + processors: [batch] + exporters: [zipkin] + +exporters: + zipkin: + endpoint: "http://zipkin:9411/api/v2/spans" + + diff --git a/src/Services/Basket/Basket.API/Basket.API.csproj b/src/Services/Basket/Basket.API/Basket.API.csproj index ebb224824..1a1d410ba 100644 --- a/src/Services/Basket/Basket.API/Basket.API.csproj +++ b/src/Services/Basket/Basket.API/Basket.API.csproj @@ -13,7 +13,12 @@ - + + + + + + @@ -23,7 +28,7 @@ - + diff --git a/src/Services/Basket/Basket.API/Startup.cs b/src/Services/Basket/Basket.API/Startup.cs index 4dc01fff2..38be79b64 100644 --- a/src/Services/Basket/Basket.API/Startup.cs +++ b/src/Services/Basket/Basket.API/Startup.cs @@ -1,4 +1,8 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API; + +using OpenTelemetry.Trace; +using OpenTelemetry.Resources; + public class Startup { public Startup(IConfiguration configuration) @@ -11,6 +15,19 @@ public class Startup // This method gets called by the runtime. Use this method to add services to the container. public virtual IServiceProvider ConfigureServices(IServiceCollection services) { + services.AddOpenTelemetryTracing( builder => builder + .SetResourceBuilder( + ResourceBuilder.CreateDefault() + .AddService(Configuration.GetValue("Otlp:ServiceName")) + ) + .AddHttpClientInstrumentation() + .AddAspNetCoreInstrumentation() + .AddOtlpExporter( otlpOptions => { + var url = Configuration.GetValue("Otlp:Endpoint"); + otlpOptions.Endpoint = new Uri( url ); + }) + ); + services.AddGrpc(options => { options.EnableDetailedErrors = true; diff --git a/src/Services/Basket/Basket.API/appsettings.json b/src/Services/Basket/Basket.API/appsettings.json index 295294308..c3784e130 100644 --- a/src/Services/Basket/Basket.API/appsettings.json +++ b/src/Services/Basket/Basket.API/appsettings.json @@ -26,5 +26,9 @@ "Name": "eshop", "ClientId": "your-client-id", "ClientSecret": "your-client-secret" + }, + "Otlp": { + "ServiceName": "Basket API", + "Endpoint": "http://otel-collector:4317" } } diff --git a/src/Services/Catalog/Catalog.API/Catalog.API.csproj b/src/Services/Catalog/Catalog.API/Catalog.API.csproj index b78ce2af3..f29ff03d5 100644 --- a/src/Services/Catalog/Catalog.API/Catalog.API.csproj +++ b/src/Services/Catalog/Catalog.API/Catalog.API.csproj @@ -41,6 +41,11 @@ + + + + + @@ -49,7 +54,7 @@ - + diff --git a/src/Services/Catalog/Catalog.API/Startup.cs b/src/Services/Catalog/Catalog.API/Startup.cs index f7b46cb6f..de9193cc5 100644 --- a/src/Services/Catalog/Catalog.API/Startup.cs +++ b/src/Services/Catalog/Catalog.API/Startup.cs @@ -1,5 +1,8 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API; +using OpenTelemetry.Trace; +using OpenTelemetry.Resources; + public class Startup { public Startup(IConfiguration configuration) @@ -11,6 +14,19 @@ public class Startup public IServiceProvider ConfigureServices(IServiceCollection services) { + services.AddOpenTelemetryTracing( builder => builder + .SetResourceBuilder( + ResourceBuilder.CreateDefault() + .AddService(Configuration.GetValue("Otlp:ServiceName")) + ) + .AddHttpClientInstrumentation() + .AddAspNetCoreInstrumentation() + .AddOtlpExporter( otlpOptions => { + var url = Configuration.GetValue("Otlp:Endpoint"); + otlpOptions.Endpoint = new Uri( url ); + }) + ); + services.AddAppInsight(Configuration) .AddGrpc().Services .AddCustomMVC(Configuration) diff --git a/src/Services/Catalog/Catalog.API/appsettings.json b/src/Services/Catalog/Catalog.API/appsettings.json index f8342fe8d..4da72880f 100644 --- a/src/Services/Catalog/Catalog.API/appsettings.json +++ b/src/Services/Catalog/Catalog.API/appsettings.json @@ -24,7 +24,10 @@ "Name": "eshop", "ClientId": "your-client-id", "ClientSecret": "your-client-secret" + }, + "Otlp": { + "ServiceName": "Catelog API", + "Endpoint": "http://otel-collector:4317" } - } diff --git a/src/Services/Ordering/Ordering.API/Ordering.API.csproj b/src/Services/Ordering/Ordering.API/Ordering.API.csproj index 5d779ffc0..e60918540 100644 --- a/src/Services/Ordering/Ordering.API/Ordering.API.csproj +++ b/src/Services/Ordering/Ordering.API/Ordering.API.csproj @@ -36,6 +36,11 @@ + + + + + @@ -45,7 +50,7 @@ - + diff --git a/src/Services/Ordering/Ordering.API/Startup.cs b/src/Services/Ordering/Ordering.API/Startup.cs index a4bfacca1..1639463f8 100644 --- a/src/Services/Ordering/Ordering.API/Startup.cs +++ b/src/Services/Ordering/Ordering.API/Startup.cs @@ -1,5 +1,8 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API; +using OpenTelemetry.Trace; +using OpenTelemetry.Resources; + public class Startup { public Startup(IConfiguration configuration) @@ -11,6 +14,19 @@ public class Startup public virtual IServiceProvider ConfigureServices(IServiceCollection services) { + services.AddOpenTelemetryTracing( builder => builder + .SetResourceBuilder( + ResourceBuilder.CreateDefault() + .AddService(Configuration.GetValue("Otlp:ServiceName")) + ) + .AddHttpClientInstrumentation() + .AddAspNetCoreInstrumentation() + .AddOtlpExporter( otlpOptions => { + var url = Configuration.GetValue("Otlp:Endpoint"); + otlpOptions.Endpoint = new Uri( url ); + }) + ); + services .AddGrpc(options => { diff --git a/src/Services/Ordering/Ordering.API/appsettings.json b/src/Services/Ordering/Ordering.API/appsettings.json index 9c7f07ec1..a405ecd79 100644 --- a/src/Services/Ordering/Ordering.API/appsettings.json +++ b/src/Services/Ordering/Ordering.API/appsettings.json @@ -28,5 +28,9 @@ "Name": "eshop", "ClientId": "your-client-id", "ClientSecret": "your-client-secret" + }, + "Otlp": { + "ServiceName": "Ordering API", + "Endpoint": "http://otel-collector:4317" } } diff --git a/src/Web/WebMVC/Startup.cs b/src/Web/WebMVC/Startup.cs index b42b0bc28..83685a781 100644 --- a/src/Web/WebMVC/Startup.cs +++ b/src/Web/WebMVC/Startup.cs @@ -1,5 +1,8 @@ namespace Microsoft.eShopOnContainers.WebMVC; +using OpenTelemetry.Trace; +using OpenTelemetry.Resources; + public class Startup { public Startup(IConfiguration configuration) @@ -12,6 +15,19 @@ public class Startup // This method gets called by the runtime. Use this method to add services to the IoC container. public void ConfigureServices(IServiceCollection services) { + services.AddOpenTelemetryTracing( builder => builder + .SetResourceBuilder( + ResourceBuilder.CreateDefault() + .AddService(Configuration.GetValue("Otlp:ServiceName")) + ) + .AddHttpClientInstrumentation() + .AddAspNetCoreInstrumentation() + .AddOtlpExporter( otlpOptions => { + var url = Configuration.GetValue("Otlp:Endpoint"); + otlpOptions.Endpoint = new Uri( url ); + }) + ); + services.AddControllersWithViews() .Services .AddAppInsight(Configuration) diff --git a/src/Web/WebMVC/WebMVC.csproj b/src/Web/WebMVC/WebMVC.csproj index db6ad97db..0721f177a 100644 --- a/src/Web/WebMVC/WebMVC.csproj +++ b/src/Web/WebMVC/WebMVC.csproj @@ -21,6 +21,12 @@ + + + + + + diff --git a/src/Web/WebMVC/appsettings.json b/src/Web/WebMVC/appsettings.json index 7c169c27c..715cc6cf9 100644 --- a/src/Web/WebMVC/appsettings.json +++ b/src/Web/WebMVC/appsettings.json @@ -21,5 +21,9 @@ }, "HttpClientRetryCount": 8, "HttpClientExceptionsAllowedBeforeBreaking": 7, - "SessionCookieLifetimeMinutes": 60 + "SessionCookieLifetimeMinutes": 60, + "Otlp": { + "ServiceName": "Catelog API", + "Endpoint": "http://otel-collector:4317" + } } diff --git a/src/docker-compose.otel.yml b/src/docker-compose.otel.yml new file mode 100644 index 000000000..d3419a982 --- /dev/null +++ b/src/docker-compose.otel.yml @@ -0,0 +1,19 @@ +version: '3.4' + +services: + # Collector + otel-collector: + image: otel/opentelemetry-collector-contrib + command: ["--config=/etc/otel-collector-config.yaml"] + volumes: + - ../deploy/otel/otel-collector-config.yaml:/etc/otel-collector-config.yaml + ports: + - "4317:4317" # OTLP gRPC receiver + - "4318:4318" # OTLP http receiver + + zipkin: + image: openzipkin/zipkin + ports: + - "9411:9411" + +