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

Generate random errors in cartservice #824

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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