Skip to content

Commit

Permalink
include metric tags in tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
EddeCCC committed Dec 4, 2023
1 parent 336160c commit 4cbb251
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
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,7 +202,13 @@ List<IHookAction> buildTracingExitActions(RuleTracingSettings tracing) {
result.add(new SetSpanStatusAction(accessor));
}

val attributes = tracing.getAttributes();
val tracingAttributes = tracing.getAttributes();

//TODO Make this configurable -> if(tracing.autoCopy.enabled)
Collection<MetricRecordingSettings> metrics = config.getMetrics();
Map<String, String> attributes = collectMetricTags(metrics);
attributes.putAll(tracingAttributes);

if (!attributes.isEmpty()) {
Map<String, VariableAccessor> attributeAccessors = new HashMap<>();
attributes.forEach((attribute, variable) -> attributeAccessors.put(attribute, variableAccessorFactory.getVariableAccessor(variable)));
Expand All @@ -219,6 +226,16 @@ List<IHookAction> buildTracingExitActions(RuleTracingSettings tracing) {
return result;
}

private Map<String, String> collectMetricTags(Collection<MetricRecordingSettings> metrics) {
Map<String, String> tags = new HashMap<>();
metrics.forEach(metric -> {
//tags.putAll(metric.getConstantTags());
tags.putAll(metric.getDataTags());
});

return tags;
}

private Optional<IHookAction> buildMetricsRecorder(MethodHookConfiguration config) {
Collection<MetricRecordingSettings> metricRecordingSettings = config.getMetrics();
if (!metricRecordingSettings.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ private void startSpan(ExecutionContext context) {
hasLocalParent = !(currentSpan == Span.getInvalid() || !currentSpan.getSpanContext().isValid());
}

// This is necessary, since the lambda expression needs a final value
// This is necessary, since the lambda expression needs an effectively final value
SpanContext finalRemoteParent = remoteParent;

Sampler sampler = getSampler(context);
AutoCloseable spanCtx = Instances.logTraceCorrelator.startCorrelatedSpanScope(() -> stackTraceSampler.createAndEnterSpan(spanName, finalRemoteParent, sampler, spanKind, methodInfo, autoTrace));
ctx.setSpanScope(spanCtx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ public void verifyNoActionsGeneratedIfNoSpanStartedOrContinued() {
.endSpan(true)
.build();

List<IHookAction> actions = generator.buildTracingExitActions(settings);
MethodHookConfiguration config = MethodHookConfiguration.builder().tracing(settings).build();
List<IHookAction> actions = generator.buildTracingExitActions(config);

assertThat(actions).isEmpty();
}
Expand All @@ -205,7 +206,8 @@ public void verifyActionsGeneratedIfSpanStarted() {
.endSpan(true)
.build();

List<IHookAction> actions = generator.buildTracingExitActions(settings);
MethodHookConfiguration config = MethodHookConfiguration.builder().tracing(settings).build();
List<IHookAction> actions = generator.buildTracingExitActions(config);

assertThat(actions)
.hasSize(3)
Expand All @@ -224,7 +226,8 @@ public void verifyActionsGeneratedIfSpanContinued() {
.endSpan(true)
.build();

List<IHookAction> actions = generator.buildTracingExitActions(settings);
MethodHookConfiguration config = MethodHookConfiguration.builder().tracing(settings).build();
List<IHookAction> actions = generator.buildTracingExitActions(config);

assertThat(actions)
.hasSize(3)
Expand Down

0 comments on commit 4cbb251

Please sign in to comment.