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

Upstream changes needed merge #41

Merged
merged 7 commits into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[cartservice] code cleanup (open-telemetry#943)
* 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 <austin@ap2.io>
  • Loading branch information
Kielek and austinlparker committed Jun 26, 2023
commit 3c5bd95aed3c84163fc42f05587664cb0eb9680e
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