|
| 1 | +package datadog.trace.instrumentation.mule4; |
| 2 | + |
| 3 | +import datadog.trace.api.Pair; |
| 4 | +import datadog.trace.bootstrap.CallDepthThreadLocalMap; |
| 5 | +import datadog.trace.bootstrap.ContextStore; |
| 6 | +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
| 7 | +import datadog.trace.bootstrap.instrumentation.api.AgentTracer; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Optional; |
| 10 | +import java.util.function.Supplier; |
| 11 | +import org.mule.runtime.api.event.EventContext; |
| 12 | +import org.mule.runtime.api.message.Error; |
| 13 | +import org.mule.runtime.api.profiling.tracing.Span; |
| 14 | +import org.mule.runtime.api.profiling.tracing.SpanError; |
| 15 | +import org.mule.runtime.core.api.event.CoreEvent; |
| 16 | +import org.mule.runtime.tracer.api.EventTracer; |
| 17 | +import org.mule.runtime.tracer.api.context.getter.DistributedTraceContextGetter; |
| 18 | +import org.mule.runtime.tracer.api.sniffer.SpanSnifferManager; |
| 19 | +import org.mule.runtime.tracer.api.span.info.InitialSpanInfo; |
| 20 | +import org.mule.runtime.tracer.api.span.validation.Assertion; |
| 21 | + |
| 22 | +public class DDEventTracer implements EventTracer<CoreEvent> { |
| 23 | + |
| 24 | + private final ContextStore<Span, AgentSpan> spanStore; |
| 25 | + private final ContextStore<EventContext, Pair> contextStore; |
| 26 | + private final EventTracer<CoreEvent> delegate; |
| 27 | + |
| 28 | + public DDEventTracer( |
| 29 | + ContextStore<Span, AgentSpan> spanStore, |
| 30 | + ContextStore<EventContext, Pair> contextStore, |
| 31 | + EventTracer<CoreEvent> delegate) { |
| 32 | + this.spanStore = spanStore; |
| 33 | + this.contextStore = contextStore; |
| 34 | + this.delegate = delegate; |
| 35 | + } |
| 36 | + |
| 37 | + private AgentSpan fromMuleSpan(Span muleSpan, InitialSpanInfo spanInfo) { |
| 38 | + final AgentSpan parent = |
| 39 | + muleSpan.getParent() != null ? spanStore.get(muleSpan.getParent()) : null; |
| 40 | + |
| 41 | + final AgentSpan span; |
| 42 | + if (parent == null) { |
| 43 | + span = AgentTracer.startSpan("mule", muleSpan.getName()); |
| 44 | + } else { |
| 45 | + span = AgentTracer.startSpan("mule", muleSpan.getName(), parent.context()); |
| 46 | + } |
| 47 | + spanInfo.forEachAttribute(span::setTag); |
| 48 | + return span; |
| 49 | + } |
| 50 | + |
| 51 | + private void activateOnContext(EventContext eventContext, AgentSpan span, Span muleSpan) { |
| 52 | + contextStore.put(eventContext, Pair.of(span, muleSpan)); |
| 53 | + CurrentEventHelper.attachSpanToEventContext(eventContext, contextStore); |
| 54 | + } |
| 55 | + |
| 56 | + private Optional<Span> handleNewSpan( |
| 57 | + CoreEvent event, Optional<Span> maybeSpan, InitialSpanInfo spanInfo) { |
| 58 | + if (!maybeSpan.isPresent() || CallDepthThreadLocalMap.incrementCallDepth(Span.class) > 0) { |
| 59 | + return maybeSpan; |
| 60 | + } |
| 61 | + try { |
| 62 | + final Span muleSpan = maybeSpan.get(); |
| 63 | + final AgentSpan span = fromMuleSpan(muleSpan, spanInfo); |
| 64 | + spanStore.put(muleSpan, span); |
| 65 | + activateOnContext(event.getContext(), span, muleSpan); |
| 66 | + } finally { |
| 67 | + CallDepthThreadLocalMap.reset(Span.class); |
| 68 | + } |
| 69 | + return maybeSpan; |
| 70 | + } |
| 71 | + |
| 72 | + private void handleEndOfSpan(CoreEvent event) { |
| 73 | + if (CallDepthThreadLocalMap.incrementCallDepth(Span.class) > 0) { |
| 74 | + return; |
| 75 | + } |
| 76 | + try { |
| 77 | + final Pair<AgentSpan, Span> pair = contextStore.get(event.getContext()); |
| 78 | + if (pair != null && pair.hasLeft() && pair.hasRight()) { |
| 79 | + final AgentSpan span = pair.getLeft(); |
| 80 | + final Span muleSpan = pair.getRight(); |
| 81 | + spanStore.remove(muleSpan); |
| 82 | + if (muleSpan.hasErrors()) { |
| 83 | + span.setError(true); |
| 84 | + for (final SpanError spanError : muleSpan.getErrors()) { |
| 85 | + if (spanError.getError() != null && spanError.getError().getCause() != null) { |
| 86 | + span.addThrowable(spanError.getError().getCause()); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + span.finish(); |
| 91 | + final Span muleParent = muleSpan.getParent(); |
| 92 | + if (muleParent != null) { |
| 93 | + final AgentSpan parent = spanStore.get(muleParent); |
| 94 | + if (parent != null) { |
| 95 | + activateOnContext(event.getContext(), parent, muleParent); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } finally { |
| 100 | + CallDepthThreadLocalMap.reset(Span.class); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public Optional<Span> startSpan(CoreEvent event, InitialSpanInfo spanInfo) { |
| 106 | + return handleNewSpan(event, delegate.startSpan(event, spanInfo), spanInfo); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public Optional<Span> startSpan(CoreEvent event, InitialSpanInfo spanInfo, Assertion assertion) { |
| 111 | + return handleNewSpan(event, delegate.startSpan(event, spanInfo, assertion), spanInfo); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void endCurrentSpan(CoreEvent event) { |
| 116 | + delegate.endCurrentSpan(event); |
| 117 | + handleEndOfSpan(event); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void endCurrentSpan(CoreEvent event, Assertion condition) { |
| 122 | + delegate.endCurrentSpan(event, condition); |
| 123 | + handleEndOfSpan(event); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void injectDistributedTraceContext( |
| 128 | + EventContext eventContext, DistributedTraceContextGetter distributedTraceContextGetter) { |
| 129 | + delegate.injectDistributedTraceContext(eventContext, distributedTraceContextGetter); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void recordErrorAtCurrentSpan( |
| 134 | + CoreEvent event, Supplier<Error> errorSupplier, boolean isErrorEscapingCurrentSpan) { |
| 135 | + delegate.recordErrorAtCurrentSpan(event, errorSupplier, isErrorEscapingCurrentSpan); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void setCurrentSpanName(CoreEvent event, String name) { |
| 140 | + delegate.setCurrentSpanName(event, name); |
| 141 | + Pair<AgentSpan, Span> pair = contextStore.get(event.getContext()); |
| 142 | + if (pair != null && pair.hasLeft()) { |
| 143 | + pair.getLeft().setOperationName(name); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void addCurrentSpanAttribute(CoreEvent event, String key, String value) { |
| 149 | + delegate.addCurrentSpanAttribute(event, key, value); |
| 150 | + Pair<AgentSpan, Span> pair = contextStore.get(event.getContext()); |
| 151 | + if (pair != null && pair.hasLeft()) { |
| 152 | + pair.getLeft().setTag(key, value); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void addCurrentSpanAttributes(CoreEvent event, Map<String, String> attributes) { |
| 158 | + delegate.addCurrentSpanAttributes(event, attributes); |
| 159 | + Pair<AgentSpan, Span> pair = contextStore.get(event.getContext()); |
| 160 | + if (pair != null && pair.hasLeft()) { |
| 161 | + final AgentSpan span = pair.getLeft(); |
| 162 | + for (Map.Entry<String, String> entry : attributes.entrySet()) { |
| 163 | + span.setTag(entry.getKey(), entry.getValue()); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public SpanSnifferManager getSpanSnifferManager() { |
| 170 | + return delegate.getSpanSnifferManager(); |
| 171 | + } |
| 172 | +} |
0 commit comments