-
Notifications
You must be signed in to change notification settings - Fork 169
add logs filtering #1823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add logs filtering #1823
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
processors/src/main/java/io/opentelemetry/contrib/filter/FilteringLogRecordProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package io.opentelemetry.contrib.filter; | ||
|
|
||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.sdk.logs.LogRecordProcessor; | ||
| import io.opentelemetry.sdk.logs.ReadWriteLogRecord; | ||
| import io.opentelemetry.sdk.logs.data.LogRecordData; | ||
| import java.util.function.Predicate; | ||
|
|
||
| public class FilteringLogRecordProcessor implements LogRecordProcessor { | ||
|
|
||
| public final LogRecordProcessor delegate; | ||
| public final Predicate<LogRecordData> predicate; | ||
|
|
||
| public FilteringLogRecordProcessor(LogRecordProcessor delegate, Predicate<LogRecordData> predicate) { | ||
| this.delegate = delegate; | ||
| this.predicate = predicate; | ||
| } | ||
|
|
||
| @Override | ||
| public void onEmit(Context context, ReadWriteLogRecord readWriteLogRecord) { | ||
| if (predicate.test(readWriteLogRecord.toLogRecordData())) { | ||
| delegate.onEmit(context, readWriteLogRecord); | ||
| } | ||
| } | ||
| } |
101 changes: 101 additions & 0 deletions
101
...essors/src/test/java/io/opentelemetry/contrib/filter/FilteringLogRecordProcessorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package io.opentelemetry.contrib.filter; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import io.opentelemetry.api.logs.Logger; | ||
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.api.trace.SpanContext; | ||
| import io.opentelemetry.api.trace.Tracer; | ||
| import io.opentelemetry.context.Scope; | ||
| import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
| import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; | ||
| import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; | ||
| import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
| import io.opentelemetry.sdk.logs.LogRecordProcessor; | ||
| import io.opentelemetry.sdk.logs.SdkLoggerProvider; | ||
| import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder; | ||
| import io.opentelemetry.sdk.logs.data.LogRecordData; | ||
| import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor; | ||
| import io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter; | ||
| import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter; | ||
| import io.opentelemetry.sdk.trace.data.SpanData; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.BiFunction; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class FilteringLogRecordProcessorTest { | ||
|
|
||
| private final InMemoryLogRecordExporter memoryLogRecordExporter = InMemoryLogRecordExporter.create();; | ||
| private final LogRecordProcessor logRecordProcessor = SimpleLogRecordProcessor.create(memoryLogRecordExporter);; | ||
| private final InMemorySpanExporter spansExporter = InMemorySpanExporter.create(); | ||
| private AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder ; | ||
| private Logger logger; | ||
|
|
||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); | ||
| sdkBuilder.addPropertiesSupplier(()->{ | ||
| Map<String, String> configMap = new HashMap<>(); | ||
| configMap.put("otel.metrics.exporter", "none"); | ||
| configMap.put("otel.traces.exporter", "logging"); | ||
| configMap.put("otel.logs.exporter", "logging"); | ||
| return configMap; | ||
| }) | ||
| .addSpanExporterCustomizer((exporter,c)->spansExporter) | ||
| .addLogRecordExporterCustomizer( | ||
| (logRecordExporter, configProperties) -> memoryLogRecordExporter) | ||
| .addLoggerProviderCustomizer( | ||
| new BiFunction<SdkLoggerProviderBuilder, ConfigProperties, SdkLoggerProviderBuilder>() { | ||
| @Override | ||
| public SdkLoggerProviderBuilder apply( | ||
| SdkLoggerProviderBuilder sdkLoggerProviderBuilder, | ||
| ConfigProperties configProperties) { | ||
| return sdkLoggerProviderBuilder.addLogRecordProcessor(new FilteringLogRecordProcessor( | ||
| logRecordProcessor, logRecordData -> logRecordData.getSpanContext().isSampled())); | ||
| } | ||
| }); | ||
|
|
||
| logger = | ||
| SdkLoggerProvider.builder() | ||
| .addLogRecordProcessor(new FilteringLogRecordProcessor(logRecordProcessor, | ||
| logRecordData -> { | ||
| SpanContext spanContext =logRecordData.getSpanContext(); | ||
| return spanContext.isSampled(); | ||
| }) {}) | ||
| .build() | ||
| .get("TestScope"); | ||
| } | ||
|
|
||
| @Test | ||
| void verifyLogFilteringExistSpanContext() { | ||
|
|
||
| try (OpenTelemetrySdk sdk = sdkBuilder.build().getOpenTelemetrySdk()) { | ||
| Tracer tracer = sdk.getTracer("test"); | ||
| Span span = tracer.spanBuilder("test").startSpan(); | ||
| sdk.getLogsBridge().get("test").logRecordBuilder().setBody("One Log").emit(); | ||
| List<LogRecordData> finishedLogRecordItems = memoryLogRecordExporter.getFinishedLogRecordItems(); | ||
| assertEquals(1, finishedLogRecordItems.size()); | ||
| try (Scope scope = span.makeCurrent()) { | ||
|
|
||
| } finally { | ||
| span.end(); | ||
| } | ||
| List<SpanData> finishedSpans = spansExporter.getFinishedSpanItems(); | ||
| assertEquals(1, finishedSpans.size()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void verifyFilteringNotExitSpanContext() { | ||
| logger.logRecordBuilder().setBody("One Log").emit(); | ||
| List<LogRecordData> finishedLogRecordItems = memoryLogRecordExporter.getFinishedLogRecordItems(); | ||
| assertEquals(0,finishedLogRecordItems.size()); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.