22
33import com .google .common .annotations .VisibleForTesting ;
44import com .google .protobuf .ByteString ;
5+ import com .slack .astra .bulkIngestApi .BulkIngestApi ;
56import com .slack .astra .logstore .LogMessage ;
67import com .slack .astra .logstore .schema .ReservedFields ;
8+ import com .slack .astra .proto .config .AstraConfigs ;
79import com .slack .astra .proto .schema .Schema ;
810import com .slack .astra .writer .SpanFormatter ;
911import com .slack .service .murron .trace .Trace ;
@@ -37,7 +39,13 @@ public class BulkApiRequestParser {
3739
3840 public static Map <String , List <Trace .Span >> parseRequest (
3941 byte [] postBody , Schema .IngestSchema schema ) throws IOException {
40- return convertIndexRequestToTraceFormat (parseBulkRequest (postBody ), schema );
42+ return parseRequest (postBody , schema , AstraConfigs .SchemaMode .SCHEMA_MODE_DYNAMIC );
43+ }
44+
45+ public static Map <String , List <Trace .Span >> parseRequest (
46+ byte [] postBody , Schema .IngestSchema schema , AstraConfigs .SchemaMode schemaMode )
47+ throws IOException {
48+ return convertIndexRequestToTraceFormat (parseBulkRequest (postBody ), schema , schemaMode );
4149 }
4250
4351 /**
@@ -64,6 +72,14 @@ public static long getTimestampFromIngestDocument(Map<String, Object> sourceAndM
6472 @ VisibleForTesting
6573 public static Trace .Span fromIngestDocument (
6674 IngestDocument ingestDocument , Schema .IngestSchema schema ) {
75+ return fromIngestDocument (ingestDocument , schema , AstraConfigs .SchemaMode .SCHEMA_MODE_DYNAMIC );
76+ }
77+
78+ @ VisibleForTesting
79+ public static Trace .Span fromIngestDocument (
80+ IngestDocument ingestDocument ,
81+ Schema .IngestSchema schema ,
82+ AstraConfigs .SchemaMode schemaMode ) {
6783
6884 Map <String , Object > sourceAndMetadata = ingestDocument .getSourceAndMetadata ();
6985
@@ -140,8 +156,21 @@ public static Trace.Span fromIngestDocument(
140156 sourceAndMetadata .remove (IngestDocument .Metadata .ID .getFieldName ());
141157 sourceAndMetadata .remove (IngestDocument .Metadata .INDEX .getFieldName ());
142158
159+ boolean dropUnknown =
160+ Boolean .getBoolean (BulkIngestApi .SCHEMA_ENFORCEMENT_FLAG )
161+ && schemaMode == AstraConfigs .SchemaMode .SCHEMA_MODE_DROP_UNKNOWN ;
162+
143163 boolean tagsContainServiceName = false ;
144164 for (Map .Entry <String , Object > kv : sourceAndMetadata .entrySet ()) {
165+ if (dropUnknown ) {
166+ if (!schema .containsFields (kv .getKey ())) {
167+ continue ;
168+ }
169+ Schema .SchemaField fieldDef = schema .getFieldsMap ().get (kv .getKey ());
170+ if (!SpanFormatter .isTypeCompatible (kv .getValue (), fieldDef .getType ())) {
171+ continue ;
172+ }
173+ }
145174 if (!tagsContainServiceName && kv .getKey ().equals (SERVICE_NAME_KEY )) {
146175 tagsContainServiceName = true ;
147176 }
@@ -151,7 +180,7 @@ public static Trace.Span fromIngestDocument(
151180 spanBuilder .addAllTags (tags );
152181 }
153182 }
154- if (!tagsContainServiceName ) {
183+ if (!tagsContainServiceName && (! dropUnknown || schema . containsFields ( SERVICE_NAME_KEY )) ) {
155184 spanBuilder .addTags (
156185 Trace .KeyValue .newBuilder ()
157186 .setKey (SERVICE_NAME_KEY )
@@ -165,6 +194,14 @@ public static Trace.Span fromIngestDocument(
165194
166195 protected static Map <String , List <Trace .Span >> convertIndexRequestToTraceFormat (
167196 List <IndexRequest > indexRequests , Schema .IngestSchema schema ) {
197+ return convertIndexRequestToTraceFormat (
198+ indexRequests , schema , AstraConfigs .SchemaMode .SCHEMA_MODE_DYNAMIC );
199+ }
200+
201+ protected static Map <String , List <Trace .Span >> convertIndexRequestToTraceFormat (
202+ List <IndexRequest > indexRequests ,
203+ Schema .IngestSchema schema ,
204+ AstraConfigs .SchemaMode schemaMode ) {
168205 // key - index. value - list of docs to be indexed
169206 Map <String , List <Trace .Span >> indexDocs = new HashMap <>();
170207
@@ -175,7 +212,7 @@ protected static Map<String, List<Trace.Span>> convertIndexRequestToTraceFormat(
175212 }
176213 IngestDocument ingestDocument = convertRequestToDocument (indexRequest );
177214 List <Trace .Span > docs = indexDocs .computeIfAbsent (index , key -> new ArrayList <>());
178- docs .add (BulkApiRequestParser .fromIngestDocument (ingestDocument , schema ));
215+ docs .add (BulkApiRequestParser .fromIngestDocument (ingestDocument , schema , schemaMode ));
179216 }
180217 return indexDocs ;
181218 }
0 commit comments