Skip to content

Commit

Permalink
Added feature flag for database calls
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjt committed Sep 5, 2024
1 parent b113ab5 commit 7467d0f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/cartservice/src/cartservice.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
<PackageReference Include="OpenTelemetry.Resources.Container" Version="1.0.0-beta.8" />
<PackageReference Include="OpenTelemetry.Resources.Host" Version="0.1.0-beta.2" />
<PackageReference Include="StackExchange.Redis" Version="2.7.33" />
<PackageReference Include="OpenFeature.Contrib.Providers.Flagd" Version="0.1.9" />
<PackageReference Include="OpenFeature.Contrib.Hooks.Otel" Version="0.1.4" />
<PackageReference Include="OpenFeature" Version="1.5.1" />
<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" />
</ItemGroup>

<ItemGroup>
Expand Down
32 changes: 25 additions & 7 deletions src/cartservice/src/services/CartService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using cartservice.cartstore;
using OpenFeature;
using Oteldemo;
using OpenFeature.Model;

namespace cartservice.services;

Expand Down Expand Up @@ -47,15 +48,32 @@ public override async Task<Cart> GetCart(GetCartRequest request, ServerCallConte
var cart = await _cartStore.GetCartAsync(request.UserId);
var totalCart = 0;
activity?.SetTag("app.cart.unique_items.count", cart.Items.Count);
foreach (var item in cart.Items)

_featureFlagHelper.SetContext(
EvaluationContext.Builder()
.Set("cart.unique_items.count", cart.Items.Count)
.Set("user.id", request.UserId)
.Build());

var shouldDoDatabaseCall = await _featureFlagHelper.GetBooleanValueAsync("cartservice.add-db-call", false);
if (!shouldDoDatabaseCall)
{
using var dbActivity = ActivitySource.StartActivity("SELECT * FROM products WHERE id = @id", ActivityKind.Client);
dbActivity?.SetTag("app.product.id", item.ProductId);
dbActivity?.SetTag("db.statement", "SELECT * FROM products WHERE id = @id");
dbActivity?.SetTag("db.type", "sql");
if (cart.Items.Count > 6)
dbActivity?.SetTag("db.statement", "SELECT * FROM products WHERE id IN @ids");
dbActivity?.SetTag("db.type", "sql");
}
foreach (var item in cart.Items)
{
if (shouldDoDatabaseCall)
{
await Task.Delay(random.Next(100, 300));
using var dbActivity = ActivitySource.StartActivity("SELECT * FROM products WHERE id = @id", ActivityKind.Client);
dbActivity?.SetTag("app.product.id", item.ProductId);
dbActivity?.SetTag("db.statement", "SELECT * FROM products WHERE id = @id");
dbActivity?.SetTag("db.type", "sql");
if (cart.Items.Count > 6)
{
await Task.Delay(random.Next(100, 300));
}
}
totalCart += item.Quantity;
}
Expand All @@ -73,7 +91,7 @@ public override async Task<Empty> EmptyCart(EmptyCartRequest request, ServerCall
try
{
// Throw 1/10 of the time to simulate a failure when the feature flag is enabled
if (await _featureFlagHelper.GetBooleanValue("cartServiceFailure", false) && random.Next(10) == 0)
if (await _featureFlagHelper.GetBooleanValueAsync("cartServiceFailure", false) && random.Next(10) == 0)
{
await _badCartStore.EmptyCartAsync(request.UserId);
}
Expand Down
9 changes: 9 additions & 0 deletions src/flagd/demo.flagd.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@
},
"defaultVariant": "off"
},
"cartservice.add-db-call": {
"description": "Add a database call to the cart service",
"state": "ENABLED",
"variants": {
"on": true,
"off": false
},
"defaultVariant": "off"
},
"paymentServiceFailure": {
"description": "Fail payment service charge requests",
"state": "ENABLED",
Expand Down

0 comments on commit 7467d0f

Please sign in to comment.