Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sampling for Azure Functions #2652

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ public static void logErrorOnce(Throwable t) {
}
}

public static boolean shouldSample(String operationId) {
return delegate != null && delegate.shouldSample(operationId);
}

public static long getTotalMilliseconds(
long days, int hours, int minutes, int seconds, int milliseconds) {
return DAYS.toMillis(days)
Expand Down Expand Up @@ -435,5 +439,7 @@ void trackAvailability(
void flush();

void logErrorOnce(Throwable t);

boolean shouldSample(String shouldSample);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@ public void logErrorOnce(Throwable t) {
}
}

@Override
public boolean shouldSample(String operationId) {
return sample(operationId, samplingPercentage);
}

private static void track(
AbstractTelemetryBuilder telemetryBuilder, Map<String, String> tags, boolean applySampling) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ public static class SamplingPreview {
// sampled)
//
// future goal: make parentBased sampling the default if item count is received via tracestate
//
// IMPORTANT if changing this default, we need to keep it at least on Azure Functions
public boolean parentBased;

public List<SamplingOverride> overrides = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

import com.microsoft.applicationinsights.agent.bootstrap.AzureFunctions;
import com.microsoft.applicationinsights.agent.bootstrap.AzureFunctionsCustomDimensions;
import com.microsoft.applicationinsights.agent.bootstrap.BytecodeUtil;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
Expand Down Expand Up @@ -63,6 +65,19 @@ public static Scope methodEnter(@Advice.Argument(0) Object request)
.getTextMapPropagator()
.extract(Context.root(), traceContext, GETTER);
SpanContext spanContext = Span.fromContext(extractedContext).getSpanContext();

// recreate SpanContext to override the trace flags since the host currently always sends "00"
TraceFlags traceFlags =
BytecodeUtil.shouldSample(spanContext.getTraceId())
? TraceFlags.getSampled()
: TraceFlags.getDefault();
spanContext =
SpanContext.createFromRemoteParent(
spanContext.getTraceId(),
spanContext.getSpanId(),
traceFlags,
spanContext.getTraceState());

return Context.current()
.with(Span.wrap(spanContext))
.with(generateCustomDimensions(request, traceContext))
Expand Down