diff --git a/IntegrationTest/BikeService.cs b/IntegrationTest/BikeService.cs index 009180e01..f4860340e 100644 --- a/IntegrationTest/BikeService.cs +++ b/IntegrationTest/BikeService.cs @@ -9,8 +9,6 @@ public BikeService(OrderService orderService) _orderService = orderService; } - public void Order(int searchRadius) - { + public void Order(int searchRadius) => _orderService.FindNearestVehicle(searchRadius, "bike"); - } -} \ No newline at end of file +} diff --git a/IntegrationTest/CarService.cs b/IntegrationTest/CarService.cs index 885b534ee..14752db0e 100644 --- a/IntegrationTest/CarService.cs +++ b/IntegrationTest/CarService.cs @@ -9,8 +9,6 @@ public CarService(OrderService orderService) _orderService = orderService; } - public void Order(int searchRadius) - { + public void Order(int searchRadius) => _orderService.FindNearestVehicle(searchRadius, "car"); - } -} \ No newline at end of file +} diff --git a/IntegrationTest/Dockerfile.load-generator b/IntegrationTest/Dockerfile.load-generator index 5c55f3d27..dcbfbe7f2 100644 --- a/IntegrationTest/Dockerfile.load-generator +++ b/IntegrationTest/Dockerfile.load-generator @@ -1,4 +1,4 @@ -FROM python:3.9 +FROM python:3.12 RUN pip3 install requests @@ -7,4 +7,3 @@ COPY load-generator.py ./load-generator.py ENV PYTHONUNBUFFERED=1 CMD [ "python", "load-generator.py" ] - diff --git a/IntegrationTest/OrderService.cs b/IntegrationTest/OrderService.cs index 2332330ff..68e482370 100644 --- a/IntegrationTest/OrderService.cs +++ b/IntegrationTest/OrderService.cs @@ -1,4 +1,4 @@ -using System; +using Pyroscope; namespace Example; @@ -8,50 +8,54 @@ public void FindNearestVehicle(long searchRadius, string vehicle) { lock (_lock) { - var labels = Pyroscope.LabelSet.Empty.BuildUpon() + var labels = LabelSet.Empty.BuildUpon() .Add("vehicle", vehicle) .Build(); - Pyroscope.LabelsWrapper.Do(labels, () => + + LabelsWrapper.Do(labels, static (state) => { - for (long i = 0; i < searchRadius * 1000000000; i++) + for (long i = 0; i < state.searchRadius * 1_000_000_000; i++) { } - if (vehicle.Equals("car")) + if (string.Equals("car", state.vehicle)) { - CheckDriverAvailability(labels, searchRadius); + CheckDriverAvailability(state.labels, state.searchRadius); } - }); + }, + (labels, searchRadius, vehicle)); } } private readonly object _lock = new(); - private static void CheckDriverAvailability(Pyroscope.LabelSet ctx, long searchRadius) + private static void CheckDriverAvailability(LabelSet labels, long searchRadius) { - var region = System.Environment.GetEnvironmentVariable("REGION") ?? "unknown_region"; - ctx = ctx.BuildUpon() + var region = Environment.GetEnvironmentVariable("REGION") ?? "unknown_region"; + labels = labels.BuildUpon() .Add("driver_region", region) .Build(); - Pyroscope.LabelsWrapper.Do(ctx, () => + + LabelsWrapper.Do(labels, static (state) => { - for (long i = 0; i < searchRadius * 1000000000; i++) + for (long i = 0; i < state.searchRadius * 1_000_000_000; i++) { } - var now = DateTime.Now.Minute % 2 == 0; var forceMutexLock = DateTime.Now.Minute % 2 == 0; - if ("eu-north".Equals(region) && forceMutexLock) + + if (string.Equals("eu-north", state.region) && forceMutexLock) { - MutexLock(searchRadius); + MutexLock(state.searchRadius); } - }); + }, + (region, searchRadius)); } private static void MutexLock(long searchRadius) { - for (long i = 0; i < 30 * searchRadius * 1000000000; i++) + for (long i = 0; i < 30 * searchRadius * 1_000_000_000; i++) { } } -} \ No newline at end of file +} diff --git a/IntegrationTest/Program.cs b/IntegrationTest/Program.cs index 3a74a0aa2..4f9e866de 100644 --- a/IntegrationTest/Program.cs +++ b/IntegrationTest/Program.cs @@ -1,45 +1,30 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Threading; -using System.Collections; +using Example; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.DependencyInjection; +var builder = WebApplication.CreateBuilder(args); -namespace Example; +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); -public static class Program -{ - private static readonly List Files = new(); - public static void Main(string[] args) - { - var orderService = new OrderService(); - var bikeService = new BikeService(orderService); - var scooterService = new ScooterService(orderService); - var carService = new CarService(orderService); +var app = builder.Build(); - var builder = WebApplication.CreateBuilder(args); - var app = builder.Build(); +app.MapGet("/bike", (BikeService service) => +{ + service.Order(1); + return "Bike ordered"; +}); - app.MapGet("/bike", () => - { - bikeService.Order(1); - return "Bike ordered"; - }); - app.MapGet("/scooter", () => - { - scooterService.Order(2); - return "Scooter ordered"; - }); +app.MapGet("/scooter", (ScooterService service) => +{ + service.Order(2); + return "Scooter ordered"; +}); - app.MapGet("/car", () => - { - carService.Order(3); - return "Car ordered"; - }); +app.MapGet("/car", (CarService service) => +{ + service.Order(3); + return "Car ordered"; +}); - app.Run(); - } -} +app.Run(); diff --git a/IntegrationTest/Properties/launchSettings.json b/IntegrationTest/Properties/launchSettings.json new file mode 100644 index 000000000..ec994b8b6 --- /dev/null +++ b/IntegrationTest/Properties/launchSettings.json @@ -0,0 +1,12 @@ +{ + "profiles": { + "Rideshare": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "https://localhost:56129;http://localhost:56130" + } + } +} diff --git a/IntegrationTest/Rideshare.csproj b/IntegrationTest/Rideshare.csproj index 79ab4ca2e..aea0860da 100644 --- a/IntegrationTest/Rideshare.csproj +++ b/IntegrationTest/Rideshare.csproj @@ -1,10 +1,14 @@ - net6.0 + net8.0;net6.0 rideshare Exe rideshare + enable + latest enable + + false diff --git a/IntegrationTest/ScooterService.cs b/IntegrationTest/ScooterService.cs index 492040965..6bf838643 100644 --- a/IntegrationTest/ScooterService.cs +++ b/IntegrationTest/ScooterService.cs @@ -1,5 +1,3 @@ -using System.Diagnostics; - namespace Example; internal class ScooterService @@ -13,9 +11,10 @@ public ScooterService(OrderService orderService) public void Order(int searchRadius) { - for (long i = 0; i < 2000000000; i++) + for (long i = 0; i < 2_000_000_000; i++) { } + OrderInternal(searchRadius); DoSomeOtherWork(); } @@ -25,9 +24,9 @@ private void OrderInternal(int searchRadius) _orderService.FindNearestVehicle(searchRadius, "scooter"); } - private void DoSomeOtherWork() + private static void DoSomeOtherWork() { - for (long i = 0; i < 1000000000; i++) + for (long i = 0; i < 1_000_000_000; i++) { } } diff --git a/Pyroscope/Pyroscope.OpenTracing/PyroscopeSpanBuilder.cs b/Pyroscope/Pyroscope.OpenTracing/PyroscopeSpanBuilder.cs index 094b9aaa9..3034aa2da 100644 --- a/Pyroscope/Pyroscope.OpenTracing/PyroscopeSpanBuilder.cs +++ b/Pyroscope/Pyroscope.OpenTracing/PyroscopeSpanBuilder.cs @@ -8,7 +8,7 @@ public class PyroscopeSpanBuilder : ISpanBuilder private const string ProfileIdSpanTagKey = "pyroscope.profile.id"; private readonly ISpanBuilder _delegate; - private ISpanContext? _parent; + private readonly ISpanContext? _parent; internal PyroscopeSpanBuilder(ISpanBuilder spanBuilder, ISpanContext? parent) { @@ -135,7 +135,7 @@ public ISpanBuilder IgnoreActiveSpan() return this; } - class PyroscopeScope : IScope + private sealed class PyroscopeScope : IScope { private readonly IScope _delegate; private readonly ISpanContext? _parent; diff --git a/Pyroscope/Pyroscope.sln b/Pyroscope/Pyroscope.sln index de862a6f7..24a48494b 100644 --- a/Pyroscope/Pyroscope.sln +++ b/Pyroscope/Pyroscope.sln @@ -1,7 +1,16 @@  Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36429.23 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pyroscope", "Pyroscope\Pyroscope.csproj", "{7DA9EFD3-9B8D-4DF4-A1D1-108D43B53BBD}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pyroscope.OpenTelemetry", "Pyroscope.OpenTelemetry\Pyroscope.OpenTelemetry.csproj", "{AEB842E5-386E-275E-3A1B-00CD047918A4}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pyroscope.OpenTracing", "Pyroscope.OpenTracing\Pyroscope.OpenTracing.csproj", "{AE3C5FD4-119D-3AAD-DC9E-C903F2D8EFDF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rideshare", "..\IntegrationTest\Rideshare.csproj", "{230842A8-C534-4ECF-249E-0AD71F4126E2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -12,5 +21,23 @@ Global {7DA9EFD3-9B8D-4DF4-A1D1-108D43B53BBD}.Debug|Any CPU.Build.0 = Debug|Any CPU {7DA9EFD3-9B8D-4DF4-A1D1-108D43B53BBD}.Release|Any CPU.ActiveCfg = Release|Any CPU {7DA9EFD3-9B8D-4DF4-A1D1-108D43B53BBD}.Release|Any CPU.Build.0 = Release|Any CPU + {AEB842E5-386E-275E-3A1B-00CD047918A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AEB842E5-386E-275E-3A1B-00CD047918A4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AEB842E5-386E-275E-3A1B-00CD047918A4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AEB842E5-386E-275E-3A1B-00CD047918A4}.Release|Any CPU.Build.0 = Release|Any CPU + {AE3C5FD4-119D-3AAD-DC9E-C903F2D8EFDF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AE3C5FD4-119D-3AAD-DC9E-C903F2D8EFDF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AE3C5FD4-119D-3AAD-DC9E-C903F2D8EFDF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AE3C5FD4-119D-3AAD-DC9E-C903F2D8EFDF}.Release|Any CPU.Build.0 = Release|Any CPU + {230842A8-C534-4ECF-249E-0AD71F4126E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {230842A8-C534-4ECF-249E-0AD71F4126E2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {230842A8-C534-4ECF-249E-0AD71F4126E2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {230842A8-C534-4ECF-249E-0AD71F4126E2}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F1C82690-DF18-45C2-AFA5-BC1083ABCD77} EndGlobalSection EndGlobal diff --git a/Pyroscope/Pyroscope/LabelsWrapper.cs b/Pyroscope/Pyroscope/LabelsWrapper.cs index 436404dd9..10ae5328e 100644 --- a/Pyroscope/Pyroscope/LabelsWrapper.cs +++ b/Pyroscope/Pyroscope/LabelsWrapper.cs @@ -11,14 +11,32 @@ public static void Do(LabelSet labels, Action a) } finally { - if (labels.Previous == null) - { - Profiler.Instance.ClearDynamicTags(); - } - else - { - labels.Previous.Activate(); - } + ResetLabels(labels); + } + } + + public static void Do(LabelSet labels, Action a, T state) + { + try + { + labels.Activate(); + a.Invoke(state); + } + finally + { + ResetLabels(labels); + } + } + + private static void ResetLabels(LabelSet labels) + { + if (labels.Previous == null) + { + Profiler.Instance.ClearDynamicTags(); + } + else + { + labels.Previous.Activate(); } } } diff --git a/itest.Dockerfile b/itest.Dockerfile index 7533bcd56..3dc172d11 100644 --- a/itest.Dockerfile +++ b/itest.Dockerfile @@ -17,7 +17,7 @@ COPY Pyroscope/package-logo.png ./Pyroscope/package-logo.png COPY Pyroscope/Pyroscope ./Pyroscope/Pyroscope # Set the target framework to SDK_VERSION -RUN sed -i -E 's|.*|net'$SDK_VERSION'|' ./app/Rideshare.csproj +RUN sed -i -E 's|.*|net'$SDK_VERSION'|' ./app/Rideshare.csproj WORKDIR /dotnet/app