소스 검색

Add debug logging for doc parsing exceptions (#117768) (#117816)

Kostas Krikellas 10 달 전
부모
커밋
ad92657e13

+ 20 - 3
server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java

@@ -9,7 +9,9 @@
 
 package org.elasticsearch.index.mapper;
 
+import org.apache.logging.log4j.Logger;
 import org.elasticsearch.common.compress.CompressedXContent;
+import org.elasticsearch.common.logging.Loggers;
 import org.elasticsearch.features.NodeFeature;
 import org.elasticsearch.index.IndexSettings;
 import org.elasticsearch.index.IndexSortConfig;
@@ -25,6 +27,7 @@ public class DocumentMapper {
     private final DocumentParser documentParser;
     private final MapperMetrics mapperMetrics;
     private final IndexVersion indexVersion;
+    private final Logger logger;
 
     static final NodeFeature INDEX_SORTING_ON_NESTED = new NodeFeature("mapper.index_sorting_on_nested");
 
@@ -44,7 +47,8 @@ public class DocumentMapper {
             mapping,
             mapping.toCompressedXContent(),
             IndexVersion.current(),
-            mapperService.getMapperMetrics()
+            mapperService.getMapperMetrics(),
+            mapperService.index().getName()
         );
     }
 
@@ -53,7 +57,8 @@ public class DocumentMapper {
         Mapping mapping,
         CompressedXContent source,
         IndexVersion version,
-        MapperMetrics mapperMetrics
+        MapperMetrics mapperMetrics,
+        String indexName
     ) {
         this.documentParser = documentParser;
         this.type = mapping.getRoot().fullPath();
@@ -61,11 +66,18 @@ public class DocumentMapper {
         this.mappingSource = source;
         this.mapperMetrics = mapperMetrics;
         this.indexVersion = version;
+        this.logger = Loggers.getLogger(getClass(), indexName);
 
         assert mapping.toCompressedXContent().equals(source) || isSyntheticSourceMalformed(source, version)
             : "provided source [" + source + "] differs from mapping [" + mapping.toCompressedXContent() + "]";
     }
 
+    private void maybeLogDebug(Exception ex) {
+        if (logger.isDebugEnabled()) {
+            logger.debug("Error while parsing document: " + ex.getMessage(), ex);
+        }
+    }
+
     /**
      * Indexes built at v.8.7 were missing an explicit entry for synthetic_source.
      * This got restored in v.8.10 to avoid confusion. The change is only restricted to mapping printout, it has no
@@ -110,7 +122,12 @@ public class DocumentMapper {
     }
 
     public ParsedDocument parse(SourceToParse source) throws DocumentParsingException {
-        return documentParser.parseDocument(source, mappingLookup);
+        try {
+            return documentParser.parseDocument(source, mappingLookup);
+        } catch (Exception e) {
+            maybeLogDebug(e);
+            throw e;
+        }
     }
 
     public void validate(IndexSettings settings, boolean checkLimits) {

+ 8 - 1
server/src/main/java/org/elasticsearch/index/mapper/MapperService.java

@@ -595,7 +595,14 @@ public class MapperService extends AbstractIndexComponent implements Closeable {
     }
 
     private DocumentMapper newDocumentMapper(Mapping mapping, MergeReason reason, CompressedXContent mappingSource) {
-        DocumentMapper newMapper = new DocumentMapper(documentParser, mapping, mappingSource, indexVersionCreated, mapperMetrics);
+        DocumentMapper newMapper = new DocumentMapper(
+            documentParser,
+            mapping,
+            mappingSource,
+            indexVersionCreated,
+            mapperMetrics,
+            index().getName()
+        );
         newMapper.validate(indexSettings, reason != MergeReason.MAPPING_RECOVERY);
         return newMapper;
     }

+ 2 - 1
server/src/test/java/org/elasticsearch/index/mapper/DocumentMapperTests.java

@@ -75,7 +75,8 @@ public class DocumentMapperTests extends MapperServiceTestCase {
             merged,
             merged.toCompressedXContent(),
             IndexVersion.current(),
-            MapperMetrics.NOOP
+            MapperMetrics.NOOP,
+            "myIndex"
         );
         assertThat(mergedMapper.mappers().getMapper("age"), notNullValue());
         assertThat(mergedMapper.mappers().getMapper("obj1.prop1"), notNullValue());

+ 2 - 1
server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java

@@ -2625,7 +2625,8 @@ public class DocumentParserTests extends MapperServiceTestCase {
             newMapping,
             newMapping.toCompressedXContent(),
             IndexVersion.current(),
-            MapperMetrics.NOOP
+            MapperMetrics.NOOP,
+            "myIndex"
         );
         ParsedDocument doc2 = newDocMapper.parse(source("""
             {