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

Support explicit fixed sampling rate of 100% without ingestion sampling #40338

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -5,9 +5,7 @@

import io.opentelemetry.api.common.AttributeKey;

import static io.opentelemetry.api.common.AttributeKey.booleanKey;
import static io.opentelemetry.api.common.AttributeKey.longKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.api.common.AttributeKey.*;
trask marked this conversation as resolved.
Show resolved Hide resolved

public final class AiSemanticAttributes {

Expand Down Expand Up @@ -48,8 +46,8 @@ public final class AiSemanticAttributes {
public static final AttributeKey<String> OPERATION_NAME =
stringKey("applicationinsights.internal.operation_name");

public static final AttributeKey<Long> ITEM_COUNT =
longKey("applicationinsights.internal.item_count");
public static final AttributeKey<Double> SAMPLE_RATE =
trask marked this conversation as resolved.
Show resolved Hide resolved
doubleKey("applicationinsights.internal.sample_rate");

// marks whether a request is coming from a "real" user, or a "synthetic" user (e.g. a bot or
// health check)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,25 @@ public LogDataMapper(
this.telemetryInitializer = telemetryInitializer;
}

public TelemetryItem map(LogRecordData log, @Nullable String stack, @Nullable Long itemCount) {
if (itemCount == null) {
itemCount = getItemCount(log);
public TelemetryItem map(LogRecordData log, @Nullable String stack, @Nullable Double sampleRate) {
if (sampleRate == null) {
sampleRate = getSampleRate(log);
}
if (stack == null) {
return createMessageTelemetryItem(log, itemCount);
return createMessageTelemetryItem(log, sampleRate);
} else {
return createExceptionTelemetryItem(log, stack, itemCount);
return createExceptionTelemetryItem(log, stack, sampleRate);
}
}

private TelemetryItem createMessageTelemetryItem(LogRecordData log, @Nullable Long itemCount) {
private TelemetryItem createMessageTelemetryItem(LogRecordData log, @Nullable Double sampleRate) {
MessageTelemetryBuilder telemetryBuilder = MessageTelemetryBuilder.create();
telemetryInitializer.accept(telemetryBuilder, log.getResource());

// set standard properties
setOperationTags(telemetryBuilder, log);
setTime(telemetryBuilder, log);
setItemCount(telemetryBuilder, itemCount);
setSampleRate(telemetryBuilder, sampleRate);

// update tags
Attributes attributes = log.getAttributes();
Expand All @@ -140,14 +140,14 @@ private TelemetryItem createMessageTelemetryItem(LogRecordData log, @Nullable Lo
}

private TelemetryItem createExceptionTelemetryItem(
LogRecordData log, String stack, @Nullable Long itemCount) {
LogRecordData log, String stack, @Nullable Double sampleRate) {
ExceptionTelemetryBuilder telemetryBuilder = ExceptionTelemetryBuilder.create();
telemetryInitializer.accept(telemetryBuilder, log.getResource());

// set standard properties
setOperationTags(telemetryBuilder, log);
setTime(telemetryBuilder, log);
setItemCount(telemetryBuilder, itemCount);
setSampleRate(telemetryBuilder, sampleRate);

// update tags
Attributes attributes = log.getAttributes();
Expand Down Expand Up @@ -211,16 +211,15 @@ private static long getTimestampEpochNanosWithFallback(LogRecordData log) {
return log.getObservedTimestampEpochNanos();
}


private static void setItemCount(AbstractTelemetryBuilder telemetryBuilder, @Nullable Long itemCount) {
if (itemCount != null) {
telemetryBuilder.setSampleRate(100.0f / itemCount);
private static void setSampleRate(AbstractTelemetryBuilder telemetryBuilder, @Nullable Double sampleRate) {
if (sampleRate != null) {
telemetryBuilder.setSampleRate(sampleRate.floatValue());
}
}

@Nullable
private static Long getItemCount(LogRecordData log) {
return log.getAttributes().get(AiSemanticAttributes.ITEM_COUNT);
private static Double getSampleRate(LogRecordData log) {
return log.getAttributes().get(AiSemanticAttributes.SAMPLE_RATE);
}

private static void setFunctionExtraTraceAttributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,27 +124,27 @@ public SpanDataMapper(
}

public TelemetryItem map(SpanData span) {
Long itemCount = getItemCount(span);
return map(span, itemCount);
Double sampleRate = getSampleRate(span);
return map(span, sampleRate);
}

public void map(SpanData span, Consumer<TelemetryItem> consumer) {
Long itemCount = getItemCount(span);
TelemetryItem telemetryItem = map(span, itemCount);
Double sampleRate = getSampleRate(span);
TelemetryItem telemetryItem = map(span, sampleRate);
consumer.accept(telemetryItem);
exportEvents(
span,
telemetryItem.getTags().get(ContextTagKeys.AI_OPERATION_NAME.toString()),
itemCount,
sampleRate,
consumer);
}

// TODO looks like this method can be private
public TelemetryItem map(SpanData span, @Nullable Long itemCount) {
public TelemetryItem map(SpanData span, @Nullable Double sampleRate) {
if (RequestChecker.isRequest(span)) {
return exportRequest(span, itemCount);
return exportRequest(span, sampleRate);
} else {
return exportRemoteDependency(span, span.getKind() == SpanKind.INTERNAL, itemCount);
return exportRemoteDependency(span, span.getKind() == SpanKind.INTERNAL, sampleRate);
}
}

Expand All @@ -154,14 +154,14 @@ private static boolean checkIsPreAggregatedStandardMetric(SpanData span) {
return isPreAggregatedStandardMetric != null && isPreAggregatedStandardMetric;
}

private TelemetryItem exportRemoteDependency(SpanData span, boolean inProc, @Nullable Long itemCount) {
private TelemetryItem exportRemoteDependency(SpanData span, boolean inProc, @Nullable Double sampleRate) {
RemoteDependencyTelemetryBuilder telemetryBuilder = RemoteDependencyTelemetryBuilder.create();
telemetryInitializer.accept(telemetryBuilder, span.getResource());

// set standard properties
setOperationTags(telemetryBuilder, span);
setTime(telemetryBuilder, span.getStartEpochNanos());
setItemCount(telemetryBuilder, itemCount);
setSampleRate(telemetryBuilder, sampleRate);

// update tags
MAPPINGS.map(span.getAttributes(), telemetryBuilder);
Expand Down Expand Up @@ -502,7 +502,7 @@ private static int getDefaultPortForDbSystem(String dbSystem) {
}
}

private TelemetryItem exportRequest(SpanData span, @Nullable Long itemCount) {
private TelemetryItem exportRequest(SpanData span, @Nullable Double sampleRate) {
RequestTelemetryBuilder telemetryBuilder = RequestTelemetryBuilder.create();
telemetryInitializer.accept(telemetryBuilder, span.getResource());

Expand All @@ -512,7 +512,7 @@ private TelemetryItem exportRequest(SpanData span, @Nullable Long itemCount) {
// set standard properties
telemetryBuilder.setId(span.getSpanId());
setTime(telemetryBuilder, startEpochNanos);
setItemCount(telemetryBuilder, itemCount);
setSampleRate(telemetryBuilder, sampleRate);

// update tags
MAPPINGS.map(attributes, telemetryBuilder);
Expand Down Expand Up @@ -772,7 +772,7 @@ private static String nullAwareConcat(
private void exportEvents(
SpanData span,
@Nullable String operationName,
@Nullable Long itemCount,
@Nullable Double sampleRate,
Consumer<TelemetryItem> consumer) {
for (EventData event : span.getEvents()) {
String instrumentationScopeName = span.getInstrumentationScopeInfo().getName();
Expand All @@ -791,7 +791,7 @@ private void exportEvents(
if (stacktrace != null && !shouldSuppress.test(span, event)) {
String exceptionLogged = span.getAttributes().get(AiSemanticAttributes.LOGGED_EXCEPTION);
if (!stacktrace.equals(exceptionLogged)) {
consumer.accept(createExceptionTelemetryItem(event.getAttributes().get(SemanticAttributes.EXCEPTION_STACKTRACE), span, operationName, itemCount));
consumer.accept(createExceptionTelemetryItem(event.getAttributes().get(SemanticAttributes.EXCEPTION_STACKTRACE), span, operationName, sampleRate));
}
}
}
Expand All @@ -810,7 +810,7 @@ private void exportEvents(
setOperationName(telemetryBuilder, span.getAttributes());
}
setTime(telemetryBuilder, event.getEpochNanos());
setItemCount(telemetryBuilder, itemCount);
setSampleRate(telemetryBuilder, sampleRate);

// update tags
MAPPINGS.map(event.getAttributes(), telemetryBuilder);
Expand All @@ -823,7 +823,7 @@ private void exportEvents(
}

private TelemetryItem createExceptionTelemetryItem(
String errorStack, SpanData span, @Nullable String operationName, @Nullable Long itemCount) {
String errorStack, SpanData span, @Nullable String operationName, @Nullable Double sampleRate) {

ExceptionTelemetryBuilder telemetryBuilder = ExceptionTelemetryBuilder.create();
telemetryInitializer.accept(telemetryBuilder, span.getResource());
Expand All @@ -837,7 +837,7 @@ private TelemetryItem createExceptionTelemetryItem(
setOperationName(telemetryBuilder, span.getAttributes());
}
setTime(telemetryBuilder, span.getEndEpochNanos());
setItemCount(telemetryBuilder, itemCount);
setSampleRate(telemetryBuilder, sampleRate);

MAPPINGS.map(span.getAttributes(), telemetryBuilder);

Expand All @@ -860,15 +860,15 @@ private static void setTime(AbstractTelemetryBuilder telemetryBuilder, long epoc
telemetryBuilder.setTime(FormattedTime.offSetDateTimeFromEpochNanos(epochNanos));
}

private static void setItemCount(AbstractTelemetryBuilder telemetryBuilder, @Nullable Long itemCount) {
if (itemCount != null) {
telemetryBuilder.setSampleRate(100.0f / itemCount);
private static void setSampleRate(AbstractTelemetryBuilder telemetryBuilder, @Nullable Double sampleRate) {
if (sampleRate != null) {
telemetryBuilder.setSampleRate(sampleRate.floatValue());
}
}

@Nullable
private static Long getItemCount(SpanData span) {
return span.getAttributes().get(AiSemanticAttributes.ITEM_COUNT);
private static Double getSampleRate(SpanData span) {
return span.getAttributes().get(AiSemanticAttributes.SAMPLE_RATE);
}

private static void addLinks(AbstractTelemetryBuilder telemetryBuilder, List<LinkData> links) {
Expand Down