Skip to content

Commit

Permalink
Refactor debugExporter into singleton
Browse files Browse the repository at this point in the history
Signed-off-by: Chenyang Ji <cyji@amazon.com>
  • Loading branch information
ansjcy committed Jun 5, 2024
1 parent 0ab9c0b commit d171db0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,20 @@ public final class DebugExporter implements QueryInsightsExporter {
/**
* Constructor of DebugExporter
*/
public DebugExporter() {}
private DebugExporter() {}

private static class InstanceHolder {
private static final DebugExporter INSTANCE = new DebugExporter();
}

/**
Get the singleton instance of DebugExporter
*
@return DebugExporter instance
*/
public static DebugExporter getInstance() {
return InstanceHolder.INSTANCE;
}

/**
* Write the list of SearchQueryRecord to debug log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,12 @@ public void validateExporterConfig(final Settings settings) throws IllegalArgume
* @return QueryInsightsExporter the created exporter sink
*/
public QueryInsightsExporter createExporter(SinkType type, String indexPattern) {
QueryInsightsExporter exporter;
switch (type) {
case LOCAL_INDEX:
exporter = new LocalIndexExporter(client, DateTimeFormat.forPattern(indexPattern));
break;
default:
exporter = new DebugExporter();
if (SinkType.LOCAL_INDEX.equals(type)) {
QueryInsightsExporter exporter = new LocalIndexExporter(client, DateTimeFormat.forPattern(indexPattern));
this.exporters.add(exporter);
return exporter;
}
this.exporters.add(exporter);
return exporter;
return DebugExporter.getInstance();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class DebugExporterTests extends OpenSearchTestCase {

@Before
public void setup() {
debugExporter = new DebugExporter();
debugExporter = DebugExporter.getInstance();
}

public void testExport() {
Expand Down

0 comments on commit d171db0

Please sign in to comment.