Skip to content

Commit

Permalink
integration changes inspectIT#1
Browse files Browse the repository at this point in the history
  • Loading branch information
ivansenic committed Apr 13, 2020
1 parent 4d21a5a commit a41b14a
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public enum AddCommonTags {
NEVER,
ON_GLOBAL_ROOT,
ON_LOCAL_ROOT,
ALWAYS;
ALWAYS
}

/**
Expand Down Expand Up @@ -49,6 +49,6 @@ public enum AddCommonTags {
* Generically defines behavior of adding common tags to spans.
*/
@NotNull
private AddCommonTags addTags;
private AddCommonTags addCommonTags;

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

# defines to which spans to add common tags as attributes
# defines to when to add common tags as attributes to spans
# options are: NEVER, ON_GLOBAL_ROOT, ON_LOCAL_ROOT, ALWAYS
add-tags: ON_LOCAL_ROOT
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 @@ -101,7 +101,7 @@ private List<IHookAction> buildTracingEntryActions(RuleTracingSettings tracing)
if (tracing.getStartSpan() || tracing.getContinueSpan() != null) {

val actionBuilder = ContinueOrStartSpanAction.builder();
actionBuilder.commonTagsToAttributesAction(commonTagsToAttributesManager.getAction());
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,7 +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.CommonTagsToAttributesAction;
import rocks.inspectit.ocelot.core.instrumentation.hook.tags.CommonTagsToAttributesManager;

import java.util.function.Predicate;

Expand Down Expand Up @@ -69,7 +69,7 @@ public class ContinueOrStartSpanAction implements IHookAction {
/**
* Action that optionally adds common tags to the newly started span.
*/
private CommonTagsToAttributesAction commonTagsToAttributesAction;
private CommonTagsToAttributesManager commonTagsToAttributesManager;

@Override
public String getName() {
Expand Down Expand Up @@ -111,10 +111,8 @@ private void startSpan(ExecutionContext context) {
if (remoteParent != null) {
builder = Tracing.getTracer().spanBuilderWithRemoteParent(spanName, remoteParent);
} else {
// TODO is there better way of checking ctx.hasEnteredSpan();
// to avoid this stupid BlankSpan impl
final Span currentSpan = Tracing.getTracer().getCurrentSpan();
hasLocalParent = currentSpan != null && !BlankSpan.INSTANCE.equals(currentSpan);
hasLocalParent = currentSpan != BlankSpan.INSTANCE;
builder = Tracing.getTracer().spanBuilder(spanName);
}

Expand All @@ -127,7 +125,7 @@ private void startSpan(ExecutionContext context) {

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

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package rocks.inspectit.ocelot.core.instrumentation.hook.tags;

import com.google.common.annotations.VisibleForTesting;
import lombok.Getter;
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;
Expand All @@ -13,6 +15,9 @@
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 {

Expand All @@ -27,18 +32,18 @@ public class CommonTagsToAttributesManager {
private final CommonTagsManager commonTagsManager;

/**
* Action implementation.
* Currently active setting.
*/
@Getter
private CommonTagsToAttributesAction action;
private TracingSettings.AddCommonTags addCommonTags;

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

/**
Expand All @@ -50,8 +55,38 @@ public CommonTagsToAttributesManager(InspectitEnvironment env, CommonTagsManager
void update() {
InspectitConfig configuration = env.getCurrentConfig();
TracingSettings tracing = configuration.getTracing();
if (!Objects.equals(tracing.getAddTags(), action.getAddCommonTags())) {
this.action = new CommonTagsToAttributesAction(commonTagsManager, tracing.getAddTags());
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;
}
}

Expand Down
Loading

0 comments on commit a41b14a

Please sign in to comment.