Skip to content

Commit

Permalink
[cartservice] code cleanup (open-telemetry#943)
Browse files Browse the repository at this point in the history
* File scope namespaces

* Sort modifiers

* Remove redundant type declaration

* Avoid hiding variables

* join declaration and assignment

* Use standard .NET convention for fields and consts

* inline out variable

* object initializer

* collection initializer

* drop unused using

* drop unused parameters

* remove redundant field initializer

---------

Co-authored-by: Austin Parker <[email protected]>
  • Loading branch information
Kielek and austinlparker authored Jun 26, 2023
1 parent 8e09e33 commit fe53a73
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 363 deletions.
7 changes: 3 additions & 4 deletions src/cartservice/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@

var builder = WebApplication.CreateBuilder(args);
string redisAddress = builder.Configuration["REDIS_ADDR"];
RedisCartStore cartStore = null;
if (string.IsNullOrEmpty(redisAddress))
{
Console.WriteLine("REDIS_ADDR environment variable is required.");
Environment.Exit(1);
}
cartStore = new RedisCartStore(redisAddress);
var cartStore = new RedisCartStore(redisAddress);

// Initialize the redis store
await cartStore.InitializeAsync();
Expand All @@ -41,15 +40,15 @@

builder.Services.AddOpenTelemetry()
.ConfigureResource(appResourceBuilder)
.WithTracing(builder => builder
.WithTracing(tracerBuilder => tracerBuilder
.AddRedisInstrumentation(
cartStore.GetConnection(),
options => options.SetVerboseDatabaseStatements = true)
.AddAspNetCoreInstrumentation()
.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation()
.AddOtlpExporter())
.WithMetrics(builder => builder
.WithMetrics(meterBuilder => meterBuilder
.AddRuntimeInstrumentation()
.AddAspNetCoreInstrumentation()
.AddOtlpExporter());
Expand Down
17 changes: 8 additions & 9 deletions src/cartservice/src/cartstore/ICartStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
// SPDX-License-Identifier: Apache-2.0
using System.Threading.Tasks;

namespace cartservice.cartstore
namespace cartservice.cartstore;

public interface ICartStore
{
public interface ICartStore
{
Task InitializeAsync();
Task InitializeAsync();

Task AddItemAsync(string userId, string productId, int quantity);
Task EmptyCartAsync(string userId);
Task AddItemAsync(string userId, string productId, int quantity);
Task EmptyCartAsync(string userId);

Task<Oteldemo.Cart> GetCartAsync(string userId);
Task<Oteldemo.Cart> GetCartAsync(string userId);

bool Ping();
}
bool Ping();
}
85 changes: 41 additions & 44 deletions src/cartservice/src/cartstore/LocalCartStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@
using System.Linq;
using System.Threading.Tasks;

namespace cartservice.cartstore
namespace cartservice.cartstore;

internal class LocalCartStore : ICartStore
{
internal class LocalCartStore : ICartStore
{
// Maps between user and their cart
private ConcurrentDictionary<string, Oteldemo.Cart> userCartItems = new ConcurrentDictionary<string, Oteldemo.Cart>();
private readonly Oteldemo.Cart emptyCart = new Oteldemo.Cart();
// Maps between user and their cart
private readonly ConcurrentDictionary<string, Oteldemo.Cart> _userCartItems = new();
private readonly Oteldemo.Cart _emptyCart = new();

public Task InitializeAsync()
{
Console.WriteLine("Local Cart Store was initialized");
public Task InitializeAsync()
{
Console.WriteLine("Local Cart Store was initialized");

return Task.CompletedTask;
}
return Task.CompletedTask;
}

public Task AddItemAsync(string userId, string productId, int quantity)
public Task AddItemAsync(string userId, string productId, int quantity)
{
Console.WriteLine($"AddItemAsync called with userId={userId}, productId={productId}, quantity={quantity}");
var newCart = new Oteldemo.Cart
{
Console.WriteLine($"AddItemAsync called with userId={userId}, productId={productId}, quantity={quantity}");
var newCart = new Oteldemo.Cart
{
UserId = userId,
Items = { new Oteldemo.CartItem { ProductId = productId, Quantity = quantity } }
};
userCartItems.AddOrUpdate(userId, newCart,
(k, exVal) =>
UserId = userId,
Items = { new Oteldemo.CartItem { ProductId = productId, Quantity = quantity } }
};
_userCartItems.AddOrUpdate(userId, newCart,
(_, exVal) =>
{
// If the item exists, we update its quantity
var existingItem = exVal.Items.SingleOrDefault(item => item.ProductId == productId);
Expand All @@ -46,35 +46,32 @@ public Task AddItemAsync(string userId, string productId, int quantity)
return exVal;
});

return Task.CompletedTask;
}
return Task.CompletedTask;
}

public Task EmptyCartAsync(string userId)
{
var eventTags = new ActivityTagsCollection();
eventTags.Add("userId", userId);
Activity.Current?.AddEvent(new ActivityEvent("EmptyCartAsync called.", default, eventTags));
public Task EmptyCartAsync(string userId)
{
var eventTags = new ActivityTagsCollection {{"userId", userId}};
Activity.Current?.AddEvent(new ActivityEvent("EmptyCartAsync called.", default, eventTags));

userCartItems[userId] = new Oteldemo.Cart();
return Task.CompletedTask;
}
_userCartItems[userId] = new Oteldemo.Cart();
return Task.CompletedTask;
}

public Task<Oteldemo.Cart> GetCartAsync(string userId)
public Task<Oteldemo.Cart> GetCartAsync(string userId)
{
Console.WriteLine($"GetCartAsync called with userId={userId}");
if (!_userCartItems.TryGetValue(userId, out var cart))
{
Console.WriteLine($"GetCartAsync called with userId={userId}");
Oteldemo.Cart cart = null;
if (!userCartItems.TryGetValue(userId, out cart))
{
Console.WriteLine($"No carts for user {userId}");
return Task.FromResult(emptyCart);
}

return Task.FromResult(cart);
Console.WriteLine($"No carts for user {userId}");
return Task.FromResult(_emptyCart);
}

public bool Ping()
{
return true;
}
return Task.FromResult(cart);
}

public bool Ping()
{
return true;
}
}
Loading

0 comments on commit fe53a73

Please sign in to comment.