Skip to content

Commit

Permalink
[cartservice] - Add Exemplars to Cart Service (#1830)
Browse files Browse the repository at this point in the history
* WIP-Add Exemplars to Cart Service

* Bump AspNetCore instrumentation

Co-authored-by: Piotr Kiełkowicz <[email protected]>

* 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 <[email protected]>

* changelog

* indent

---------

Co-authored-by: Piotr Kiełkowicz <[email protected]>
  • Loading branch information
julianocosta89 and Kielek authored Dec 13, 2024
1 parent 7b2c0ad commit 43f448c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 27 deletions.
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
3 changes: 3 additions & 0 deletions src/cartservice/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,19 @@
builder.Services.AddOpenTelemetry()
.ConfigureResource(appResourceBuilder)
.WithTracing(tracerBuilder => tracerBuilder
.AddSource("OpenTelemetry.Demo.Cart")
.AddRedisInstrumentation(
options => options.SetVerboseDatabaseStatements = true)
.AddAspNetCoreInstrumentation()
.AddGrpcClientInstrumentation()
.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();
Expand Down
28 changes: 14 additions & 14 deletions src/cartservice/src/cartservice.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.66.0" />
<PackageReference Include="Grpc.AspNetCore.HealthChecks" Version="2.66.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.9.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="0.5.0-beta.7" />
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.9.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Resources.Container" Version="1.0.0-beta.9" />
<PackageReference Include="OpenTelemetry.Resources.Host" Version="0.1.0-beta.3" />
<PackageReference Include="StackExchange.Redis" Version="2.8.16" />
<PackageReference Include="Grpc.AspNetCore" Version="2.67.0" />
<PackageReference Include="Grpc.AspNetCore.HealthChecks" Version="2.67.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.10.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.10.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="1.10.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.10.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Resources.Container" Version="1.10.0-beta.1" />
<PackageReference Include="OpenTelemetry.Resources.Host" Version="1.10.0-beta.1" />
<PackageReference Include="StackExchange.Redis" Version="2.8.22" />
<PackageReference Include="OpenFeature.Contrib.Providers.Flagd" Version="0.3.0" />
<PackageReference Include="OpenFeature.Contrib.Hooks.Otel" Version="0.2.0" />
<PackageReference Include="OpenFeature" Version="2.0.0" />
<PackageReference Include="OpenFeature" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
26 changes: 26 additions & 0 deletions src/cartservice/src/cartstore/ValkeyCartStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using StackExchange.Redis;
using Google.Protobuf;
using Microsoft.Extensions.Logging;
using System.Diagnostics.Metrics;
using System.Diagnostics;

namespace cartservice.cartstore;

Expand All @@ -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<long> addItemHistogram = CartMeter.CreateHistogram<long>(
"app.cart.add_item.latency",
advice: new InstrumentAdvice<long>
{
HistogramBucketBoundaries = [ 500000, 600000, 700000, 800000, 900000, 1000000, 1100000 ]
});
private static readonly Histogram<long> getCartHistogram = CartMeter.CreateHistogram<long>(
"app.cart.get_cart.latency",
advice: new InstrumentAdvice<long>
{
HistogramBucketBoundaries = [ 300000, 400000, 500000, 600000, 700000, 800000, 900000 ]
});
private readonly ConfigurationOptions _redisConnectionOptions;

public ValkeyCartStore(ILogger<ValkeyCartStore> logger, string valkeyAddress)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -169,6 +190,7 @@ public async Task EmptyCartAsync(string userId)

public async Task<Oteldemo.Cart> GetCartAsync(string userId)
{
var stopwatch = Stopwatch.StartNew();
_logger.LogInformation("GetCartAsync called with userId={userId}", userId);

try
Expand All @@ -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()
Expand Down
7 changes: 3 additions & 4 deletions src/cartservice/src/services/CartService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading.Tasks;
using System;
using Grpc.Core;
using OpenTelemetry.Trace;
using cartservice.cartstore;
using OpenFeature;
using Oteldemo;
Expand Down Expand Up @@ -41,7 +40,7 @@ public override async Task<Empty> AddItem(AddItemRequest request, ServerCallCont
}
catch (RpcException ex)
{
activity?.RecordException(ex);
activity?.AddException(ex);
activity?.SetStatus(ActivityStatusCode.Error, ex.Message);
throw;
}
Expand All @@ -67,7 +66,7 @@ public override async Task<Cart> GetCart(GetCartRequest request, ServerCallConte
}
catch (RpcException ex)
{
activity?.RecordException(ex);
activity?.AddException(ex);
activity?.SetStatus(ActivityStatusCode.Error, ex.Message);
throw;
}
Expand All @@ -92,7 +91,7 @@ public override async Task<Empty> EmptyCart(EmptyCartRequest request, ServerCall
}
catch (RpcException ex)
{
Activity.Current?.RecordException(ex);
Activity.Current?.AddException(ex);
Activity.Current?.SetStatus(ActivityStatusCode.Error, ex.Message);
throw;
}
Expand Down
10 changes: 5 additions & 5 deletions src/cartservice/tests/cartservice.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Grpc.Net.Client" Version="2.63.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="8.0.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PackageReference Include="Grpc.Net.Client" Version="2.67.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="8.0.11" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit 43f448c

Please sign in to comment.