From 43f448c61f8ea993603027efad7b9a59dae47f1b Mon Sep 17 00:00:00 2001 From: Juliano Costa Date: Sat, 14 Dec 2024 00:01:08 +0100 Subject: [PATCH] [cartservice] - Add Exemplars to Cart Service (#1830) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * WIP-Add Exemplars to Cart Service * Bump AspNetCore instrumentation Co-authored-by: Piotr Kiełkowicz * Add source and meter * Remove logs and add boundaries * Add HistogramBucketBoundaries * Drop trailing space * Apply suggestions from code review Co-authored-by: Piotr Kiełkowicz * changelog * indent --------- Co-authored-by: Piotr Kiełkowicz --- CHANGELOG.md | 9 +++--- src/cartservice/src/Program.cs | 3 ++ src/cartservice/src/cartservice.csproj | 28 +++++++++---------- .../src/cartstore/ValkeyCartStore.cs | 26 +++++++++++++++++ src/cartservice/src/services/CartService.cs | 7 ++--- .../tests/cartservice.tests.csproj | 10 +++---- 6 files changed, 56 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c9a41b936..d23ee07c0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,10 +7,6 @@ the release. ## Unreleased -* [flagd] Update `paymentServiceFailure` to use a list of variants. -* [paymentservice] Add loyalty level attributes to spans. - Added `service.name` to logs. - ([#1815](https://github.com/open-telemetry/opentelemetry-demo/pull/1815)) * [grafana] Update grafana to 11.3.0 ([#1764](https://github.com/open-telemetry/opentelemetry-demo/pull/1764)) * [chore] Move build args to .env file @@ -29,8 +25,13 @@ the release. ([#1794](https://github.com/open-telemetry/opentelemetry-demo/pull/1784)) * [paymentservice] Add nodejs instrumentation for runtime metrics ([#1797](https://github.com/open-telemetry/opentelemetry-demo/pull/1797)) +* [flagd and paymentservice] Update `paymentServiceFailure` to use a list of + variants and add loyalty level attributes to spans. Added `service.name` to logs. + ([#1815](https://github.com/open-telemetry/opentelemetry-demo/pull/1815)) * [accounting] rename accountingservice to accounting ([#1827](https://github.com/open-telemetry/opentelemetry-demo/pull/1827)) +* [cartservice] - Add Exemplars to Cart Service + ([#1830](https://github.com/open-telemetry/opentelemetry-demo/pull/1830)) ## 1.12.0 diff --git a/src/cartservice/src/Program.cs b/src/cartservice/src/Program.cs index e4e6a28e11..6c52229ce4 100644 --- a/src/cartservice/src/Program.cs +++ b/src/cartservice/src/Program.cs @@ -61,6 +61,7 @@ builder.Services.AddOpenTelemetry() .ConfigureResource(appResourceBuilder) .WithTracing(tracerBuilder => tracerBuilder + .AddSource("OpenTelemetry.Demo.Cart") .AddRedisInstrumentation( options => options.SetVerboseDatabaseStatements = true) .AddAspNetCoreInstrumentation() @@ -68,9 +69,11 @@ .AddHttpClientInstrumentation() .AddOtlpExporter()) .WithMetrics(meterBuilder => meterBuilder + .AddMeter("OpenTelemetry.Demo.Cart") .AddProcessInstrumentation() .AddRuntimeInstrumentation() .AddAspNetCoreInstrumentation() + .SetExemplarFilter(ExemplarFilterType.TraceBased) .AddOtlpExporter()); OpenFeature.Api.Instance.AddHooks(new TracingHook()); builder.Services.AddGrpc(); diff --git a/src/cartservice/src/cartservice.csproj b/src/cartservice/src/cartservice.csproj index 5b5e31373f..d62a131f7f 100644 --- a/src/cartservice/src/cartservice.csproj +++ b/src/cartservice/src/cartservice.csproj @@ -15,22 +15,22 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + - + diff --git a/src/cartservice/src/cartstore/ValkeyCartStore.cs b/src/cartservice/src/cartstore/ValkeyCartStore.cs index aa79afedea..1a16dcc6c0 100644 --- a/src/cartservice/src/cartstore/ValkeyCartStore.cs +++ b/src/cartservice/src/cartstore/ValkeyCartStore.cs @@ -7,6 +7,8 @@ using StackExchange.Redis; using Google.Protobuf; using Microsoft.Extensions.Logging; +using System.Diagnostics.Metrics; +using System.Diagnostics; namespace cartservice.cartstore; @@ -23,6 +25,20 @@ public class ValkeyCartStore : ICartStore private readonly byte[] _emptyCartBytes; private readonly string _connectionString; + private static readonly ActivitySource CartActivitySource = new("OpenTelemetry.Demo.Cart"); + private static readonly Meter CartMeter = new Meter("OpenTelemetry.Demo.Cart"); + private static readonly Histogram addItemHistogram = CartMeter.CreateHistogram( + "app.cart.add_item.latency", + advice: new InstrumentAdvice + { + HistogramBucketBoundaries = [ 500000, 600000, 700000, 800000, 900000, 1000000, 1100000 ] + }); + private static readonly Histogram getCartHistogram = CartMeter.CreateHistogram( + "app.cart.get_cart.latency", + advice: new InstrumentAdvice + { + HistogramBucketBoundaries = [ 300000, 400000, 500000, 600000, 700000, 800000, 900000 ] + }); private readonly ConfigurationOptions _redisConnectionOptions; public ValkeyCartStore(ILogger logger, string valkeyAddress) @@ -105,6 +121,7 @@ private void EnsureRedisConnected() public async Task AddItemAsync(string userId, string productId, int quantity) { + var stopwatch = Stopwatch.StartNew(); _logger.LogInformation("AddItemAsync called with userId={userId}, productId={productId}, quantity={quantity}", userId, productId, quantity); try @@ -146,6 +163,10 @@ public async Task AddItemAsync(string userId, string productId, int quantity) { throw new RpcException(new Status(StatusCode.FailedPrecondition, $"Can't access cart storage. {ex}")); } + finally + { + addItemHistogram.Record(stopwatch.ElapsedTicks); + } } public async Task EmptyCartAsync(string userId) @@ -169,6 +190,7 @@ public async Task EmptyCartAsync(string userId) public async Task GetCartAsync(string userId) { + var stopwatch = Stopwatch.StartNew(); _logger.LogInformation("GetCartAsync called with userId={userId}", userId); try @@ -192,6 +214,10 @@ public async Task EmptyCartAsync(string userId) { throw new RpcException(new Status(StatusCode.FailedPrecondition, $"Can't access cart storage. {ex}")); } + finally + { + getCartHistogram.Record(stopwatch.ElapsedTicks); + } } public bool Ping() diff --git a/src/cartservice/src/services/CartService.cs b/src/cartservice/src/services/CartService.cs index d071c37080..093b3bda2d 100644 --- a/src/cartservice/src/services/CartService.cs +++ b/src/cartservice/src/services/CartService.cs @@ -4,7 +4,6 @@ using System.Threading.Tasks; using System; using Grpc.Core; -using OpenTelemetry.Trace; using cartservice.cartstore; using OpenFeature; using Oteldemo; @@ -41,7 +40,7 @@ public override async Task AddItem(AddItemRequest request, ServerCallCont } catch (RpcException ex) { - activity?.RecordException(ex); + activity?.AddException(ex); activity?.SetStatus(ActivityStatusCode.Error, ex.Message); throw; } @@ -67,7 +66,7 @@ public override async Task GetCart(GetCartRequest request, ServerCallConte } catch (RpcException ex) { - activity?.RecordException(ex); + activity?.AddException(ex); activity?.SetStatus(ActivityStatusCode.Error, ex.Message); throw; } @@ -92,7 +91,7 @@ public override async Task EmptyCart(EmptyCartRequest request, ServerCall } catch (RpcException ex) { - Activity.Current?.RecordException(ex); + Activity.Current?.AddException(ex); Activity.Current?.SetStatus(ActivityStatusCode.Error, ex.Message); throw; } diff --git a/src/cartservice/tests/cartservice.tests.csproj b/src/cartservice/tests/cartservice.tests.csproj index 4c7a083d85..fd079fc30f 100644 --- a/src/cartservice/tests/cartservice.tests.csproj +++ b/src/cartservice/tests/cartservice.tests.csproj @@ -5,11 +5,11 @@ - - - - - + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive