Esempio n. 1
0
        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.GetSpans().First();

            // we expect there to be 2 logs and 3 tags
            Assert.True(finishedSpan.LogData.Count == 2);
            Assert.True(finishedSpan.Tags.Count == 3);
        }
Esempio n. 2
0
        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));
        }
Esempio n. 3
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. 4
0
        public void ReportShouldBeJsonWithJsonOption()
        {
            var recorder = new SimpleMockRecorder();
            var tracer   = GetTracer(recorder);
            var span     = tracer.BuildSpan("test").Start();

            span.Finish();

            var client          = GetClient(TransportOptions.JsonHttp);
            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);
        }
Esempio n. 5
0
        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.GetSpans().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);
        }
Esempio n. 6
0
        public void TracerShouldTrapExceptions()
        {
            var x = false;
            Action <Exception> eh = delegate { x = true; };
            var collectorOptions  = new CollectorOptions("localhost", 80, true);
            var tracerOptions     = new Options().WithCollector(collectorOptions).WithExceptionHandler(eh.Invoke);
            var recorder          = new SimpleMockRecorder();
            var mockClient        = new Mock <ISplunkTracingHttpClient>();

            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);
        }
Esempio n. 7
0
        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.GetSpans().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"]);
        }
Esempio n. 8
0
        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.GetSpans().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"]);
        }
Esempio n. 9
0
        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 CollectorOptions("localhost", 8088, true);
            var tracerOpts = new Options("TEST").WithCollector(satOpts).WithAutomaticReporting(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(hexTraceId, data["ot-tracer-traceid"]);
            Assert.Equal(hexTraceId, data["X-B3-TraceId"]);
            Assert.Equal(hexSpanId, 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);
        }