Skip to content

Commit

Permalink
remove unnecessary changes
Browse files Browse the repository at this point in the history
  • Loading branch information
EddeCCC committed Sep 7, 2023
1 parent 1422ef6 commit 0fde580
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface InternalInspectitContext extends AutoCloseable, InspectitContex
*/
String REMOTE_SESSION_ID = "remote_session_id";

String createRemoteTransactionContext();

/**
* Makes this context the active one.
* This means all new contexts created from this point will use this context as a parent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Iterable<Map.Entry<String, Object>> getData() {
}

@Override
public String createTransactionContext() {
public String createRemoteTransactionContext() {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public interface InspectitContext {
*
* @return The transaction trace context of the current InspectitContext in the W3C-format
*/
String createTransactionContext();
String createRemoteTransactionContext();

/**
* Generates a map representing the globally down-propagated data stored in this context.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.sdk.common.Clock;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -15,7 +16,6 @@
import rocks.inspectit.ocelot.core.config.InspectitConfigChangedEvent;
import rocks.inspectit.ocelot.core.config.InspectitEnvironment;
import rocks.inspectit.ocelot.core.instrumentation.hook.MethodReflectionInformation;
import rocks.inspectit.ocelot.core.instrumentation.context.wrapper.SpanWrapper;
import rocks.inspectit.ocelot.core.opentelemetry.OpenTelemetryControllerImpl;
import rocks.inspectit.ocelot.core.utils.HighPrecisionTimer;
import rocks.inspectit.ocelot.core.utils.OpenTelemetryUtils;
Expand Down Expand Up @@ -134,18 +134,18 @@ void shutdown() {
*
* @return The generated span is activated via {@link Span#makeCurrent()}. The resulting context is wrapped with a context which terminates the stack-trace-sampling.
*/
public Span createAndEnterSpan(String name, SpanContext remoteParent, Sampler sampler, SpanKind kind, MethodReflectionInformation actualMethod, Mode mode) {
public AutoCloseable createAndEnterSpan(String name, SpanContext remoteParent, Sampler sampler, SpanKind kind, MethodReflectionInformation actualMethod, Mode mode) {
Thread self = Thread.currentThread();
SampledTrace activeSampling = activeSamplings.get(self);
if (activeSampling == null) {
if (mode == Mode.ENABLE) {
return startSampling(name, remoteParent, sampler, kind);
} else {
return createNormalSpan(name, remoteParent, sampler, kind);
return createNormalSpan(name, remoteParent, sampler, kind).makeCurrent();
}
} else {
Span samplingAwareSpan = createSamplingAwareSpan(name, remoteParent, kind, actualMethod, activeSampling);
return createPauseAdjustingContext(mode, activeSampling, samplingAwareSpan);
AutoCloseable samplingAwareSpanContext = createSamplingAwareSpan(name, remoteParent, kind, actualMethod, activeSampling);
return createPauseAdjustingContext(mode, activeSampling, samplingAwareSpanContext);
}
}

Expand All @@ -166,18 +166,18 @@ public Span createAndEnterSpan(String name, SpanContext remoteParent, Sampler sa
*
* @return The span is activated via {@link Span#makeCurrent()}. The resulting context is wrapped with a context which terminates the stack-trace-sampling.
*/
public Span continueSpan(Span span, MethodReflectionInformation actualMethod, Mode mode) {
public AutoCloseable continueSpan(Span span, MethodReflectionInformation actualMethod, Mode mode) {
Thread self = Thread.currentThread();
SampledTrace activeSampling = activeSamplings.get(self);
if (activeSampling == null) {
if (mode == Mode.ENABLE) {
return startSampling(span);
} else {
return span;
return span.makeCurrent();
}
} else {
Span samplingAwareSpan = continueSamplingAwareSpan(span, actualMethod, activeSampling);
return createPauseAdjustingContext(mode, activeSampling, samplingAwareSpan);
AutoCloseable samplingAwareSpanContext = continueSamplingAwareSpan(span, actualMethod, activeSampling);
return createPauseAdjustingContext(mode, activeSampling, samplingAwareSpanContext);
}
}

Expand All @@ -190,54 +190,62 @@ public Span continueSpan(Span span, MethodReflectionInformation actualMethod, Mo
*
* @return the wrapped span-context which alters the pause state accordingly.
*/
private Span createPauseAdjustingContext(Mode mode, SampledTrace activeSampling, Span spanContext) {
private AutoCloseable createPauseAdjustingContext(Mode mode, SampledTrace activeSampling, AutoCloseable spanContext) {
if (activeSampling.isPaused() && mode == Mode.ENABLE) {
activeSampling.setPaused(false);
AutoCloseable autoCloseable = () -> activeSampling.setPaused(true);
return new SpanWrapper(spanContext, autoCloseable);
return () -> {
spanContext.close();
activeSampling.setPaused(true);
};
} else if (!activeSampling.isPaused() && mode == Mode.DISABLE) {
activeSampling.setPaused(true);
AutoCloseable autoCloseable = () -> activeSampling.setPaused(false);
return new SpanWrapper(spanContext, autoCloseable);
return () -> {
spanContext.close();
activeSampling.setPaused(false);
};
} else {
return spanContext;
}
}

private Span continueSamplingAwareSpan(Span spanToContinue, MethodReflectionInformation actualMethod, SampledTrace activeSampling) {
private AutoCloseable continueSamplingAwareSpan(Span spanToContinue, MethodReflectionInformation actualMethod, SampledTrace activeSampling) {
SampledTrace.MethodExitNotifier exitCallback = activeSampling.spanContinued(spanToContinue, clock.nanoTime(), actualMethod.getDeclaringClass()
.getName(), actualMethod.getName());
AutoCloseable autoCloseable = () -> exitCallback.methodFinished(clock.nanoTime());
return new SpanWrapper(spanToContinue, autoCloseable);
Scope ctx = spanToContinue.makeCurrent();
return () -> {
ctx.close();
exitCallback.methodFinished(clock.nanoTime());
};
}

private Span startSampling(String name, SpanContext remoteParent, Sampler sampler, SpanKind kind) {
private AutoCloseable startSampling(String name, SpanContext remoteParent, Sampler sampler, SpanKind kind) {
Span rootSpan = createNormalSpan(name, remoteParent, sampler, kind);
return startSampling(rootSpan);
}

private Span startSampling(Span rootSpan) {
private AutoCloseable startSampling(Span rootSpan) {
boolean spanExists = rootSpan.getSpanContext().isValid() && rootSpan.getSpanContext()
.getTraceFlags()
.isSampled();
if (!spanExists) {
return rootSpan;
return rootSpan.makeCurrent();
} else {
Throwable stackTrace = new Throwable(); //the constructor collects the current stack-trace
SampledTrace sampledTrace = new SampledTrace(rootSpan, () -> StackTrace.createFromThrowable(stackTrace));
Thread selfThread = Thread.currentThread();
activeSamplings.put(selfThread, sampledTrace);
sampleTimer.start();
AutoCloseable autoCloseable = () -> {
AutoCloseable spanScope = rootSpan.makeCurrent();
return () -> {
spanScope.close();
activeSamplings.remove(selfThread);
sampledTrace.finish();
addToExportQueue(sampledTrace);
};
return new SpanWrapper(rootSpan, autoCloseable);
}
}

private Span createSamplingAwareSpan(String name, SpanContext remoteParent, SpanKind kind, MethodReflectionInformation actualMethod, SampledTrace activeSampling) {
private AutoCloseable createSamplingAwareSpan(String name, SpanContext remoteParent, SpanKind kind, MethodReflectionInformation actualMethod, SampledTrace activeSampling) {
SpanContext parent = remoteParent;
if (remoteParent == null) {
parent = Span.current().getSpanContext();
Expand All @@ -246,8 +254,11 @@ private Span createSamplingAwareSpan(String name, SpanContext remoteParent, Span

SampledTrace.MethodExitNotifier exitCallback = activeSampling.newSpanStarted(span, actualMethod.getDeclaringClass()
.getName(), actualMethod.getName());
AutoCloseable autoCloseable = () -> exitCallback.methodFinished(clock.nanoTime());
return new SpanWrapper(span, autoCloseable);
io.opentelemetry.context.Scope ctx = span.makeCurrent();
return () -> {
ctx.close();
exitCallback.methodFinished(clock.nanoTime());
};
}

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rocks.inspectit.ocelot.core.instrumentation.context;

import io.opencensus.tags.*;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceState;
Expand Down Expand Up @@ -190,14 +189,27 @@ public class InspectitContextImpl implements InternalInspectitContext {
private Map<String, Object> cachedActivePhaseDownPropagatedData = null;

/**
* Trace context of the current InspectitContext in the W3C-format
*/
private String traceContext;

/**
*
* This span context serves as a placeholder for a remote transaction span context.
* A transaction span is a root span, which contains all other spans created during one session.
* It normally starts with the document load in the frontend.
* <p>
* The documentLoad-span will always be created after the spans in the JVM are closed.
* Thus, it's context cannot be down propagated to the JVM.
* However, the documentLoad-span has to use the same trace-ID as the JVM-spans.
* Since the trace-IDs of the JVM-spans can't be altered afterward, the documentLoad-span has to use the trace-ID, which
* was generated in the JVM.
* Additionally, the documentLoad-span has to serve as a parent-span for the root-span in the JVM to link them correctly.
* Therefore, this placeholder context is created, so it can be used as a parent context in the JVM and stores the
* current trace-ID.
* <p>
* The remote transaction context can be transmitted to the frontend via the Http-response-header.
* After that, the frontend instrumentation can use the context to create the transaction span with the same trace-ID
* as the JVM-spans.
* The transaction span will also be linked to the JVM-span, since the JVM-span used this transaction context as parent context.
* <p>
* Note that this context will not be used as a parent context, if a REMOTE_PARENT_SPAN_CONTEXT_KEY was down-propagated
*/
private SpanContext transactionContext;
private SpanContext remoteTransactionContext;

/**
* Data storage for all tags that should be propagated up to or down from the browser
Expand Down Expand Up @@ -254,20 +266,20 @@ public void setSpanScope(AutoCloseable spanScope) {
}

@Override
public String createTransactionContext() {
public String createRemoteTransactionContext() {
IdGenerator generator = IdGenerator.random();
String traceId = generator.generateTraceId();
String spanId = generator.generateSpanId();
TraceFlags traceFlags = TraceFlags.getSampled();
TraceState traceState = TraceState.getDefault();
this.transactionContext = SpanContext.create(traceId, spanId, traceFlags, traceState);
this.remoteTransactionContext = SpanContext.create(traceId, spanId, traceFlags, traceState);

String traceContext = "00-" + traceId + "-" + spanId + "-" + traceFlags.asHex();
return traceContext;
}

public SpanContext getTransactionContext() {
return this.transactionContext;
public SpanContext getRemoteTransactionContext() {
return this.remoteTransactionContext;
}

/**
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 0fde580

Please sign in to comment.