Skip to content

Commit

Permalink
reimplement metrics-tracing-tags
Browse files Browse the repository at this point in the history
  • Loading branch information
EddeCCC committed Dec 8, 2023
1 parent 36a8732 commit d0a1e6c
Show file tree
Hide file tree
Showing 6 changed files with 349 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public enum AddCommonTags {
@NotNull
private AddCommonTags addCommonTags;

/**
* If enabled, metric tags will be added as attributes to tracing within the same rule
*/
@NotNull
private boolean addMetricTags;

/**
* Settings for automatic tracing (stack trace sampling)
*/
Expand All @@ -79,7 +85,7 @@ public enum AddCommonTags {
private long scheduleDelayMillis = 5000;

/**
* I enabled 64 Bit Trace Ids are used instead of the default 128 Bit.
* If enabled, 64 Bit Trace Ids are used instead of the default 128 Bit.
*/
private boolean use64BitTraceIds = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ inspectit:
# 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
# if enabled, metric tags will be added as attributes to tracing within the same rule
add-metric-tags: true
# settings regarding automatic tracing (stack-trace-sampling)
auto-tracing:
frequency: 50ms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public MethodHook buildHook(Class<?> declaringClass, MethodDescription method, M
builder.exitActions(buildActionCalls(config.getPreExitActions(), methodInfo));
builder.exitActions(buildActionCalls(config.getExitActions(), methodInfo));
if (tracingSettings != null) {
List<IHookAction> actions = buildTracingExitActions(tracingSettings);
List<IHookAction> actions = buildTracingExitActions(config);
if (isTracingInternalActions() && config.isTraceExitHook()) {
actions = wrapActionsWithTracing(actions);
}
Expand Down Expand Up @@ -190,7 +190,8 @@ private void configureSampling(RuleTracingSettings tracing, ContinueOrStartSpanA
}

@VisibleForTesting
List<IHookAction> buildTracingExitActions(RuleTracingSettings tracing) {
List<IHookAction> buildTracingExitActions(MethodHookConfiguration config) {
RuleTracingSettings tracing = config.getTracing();
val result = new ArrayList<IHookAction>();

boolean isSpanStartedOrContinued = tracing.getStartSpan() || StringUtils.isNotBlank(tracing.getContinueSpan());
Expand All @@ -201,10 +202,24 @@ List<IHookAction> buildTracingExitActions(RuleTracingSettings tracing) {
result.add(new SetSpanStatusAction(accessor));
}

val attributes = tracing.getAttributes();
if (!attributes.isEmpty()) {
Map<String, String> tracingAttributes = tracing.getAttributes();
Map<String, String> attributes = tracingAttributes;
Map<String, String> constantAttributes = new HashMap<>();

if(addMetricsToTracing()) {
Collection<MetricRecordingSettings> metrics = config.getMetrics();
constantAttributes = collectMetricConstantTags(metrics);
attributes = collectMetricDataTags(metrics);
// write tracing attributes after metric tags, to allow overwriting of metric tags
attributes.putAll(tracingAttributes);
}

if (!attributes.isEmpty() || !constantAttributes.isEmpty()) {
Map<String, VariableAccessor> attributeAccessors = new HashMap<>();
constantAttributes.forEach((attribute, constant) -> attributeAccessors.put(attribute, variableAccessorFactory.getConstantAccessor(constant)));
// if necessary, overwrite constant attributes
attributes.forEach((attribute, variable) -> attributeAccessors.put(attribute, variableAccessorFactory.getVariableAccessor(variable)));

IHookAction endTraceAction = new WriteSpanAttributesAction(attributeAccessors, obfuscationManager.obfuscatorySupplier());
IHookAction actionWithConditions = ConditionalHookAction.wrapWithConditionChecks(tracing.getAttributeConditions(), endTraceAction, variableAccessorFactory);
result.add(actionWithConditions);
Expand All @@ -219,13 +234,24 @@ List<IHookAction> buildTracingExitActions(RuleTracingSettings tracing) {
return result;
}

private Map<String, String> collectMetricDataTags(Collection<MetricRecordingSettings> metrics) {
Map<String, String> dataTags = new HashMap<>();
metrics.forEach(metric -> dataTags.putAll(metric.getDataTags()));
return dataTags;
}

private Map<String, String> collectMetricConstantTags(Collection<MetricRecordingSettings> metrics) {
Map<String, String> constantTags = new HashMap<>();
metrics.forEach(metric -> constantTags.putAll(metric.getConstantTags()));
return constantTags;
}

private Optional<IHookAction> buildMetricsRecorder(MethodHookConfiguration config) {
Collection<MetricRecordingSettings> metricRecordingSettings = config.getMetrics();
if (!metricRecordingSettings.isEmpty()) {
List<MetricAccessor> metricAccessors = metricRecordingSettings.stream()
.map(this::buildMetricAccessor)
.collect(Collectors.toList());

IHookAction recorder = new MetricsRecorder(metricAccessors, commonTagsManager, metricsManager, tagValueGuard);

if (isTracingInternalActions() && config.isTraceExitHook()) {
Expand All @@ -238,6 +264,13 @@ private Optional<IHookAction> buildMetricsRecorder(MethodHookConfiguration confi
}
}

/**
* @return Returns whether metrics tags should be added to tracing as attributes
*/
private boolean addMetricsToTracing() {
return environment.getCurrentConfig().getTracing().isAddMetricTags();
}

/**
* @return Returns whether action tracing should be enabled for internal actions (e.g. {@link MetricsRecorder}).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
public class WriteSpanAttributesAction implements IHookAction {

@Singular
@Getter
private final Map<String, VariableAccessor> attributeAccessors;

private final Supplier<IObfuscatory> obfuscatorySupplier;
Expand Down
Loading

0 comments on commit d0a1e6c

Please sign in to comment.