@@ -387,80 +387,77 @@ public void testJsonInStacktraceMessageIsSplitted() throws IOException {
387387 public void testDuplicateLogMessages () throws IOException {
388388 final DeprecationLogger deprecationLogger = new DeprecationLogger (LogManager .getLogger ("test" ));
389389
390-
391390 // For the same key and X-Opaque-ID deprecation should be once
392- try (ThreadContext threadContext = new ThreadContext (Settings .EMPTY )) {
393- try {
394- threadContext .putHeader (Task .X_OPAQUE_ID , "ID1" );
395- DeprecationLogger .setThreadContext (threadContext );
396- deprecationLogger .deprecatedAndMaybeLog ("key" , "message1" );
397- deprecationLogger .deprecatedAndMaybeLog ("key" , "message2" );
398- assertWarnings ("message1" , "message2" );
399-
400- final Path path = PathUtils .get (System .getProperty ("es.logs.base_path" ),
401- System .getProperty ("es.logs.cluster_name" ) + "_deprecated.json" );
402- try (Stream <Map <String , String >> stream = JsonLogsStream .mapStreamFrom (path )) {
403- List <Map <String , String >> jsonLogs = stream
404- .collect (Collectors .toList ());
405-
406- assertThat (jsonLogs , contains (
407- allOf (
408- hasEntry ("type" , "deprecation" ),
409- hasEntry ("level" , "WARN" ),
410- hasEntry ("component" , "d.test" ),
411- hasEntry ("cluster.name" , "elasticsearch" ),
412- hasEntry ("node.name" , "sample-name" ),
413- hasEntry ("message" , "message1" ),
414- hasEntry ("x-opaque-id" , "ID1" ))
415- )
416- );
417- }
418- }finally {
419- DeprecationLogger .removeThreadContext (threadContext );
391+ ThreadContext threadContext = new ThreadContext (Settings .EMPTY );
392+ try (ThreadContext .StoredContext ignore = threadContext .stashContext ()) {
393+ threadContext .putHeader (Task .X_OPAQUE_ID , "ID1" );
394+ DeprecationLogger .setThreadContext (threadContext );
395+ deprecationLogger .deprecatedAndMaybeLog ("key" , "message1" );
396+ deprecationLogger .deprecatedAndMaybeLog ("key" , "message2" );
397+ assertWarnings ("message1" , "message2" );
398+
399+ final Path path = PathUtils .get (System .getProperty ("es.logs.base_path" ),
400+ System .getProperty ("es.logs.cluster_name" ) + "_deprecated.json" );
401+ try (Stream <Map <String , String >> stream = JsonLogsStream .mapStreamFrom (path )) {
402+ List <Map <String , String >> jsonLogs = stream
403+ .collect (Collectors .toList ());
404+
405+ assertThat (jsonLogs , contains (
406+ allOf (
407+ hasEntry ("type" , "deprecation" ),
408+ hasEntry ("level" , "WARN" ),
409+ hasEntry ("component" , "d.test" ),
410+ hasEntry ("cluster.name" , "elasticsearch" ),
411+ hasEntry ("node.name" , "sample-name" ),
412+ hasEntry ("message" , "message1" ),
413+ hasEntry ("x-opaque-id" , "ID1" ))
414+ )
415+ );
420416 }
417+ } finally {
418+ DeprecationLogger .removeThreadContext (threadContext );
421419 }
422420
421+
423422 // For the same key and different X-Opaque-ID should be multiple times per key/x-opaque-id
424423 //continuing with message1-ID1 in logs already, adding a new deprecation log line with message2-ID2
425- try (ThreadContext threadContext = new ThreadContext (Settings .EMPTY )) {
426- try {
427- threadContext .putHeader (Task .X_OPAQUE_ID , "ID2" );
428- DeprecationLogger .setThreadContext (threadContext );
429- deprecationLogger .deprecatedAndMaybeLog ("key" , "message1" );
430- deprecationLogger .deprecatedAndMaybeLog ("key" , "message2" );
431- assertWarnings ("message1" , "message2" );
432-
433- final Path path = PathUtils .get (System .getProperty ("es.logs.base_path" ),
434- System .getProperty ("es.logs.cluster_name" ) + "_deprecated.json" );
435- try (Stream <Map <String , String >> stream = JsonLogsStream .mapStreamFrom (path )) {
436- List <Map <String , String >> jsonLogs = stream
437- .collect (Collectors .toList ());
438-
439- assertThat (jsonLogs , contains (
440- allOf (
441- hasEntry ("type" , "deprecation" ),
442- hasEntry ("level" , "WARN" ),
443- hasEntry ("component" , "d.test" ),
444- hasEntry ("cluster.name" , "elasticsearch" ),
445- hasEntry ("node.name" , "sample-name" ),
446- hasEntry ("message" , "message1" ),
447- hasEntry ("x-opaque-id" , "ID1" )
448- ),
449- allOf (
450- hasEntry ("type" , "deprecation" ),
451- hasEntry ("level" , "WARN" ),
452- hasEntry ("component" , "d.test" ),
453- hasEntry ("cluster.name" , "elasticsearch" ),
454- hasEntry ("node.name" , "sample-name" ),
455- hasEntry ("message" , "message1" ),
456- hasEntry ("x-opaque-id" , "ID2" )
457- )
458- )
459- );
460- }
461- }finally {
462- DeprecationLogger .removeThreadContext (threadContext );
424+ try (ThreadContext .StoredContext ignore = threadContext .stashContext ()) {
425+ threadContext .putHeader (Task .X_OPAQUE_ID , "ID2" );
426+ DeprecationLogger .setThreadContext (threadContext );
427+ deprecationLogger .deprecatedAndMaybeLog ("key" , "message1" );
428+ deprecationLogger .deprecatedAndMaybeLog ("key" , "message2" );
429+ assertWarnings ("message1" , "message2" );
430+
431+ final Path path = PathUtils .get (System .getProperty ("es.logs.base_path" ),
432+ System .getProperty ("es.logs.cluster_name" ) + "_deprecated.json" );
433+ try (Stream <Map <String , String >> stream = JsonLogsStream .mapStreamFrom (path )) {
434+ List <Map <String , String >> jsonLogs = stream
435+ .collect (Collectors .toList ());
436+
437+ assertThat (jsonLogs , contains (
438+ allOf (
439+ hasEntry ("type" , "deprecation" ),
440+ hasEntry ("level" , "WARN" ),
441+ hasEntry ("component" , "d.test" ),
442+ hasEntry ("cluster.name" , "elasticsearch" ),
443+ hasEntry ("node.name" , "sample-name" ),
444+ hasEntry ("message" , "message1" ),
445+ hasEntry ("x-opaque-id" , "ID1" )
446+ ),
447+ allOf (
448+ hasEntry ("type" , "deprecation" ),
449+ hasEntry ("level" , "WARN" ),
450+ hasEntry ("component" , "d.test" ),
451+ hasEntry ("cluster.name" , "elasticsearch" ),
452+ hasEntry ("node.name" , "sample-name" ),
453+ hasEntry ("message" , "message1" ),
454+ hasEntry ("x-opaque-id" , "ID2" )
455+ )
456+ )
457+ );
463458 }
459+ } finally {
460+ DeprecationLogger .removeThreadContext (threadContext );
464461 }
465462 }
466463
0 commit comments