示例#1
0
        private LightStepHttpClient GetClient(TransportOptions t = TransportOptions.BinaryProto)
        {
            var satelliteOptions = new SatelliteOptions("localhost", 80, true);
            var tracerOptions    = new Options("TEST").WithSatellite(satelliteOptions).WithAutomaticReporting(false).WithTransport(t);

            return(new LightStepHttpClient("http://localhost:80", tracerOptions));
        }
        private ReportTranslator GetTranslator()
        {
            var satelliteOptions = new SatelliteOptions("localhost", 80, true);
            var tracerOptions    = new Options("TEST").WithSatellite(satelliteOptions).WithAutomaticReporting(false);

            return(new ReportTranslator(tracerOptions));
        }
        private static void Main(string[] args)
        {
            // substitute your own LS API Key here
            var lsKey      = "TEST_TOKEN";
            var lsSettings = new SatelliteOptions("localhost", 9996, false);
            var tracer     = new Tracer(new Options(lsKey, lsSettings));

            GlobalTracer.Register(tracer);

            for (var i = 0; i < 500; i++)
            {
                using (var scope = tracer.BuildSpan("testParent").WithTag("testSpan", "true").StartActive(true))
                {
                    scope.Span.Log("test");
                    tracer.ActiveSpan.Log($"iteration {i}");
                    Console.WriteLine("sleeping for a bit");
                    Thread.Sleep(new Random().Next(5, 10));
                    var innerSpan = tracer.BuildSpan("childSpan").Start();
                    innerSpan.SetTag("innerTestTag", "true");
                    Console.WriteLine("sleeping more...");
                    Thread.Sleep(new Random().Next(10, 20));
                    innerSpan.Finish();
                }
            }

            tracer.Flush();
        }
        private Tracer GetTracer(ISpanRecorder recorder = null)
        {
            var spanRecorder     = recorder ?? new SimpleMockRecorder();
            var satelliteOptions = new SatelliteOptions("localhost", 80, true);
            var tracerOptions    = new Options("TEST").WithSatellite(satelliteOptions).WithAutomaticReporting(false);

            return(new Tracer(tracerOptions, spanRecorder));
        }
        private Tracer GetTracer(ISpanRecorder recorder = null)
        {
            var spanRecorder     = recorder ?? new SimpleMockRecorder();
            var satelliteOptions = new SatelliteOptions("localhost", 80, true);
            var tracerOptions    = new Options("TEST", satelliteOptions);

            tracerOptions.Run = false;
            return(new Tracer(tracerOptions, spanRecorder));
        }
        public void TracerOptionsShouldLetYouOverrideTags()
        {
            var satelliteOptions = new SatelliteOptions("localhost", 80, true);
            var overrideTags     = new Dictionary <string, object> {
                { LightStepConstants.ComponentNameKey, "test_component" }
            };
            var tracerOptions = new Options("TEST").WithSatellite(satelliteOptions).WithTags(overrideTags).WithAutomaticReporting(false);

            Assert.Equal("test_component", tracerOptions.Tags[LightStepConstants.ComponentNameKey]);
            Assert.Equal(LightStepConstants.TracerPlatformValue,
                         tracerOptions.Tags[LightStepConstants.TracerPlatformKey]);
        }
示例#7
0
        static void Main(string[] args)
        {
            // create your tracer options, initialize it, assign it to the GlobalTracer
            var lsKey      = Environment.GetEnvironmentVariable("LS_KEY");
            var lsSettings = new SatelliteOptions("collector.lightstep.com");
            var lsOptions  = new Options(lsKey).WithSatellite(lsSettings);
            var tracer     = new Tracer(lsOptions);

            GlobalTracer.Register(tracer);

            // do some work in parallel, this work also includes awaited calls
            Parallel.For(1, 100, i => DoThing(i));

            // block until you enter a key
            Console.ReadKey();
        }
示例#8
0
        private void Init()
        {
            var overrideTags = new Dictionary <string, object>
            {
                {
                    LightStepConstants.ComponentNameKey, "ServiceName"
                },
            };
            var     satelliteOptions = new SatelliteOptions(Host, Port, true);
            Options options          = new Options(Token)
                                       .WithSatellite(satelliteOptions)
                                       .WithTags(overrideTags)
                                       .WithMaxBufferedSpans(BufferSize)
                                       .WithReportPeriod(TimeSpan.FromSeconds(ReportPeriod));

            Tracer = new Tracer(options);
        }
        public void TracerShouldTrapExceptions()
        {
            var x = false;
            Action <Exception> eh = delegate { x = true; };
            var satelliteOptions  = new SatelliteOptions("localhost", 80, true);
            var tracerOptions     = new Options().WithSatellite(satelliteOptions).WithExceptionHandler(eh.Invoke);
            var recorder          = new SimpleMockRecorder();
            var mockClient        = new Mock <ILightStepHttpClient>();

            mockClient.Setup(client => client.Translate(recorder)).Throws <OverflowException>();

            var tracer = new Tracer(tracerOptions, recorder, mockClient.Object);

            var span = tracer.BuildSpan("test").Start();

            span.Finish();

            Assert.False(x);
            tracer.Flush();
            Assert.True(x);
        }
        public void PropagatorStackInTracerShouldInjectAndExtract()
        {
            var ps = new PropagatorStack(BuiltinFormats.HttpHeaders);

            ps.AddPropagator(new B3Propagator());
            ps.AddPropagator(new HttpHeadersPropagator());
            ps.AddPropagator(new TextMapPropagator());

            var sr         = new SimpleMockRecorder();
            var satOpts    = new SatelliteOptions("localhost", 80, true);
            var tracerOpts = new Options("TEST", satOpts);

            tracerOpts.Run = false;

            var tracer = new Tracer(tracerOpts, sr, ps);

            var span = tracer.BuildSpan("propTest").Start();

            var traceId = span.TypedContext().TraceId;
            var spanId  = span.TypedContext().SpanId;

            var hexTraceId = Convert.ToUInt64(traceId).ToString("X");
            var hexSpanId  = Convert.ToUInt64(spanId).ToString("X");

            var data = new Dictionary <string, string>();

            tracer.Inject(span.Context, BuiltinFormats.HttpHeaders, new TextMapInjectAdapter(data));

            Assert.Equal(traceId, data["ot-tracer-traceid"]);
            Assert.Equal(hexTraceId, data["X-B3-TraceId"]);
            Assert.Equal(spanId, data["ot-tracer-spanid"]);
            Assert.Equal(hexSpanId, data["X-B3-SpanId"]);

            span.Finish();

            var ctx = tracer.Extract(BuiltinFormats.HttpHeaders, new TextMapExtractAdapter(data));

            Assert.Equal(ctx.SpanId, spanId);
            Assert.Equal(ctx.TraceId, traceId);
        }
示例#11
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;
            }
        }