public static IServiceCollection AddLightStep(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.AddSingleton <ITracer>(serviceProvider =>
            {
                string serviceName           = Assembly.GetEntryAssembly().GetName().Name;
                ILoggerFactory loggerFactory = serviceProvider.GetRequiredService <ILoggerFactory>();

                var options = new LightStep.Options(_lightStepProjectKey, new SatelliteOptions("collector.lightstep.com"));

                ITracer tracer = new LightStep.Tracer(options);

                GlobalTracer.Register(tracer);

                return(tracer);
            });

            return(services);
        }
Example #2
0
        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;
            }
        }