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

[cartservice] Update to .NET8 #145

Merged
merged 1 commit into from
Nov 24, 2023
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
160 changes: 79 additions & 81 deletions src/cartservice/CartServiceImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,106 +21,104 @@
using static Hipstershop.CartService;
using System.Diagnostics;

namespace cartservice
{
namespace cartservice;

// Cart wrapper to deal with grpc communication
internal class CartServiceImpl : CartServiceBase
{
private ICartStore cartStore;
private static readonly Empty Empty = new();
// Cart wrapper to deal with grpc communication
internal class CartServiceImpl : CartServiceBase
{
private ICartStore cartStore;
private static readonly Empty Empty = new();

private static readonly Func<Grpc.Core.Metadata, string, IEnumerable<string>> Getter =
(md, key) =>
private static readonly Func<Grpc.Core.Metadata, string, IEnumerable<string>> Getter =
(md, key) =>
{
List<string> result = new List<string>();
foreach (var item in md.GetAll(key.ToLower()))
{
List<string> result = new List<string>();
foreach (var item in md.GetAll(key.ToLower()))
{
result.Add(item.Value);
}
return result;
};
result.Add(item.Value);
}
return result;
};

public CartServiceImpl(ICartStore cartStore)
{
this.cartStore = cartStore;
}
public CartServiceImpl(ICartStore cartStore)
{
this.cartStore = cartStore;
}

// Simplified implementation for B3multi propagator
// It is needed as we do not have support for Grpc server in OpenTelemetry .NET Instrumentation
public ActivityContext TraceContextFromGrpcContext(ServerCallContext context)
// Simplified implementation for B3multi propagator
// It is needed as we do not have support for Grpc server in OpenTelemetry .NET Instrumentation
public ActivityContext TraceContextFromGrpcContext(ServerCallContext context)
{
try
{
try
string traceId = null;
string spanId = null;
string sampled = null;
foreach (var header in context.RequestHeaders)
{
string traceId = null;
string spanId = null;
string sampled = null;
foreach (var header in context.RequestHeaders)
if (header.IsBinary)
{
if (header.IsBinary)
{
continue;
}

switch (header.Key.ToLowerInvariant())
{
case "x-b3-traceid":
traceId = header.Value;
if (traceId.Length == 16)
{
traceId = "0000000000000000" + traceId;
}
break;
case "x-b3-spanid":
spanId = header.Value;
break;
case "x-b3-sampled":
sampled = header.Value;
break;
}
continue;
}

return !string.IsNullOrEmpty(traceId) && !string.IsNullOrEmpty(spanId) && !string.IsNullOrEmpty(sampled)
? new ActivityContext(ActivityTraceId.CreateFromString(traceId),
ActivitySpanId.CreateFromString(spanId),
sampled == "1" ? ActivityTraceFlags.Recorded : ActivityTraceFlags.None, isRemote: true)
: new ActivityContext();
}
catch
{
return new ActivityContext();
switch (header.Key.ToLowerInvariant())
{
case "x-b3-traceid":
traceId = header.Value;
if (traceId.Length == 16)
{
traceId = "0000000000000000" + traceId;
}
break;
case "x-b3-spanid":
spanId = header.Value;
break;
case "x-b3-sampled":
sampled = header.Value;
break;
}
}
}

public override async Task<Empty> AddItem(AddItemRequest request, ServerCallContext context)
return !string.IsNullOrEmpty(traceId) && !string.IsNullOrEmpty(spanId) && !string.IsNullOrEmpty(sampled)
? new ActivityContext(ActivityTraceId.CreateFromString(traceId),
ActivitySpanId.CreateFromString(spanId),
sampled == "1" ? ActivityTraceFlags.Recorded : ActivityTraceFlags.None, isRemote: true)
: new ActivityContext();
}
catch
{
using var activity = ActivitySourceUtil.ActivitySource.StartActivity("AddItem", ActivityKind.Server, TraceContextFromGrpcContext(context));
activity?.SetTag("component", "rpc");
activity?.SetTag("grpc.method", "/hipstershop.CartService/AddItem");

await cartStore.AddItemAsync(request.UserId, request.Item.ProductId, request.Item.Quantity);
return Empty;
return new ActivityContext();
}
}

public override async Task<Empty> EmptyCart(EmptyCartRequest request, ServerCallContext context)
{
using var activity = ActivitySourceUtil.ActivitySource.StartActivity("EmptyCart", ActivityKind.Server, TraceContextFromGrpcContext(context));
activity?.SetTag("component", "rpc");
activity?.SetTag("grpc.method", "/hipstershop.CartService/EmptyCart");
public override async Task<Empty> AddItem(AddItemRequest request, ServerCallContext context)
{
using var activity = ActivitySourceUtil.ActivitySource.StartActivity("AddItem", ActivityKind.Server, TraceContextFromGrpcContext(context));
activity?.SetTag("component", "rpc");
activity?.SetTag("grpc.method", "/hipstershop.CartService/AddItem");

await cartStore.EmptyCartAsync(request.UserId);
return Empty;
}
await cartStore.AddItemAsync(request.UserId, request.Item.ProductId, request.Item.Quantity);
return Empty;
}

public override Task<Cart> GetCart(GetCartRequest request, ServerCallContext context)
public override async Task<Empty> EmptyCart(EmptyCartRequest request, ServerCallContext context)
{
using var activity = ActivitySourceUtil.ActivitySource.StartActivity("EmptyCart", ActivityKind.Server, TraceContextFromGrpcContext(context));
activity?.SetTag("component", "rpc");
activity?.SetTag("grpc.method", "/hipstershop.CartService/EmptyCart");

await cartStore.EmptyCartAsync(request.UserId);
return Empty;
}

public override Task<Cart> GetCart(GetCartRequest request, ServerCallContext context)
{
using var activity = ActivitySourceUtil.ActivitySource.StartActivity("GetCart", ActivityKind.Server, TraceContextFromGrpcContext(context));
{
using var activity = ActivitySourceUtil.ActivitySource.StartActivity("GetCart", ActivityKind.Server, TraceContextFromGrpcContext(context));
{
activity?.SetTag("component", "rpc");
activity?.SetTag("grpc.method", "/hipstershop.CartService/GetCart");
activity?.SetTag("component", "rpc");
activity?.SetTag("grpc.method", "/hipstershop.CartService/GetCart");

return cartStore.GetCartAsync(request.UserId);
}
return cartStore.GetCartAsync(request.UserId);
}
}
}
4 changes: 2 additions & 2 deletions src/cartservice/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG NET_VERSION=7.0
ARG NET_VERSION=8.0
FROM mcr.microsoft.com/dotnet/sdk:${NET_VERSION} as builder

WORKDIR /app
Expand All @@ -14,7 +14,7 @@ FROM mcr.microsoft.com/dotnet/aspnet:${NET_VERSION}
RUN apt-get update
RUN apt-get -y dist-upgrade

ARG GRPC_HEALTH_PROBE_VERSION=v0.4.11
ARG GRPC_HEALTH_PROBE_VERSION=v0.4.22
ADD https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 /bin/grpc_health_probe
RUN chmod +x /bin/grpc_health_probe

Expand Down
26 changes: 12 additions & 14 deletions src/cartservice/HealthImpl.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
using System;
using System.Threading.Tasks;
using cartservice.interfaces;
using Grpc.Core;
using Grpc.Health.V1;
using StackExchange.Redis;
using static Grpc.Health.V1.Health;

namespace cartservice {
internal class HealthImpl : HealthBase {
private ICartStore dependency { get; }
public HealthImpl (ICartStore dependency) {
this.dependency = dependency;
}
namespace cartservice;

public override Task<HealthCheckResponse> Check(HealthCheckRequest request, ServerCallContext context){
return Task.FromResult(new HealthCheckResponse {
Status = dependency.Ping() ? HealthCheckResponse.Types.ServingStatus.Serving : HealthCheckResponse.Types.ServingStatus.NotServing
});
}
internal class HealthImpl : HealthBase {
private ICartStore dependency { get; }
public HealthImpl (ICartStore dependency) {
this.dependency = dependency;
}
}

public override Task<HealthCheckResponse> Check(HealthCheckRequest request, ServerCallContext context){
return Task.FromResult(new HealthCheckResponse {
Status = dependency.Ping() ? HealthCheckResponse.Types.ServingStatus.Serving : HealthCheckResponse.Types.ServingStatus.NotServing
});
}
}
Loading
Loading