From 9fcd6857461809b745d2e9a91266b2075d6d0c48 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Wed, 6 Mar 2024 13:26:44 -0800 Subject: [PATCH 1/2] Support ingestion sampling --- .../exporter/implementation/LogDataMapper.java | 8 ++++++-- .../exporter/implementation/SpanDataMapper.java | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/LogDataMapper.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/LogDataMapper.java index 0d5fe67617264..438964f1cf30d 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/LogDataMapper.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/LogDataMapper.java @@ -38,6 +38,8 @@ public class LogDataMapper { private static final AttributeKey LOG4J_MARKER = stringKey("log4j.marker"); private static final AttributeKey> LOGBACK_MARKER = stringArrayKey("logback.marker"); + private static final long USE_INGESTION_SAMPLING = -1; + private static final Mappings MAPPINGS; static { @@ -213,12 +215,14 @@ private static long getTimestampEpochNanosWithFallback(LogRecordData log) { private static void setItemCount(AbstractTelemetryBuilder telemetryBuilder, long itemCount) { - telemetryBuilder.setSampleRate(100.0f / itemCount); + if (itemCount != USE_INGESTION_SAMPLING) { + telemetryBuilder.setSampleRate(100.0f / itemCount); + } } private static long getItemCount(LogRecordData log) { Long itemCount = log.getAttributes().get(AiSemanticAttributes.ITEM_COUNT); - return itemCount == null ? 1 : itemCount; + return itemCount == null ? USE_INGESTION_SAMPLING : itemCount; } private static void setFunctionExtraTraceAttributes( diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/SpanDataMapper.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/SpanDataMapper.java index a87ffc1062a3a..8aab36c8768f5 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/SpanDataMapper.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/SpanDataMapper.java @@ -64,6 +64,8 @@ public final class SpanDataMapper { // this is needed until Azure SDK moves to latest OTel semantic conventions private static final String COSMOS = "Cosmos"; + private static final long USE_INGESTION_SAMPLING = -1; + private static final Mappings MAPPINGS; // TODO (trask) add to generated ContextTagKeys class @@ -837,12 +839,14 @@ private static void setTime(AbstractTelemetryBuilder telemetryBuilder, long epoc } private static void setItemCount(AbstractTelemetryBuilder telemetryBuilder, long itemCount) { - telemetryBuilder.setSampleRate(100.0f / itemCount); + if (itemCount != USE_INGESTION_SAMPLING) { + telemetryBuilder.setSampleRate(100.0f / itemCount); + } } private static long getItemCount(SpanData span) { Long itemCount = span.getAttributes().get(AiSemanticAttributes.ITEM_COUNT); - return itemCount == null ? 1 : itemCount; + return itemCount == null ? USE_INGESTION_SAMPLING : itemCount; } private static void addLinks(AbstractTelemetryBuilder telemetryBuilder, List links) { From d5163639efbf6401016c60bb27e8c6776318a58a Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Wed, 6 Mar 2024 13:52:21 -0800 Subject: [PATCH 2/2] Use Nullable Long --- .../implementation/LogDataMapper.java | 16 +++++------- .../implementation/SpanDataMapper.java | 26 +++++++++---------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/LogDataMapper.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/LogDataMapper.java index 438964f1cf30d..cbf41cc5ba52f 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/LogDataMapper.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/LogDataMapper.java @@ -38,8 +38,6 @@ public class LogDataMapper { private static final AttributeKey LOG4J_MARKER = stringKey("log4j.marker"); private static final AttributeKey> LOGBACK_MARKER = stringArrayKey("logback.marker"); - private static final long USE_INGESTION_SAMPLING = -1; - private static final Mappings MAPPINGS; static { @@ -112,7 +110,7 @@ public TelemetryItem map(LogRecordData log, @Nullable String stack, @Nullable Lo } } - private TelemetryItem createMessageTelemetryItem(LogRecordData log, long itemCount) { + private TelemetryItem createMessageTelemetryItem(LogRecordData log, @Nullable Long itemCount) { MessageTelemetryBuilder telemetryBuilder = MessageTelemetryBuilder.create(); telemetryInitializer.accept(telemetryBuilder, log.getResource()); @@ -142,7 +140,7 @@ private TelemetryItem createMessageTelemetryItem(LogRecordData log, long itemCou } private TelemetryItem createExceptionTelemetryItem( - LogRecordData log, String stack, long itemCount) { + LogRecordData log, String stack, @Nullable Long itemCount) { ExceptionTelemetryBuilder telemetryBuilder = ExceptionTelemetryBuilder.create(); telemetryInitializer.accept(telemetryBuilder, log.getResource()); @@ -214,15 +212,15 @@ private static long getTimestampEpochNanosWithFallback(LogRecordData log) { } - private static void setItemCount(AbstractTelemetryBuilder telemetryBuilder, long itemCount) { - if (itemCount != USE_INGESTION_SAMPLING) { + private static void setItemCount(AbstractTelemetryBuilder telemetryBuilder, @Nullable Long itemCount) { + if (itemCount != null) { telemetryBuilder.setSampleRate(100.0f / itemCount); } } - private static long getItemCount(LogRecordData log) { - Long itemCount = log.getAttributes().get(AiSemanticAttributes.ITEM_COUNT); - return itemCount == null ? USE_INGESTION_SAMPLING : itemCount; + @Nullable + private static Long getItemCount(LogRecordData log) { + return log.getAttributes().get(AiSemanticAttributes.ITEM_COUNT); } private static void setFunctionExtraTraceAttributes( diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/SpanDataMapper.java b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/SpanDataMapper.java index 8aab36c8768f5..380eaec5d9e1c 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/SpanDataMapper.java +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/SpanDataMapper.java @@ -64,8 +64,6 @@ public final class SpanDataMapper { // this is needed until Azure SDK moves to latest OTel semantic conventions private static final String COSMOS = "Cosmos"; - private static final long USE_INGESTION_SAMPLING = -1; - private static final Mappings MAPPINGS; // TODO (trask) add to generated ContextTagKeys class @@ -126,12 +124,12 @@ public SpanDataMapper( } public TelemetryItem map(SpanData span) { - long itemCount = getItemCount(span); + Long itemCount = getItemCount(span); return map(span, itemCount); } public void map(SpanData span, Consumer consumer) { - long itemCount = getItemCount(span); + Long itemCount = getItemCount(span); TelemetryItem telemetryItem = map(span, itemCount); consumer.accept(telemetryItem); exportEvents( @@ -142,7 +140,7 @@ public void map(SpanData span, Consumer consumer) { } // TODO looks like this method can be private - public TelemetryItem map(SpanData span, long itemCount) { + public TelemetryItem map(SpanData span, @Nullable Long itemCount) { if (RequestChecker.isRequest(span)) { return exportRequest(span, itemCount); } else { @@ -156,7 +154,7 @@ private static boolean checkIsPreAggregatedStandardMetric(SpanData span) { return isPreAggregatedStandardMetric != null && isPreAggregatedStandardMetric; } - private TelemetryItem exportRemoteDependency(SpanData span, boolean inProc, long itemCount) { + private TelemetryItem exportRemoteDependency(SpanData span, boolean inProc, @Nullable Long itemCount) { RemoteDependencyTelemetryBuilder telemetryBuilder = RemoteDependencyTelemetryBuilder.create(); telemetryInitializer.accept(telemetryBuilder, span.getResource()); @@ -504,7 +502,7 @@ private static int getDefaultPortForDbSystem(String dbSystem) { } } - private TelemetryItem exportRequest(SpanData span, long itemCount) { + private TelemetryItem exportRequest(SpanData span, @Nullable Long itemCount) { RequestTelemetryBuilder telemetryBuilder = RequestTelemetryBuilder.create(); telemetryInitializer.accept(telemetryBuilder, span.getResource()); @@ -750,7 +748,7 @@ private static String nullAwareConcat( private void exportEvents( SpanData span, @Nullable String operationName, - long itemCount, + @Nullable Long itemCount, Consumer consumer) { for (EventData event : span.getEvents()) { String instrumentationScopeName = span.getInstrumentationScopeInfo().getName(); @@ -801,7 +799,7 @@ private void exportEvents( } private TelemetryItem createExceptionTelemetryItem( - String errorStack, SpanData span, @Nullable String operationName, long itemCount) { + String errorStack, SpanData span, @Nullable String operationName, @Nullable Long itemCount) { ExceptionTelemetryBuilder telemetryBuilder = ExceptionTelemetryBuilder.create(); telemetryInitializer.accept(telemetryBuilder, span.getResource()); @@ -838,15 +836,15 @@ private static void setTime(AbstractTelemetryBuilder telemetryBuilder, long epoc telemetryBuilder.setTime(FormattedTime.offSetDateTimeFromEpochNanos(epochNanos)); } - private static void setItemCount(AbstractTelemetryBuilder telemetryBuilder, long itemCount) { - if (itemCount != USE_INGESTION_SAMPLING) { + private static void setItemCount(AbstractTelemetryBuilder telemetryBuilder, @Nullable Long itemCount) { + if (itemCount != null) { telemetryBuilder.setSampleRate(100.0f / itemCount); } } - private static long getItemCount(SpanData span) { - Long itemCount = span.getAttributes().get(AiSemanticAttributes.ITEM_COUNT); - return itemCount == null ? USE_INGESTION_SAMPLING : itemCount; + @Nullable + private static Long getItemCount(SpanData span) { + return span.getAttributes().get(AiSemanticAttributes.ITEM_COUNT); } private static void addLinks(AbstractTelemetryBuilder telemetryBuilder, List links) {