Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/675-replaceSpecialCharacters
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKunz authored Apr 16, 2020
2 parents c009496 + 917053e commit fca58c3
Show file tree
Hide file tree
Showing 10 changed files with 398 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ void testAttributeWritingToParentSpan() {
.anySatisfy((sp) -> {
assertThat(sp.getName()).endsWith("TraceSettingsTest.attributesSetter");
assertThat(sp.getAttributes().getAttributeMap())
.hasSize(4)
.hasSize(7)
.containsEntry("entry", AttributeValue.stringAttributeValue("const"))
.containsEntry("exit", AttributeValue.stringAttributeValue("Hello A!"))
.containsEntry("toObfuscate", AttributeValue.stringAttributeValue("***"))
.containsEntry("anything", AttributeValue.stringAttributeValue("***"));
.containsEntry("anything", AttributeValue.stringAttributeValue("***"))
// plus include all common tags (service + key validation only)
.containsEntry("service", AttributeValue.stringAttributeValue("systemtest"))
.containsKeys("host", "host_address");
})

);
Expand All @@ -61,7 +64,8 @@ void testConditionalAttributeWriting() {
.anySatisfy((sp) -> {
assertThat(sp.getName()).endsWith("TraceSettingsTest.attributesSetterWithConditions");
assertThat(sp.getAttributes().getAttributeMap())
.hasSize(0);
.hasSize(3)
.containsKeys("service", "host", "host_address");
})

);
Expand All @@ -72,10 +76,10 @@ void testConditionalAttributeWriting() {
.anySatisfy((sp) -> {
assertThat(sp.getName()).endsWith("TraceSettingsTest.attributesSetterWithConditions");
assertThat(sp.getAttributes().getAttributeMap())
.hasSize(2)
.hasSize(5)
.containsEntry("entry", AttributeValue.stringAttributeValue("const"))
.containsEntry("exit", AttributeValue.stringAttributeValue("Hello B!"));

.containsEntry("exit", AttributeValue.stringAttributeValue("Hello B!"))
.containsKeys("service", "host", "host_address");
})

);
Expand Down Expand Up @@ -152,6 +156,28 @@ void testSpanNameCustomization() {

}

@Test
void testNoCommonTagsOnChild() {
TestUtils.waitForClassInstrumentation(TraceSettingsTest.class, 15, TimeUnit.SECONDS);

namedA("whatever");

assertTraceExported((spans) ->
assertThat(spans)
.hasSize(2)
.anySatisfy((sp) -> {
assertThat(sp.getParentSpanId()).isNull();
assertThat(sp.getAttributes().getAttributeMap()).hasSize(3);
})
.anySatisfy((sp) -> {
assertThat(sp.getParentSpanId()).isNotNull();
assertThat(sp.getAttributes().getAttributeMap()).hasSize(0);
})

);

}

static class AsyncTask {
void doAsync(String att1, String att2, String att3, boolean isFinished) {
}
Expand Down Expand Up @@ -194,14 +220,16 @@ void testInterleavedAsyncSpans() throws Exception {

//ensure that all method invocations have been combined to single spans
assertThat(firstSpan.getAttributes().getAttributeMap())
.hasSize(2)
.hasSize(5)
.containsEntry("1", AttributeValue.stringAttributeValue("a1"))
.containsEntry("2", AttributeValue.stringAttributeValue("a2"));
.containsEntry("2", AttributeValue.stringAttributeValue("a2"))
.containsKeys("service", "host", "host_address");
assertThat(secondSpan.getAttributes().getAttributeMap())
.hasSize(3)
.hasSize(6)
.containsEntry("1", AttributeValue.stringAttributeValue("b1"))
.containsEntry("2", AttributeValue.stringAttributeValue("b2"))
.containsEntry("3", AttributeValue.stringAttributeValue("b3"));
.containsEntry("3", AttributeValue.stringAttributeValue("b3"))
.containsKeys("service", "host", "host_address");

//ensure that the timings are valid
assertThat(firstSpan.getEndTimestamp()).isLessThan(secondSpan.getEndTimestamp());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

@Data
@NoArgsConstructor
public class TracingSettings {

/**
* Enum that defines when are common tags added to span attributes.
*/
public enum AddCommonTags {
NEVER,
ON_GLOBAL_ROOT,
ON_LOCAL_ROOT,
ALWAYS
}

/**
* Master switch for disabling trace recording and exporting.
* If disabled the following happens:
Expand All @@ -32,4 +43,12 @@ public class TracingSettings {
*/
@Valid
private LogCorrelationSettings logCorrelation = new LogCorrelationSettings();


/**
* Generically defines behavior of adding common tags to spans.
*/
@NotNull
private AddCommonTags addCommonTags;

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ inspectit:
# this value can be overridden by the tracing settings of individual instrumentation rules.
sample-probability: 1.0

# defines when to add common tags as attributes to spans
# options are: NEVER, ON_GLOBAL_ROOT, ON_LOCAL_ROOT, ALWAYS
add-common-tags: ON_LOCAL_ROOT

# settings regarding log correlation
log-correlation:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import rocks.inspectit.ocelot.core.instrumentation.hook.actions.MetricsRecorder;
import rocks.inspectit.ocelot.core.instrumentation.hook.actions.model.MetricAccessor;
import rocks.inspectit.ocelot.core.instrumentation.hook.actions.span.*;
import rocks.inspectit.ocelot.core.instrumentation.hook.tags.CommonTagsToAttributesManager;
import rocks.inspectit.ocelot.core.metrics.MeasuresAndViewsManager;
import rocks.inspectit.ocelot.core.privacy.obfuscation.ObfuscationManager;
import rocks.inspectit.ocelot.core.tags.CommonTagsManager;
Expand Down Expand Up @@ -56,6 +57,9 @@ public class MethodHookGenerator {
@Autowired
private ObfuscationManager obfuscationManager;

@Autowired
private CommonTagsToAttributesManager commonTagsToAttributesManager;

/**
* Builds a executable method hook based on the given configuration.
*
Expand Down Expand Up @@ -97,7 +101,7 @@ private List<IHookAction> buildTracingEntryActions(RuleTracingSettings tracing)
if (tracing.getStartSpan() || tracing.getContinueSpan() != null) {

val actionBuilder = ContinueOrStartSpanAction.builder();

actionBuilder.commonTagsToAttributesManager(commonTagsToAttributesManager);

if (tracing.getStartSpan()) {
VariableAccessor name = Optional.ofNullable(tracing.getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import rocks.inspectit.ocelot.core.instrumentation.hook.MethodReflectionInformation;
import rocks.inspectit.ocelot.core.instrumentation.hook.VariableAccessor;
import rocks.inspectit.ocelot.core.instrumentation.hook.actions.IHookAction;
import rocks.inspectit.ocelot.core.instrumentation.hook.tags.CommonTagsToAttributesManager;

import java.util.function.Predicate;

Expand Down Expand Up @@ -65,6 +66,11 @@ public class ContinueOrStartSpanAction implements IHookAction {
*/
private Predicate<ExecutionContext> startSpanCondition;

/**
* Action that optionally adds common tags to the newly started span.
*/
private CommonTagsToAttributesManager commonTagsToAttributesManager;

@Override
public String getName() {
return "Span continuing / creation";
Expand Down Expand Up @@ -92,23 +98,37 @@ private boolean continueSpan(ExecutionContext context) {

private void startSpan(ExecutionContext context) {
if (startSpanCondition.test(context)) {
// resolve span name
InspectitContextImpl ctx = context.getInspectitContext();

String spanName = getSpanName(context, context.getHook().getMethodInformation());

// load remote parent if it exist
SpanContext remoteParent = ctx.getAndClearCurrentRemoteSpanContext();
boolean hasLocalParent = false;

// create builder based on the remote parent availability
SpanBuilder builder;
if (remoteParent != null) {
builder = Tracing.getTracer().spanBuilderWithRemoteParent(spanName, remoteParent);
} else {
final Span currentSpan = Tracing.getTracer().getCurrentSpan();
hasLocalParent = currentSpan != BlankSpan.INSTANCE;
builder = Tracing.getTracer().spanBuilder(spanName);
}

// resolve kind and sampler
builder.setSpanKind(spanKind);
Sampler sampler = getSampler(context);
if (sampler != null) {
builder.setSampler(sampler);
}

ctx.enterSpan(builder.startSpan());
// start span and add common tags
final Span span = builder.startSpan();
commonTagsToAttributesManager.writeCommonTags(span, remoteParent != null, hasLocalParent);

// enter in the our context
ctx.enterSpan(span);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package rocks.inspectit.ocelot.core.instrumentation.hook.tags;

import com.google.common.annotations.VisibleForTesting;
import io.opencensus.trace.AttributeValue;
import io.opencensus.trace.Span;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import rocks.inspectit.ocelot.config.model.InspectitConfig;
import rocks.inspectit.ocelot.config.model.tracing.TracingSettings;
import rocks.inspectit.ocelot.core.config.InspectitConfigChangedEvent;
import rocks.inspectit.ocelot.core.config.InspectitEnvironment;
import rocks.inspectit.ocelot.core.tags.CommonTagsManager;

import javax.annotation.PostConstruct;
import java.util.Objects;

/**
* This class is used when creating spans to determine if the common tags should be added to the created span or not.
*/
@Component
public class CommonTagsToAttributesManager {

/**
* Environment.
*/
private final InspectitEnvironment env;

/**
* Common tags manager.
*/
private final CommonTagsManager commonTagsManager;

/**
* Currently active setting.
*/
private TracingSettings.AddCommonTags addCommonTags;

/**
* Default constructor.
*/
@Autowired
public CommonTagsToAttributesManager(InspectitEnvironment env, CommonTagsManager commonTagsManager) {
this.env = env;
this.commonTagsManager = commonTagsManager;
this.addCommonTags = TracingSettings.AddCommonTags.NEVER;
}

/**
* Creates the new #action based on the current config.
*/
@EventListener(InspectitConfigChangedEvent.class)
@PostConstruct
@VisibleForTesting
void update() {
InspectitConfig configuration = env.getCurrentConfig();
TracingSettings tracing = configuration.getTracing();
if (!Objects.equals(tracing.getAddCommonTags(), addCommonTags)) {
this.addCommonTags = tracing.getAddCommonTags();
}
}

/**
* Writes common tags to span depending on the current {@link #addCommonTags} setting and the provided information about the span.
*
* @param span Span
* @param hasRemoteParent If span has remote parent
* @param hasLocalParent If span has local parent
*/
public void writeCommonTags(Span span, boolean hasRemoteParent, boolean hasLocalParent) {
if (shouldAdd(hasRemoteParent, hasLocalParent)) {
commonTagsManager.getCommonTagValueMap()
.forEach((k, v) -> span.putAttribute(k, AttributeValue.stringAttributeValue(v)));
}
}

/**
* If tags should be added.
*/
private boolean shouldAdd(boolean hasRemoteParent, boolean hasLocalParent) {
switch (addCommonTags) {
case ALWAYS:
return true;
case ON_LOCAL_ROOT:
return !hasLocalParent;
case ON_GLOBAL_ROOT:
return !hasRemoteParent && !hasLocalParent;
default:
return false;
}
}

}
Loading

0 comments on commit fca58c3

Please # to comment.