Skip to content

Commit

Permalink
Generate random errors in cartservice (#824)
Browse files Browse the repository at this point in the history
* Generate random errors from cart service

* Update migration script?

* Update changelog

* Update changelog

* Add Copyright to FeatureFlagHelper.cs

---------

Co-authored-by: Carter Socha <43380952+cartersocha@users.noreply.github.com>
Co-authored-by: Juliano Costa <julianocosta89@outlook.com>
  • Loading branch information
3 people authored May 8, 2023
1 parent 8222db7 commit 02a91d4
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ release.

## Unreleased

* Add cartServiceFailure feature flag triggering Cart Service errors
([#824](https://github.com/open-telemetry/opentelemetry-demo/pull/824))
* [paymentservice] update JS SDKs to 1.12.0/0.38.0
([#853](https://github.com/open-telemetry/opentelemetry-demo/pull/853))
* [frontend] update JS SDKs to 1.12.0/0.38.0
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ services:
- "${CART_SERVICE_PORT}"
environment:
- CART_SERVICE_PORT
- FEATURE_FLAG_GRPC_SERVICE_ADDR
- REDIS_ADDR
- OTEL_EXPORTER_OTLP_ENDPOINT
- OTEL_RESOURCE_ATTRIBUTES
Expand Down
2 changes: 2 additions & 0 deletions src/cartservice/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;

using cartservice.cartstore;
using cartservice.featureflags;
using cartservice.services;

using Microsoft.AspNetCore.Builder;
Expand Down Expand Up @@ -30,6 +31,7 @@
Console.WriteLine("Initialization completed");

builder.Services.AddSingleton<ICartStore>(cartStore);
builder.Services.AddSingleton<FeatureFlagHelper>();

// see https://opentelemetry.io/docs/instrumentation/net/getting-started/

Expand Down
32 changes: 32 additions & 0 deletions src/cartservice/src/featureflags/FeatureFlagHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System;
using System.Threading.Tasks;
using Oteldemo;

namespace cartservice.featureflags;

public class FeatureFlagHelper
{
private readonly static Random Random = new Random();
private readonly FeatureFlagService.FeatureFlagServiceClient _featureFlagServiceClient;

public FeatureFlagHelper()
{
var featureFlagServiceUri = new Uri($"http://{Environment.GetEnvironmentVariable("FEATURE_FLAG_GRPC_SERVICE_ADDR")}");
var channel = Grpc.Net.Client.GrpcChannel.ForAddress(featureFlagServiceUri);
_featureFlagServiceClient = new FeatureFlagService.FeatureFlagServiceClient(channel);
}

public async Task<bool> GenerateCartError()
{
if (Random.Next(10) != 1)
{
return false;
}

var getFlagRequest = new GetFlagRequest { Name = "cartServiceFailure" };
var getFlagResponse = await _featureFlagServiceClient.GetFlagAsync(getFlagRequest);
return getFlagResponse.Flag.Enabled;
}
}
26 changes: 24 additions & 2 deletions src/cartservice/src/services/CartService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
using System.Diagnostics;
using System.Threading.Tasks;
using Grpc.Core;
using OpenTelemetry.Trace;
using cartservice.cartstore;
using cartservice.featureflags;
using Oteldemo;

namespace cartservice.services
{
public class CartService : Oteldemo.CartService.CartServiceBase
{
private readonly static Empty Empty = new Empty();
private readonly static ICartStore BadCartStore = new RedisCartStore("badhost:1234");
private readonly ICartStore _cartStore;
private readonly FeatureFlagHelper _featureFlagHelper;

public CartService(ICartStore cartStore)
public CartService(ICartStore cartStore, FeatureFlagHelper featureFlagService)
{
_cartStore = cartStore;
_featureFlagHelper = featureFlagService;
}

public async override Task<Empty> AddItem(AddItemRequest request, ServerCallContext context)
Expand Down Expand Up @@ -52,7 +57,24 @@ public async override Task<Empty> EmptyCart(EmptyCartRequest request, ServerCall
activity?.SetTag("app.user.id", request.UserId);
activity?.AddEvent(new("Empty cart"));

await _cartStore.EmptyCartAsync(request.UserId);
try
{
if (await _featureFlagHelper.GenerateCartError())
{
await BadCartStore.EmptyCartAsync(request.UserId);
}
else
{
await _cartStore.EmptyCartAsync(request.UserId);
}
}
catch (RpcException ex)
{
Activity.Current?.RecordException(ex);
Activity.Current?.SetStatus(ActivityStatusCode.Error, ex.Message);
throw;
}

return Empty;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ defmodule Featureflagservice.Repo.Migrations.CreateFeatureflags do
name: "adServiceFailure",
description: "Fail ad service requests sporadically",
enabled: false})

repo().insert(%Featureflagservice.FeatureFlags.FeatureFlag{
name: "cartServiceFailure",
description: "Fail cart service requests sporadically",
enabled: false})
end

defp execute_down do
repo().delete(%Featureflagservice.FeatureFlags.FeatureFlag{name: "productCatalogFailure"})
repo().delete(%Featureflagservice.FeatureFlags.FeatureFlag{name: "recommendationCache"})
repo().delete(%Featureflagservice.FeatureFlags.FeatureFlag{name: "adServiceFailure"})
repo().delete(%Featureflagservice.FeatureFlags.FeatureFlag{name: "cartServiceFailure"})
end
end

0 comments on commit 02a91d4

Please sign in to comment.