Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cartservice] - Add Exemplars to Cart Service #1830

Merged
merged 11 commits into from
Dec 13, 2024
9 changes: 9 additions & 0 deletions src/cartservice/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,25 @@
builder.Services.AddOpenTelemetry()
.ConfigureResource(appResourceBuilder)
.WithTracing(tracerBuilder => tracerBuilder
.AddSource("OpenTelemetry.Demo.Cart.Source")
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved
.AddRedisInstrumentation(
options => options.SetVerboseDatabaseStatements = true)
.AddAspNetCoreInstrumentation()
.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation()
.AddOtlpExporter())
.WithMetrics(meterBuilder => meterBuilder
.AddMeter("OpenTelemetry.Demo.Cart.Meter")
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved
.AddView(
instrumentName: "CartLatency",
new ExplicitBucketHistogramConfiguration {
Boundaries = new double[] {300000, 400000, 500000, 600000, 700000, 800000, 900000}
}
)
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved
.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
17 changes: 17 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,11 @@ public class ValkeyCartStore : ICartStore
private readonly byte[] _emptyCartBytes;
private readonly string _connectionString;

private static readonly ActivitySource CartActivitySource = new("OpenTelemetry.Demo.Cart.Source");
private static readonly Meter CartMeter = new Meter("OpenTelemetry.Demo.Cart.Meter");
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved
private static readonly Histogram<long> addItemHistogram = CartMeter.CreateHistogram<long>("app.cart.add_item.latency");
private static readonly Histogram<long> getCartHistogram = CartMeter.CreateHistogram<long>("app.cart.get_cart.latency");

private readonly ConfigurationOptions _redisConnectionOptions;

public ValkeyCartStore(ILogger<ValkeyCartStore> logger, string valkeyAddress)
Expand Down Expand Up @@ -105,6 +112,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 +154,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 +181,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 +205,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
Loading