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

Do not apply sampling to metrics #1942

Merged
merged 1 commit into from
Nov 1, 2021
Merged
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 @@ -105,7 +105,7 @@ public void trackEvent(
telemetry.setInstrumentationKey(instrumentationKey);
}

track(telemetry);
track(telemetry, true);
}

// TODO do not track if perf counter (?)
Expand Down Expand Up @@ -161,7 +161,7 @@ public void trackMetric(
telemetry.setInstrumentationKey(instrumentationKey);
}

track(telemetry);
track(telemetry, false);
}

@Override
Expand Down Expand Up @@ -222,7 +222,7 @@ public void trackDependency(
telemetry.setInstrumentationKey(instrumentationKey);
}

track(telemetry);
track(telemetry, true);
}

@Override
Expand Down Expand Up @@ -269,7 +269,7 @@ public void trackPageView(
telemetry.setInstrumentationKey(instrumentationKey);
}

track(telemetry);
track(telemetry, true);
}

@Override
Expand Down Expand Up @@ -311,7 +311,7 @@ public void trackTrace(
telemetry.setInstrumentationKey(instrumentationKey);
}

track(telemetry);
track(telemetry, true);
}

@Override
Expand Down Expand Up @@ -371,7 +371,7 @@ public void trackRequest(
telemetry.setInstrumentationKey(instrumentationKey);
}

track(telemetry);
track(telemetry, true);
}

@Override
Expand Down Expand Up @@ -412,7 +412,7 @@ public void trackException(
telemetry.setInstrumentationKey(instrumentationKey);
}

track(telemetry);
track(telemetry, true);
}

@Nullable
Expand Down Expand Up @@ -448,22 +448,22 @@ public void logErrorOnce(Throwable t) {
}
}

private static void track(TelemetryItem telemetry) {
private static void track(TelemetryItem telemetry, boolean applySampling) {
SpanContext context = Span.current().getSpanContext();
if (context.isValid()) {
trackInsideValidSpanContext(telemetry, context);
trackInsideValidSpanContext(telemetry, context, applySampling);
} else {
trackAsStandalone(telemetry);
trackAsStandalone(telemetry, applySampling);
}
}

private static void trackInsideValidSpanContext(
TelemetryItem telemetry, SpanContext spanContext) {
TelemetryItem telemetry, SpanContext spanContext, boolean applySampling) {

String operationId = telemetry.getTags().get(ContextTagKeys.AI_OPERATION_ID.toString());

if (operationId != null && !operationId.equals(spanContext.getTraceId())) {
trackAsStandalone(telemetry);
trackAsStandalone(telemetry, applySampling);
return;
}

Expand Down Expand Up @@ -491,30 +491,35 @@ private static void trackInsideValidSpanContext(
}
}

float samplingPercentage =
TelemetryUtil.getSamplingPercentage(
spanContext.getTraceState(), BytecodeUtilImpl.samplingPercentage, false);
if (applySampling) {
float samplingPercentage =
TelemetryUtil.getSamplingPercentage(
spanContext.getTraceState(), BytecodeUtilImpl.samplingPercentage, false);

if (samplingPercentage != 100) {
telemetry.setSampleRate(samplingPercentage);
if (samplingPercentage != 100) {
telemetry.setSampleRate(samplingPercentage);
}
}
// this is not null because sdk instrumentation is not added until TelemetryClient.setActive()
// is called
TelemetryClient.getActive().trackAsync(telemetry);
}

private static void trackAsStandalone(TelemetryItem telemetry) {
// sampling is done using the configured sampling percentage
float samplingPercentage = BytecodeUtilImpl.samplingPercentage;
if (!sample(telemetry, samplingPercentage)) {
// sampled out
return;
}
// sampled in
private static void trackAsStandalone(TelemetryItem telemetry, boolean applySampling) {
if (applySampling) {
// sampling is done using the configured sampling percentage
float samplingPercentage = BytecodeUtilImpl.samplingPercentage;
if (!sample(telemetry, samplingPercentage)) {
// sampled out
return;
}
// sampled in

if (samplingPercentage != 100) {
telemetry.setSampleRate(samplingPercentage);
if (samplingPercentage != 100) {
telemetry.setSampleRate(samplingPercentage);
}
}

// this is not null because sdk instrumentation is not added until TelemetryClient.setActive()
// is called
TelemetryClient.getActive().trackAsync(telemetry);
Expand Down