public void SpansShouldAllowThreadSafeAccess()
        {
            var recorder = new SimpleMockRecorder();
            var tracer   = GetTracer(recorder);
            var span     = tracer.BuildSpan("testOperation").Start();

            var t1 = Task.Run(() =>
            {
                span.Log("t1 run");
                span.SetTag("sameTag", "t1");
                span.SetTag("t1Tag", "t1");
            });
            var t2 = Task.Run(() =>
            {
                span.Log("t2 run");
                span.SetTag("sameTag", "t2");
                span.SetTag("t2Tag", "t2");
            });

            t1.Wait();
            t2.Wait();

            span.Finish();

            var finishedSpan = recorder.GetSpanBuffer().First();

            // we expect there to be 2 logs and 3 tags
            Assert.True(finishedSpan.LogData.Count == 2);
            Assert.True(finishedSpan.Tags.Count == 3);
        }
        public void TracerShouldRecordAllSpanData()
        {
            var recorder = new SimpleMockRecorder();
            var tracer   = GetTracer(recorder);

            var startTimeStamp = new DateTimeOffset(2018, 2, 19, 12, 0, 0, TimeSpan.Zero);
            var endTimeStamp   = new DateTimeOffset(2018, 2, 19, 12, 0, 5, TimeSpan.Zero);

            var span = tracer.BuildSpan("test")
                       .WithStartTimestamp(startTimeStamp)
                       .Start()
                       .SetTag("key", "testValue")
                       .SetBaggageItem("baggageKey", "bagTestValue");

            span.Finish(endTimeStamp);

            var recordedSpans = recorder.GetSpans().First();

            Assert.Equal("test", recordedSpans.OperationName);
            Assert.Equal(startTimeStamp, recordedSpans.StartTimestamp);
            Assert.Equal(TimeSpan.FromSeconds(5), recordedSpans.Duration);

            Assert.Equal("bagTestValue", recordedSpans.Context.GetBaggageItem("baggageKey"));
            Assert.Equal("testValue", recordedSpans.Tags["key"]);

            Assert.False(string.IsNullOrWhiteSpace(recordedSpans.Context.TraceId));
            Assert.False(string.IsNullOrWhiteSpace(recordedSpans.Context.SpanId));
        }
        public void ConverterShouldConvertValues()
        {
            var recorder   = new SimpleMockRecorder();
            var translator = GetTranslator();
            var tracer     = GetTracer(recorder);
            var span       = tracer.BuildSpan("testOperation")
                             .WithTag("boolTrueTag", true)
                             .WithTag("boolFalseTag", false)
                             .WithTag("intTag", 0)
                             .WithTag("stringTag", "test")
                             .WithTag("doubleTag", 0.1)
                             .WithTag("nullTag", null)
                             .WithTag("jsonTag", @"{""key"":""value""}")
                             .Start();

            span.Finish();

            var client = GetClient();

            var translatedSpans = translator.Translate(recorder.GetSpanBuffer());
            var translatedSpan  = translatedSpans.Spans[0];

            foreach (var tag in translatedSpan.Tags)
            {
                switch (tag.Key)
                {
                case "boolTrueFlag":
                    Assert.True(tag.BoolValue);
                    break;

                case "boolFalseFlag":
                    Assert.False(tag.BoolValue);
                    break;

                case "intTag":
                    Assert.Equal(0, tag.IntValue);
                    break;

                case "stringTag":
                    Assert.Equal("test", tag.StringValue);
                    break;

                case "doubleTag":
                    Assert.Equal(0.1, tag.DoubleValue);
                    break;

                case "nullTag":
                    Assert.Equal("null", tag.StringValue);
                    break;

                case "jsonTag":
                    Assert.Equal(@"{""key"":""value""}", tag.JsonValue);
                    break;

                default:
                    continue;
                }
            }
        }
Esempio n. 4
0
        public void DroppedSpanCountShouldSerializeCorrectly()
        {
            var mockBuffer = new SimpleMockRecorder();

            mockBuffer.RecordDroppedSpans(1);

            var client           = GetClient();
            var translatedBuffer = client.Translate(mockBuffer.GetSpanBuffer());

            Assert.Equal(1, translatedBuffer.InternalMetrics.Counts[0].IntValue);
        }
Esempio n. 5
0
        public void DroppedSpanCountShouldIncrementOnBadSpan()
        {
            var recorder = new SimpleMockRecorder();
            var badSpan  = new SpanData {
                Duration      = new TimeSpan(-1),
                OperationName = "badSpan"
            };

            recorder.RecordSpan(badSpan);
            var client           = GetClient();
            var translatedBuffer = client.Translate(recorder.GetSpanBuffer());

            Assert.Equal(1, translatedBuffer.InternalMetrics.Counts[0].IntValue);
        }
Esempio n. 6
0
        public void InternalMetricsShouldExist()
        {
            var recorder = new SimpleMockRecorder();
            var tracer   = GetTracer(recorder);
            var span     = tracer.BuildSpan("test").Start();

            span.Finish();

            var client = GetClient();

            var translatedSpans = client.Translate(recorder.GetSpanBuffer());

            Assert.Equal("spans.dropped", translatedSpans.InternalMetrics.Counts[0].Name);
        }
Esempio n. 7
0
        public void ReportShouldBeBinaryWithoutJsonOption()
        {
            var recorder = new SimpleMockRecorder();
            var tracer   = GetTracer(recorder);
            var span     = tracer.BuildSpan("test").Start();

            span.Finish();

            var client          = GetClient();
            var translatedSpans = client.Translate(recorder.GetSpanBuffer());
            var report          = client.BuildRequest(translatedSpans);

            Assert.Equal("application/octet-stream", report.Content.Headers.ContentType.MediaType);
        }
Esempio n. 8
0
        public void ReportShouldBeJsonWithJsonOption()
        {
            var recorder = new SimpleMockRecorder();
            var tracer   = GetTracer(recorder);
            var span     = tracer.BuildSpan("test").Start();

            span.Finish();

            var client          = GetClient(TransportOptions.JsonProto);
            var translatedSpans = client.Translate(recorder.GetSpanBuffer());
            var report          = client.BuildRequest(translatedSpans);

            Assert.Equal("application/json", report.Content.Headers.ContentType.MediaType);
            var contentString = report.Content.ReadAsStringAsync().Result;

            Assert.Contains("test", contentString);
        }
        public void SpansShouldRecordLogs()
        {
            var recorder = new SimpleMockRecorder();
            var tracer   = GetTracer(recorder);
            var span     = tracer.BuildSpan("testOperation").Start();

            span.Log("hello world!");
            span.Finish();

            var finishedSpan = recorder.GetSpanBuffer().First();
            // project the sequence of logdata into an array with one item, the aforementioned log message
            var finishedSpanLogData = finishedSpan.LogData.Select(
                item => item.Fields.First(
                    f => f.Value.Equals("hello world!")
                    ).Value).ToArray()[0];

            Assert.Equal("hello world!", (string)finishedSpanLogData);
        }
        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 SpansShouldBuildProperTypedKeyTags()
        {
            var recorder = new SimpleMockRecorder();
            var tracer   = GetTracer(recorder);
            var span     = tracer.BuildSpan("testOperation")
                           .WithTag(new BooleanTag("testBoolTag"), true)
                           .WithTag(new IntTag("testIntTag"), 1)
                           .WithTag(new StringTag("testStringTag"), "test")
                           .WithTag(new IntOrStringTag("testIntOrStringTagAsString"), "string")
                           .WithTag(new IntOrStringTag("testIntOrStringTagAsInt"), 1)
                           .Start();

            span.Finish();
            var finishedSpan = recorder.GetSpanBuffer().First();

            Assert.True((bool)finishedSpan.Tags["testBoolTag"]);
            Assert.Equal(1, finishedSpan.Tags["testIntTag"]);
            Assert.Equal("test", finishedSpan.Tags["testStringTag"]);
            Assert.Equal("string", finishedSpan.Tags["testIntOrStringTagAsString"]);
            Assert.Equal(1, finishedSpan.Tags["testIntOrStringTagAsInt"]);
        }
        public void SpansShouldBuildProperStringKeyTags()
        {
            var recorder = new SimpleMockRecorder();
            var tracer   = GetTracer(recorder);
            var span     = tracer.BuildSpan("testOperation")
                           .WithTag("boolTrueTag", true)
                           .WithTag("boolFalseTag", false)
                           .WithTag("intTag", 0)
                           .WithTag("stringTag", "test")
                           .WithTag("doubleTag", 0.1)
                           .Start();

            span.Finish();
            var finishedSpan = recorder.GetSpanBuffer().First();

            Assert.True((bool)finishedSpan.Tags["boolTrueTag"]);
            Assert.False((bool)finishedSpan.Tags["boolFalseTag"]);
            Assert.Equal(0, finishedSpan.Tags["intTag"]);
            Assert.Equal("test", finishedSpan.Tags["stringTag"]);
            Assert.Equal(0.1, finishedSpan.Tags["doubleTag"]);
        }
        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);
        }