From 12f409b77c8513ccbda5416c74135c828e4923f4 Mon Sep 17 00:00:00 2001 From: Umer Farooq Date: Sun, 14 Jul 2024 19:31:02 +0100 Subject: [PATCH] Made telemetry optional for local development --- src/Inshapardaz.Api/Inshapardaz.Api.sln | 25 -------- src/Inshapardaz.Api/Program.cs | 62 ++++++++++--------- .../WhenGettingBookPagesInMiddle.cs | 2 +- tests/Inshapardaz.Api.Tests/appsettings.json | 2 +- 4 files changed, 36 insertions(+), 55 deletions(-) delete mode 100644 src/Inshapardaz.Api/Inshapardaz.Api.sln diff --git a/src/Inshapardaz.Api/Inshapardaz.Api.sln b/src/Inshapardaz.Api/Inshapardaz.Api.sln deleted file mode 100644 index d6b8b3e0..00000000 --- a/src/Inshapardaz.Api/Inshapardaz.Api.sln +++ /dev/null @@ -1,25 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.9.34723.18 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Inshapardaz.Api", "Inshapardaz.Api.csproj", "{C9B37DB6-DEF4-4733-9AD9-A6804CCDBB6A}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {C9B37DB6-DEF4-4733-9AD9-A6804CCDBB6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C9B37DB6-DEF4-4733-9AD9-A6804CCDBB6A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C9B37DB6-DEF4-4733-9AD9-A6804CCDBB6A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C9B37DB6-DEF4-4733-9AD9-A6804CCDBB6A}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {C1F4BC26-7E25-41B8-8049-9EA212969035} - EndGlobalSection -EndGlobal diff --git a/src/Inshapardaz.Api/Program.cs b/src/Inshapardaz.Api/Program.cs index 6d4c6a0d..1feaa996 100644 --- a/src/Inshapardaz.Api/Program.cs +++ b/src/Inshapardaz.Api/Program.cs @@ -36,34 +36,37 @@ string tracingOtlpEndpoint = builder.Configuration["OLTP_ENDPOINT_URL"]; -builder.Logging.AddOpenTelemetry(options => options - .SetResourceBuilder(ResourceBuilder.CreateDefault() +if (!builder.Environment.IsDevelopment()) +{ + builder.Logging.AddOpenTelemetry(options => options + .SetResourceBuilder(ResourceBuilder.CreateDefault() .AddService(serviceName)) - .AddConsoleExporter()); - -builder.Services.AddOpenTelemetry() - .ConfigureResource(resource => resource.AddService(serviceName)) - .WithMetrics(metrics => metrics - .AddAspNetCoreInstrumentation() - .AddMeter("Microsoft.AspnetCore.Hosting") - .AddMeter("Microsoft.AspnetCore.Server.Kestrel") - .AddPrometheusExporter() - .AddConsoleExporter()) - .WithTracing(tracing => - { - tracing - .AddAspNetCoreInstrumentation() - .AddHttpClientInstrumentation(); - if (tracingOtlpEndpoint != null) - { - tracing.AddOtlpExporter(opt => opt.Endpoint = new Uri(tracingOtlpEndpoint)); - } - else - { - tracing.AddConsoleExporter(); - } - }); - + .AddConsoleExporter()); + + builder.Services.AddOpenTelemetry() + .ConfigureResource(resource => resource.AddService(serviceName)) + .WithMetrics(metrics => metrics + .AddAspNetCoreInstrumentation() + .AddMeter("Microsoft.AspnetCore.Hosting") + .AddMeter("Microsoft.AspnetCore.Server.Kestrel") + .AddPrometheusExporter() + .AddConsoleExporter()) + .WithTracing(tracing => + { + tracing + .AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation(); + if (tracingOtlpEndpoint != null) + { + tracing.AddOtlpExporter(opt => opt.Endpoint = new Uri(tracingOtlpEndpoint)); + } + else + { + tracing.AddConsoleExporter(); + } + }); +} + //===================================================================== // Add services to the container. @@ -183,6 +186,10 @@ app.UseSwagger(); app.UseSwaggerUI(); } +else +{ + app.MapPrometheusScrapingEndpoint(); +} app.UseCors(x => x .SetIsOriginAllowed(origin => true) @@ -192,7 +199,6 @@ .WithExposedHeaders(HeaderNames.Location, HeaderNames.ContentDisposition, HeaderNames.ContentType)); app.UseHttpsRedirection(); -app.MapPrometheusScrapingEndpoint(); app.UseAuthorization(); app.UseRequestLogging(); diff --git a/tests/Inshapardaz.Api.Tests/Library/BookPage/GetBookPages/WhenGettingBookPagesInMiddle.cs b/tests/Inshapardaz.Api.Tests/Library/BookPage/GetBookPages/WhenGettingBookPagesInMiddle.cs index 9499dafb..41bfedd8 100644 --- a/tests/Inshapardaz.Api.Tests/Library/BookPage/GetBookPages/WhenGettingBookPagesInMiddle.cs +++ b/tests/Inshapardaz.Api.Tests/Library/BookPage/GetBookPages/WhenGettingBookPagesInMiddle.cs @@ -69,7 +69,7 @@ public void ShouldHavePreviousLink() } [Test] - public void ShouldReturExpectedBookPages() + public void ShouldReturnExpectedBookPages() { var expectedItems = BookBuilder.GetPages(_book.Id).OrderBy(p => p.SequenceNumber).Skip(10).Take(10); foreach (var item in expectedItems) diff --git a/tests/Inshapardaz.Api.Tests/appsettings.json b/tests/Inshapardaz.Api.Tests/appsettings.json index 2598dde5..0e4306fe 100644 --- a/tests/Inshapardaz.Api.Tests/appsettings.json +++ b/tests/Inshapardaz.Api.Tests/appsettings.json @@ -28,7 +28,7 @@ }, "Database": { "DatabaseConnectionType": "MySql", - "ConnectionString": "Server=127.0.0.1;Database=nawishta_test;Uid=root;Pwd=Passw0rd;SslMode=none;" + "ConnectionString": "Server=127.0.0.1;Database=nawishta_test;Uid=root;Pwd=Passw0rd;" }, "Storage": { "FileStoreType": "Database"