// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { string redisAddress = Configuration["REDIS_ADDR"]; ICartStore cartStore = null; if (!string.IsNullOrEmpty(redisAddress)) { cartStore = new RedisCartStore(redisAddress); } else { Console.WriteLine("Redis cache host(hostname+port) was not specified. Starting a cart service using local store"); Console.WriteLine("If you wanted to use Redis Cache as a backup store, you should provide its address via command line or REDIS_ADDRESS environment variable."); cartStore = new LocalCartStore(); } // Initialize the redis store cartStore.InitializeAsync().GetAwaiter().GetResult(); Console.WriteLine("Initialization completed"); services.AddControllers(); services.AddGrpc(); services.AddSingleton <ICartStore>(cartStore); services.AddOpenTelemetryTracing(builder => ConfigureOpenTelemetry(builder, cartStore)); }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); string lsHost = Environment.GetEnvironmentVariable(LIGHTSTEP_HOST); int lsPort = Int32.Parse(Environment.GetEnvironmentVariable(LIGHTSTEP_PORT)); string serviceName = Environment.GetEnvironmentVariable("LS_SERVICE_NAME"); string accessToken = Environment.GetEnvironmentVariable(LIGHTSTEP_ACCESS_TOKEN); // create and register an activity source var activitySource = new ActivitySource(serviceName); services.AddSingleton(activitySource); // from: https://github.com/kellybirr/tracing-demo OpenTelemetry.Sdk.SetDefaultTextMapPropagator(new B3Propagator()); string redisAddress = Configuration["REDIS_ADDR"]; ICartStore cartStore = null; if (!string.IsNullOrEmpty(redisAddress)) { cartStore = new RedisCartStore(redisAddress); } else { Console.WriteLine("Redis cache host(hostname+port) was not specified. Starting a cart service using local store"); Console.WriteLine("If you wanted to use Redis Cache as a backup store, you should provide its address via command line or REDIS_ADDR environment variable."); cartStore = new LocalCartStore(); } services.AddOpenTelemetryTracing((builder) => builder .AddSource(activitySource.Name) .AddAspNetCoreInstrumentation(opt => { opt.EnableGrpcAspNetCoreSupport = true; }) .AddHttpClientInstrumentation() .AddGrpcClientInstrumentation() .AddConsoleExporter() .AddRedisInstrumentation(cartStore.Connection) .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName)) .AddOtlpExporter(opt => { opt.Endpoint = $"{lsHost}:{lsPort}"; opt.Headers = new Metadata { { "lightstep-access-token", accessToken } }; opt.Credentials = new SslCredentials(); })); // Initialize the redis store cartStore.InitializeAsync().GetAwaiter().GetResult(); Console.WriteLine("Initialization completed"); services.AddSingleton <ICartStore>(cartStore); services.AddGrpc(); }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { string redisAddress = Configuration["REDIS_ADDR"]; ICartStore cartStore = null; if (!string.IsNullOrEmpty(redisAddress)) { cartStore = new RedisCartStore(redisAddress); } else { Console.WriteLine("Redis cache host(hostname+port) was not specified. Starting a cart service using local store"); Console.WriteLine("If you wanted to use Redis Cache as a backup store, you should provide its address via command line or REDIS_ADDR environment variable."); cartStore = new LocalCartStore(); } // Initialize the redis store cartStore.InitializeAsync().GetAwaiter().GetResult(); Console.WriteLine("Initialization completed"); services.AddSingleton <ICartStore>(cartStore); services.AddGrpc(); // Adding the OtlpExporter creates a GrpcChannel. // This switch must be set before creating a GrpcChannel/HttpClient when calling an insecure gRPC service. // See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); services.AddOpenTelemetryTracing(builder => { builder.AddAspNetCoreInstrumentation() .AddOtlpExporter(options => options.Endpoint = new Uri(Configuration["OTEL_COLLECTOR_ADDR"])); if (cartStore is RedisCartStore redisCartStore) { builder.AddRedisInstrumentation(redisCartStore.RedisConnectionMultiplexer); } } ); }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { string redisAddress = Configuration["REDIS_ADDR"]; ICartStore cartStore = null; if (!string.IsNullOrEmpty(redisAddress)) { cartStore = new RedisCartStore(redisAddress); } else { Console.WriteLine("Redis cache host(hostname+port) was not specified. Starting a cart service using local store"); Console.WriteLine("If you wanted to use Redis Cache as a backup store, you should provide its address via command line or REDIS_ADDR environment variable."); cartStore = new LocalCartStore(); } // Initialize the redis store cartStore.InitializeAsync().GetAwaiter().GetResult(); Console.WriteLine("Initialization completed"); services.AddSingleton <ICartStore>(cartStore); services.AddGrpc(); string otelAddress = Configuration["OTEL_COLLECTOR_ADDR"]; AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); services.AddOpenTelemetryTracing((builder) => builder .SetResourceBuilder(ResourceBuilder.CreateDefault() .AddAttributes(new Dictionary <string, object> { ["telemetry.sdk.language"] = "dotnet", }) .AddService(this.Configuration.GetValue <string>("Otel:ServiceName"))) .AddAspNetCoreInstrumentation() .AddRedisInstrumentation(cartStore.GetConnection()) .AddOtlpExporter(opt => opt.Endpoint = new Uri(otelAddress))); }
static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine("Invalid number of arguments supplied"); Environment.Exit(-1); } switch (args[0]) { case "start": Parser.Default.ParseArguments <ServerOptions>(args).MapResult( (ServerOptions options) => { Console.WriteLine($"Started as process with id {System.Diagnostics.Process.GetCurrentProcess().Id}"); // Set hostname/ip address string hostname = options.Host; if (string.IsNullOrEmpty(hostname)) { Console.WriteLine($"Reading host address from {CART_SERVICE_ADDRESS} environment variable"); hostname = Environment.GetEnvironmentVariable(CART_SERVICE_ADDRESS); if (string.IsNullOrEmpty(hostname)) { Console.WriteLine($"Environment variable {CART_SERVICE_ADDRESS} was not set. Setting the host to 0.0.0.0"); hostname = "0.0.0.0"; } } // Set the port int port = options.Port; if (options.Port <= 0) { Console.WriteLine($"Reading cart service port from {CART_SERVICE_PORT} environment variable"); string portStr = Environment.GetEnvironmentVariable(CART_SERVICE_PORT); if (string.IsNullOrEmpty(portStr)) { Console.WriteLine($"{CART_SERVICE_PORT} environment variable was not set. Setting the port to 8080"); port = 8080; } else { port = int.Parse(portStr); } } // Set redis cache host (hostname+port) ICartStore cartStore; string YSQL = ReadYSQLAddress(options.YSQL); // Extract and set YBDB host from kubernetes-manifest cartservice.yaml // string ysql = ReadYSQLAddress(options.Redis); // Redis was specified via command line or environment variable if (!string.IsNullOrEmpty(YSQL)) { // If you want to start cart store using local cache in process, you can replace the following line with this: // cartStore = new LocalCartStore(); // Redis cart store cartStore = new YSQLCartStore(YSQL); // YSQL cart store // cartStore = new SQLCartStore(ysql); return(StartServer(hostname, port, cartStore)); } else { Console.WriteLine("Redis cache host(hostname+port) was not specified. Starting a cart service using local store"); Console.WriteLine("If you wanted to use Redis Cache as a backup store, you should provide its address via command line or REDIS_ADDRESS environment variable."); cartStore = new LocalCartStore(); } return(StartServer(hostname, port, cartStore)); }, errs => 1); break; default: Console.WriteLine("Invalid command"); break; } }
static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine("Invalid number of arguments supplied"); Environment.Exit(-1); } switch (args[0]) { case "start": Parser.Default.ParseArguments <ServerOptions>(args).MapResult( (ServerOptions options) => { Console.WriteLine($"Started as process with id {System.Diagnostics.Process.GetCurrentProcess().Id}"); // Set hostname/ip address string hostname = options.Host; if (string.IsNullOrEmpty(hostname)) { Console.WriteLine($"Reading host address from {CART_SERVICE_ADDRESS} environment variable"); hostname = Environment.GetEnvironmentVariable(CART_SERVICE_ADDRESS); if (string.IsNullOrEmpty(hostname)) { Console.WriteLine($"Environment variable {CART_SERVICE_ADDRESS} was not set. Setting the host to 0.0.0.0"); hostname = "0.0.0.0"; } } // Set the port int port = options.Port; if (options.Port <= 0) { Console.WriteLine($"Reading cart service port from {CART_SERVICE_PORT} environment variable"); string portStr = Environment.GetEnvironmentVariable(CART_SERVICE_PORT); if (string.IsNullOrEmpty(portStr)) { Console.WriteLine($"{CART_SERVICE_PORT} environment variable was not set. Setting the port to 8080"); port = 8080; } else { port = int.Parse(portStr); } } // Setup LightStep Tracer Console.WriteLine($"Reading Lightstep Access Token {LIGHTSTEP_ACCESS_TOKEN} environment variable"); string serviceName = "cartservice"; string accessToken = Environment.GetEnvironmentVariable(LIGHTSTEP_ACCESS_TOKEN); string lsHost = Environment.GetEnvironmentVariable(LIGHTSTEP_HOST); int lsPort = Int32.Parse(Environment.GetEnvironmentVariable(LIGHTSTEP_PORT)); bool plaintext = (Environment.GetEnvironmentVariable(LIGHTSTEP_PLAINTEXT) == "true"); var satelliteOptions = new SatelliteOptions(lsHost, lsPort, plaintext); // BEGIN // Used for GCP Demo var overrideTags = new Dictionary <string, object> { { LightStepConstants.ComponentNameKey, serviceName }, { "service.version", RedisCartStore.updateUserProfileValue ? RedisCartStore.UnhealthyVersion : RedisCartStore.HealthyVersion }, { "cartservice.identity", "f738e221f8" }, { "lightstep.hostname", serviceName + "-0" }, }; // END var tracerOptions = new Options(accessToken). WithSatellite(satelliteOptions). WithTags(overrideTags); var lightStepTracer = new LightStep.Tracer( tracerOptions, new LightStepSpanRecorder(), new B3Propagator() ); GlobalTracer.Register(lightStepTracer); // Set redis cache host (hostname+port) ICartStore cartStore; string redis = ReadRedisAddress(options.Redis); // Redis was specified via command line or environment variable if (!string.IsNullOrEmpty(redis)) { // If you want to start cart store using local cache in process, you can replace the following line with this: // cartStore = new LocalCartStore(); cartStore = new RedisCartStore(redis); return(StartServer(hostname, port, cartStore)); } else { Console.WriteLine("Redis cache host(hostname+port) was not specified. Starting a cart service using local store"); Console.WriteLine("If you wanted to use Redis Cache as a backup store, you should provide its address via command line or REDIS_ADDRESS environment variable."); cartStore = new LocalCartStore(); } return(StartServer(hostname, port, cartStore)); }, errs => 1); break; default: Console.WriteLine("Invalid command"); break; } }
static void Main(string[] args) { Log.Logger = new LoggerConfiguration().ReadFrom.AppSettings().WriteTo.Console(new EcsTextFormatter()).CreateLogger(); if (args.Length == 0) { Log.Error("Invalid number of arguments supplied"); Environment.Exit(-1); } switch (args[0]) { case "start": Parser.Default.ParseArguments <ServerOptions>(args).MapResult( (ServerOptions options) => { Log.Information("Started as process with id {Id}", System.Diagnostics.Process.GetCurrentProcess().Id); // Set hostname/ip address string hostname = options.Host; if (string.IsNullOrEmpty(hostname)) { Log.Information("Reading host address from {CART_SERVICE_ADDRESS} environment variable", CART_SERVICE_ADDRESS); hostname = Environment.GetEnvironmentVariable(CART_SERVICE_ADDRESS); if (string.IsNullOrEmpty(hostname)) { Log.Information("Environment variable {CART_SERVICE_ADDRESS} was not set. Setting the host to 0.0.0.0", CART_SERVICE_ADDRESS); hostname = "0.0.0.0"; } } string loglevel = Environment.GetEnvironmentVariable("ELASTIC_APM_LOG_LEVEL"); Log.Information("APM Log Level is: {loglevel}", loglevel); // Set the port int port = options.Port; if (options.Port <= 0) { Log.Information("Reading cart service port from {CART_SERVICE_PORT} environment variable", CART_SERVICE_PORT); string portStr = Environment.GetEnvironmentVariable(CART_SERVICE_PORT); if (string.IsNullOrEmpty(portStr)) { Log.Information("{CART_SERVICE_PORT} environment variable was not set. Setting the port to 8080", CART_SERVICE_PORT); port = 8080; } else { port = int.Parse(portStr); } } Agent.Subscribe(new HttpDiagnosticsSubscriber()); // Set redis cache host (hostname+port) ICartStore cartStore; string redis = ReadRedisAddress(options.Redis); // Redis was specified via command line or environment variable if (!string.IsNullOrEmpty(redis)) { // If you want to start cart store using local cache in process, you can replace the following line with this: // cartStore = new LocalCartStore(); cartStore = new RedisCartStore(redis); return(StartServer(hostname, port, cartStore)); } else { Log.Information("Redis cache host(hostname+port) was not specified. Starting a cart service using local store"); Log.Information("If you wanted to use Redis Cache as a backup store, you should provide its address via command line or REDIS_ADDRESS environment variable."); cartStore = new LocalCartStore(); } return(StartServer(hostname, port, cartStore)); }, errs => 1); break; default: Log.Error("Invalid command"); break; } }