|
19 | 19 | import io.opentelemetry.sdk.declarativeconfig.internal.model.LogRecordProcessorPropertyModel; |
20 | 20 | import io.opentelemetry.sdk.declarativeconfig.internal.model.OtlpHttpExporterModel; |
21 | 21 | import io.opentelemetry.sdk.declarativeconfig.internal.model.SimpleLogRecordProcessorModel; |
| 22 | +import io.opentelemetry.sdk.extension.incubator.logs.EventToSpanEventBridge; |
22 | 23 | import io.opentelemetry.sdk.logs.LogRecordProcessor; |
23 | 24 | import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor; |
24 | 25 | import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor; |
25 | | -import java.io.Closeable; |
26 | 26 | import java.time.Duration; |
27 | | -import java.util.ArrayList; |
28 | | -import java.util.List; |
29 | | -import org.assertj.core.api.Assertions; |
| 27 | +import java.util.stream.Stream; |
30 | 28 | import org.junit.jupiter.api.BeforeEach; |
31 | | -import org.junit.jupiter.api.Test; |
32 | 29 | import org.junit.jupiter.api.extension.RegisterExtension; |
| 30 | +import org.junit.jupiter.params.ParameterizedTest; |
| 31 | +import org.junit.jupiter.params.provider.Arguments; |
| 32 | +import org.junit.jupiter.params.provider.MethodSource; |
33 | 33 |
|
34 | 34 | class LogRecordProcessorFactoryTest { |
35 | 35 |
|
36 | 36 | @RegisterExtension CleanupExtension cleanup = new CleanupExtension(); |
37 | 37 |
|
38 | | - private final DeclarativeConfigContext context = |
39 | | - new DeclarativeConfigContext(ComponentLoader.forClassLoader(getClass().getClassLoader())); |
| 38 | + private static final DeclarativeConfigContext context = |
| 39 | + new DeclarativeConfigContext( |
| 40 | + ComponentLoader.forClassLoader(LogRecordProcessorFactoryTest.class.getClassLoader())); |
40 | 41 |
|
41 | 42 | @BeforeEach |
42 | 43 | void setup() { |
43 | 44 | context.setBuilder(new DeclarativeConfigurationBuilder()); |
44 | 45 | } |
45 | 46 |
|
46 | | - @Test |
47 | | - void create_BatchNullExporter() { |
48 | | - assertThatThrownBy( |
49 | | - () -> |
50 | | - LogRecordProcessorFactory.getInstance() |
51 | | - .create( |
52 | | - new LogRecordProcessorModel().withBatch(new BatchLogRecordProcessorModel()), |
53 | | - context)) |
54 | | - .isInstanceOf(DeclarativeConfigException.class) |
55 | | - .hasMessage("batch log record processor exporter is required but is null"); |
56 | | - } |
57 | | - |
58 | | - @Test |
59 | | - void create_BatchDefaults() { |
60 | | - List<Closeable> closeables = new ArrayList<>(); |
61 | | - BatchLogRecordProcessor expectedProcessor = |
62 | | - BatchLogRecordProcessor.builder( |
63 | | - OtlpHttpLogRecordExporter.builder().setComponentLoader(context).build()) |
64 | | - .build(); |
65 | | - cleanup.addCloseable(expectedProcessor); |
66 | | - |
67 | | - LogRecordProcessor processor = |
68 | | - LogRecordProcessorFactory.getInstance() |
69 | | - .create( |
70 | | - new LogRecordProcessorModel() |
71 | | - .withBatch( |
72 | | - new BatchLogRecordProcessorModel() |
73 | | - .withExporter( |
74 | | - new LogRecordExporterModel() |
75 | | - .withOtlpHttp(new OtlpHttpExporterModel()))), |
76 | | - context); |
77 | | - cleanup.addCloseable(processor); |
78 | | - cleanup.addCloseables(closeables); |
79 | | - |
80 | | - assertThat(processor.toString()).isEqualTo(expectedProcessor.toString()); |
81 | | - } |
82 | | - |
83 | | - @Test |
84 | | - void create_BatchConfigured() { |
85 | | - List<Closeable> closeables = new ArrayList<>(); |
86 | | - BatchLogRecordProcessor expectedProcessor = |
87 | | - BatchLogRecordProcessor.builder( |
88 | | - OtlpHttpLogRecordExporter.builder().setComponentLoader(context).build()) |
89 | | - .setScheduleDelay(Duration.ofMillis(1)) |
90 | | - .setMaxExportBatchSize(2) |
91 | | - .setExporterTimeout(Duration.ofMillis(3)) |
92 | | - .build(); |
| 47 | + @ParameterizedTest |
| 48 | + @MethodSource("createTestCases") |
| 49 | + void create(LogRecordProcessorModel model, LogRecordProcessor expectedProcessor) { |
93 | 50 | cleanup.addCloseable(expectedProcessor); |
94 | | - |
95 | | - LogRecordProcessor processor = |
96 | | - LogRecordProcessorFactory.getInstance() |
97 | | - .create( |
98 | | - new LogRecordProcessorModel() |
99 | | - .withBatch( |
100 | | - new BatchLogRecordProcessorModel() |
101 | | - .withExporter( |
102 | | - new LogRecordExporterModel() |
103 | | - .withOtlpHttp(new OtlpHttpExporterModel())) |
104 | | - .withScheduleDelay(1) |
105 | | - .withMaxExportBatchSize(2) |
106 | | - .withExportTimeout(3)), |
107 | | - context); |
| 51 | + LogRecordProcessor processor = LogRecordProcessorFactory.getInstance().create(model, context); |
108 | 52 | cleanup.addCloseable(processor); |
109 | | - cleanup.addCloseables(closeables); |
110 | | - |
111 | 53 | assertThat(processor.toString()).isEqualTo(expectedProcessor.toString()); |
112 | 54 | } |
113 | 55 |
|
114 | | - @Test |
115 | | - void create_SimpleNullExporter() { |
116 | | - assertThatThrownBy( |
117 | | - () -> |
118 | | - LogRecordProcessorFactory.getInstance() |
119 | | - .create( |
120 | | - new LogRecordProcessorModel() |
121 | | - .withSimple(new SimpleLogRecordProcessorModel()), |
122 | | - context)) |
123 | | - .isInstanceOf(DeclarativeConfigException.class) |
124 | | - .hasMessage("simple log record processor exporter is required but is null"); |
125 | | - } |
126 | | - |
127 | | - @Test |
128 | | - void create_SimpleConfigured() { |
129 | | - List<Closeable> closeables = new ArrayList<>(); |
130 | | - LogRecordProcessor expectedProcessor = |
131 | | - SimpleLogRecordProcessor.create( |
132 | | - OtlpHttpLogRecordExporter.builder().setComponentLoader(context).build()); |
133 | | - cleanup.addCloseable(expectedProcessor); |
134 | | - |
135 | | - LogRecordProcessor processor = |
136 | | - LogRecordProcessorFactory.getInstance() |
137 | | - .create( |
138 | | - new LogRecordProcessorModel() |
139 | | - .withSimple( |
140 | | - new SimpleLogRecordProcessorModel() |
141 | | - .withExporter( |
142 | | - new LogRecordExporterModel() |
143 | | - .withOtlpHttp(new OtlpHttpExporterModel()))), |
144 | | - context); |
145 | | - cleanup.addCloseable(processor); |
146 | | - cleanup.addCloseables(closeables); |
147 | | - |
148 | | - assertThat(processor.toString()).isEqualTo(expectedProcessor.toString()); |
| 56 | + private static Stream<Arguments> createTestCases() { |
| 57 | + return Stream.of( |
| 58 | + Arguments.of( |
| 59 | + new LogRecordProcessorModel() |
| 60 | + .withBatch( |
| 61 | + new BatchLogRecordProcessorModel() |
| 62 | + .withExporter( |
| 63 | + new LogRecordExporterModel() |
| 64 | + .withOtlpHttp(new OtlpHttpExporterModel()))), |
| 65 | + BatchLogRecordProcessor.builder( |
| 66 | + OtlpHttpLogRecordExporter.builder().setComponentLoader(context).build()) |
| 67 | + .build()), |
| 68 | + Arguments.of( |
| 69 | + new LogRecordProcessorModel() |
| 70 | + .withBatch( |
| 71 | + new BatchLogRecordProcessorModel() |
| 72 | + .withExporter( |
| 73 | + new LogRecordExporterModel().withOtlpHttp(new OtlpHttpExporterModel())) |
| 74 | + .withScheduleDelay(1) |
| 75 | + .withMaxExportBatchSize(2) |
| 76 | + .withExportTimeout(3)), |
| 77 | + BatchLogRecordProcessor.builder( |
| 78 | + OtlpHttpLogRecordExporter.builder().setComponentLoader(context).build()) |
| 79 | + .setScheduleDelay(Duration.ofMillis(1)) |
| 80 | + .setMaxExportBatchSize(2) |
| 81 | + .setExporterTimeout(Duration.ofMillis(3)) |
| 82 | + .build()), |
| 83 | + Arguments.of( |
| 84 | + new LogRecordProcessorModel() |
| 85 | + .withSimple( |
| 86 | + new SimpleLogRecordProcessorModel() |
| 87 | + .withExporter( |
| 88 | + new LogRecordExporterModel() |
| 89 | + .withOtlpHttp(new OtlpHttpExporterModel()))), |
| 90 | + SimpleLogRecordProcessor.create( |
| 91 | + OtlpHttpLogRecordExporter.builder().setComponentLoader(context).build())), |
| 92 | + Arguments.of( |
| 93 | + new LogRecordProcessorModel() |
| 94 | + .withAdditionalProperty( |
| 95 | + "event_to_span_event_bridge/development", |
| 96 | + new LogRecordProcessorPropertyModel()), |
| 97 | + EventToSpanEventBridge.create()), |
| 98 | + Arguments.of( |
| 99 | + new LogRecordProcessorModel() |
| 100 | + .withAdditionalProperty("test", new LogRecordProcessorPropertyModel()), |
| 101 | + LogRecordProcessorComponentProvider.TestLogRecordProcessor.create())); |
149 | 102 | } |
150 | 103 |
|
151 | | - @Test |
152 | | - void create_SpiProcessor_Unknown() { |
153 | | - assertThatThrownBy( |
154 | | - () -> |
155 | | - LogRecordProcessorFactory.getInstance() |
156 | | - .create( |
157 | | - new LogRecordProcessorModel() |
158 | | - .withAdditionalProperty( |
159 | | - "unknown_key", |
160 | | - new LogRecordProcessorPropertyModel() |
161 | | - .withAdditionalProperty("key1", "value1")), |
162 | | - context)) |
| 104 | + @ParameterizedTest |
| 105 | + @MethodSource("createInvalidTestCases") |
| 106 | + void create_Invalid(LogRecordProcessorModel model, String expectedMessage) { |
| 107 | + assertThatThrownBy(() -> LogRecordProcessorFactory.getInstance().create(model, context)) |
163 | 108 | .isInstanceOf(DeclarativeConfigException.class) |
164 | | - .hasMessage( |
165 | | - "No component provider detected for io.opentelemetry.sdk.logs.LogRecordProcessor with name \"unknown_key\"."); |
| 109 | + .hasMessage(expectedMessage); |
166 | 110 | } |
167 | 111 |
|
168 | | - @Test |
169 | | - void create_SpiExporter_Valid() { |
170 | | - LogRecordProcessor logRecordProcessor = |
171 | | - LogRecordProcessorFactory.getInstance() |
172 | | - .create( |
173 | | - new LogRecordProcessorModel() |
174 | | - .withAdditionalProperty( |
175 | | - "test", |
176 | | - new LogRecordProcessorPropertyModel() |
177 | | - .withAdditionalProperty("key1", "value1")), |
178 | | - context); |
179 | | - assertThat(logRecordProcessor) |
180 | | - .isInstanceOf(LogRecordProcessorComponentProvider.TestLogRecordProcessor.class); |
181 | | - Assertions.assertThat( |
182 | | - ((LogRecordProcessorComponentProvider.TestLogRecordProcessor) logRecordProcessor) |
183 | | - .config.getString("key1")) |
184 | | - .isEqualTo("value1"); |
| 112 | + private static Stream<Arguments> createInvalidTestCases() { |
| 113 | + return Stream.of( |
| 114 | + Arguments.of( |
| 115 | + new LogRecordProcessorModel().withBatch(new BatchLogRecordProcessorModel()), |
| 116 | + "batch log record processor exporter is required but is null"), |
| 117 | + Arguments.of( |
| 118 | + new LogRecordProcessorModel().withSimple(new SimpleLogRecordProcessorModel()), |
| 119 | + "simple log record processor exporter is required but is null"), |
| 120 | + Arguments.of( |
| 121 | + new LogRecordProcessorModel() |
| 122 | + .withAdditionalProperty( |
| 123 | + "unknown_key", |
| 124 | + new LogRecordProcessorPropertyModel().withAdditionalProperty("key1", "value1")), |
| 125 | + "No component provider detected for io.opentelemetry.sdk.logs.LogRecordProcessor with name \"unknown_key\".")); |
185 | 126 | } |
186 | 127 | } |
0 commit comments