public void HttpRequestMessage_InjectExtract_Identity_WithParent() { var propagator = new B3SpanContextPropagator(); var traceId = TraceId.CreateFromUlong(18446744073709551615); const ulong spanId = 18446744073709551614; const SamplingPriority samplingPriority = SamplingPriority.UserKeep; IHeadersCollection headers = new HttpRequestMessage().Headers.Wrap(); var parentContext = new SpanContext(traceId, spanId, samplingPriority); var traceContext = new TraceContext(null) { SamplingPriority = samplingPriority }; var context = new SpanContext(parentContext, traceContext, null); propagator.Inject(context, headers); AssertExpected(headers, B3HttpHeaderNames.B3TraceId, "0000000000000000ffffffffffffffff"); AssertExpected(headers, B3HttpHeaderNames.B3SpanId, context.SpanId.ToString("x16")); AssertExpected(headers, B3HttpHeaderNames.B3ParentId, "fffffffffffffffe"); AssertExpected(headers, B3HttpHeaderNames.B3Flags, "1"); AssertMissing(headers, B3HttpHeaderNames.B3Sampled); var resultContext = propagator.Extract(headers); Assert.NotNull(resultContext); Assert.Equal(context.SpanId, resultContext.SpanId); Assert.Equal(context.TraceId, resultContext.TraceId); Assert.Equal(samplingPriority, resultContext.SamplingPriority); }
public void WebRequest_InjectExtract_Identity() { var propagator = new B3SpanContextPropagator(); var traceId = TraceId.CreateFromUlong(2147483647); const int spanId = 2147483646; const SamplingPriority samplingPriority = SamplingPriority.AutoReject; IHeadersCollection headers = WebRequest.CreateHttp("http://localhost").Headers.Wrap(); var context = new SpanContext(traceId, spanId, samplingPriority); propagator.Inject(context, headers); AssertExpected(headers, B3HttpHeaderNames.B3TraceId, "0000000000000000000000007fffffff"); AssertExpected(headers, B3HttpHeaderNames.B3SpanId, "000000007ffffffe"); AssertExpected(headers, B3HttpHeaderNames.B3Sampled, "0"); AssertMissing(headers, B3HttpHeaderNames.B3ParentId); AssertMissing(headers, B3HttpHeaderNames.B3Flags); var resultContext = propagator.Extract(headers); Assert.NotNull(resultContext); Assert.Equal(context.SpanId, resultContext.SpanId); Assert.Equal(context.TraceId, resultContext.TraceId); Assert.Equal(context.SamplingPriority, resultContext.SamplingPriority); }
public void Overflow() { var buffer = new Ci.Agent.Payloads.EventsBuffer <Ci.IEvent>(10, Ci.Agent.MessagePack.CIFormatterResolver.Instance); Assert.False(buffer.IsFull); var spanEvent = new SpanEvent(new Span(new SpanContext(TraceId.CreateFromUlong(1), 1), DateTimeOffset.UtcNow)); var result = buffer.TryWrite(spanEvent); Assert.False(result); Assert.Equal(0, buffer.Count); Assert.True(buffer.IsFull); buffer.Lock(); var innerBuffer = buffer.Data; var actualBuffer = innerBuffer.Skip(innerBuffer.Offset).Take(innerBuffer.Count).ToArray(); Assert.True(actualBuffer.Skip(Ci.Agent.Payloads.EventsBuffer <Ci.IEvent> .HeaderSize).All(b => b == 0x0), "No data should have been written to the buffer"); buffer.Clear(); Assert.False(buffer.IsFull); }
public bool TryExtract <TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGetter carrierGetter, out SpanContext?spanContext) where TCarrierGetter : struct, ICarrierGetter <TCarrier> { spanContext = null; if (carrier is not IReadOnlyDictionary <string, string?> ) { return(false); } var traceIdAsUint64 = ParseUtility.ParseUInt64(carrier, carrierGetter, SpanContext.Keys.TraceId); if (traceIdAsUint64 == null) { // a valid traceId is required to use distributed tracing return(false); } var traceId = TraceId.CreateFromUlong(traceIdAsUint64.Value); if (traceId == TraceId.Zero) { // a valid traceId is required to use distributed tracing return(false); } var parentId = ParseUtility.ParseUInt64(carrier, carrierGetter, SpanContext.Keys.ParentId) ?? 0; var samplingPriority = ParseUtility.ParseInt32(carrier, carrierGetter, SpanContext.Keys.SamplingPriority); var origin = ParseUtility.ParseString(carrier, carrierGetter, SpanContext.Keys.Origin); var rawTraceId = ParseUtility.ParseString(carrier, carrierGetter, SpanContext.Keys.RawTraceId); var rawSpanId = ParseUtility.ParseString(carrier, carrierGetter, SpanContext.Keys.RawSpanId); spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, origin, rawTraceId, rawSpanId); return(true); }
public async Task AgentlessTestEventTest() { var sender = new Mock <ICIAgentlessWriterSender>(); var agentlessWriter = new CIAgentlessWriter(sender.Object); var span = new Span(new SpanContext(TraceId.CreateFromUlong(1), 1), DateTimeOffset.UtcNow); span.Type = SpanTypes.Test; span.SetTag(TestTags.Type, TestTags.TypeTest); var expectedPayload = new Ci.Agent.Payloads.CITestCyclePayload(); expectedPayload.TryProcessEvent(new TestEvent(span)); var expectedBytes = expectedPayload.ToArray(); byte[] finalPayload = null; sender.Setup(x => x.SendPayloadAsync(It.IsAny <Ci.Agent.Payloads.EventsPayload>())) .Returns <Ci.Agent.Payloads.EventsPayload>(payload => { finalPayload = payload.ToArray(); return(Task.CompletedTask); }); var trace = new[] { span }; agentlessWriter.WriteTrace(new ArraySegment <Span>(trace)); await agentlessWriter.FlushTracesAsync(); // Force a flush to make sure the trace is written to the API Assert.True(finalPayload.SequenceEqual(expectedBytes)); }
public void EventsBufferTest() { int headerSize = Ci.Agent.Payloads.EventsBuffer <Ci.IEvent> .HeaderSize; var span = new Span(new SpanContext(TraceId.CreateFromUlong(1), 1), DateTimeOffset.UtcNow); var spanEvent = new SpanEvent(span); var individualType = MessagePackSerializer.Serialize <Ci.IEvent>(spanEvent, Ci.Agent.MessagePack.CIFormatterResolver.Instance); int bufferSize = 256; int maxBufferSize = (int)(4.5 * 1024 * 1024); while (bufferSize < maxBufferSize) { var eventBuffer = new Ci.Agent.Payloads.EventsBuffer <Ci.IEvent>(bufferSize, Ci.Agent.MessagePack.CIFormatterResolver.Instance); while (eventBuffer.TryWrite(spanEvent)) { // . } // The number of items in the events should be the same as the num calculated // without decimals (items that doesn't fit doesn't get added) var numItemsTrunc = (bufferSize - headerSize) / individualType.Length; Assert.Equal(numItemsTrunc, eventBuffer.Count); bufferSize *= 2; } }
private static Span GetMyServiceSpan() { var span = new Span(new SpanContext(TraceId.CreateFromUlong(_id++), _id++, null, serviceName: ServiceName), DateTimeOffset.Now) { OperationName = OperationName }; span.SetTag(Tags.Environment, Env); return(span); }
public async Task SlowSenderTest() { var flushTcs = new TaskCompletionSource <bool>(); var sender = new Mock <ICIAgentlessWriterSender>(); var agentlessWriter = new CIAgentlessWriter(sender.Object); var lstPayloads = new List <byte[]>(); sender.Setup(x => x.SendPayloadAsync(It.IsAny <Ci.Agent.Payloads.EventsPayload>())) .Returns <Ci.Agent.Payloads.EventsPayload>(payload => { lstPayloads.Add(payload.ToArray()); return(flushTcs.Task); }); var span = new Span(new SpanContext(TraceId.CreateFromUlong(1), 1), DateTimeOffset.UtcNow); var expectedPayload = new Ci.Agent.Payloads.CITestCyclePayload(); expectedPayload.TryProcessEvent(new SpanEvent(span)); expectedPayload.TryProcessEvent(new SpanEvent(span)); expectedPayload.TryProcessEvent(new SpanEvent(span)); var expectedBytes = expectedPayload.ToArray(); agentlessWriter.WriteEvent(new SpanEvent(span)); agentlessWriter.WriteEvent(new SpanEvent(span)); agentlessWriter.WriteEvent(new SpanEvent(span)); var firstFlush = agentlessWriter.FlushTracesAsync(); agentlessWriter.WriteEvent(new SpanEvent(span)); agentlessWriter.WriteEvent(new SpanEvent(span)); agentlessWriter.WriteEvent(new SpanEvent(span)); var secondFlush = agentlessWriter.FlushTracesAsync(); flushTcs.TrySetResult(true); agentlessWriter.WriteEvent(new SpanEvent(span)); agentlessWriter.WriteEvent(new SpanEvent(span)); agentlessWriter.WriteEvent(new SpanEvent(span)); var thirdFlush = agentlessWriter.FlushTracesAsync(); await Task.WhenAll(firstFlush, secondFlush, thirdFlush); // We expect 3 batches. Assert.Equal(3, lstPayloads.Count); foreach (var payloadBytes in lstPayloads) { Assert.True(payloadBytes.SequenceEqual(expectedBytes)); } }
public void LockingBuffer() { var buffer = new Ci.Agent.Payloads.EventsBuffer <Ci.IEvent>(10 * 1024 * 1024, Ci.Agent.MessagePack.CIFormatterResolver.Instance); var spanEvent = new SpanEvent(new Span(new SpanContext(TraceId.CreateFromUlong(1), 1), DateTimeOffset.UtcNow)); Assert.True(buffer.TryWrite(spanEvent)); buffer.Lock(); Assert.False(buffer.TryWrite(spanEvent)); buffer.Clear(); Assert.True(buffer.TryWrite(spanEvent)); }
public void Extract_InvalidSamplingPriority(string samplingPriority) { var propagator = new B3SpanContextPropagator(); var traceId = TraceId.CreateFromUlong(12345678); const ulong spanId = 23456789; var headers = InjectContext( traceId.ToString(), spanId.ToString("x16", CultureInfo.InvariantCulture), samplingPriority); var resultContext = propagator.Extract(headers); Assert.NotNull(resultContext); Assert.Equal(traceId, resultContext.TraceId); Assert.Equal(spanId, resultContext.SpanId); Assert.Null(resultContext.SamplingPriority); }
public void Extract_InvalidSpanId(string spanId) { var propagator = new B3SpanContextPropagator(); var traceId = TraceId.CreateFromUlong(12345678); const SamplingPriority samplingPriority = SamplingPriority.UserKeep; var headers = InjectContext( traceId.ToString(), spanId, ((int)samplingPriority).ToString(CultureInfo.InvariantCulture)); var resultContext = propagator.Extract(headers); Assert.NotNull(resultContext); Assert.Equal(traceId, resultContext.TraceId); Assert.Equal(default(ulong), resultContext.SpanId); Assert.Equal(samplingPriority, resultContext.SamplingPriority); }
public void ClearingBuffer() { var buffer = new Ci.Agent.Payloads.EventsBuffer <Ci.IEvent>(10 * 1024 * 1024, Ci.Agent.MessagePack.CIFormatterResolver.Instance); var spanEvent = new SpanEvent(new Span(new SpanContext(TraceId.CreateFromUlong(1), 1), DateTimeOffset.UtcNow)); Assert.True(buffer.TryWrite(spanEvent)); Assert.Equal(1, buffer.Count); buffer.Clear(); Assert.Equal(0, buffer.Count); buffer.Lock(); var innerBuffer = buffer.Data; Assert.Equal(Ci.Agent.Payloads.EventsBuffer <Ci.IEvent> .HeaderSize, innerBuffer.Count); }
public async Task WriteTrace_2Traces_SendToApi() { var trace = new[] { new Span(new SpanContext(TraceId.CreateFromUlong(1), 1), DateTimeOffset.UtcNow) }; var expectedData1 = Vendors.MessagePack.MessagePackSerializer.Serialize(trace, SpanFormatterResolver.Instance); _ciAgentWriter.WriteTrace(new ArraySegment <Span>(trace)); await _ciAgentWriter.FlushTracesAsync(); // Force a flush to make sure the trace is written to the API _api.Verify(x => x.SendTracesAsync(It.Is <ArraySegment <byte> >(y => Equals(y, expectedData1)), It.Is <int>(i => i == 1)), Times.Once); _api.Invocations.Clear(); trace = new[] { new Span(new SpanContext(TraceId.CreateFromUlong(2), 2), DateTimeOffset.UtcNow) }; var expectedData2 = Vendors.MessagePack.MessagePackSerializer.Serialize(trace, SpanFormatterResolver.Instance); _ciAgentWriter.WriteTrace(new ArraySegment <Span>(trace)); await _ciAgentWriter.FlushTracesAsync(); // Force a flush to make sure the trace is written to the API _api.Verify(x => x.SendTracesAsync(It.Is <ArraySegment <byte> >(y => Equals(y, expectedData2)), It.Is <int>(i => i == 1)), Times.Once); await _ciAgentWriter.FlushAndCloseAsync(); }
internal static Scope CreatePlaceholderScope(Tracer tracer, string traceId, string samplingPriority) { Span span = null; if (traceId == null) { Serverless.Debug("traceId not found"); span = tracer.StartSpan(PlaceholderOperationName, tags: null, serviceName: PlaceholderServiceName, addToTraceContext: false); } else { Serverless.Debug($"creating the placeholder traceId = {traceId}"); span = tracer.StartSpan(PlaceholderOperationName, tags: null, serviceName: PlaceholderServiceName, traceId: TraceId.CreateFromUlong(Convert.ToUInt64(traceId)), addToTraceContext: false); } if (samplingPriority == null) { Serverless.Debug($"samplingPriority not found"); span.Context.TraceContext.SetSamplingPriority(tracer.TracerManager.Sampler?.GetSamplingPriority(span)); } else { Serverless.Debug($"setting the placeholder sampling proirity to = {samplingPriority}"); span.Context.TraceContext.SetSamplingPriority(Convert.ToInt32(samplingPriority)); } return(tracer.TracerManager.ScopeManager.Activate(span, false)); }
public void CreateFromUlong_CreatesIdCorrectly() { var traceId = TraceId.CreateFromUlong(3212132132132132121); traceId.ToString().Should().Be("3212132132132132121"); }
public void CreateFromUlong_CreatesIdCorrectly() { var traceId = TraceId.CreateFromUlong(3212132132132132121); traceId.ToString().Should().Be("00000000000000002c93c927d35a9519"); }