void TriggerParentsAndSelfEmission(ISpan span) { if (!HasBeenEmitted(span)) { lock (emissionLock) if (!HasBeenEmitted(span)) { // parents first var parentSpan = GetParentSpanFromBaggageOkayToNotBeReproducible(span); if (parentSpan != null) { TriggerParentsAndSelfEmission(parentSpan); } var whenActivated = span.GetBaggageItem(WhenActivatedKey); var parsedWhenActivated = DateTimeOffset.Parse(whenActivated); var spanName = span.GetBaggageItem(SpanNameKey); emitSpan((span, spanName, parsedWhenActivated)); foreach (var setTagEventArgs in GetSavedSetTagEventsForLater(span)) { emitSetTagEvent(setTagEventArgs); } foreach (var logEventArgs in GetSavedLogEventsForLater(span)) { emitLogEvent(logEventArgs); } span.SetBaggageItem(HasBeenEmittedKey, true.ToString(CultureInfo.InvariantCulture)); } } }
public void TestBaggageOneReference() { ISpan parent = tracer.BuildSpan("foo").Start(); parent.SetBaggageItem("foo", "bar"); ISpan child = tracer.BuildSpan("foo") .AsChildOf(parent) .Start(); child.SetBaggageItem("a", "a"); Assert.Null(parent.GetBaggageItem("a")); Assert.Equal("a", child.GetBaggageItem("a")); Assert.Equal("bar", child.GetBaggageItem("foo")); }
public static bool ShouldCreateUserCrash(ISpan activeSpan) { if (activeSpan == null) { return(false); } return(activeSpan.GetBaggageItem(Headers.ChaosType) == Headers.CreateUserDown); }
public async Task <ActionResult <string> > Get(string name, CancellationToken ct) { using (IScope scope = _tracer.BuildActiveSpan("Hello-get")) { ISpan span = scope.Span; span.LogMessage("baggage", span.GetBaggageItem("baggage")); // Here you can have TaskCanceledException, which will be handled by ExceptionHandlingMiddleware. await Task.Delay(_random.Next(100, 250), ct); HttpStatusCode selectedStatusCode = _httpStatusCodes[_random.Next(_httpStatusCodes.Length)]; span.LogMessage("name", name); span.Log($"Selected status code: {selectedStatusCode}"); // --> Return OK. if (selectedStatusCode == HttpStatusCode.OK) { return(Ok($"Hello {name}.")); } // --> Delay. if (selectedStatusCode == HttpStatusCode.RequestTimeout) { try { // If your method do not accept token in the argument, you can check it here beforehand. ct.ThrowIfCancellationRequested(); await Task.Delay(5000, ct); } catch (OperationCanceledException) { span.Log("The operation was canceled."); return(NoContent()); } // The timeout policy cancel this call earlier, so you won't see this line. span.Log($"After the delay."); } if (selectedStatusCode == HttpStatusCode.InternalServerError) { throw new Exception("Throw exception for test purpose."); } // --> Other returns. return(new ContentResult { StatusCode = (int)selectedStatusCode, Content = $"Selected status code: {selectedStatusCode}" }); } }
ISpan GetParentSpanFromBaggageOkayToNotBeReproducible(ISpan spanStoredOn) { string baggageItem = spanStoredOn.GetBaggageItem(ParentReferenceKey); if (!storedSpans.TryRemove(baggageItem, out ISpan value)) { // bug race condition - cleanup could beat us to the timer // throw new InvalidOperationException("NOT reproducible -- did you properly instrument with OpenTracing or did you orphan/double-Finish/improperly parent a follows-from span somewhere?"); return(null); } return(value); }
public void TestBaggageMultipleReferences() { ISpan parent1 = tracer.BuildSpan("foo").Start(); parent1.SetBaggageItem("foo", "bar"); ISpan parent2 = tracer.BuildSpan("foo").Start(); parent2.SetBaggageItem("foo2", "bar"); ISpan child = tracer.BuildSpan("foo") .AsChildOf(parent1) .AddReference(References.FollowsFrom, parent2.Context) .Start(); child.SetBaggageItem("a", "a"); child.SetBaggageItem("foo2", "b"); Assert.Null(parent1.GetBaggageItem("a")); Assert.Null(parent2.GetBaggageItem("a")); Assert.Equal("a", child.GetBaggageItem("a")); Assert.Equal("bar", child.GetBaggageItem("foo")); Assert.Equal("b", child.GetBaggageItem("foo2")); }
public virtual string GetBaggageItem(string key) => _span.GetBaggageItem(key);
public string GetBaggageItem(string key) { return(_innerSpan.GetBaggageItem(key)); }
void RemoveParentSpanFromBaggageForCleanup(ISpan spanStoredOn) { string baggageItem = spanStoredOn.GetBaggageItem(ParentReferenceKey); storedSpans.TryRemove(baggageItem, out ISpan _); }