Skip to content

Commit

Permalink
Updates the user_agent processor to use the EventKey.
Browse files Browse the repository at this point in the history
Signed-off-by: David Venable <dlv@amazon.com>
  • Loading branch information
dlvenable committed Jun 14, 2024
1 parent 7f64a6d commit a903eec
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions data-prepper-plugins/user-agent-processor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind'
implementation 'com.github.ua-parser:uap-java:1.6.1'
implementation libs.caffeine
testImplementation project(':data-prepper-test-event')
}

jacocoTestCoverageVerification {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.opensearch.dataprepper.model.annotations.DataPrepperPlugin;
import org.opensearch.dataprepper.model.annotations.DataPrepperPluginConstructor;
import org.opensearch.dataprepper.model.event.Event;
import org.opensearch.dataprepper.model.event.EventKey;
import org.opensearch.dataprepper.model.event.EventKeyFactory;
import org.opensearch.dataprepper.model.processor.AbstractProcessor;
import org.opensearch.dataprepper.model.processor.Processor;
import org.opensearch.dataprepper.model.record.Record;
Expand All @@ -30,12 +32,19 @@ public class UserAgentProcessor extends AbstractProcessor<Record<Event>, Record<
private static final Logger LOG = LoggerFactory.getLogger(UserAgentProcessor.class);
private final UserAgentProcessorConfig config;
private final Parser userAgentParser;
private final EventKey sourceKey;
private final EventKey targetKey;

@DataPrepperPluginConstructor
public UserAgentProcessor(final PluginMetrics pluginMetrics, final UserAgentProcessorConfig config) {
public UserAgentProcessor(
final UserAgentProcessorConfig config,
final EventKeyFactory eventKeyFactory,
final PluginMetrics pluginMetrics) {
super(pluginMetrics);
this.config = config;
this.userAgentParser = new CaffeineCachingParser(config.getCacheSize());
sourceKey = eventKeyFactory.createEventKey(config.getSource(), EventKeyFactory.EventAction.GET);
targetKey = eventKeyFactory.createEventKey(config.getTarget(), EventKeyFactory.EventAction.PUT);
}

@Override
Expand All @@ -44,7 +53,7 @@ public Collection<Record<Event>> doExecute(final Collection<Record<Event>> recor
final Event event = record.getData();

try {
final String userAgentStr = event.get(config.getSource(), String.class);
final String userAgentStr = event.get(sourceKey, String.class);
Objects.requireNonNull(userAgentStr);

final Client clientInfo = this.userAgentParser.parse(userAgentStr);
Expand All @@ -53,10 +62,10 @@ public Collection<Record<Event>> doExecute(final Collection<Record<Event>> recor
if (!config.getExcludeOriginal()) {
parsedUserAgent.put("original", userAgentStr);
}
event.put(config.getTarget(), parsedUserAgent);
event.put(targetKey, parsedUserAgent);
} catch (Exception e) {
LOG.error(EVENT, "An exception occurred when parsing user agent data from event [{}] with source key [{}]",
event, config.getSource(), e);
event, sourceKey.getKey(), e);

final List<String> tagsOnParseFailure = config.getTagsOnParseFailure();
if (Objects.nonNull(tagsOnParseFailure) && tagsOnParseFailure.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.event.TestEventKeyFactory;
import org.opensearch.dataprepper.metrics.PluginMetrics;
import org.opensearch.dataprepper.model.event.Event;
import org.opensearch.dataprepper.model.event.EventKeyFactory;
import org.opensearch.dataprepper.model.event.JacksonEvent;
import org.opensearch.dataprepper.model.record.Record;

Expand All @@ -38,6 +40,8 @@ class UserAgentProcessorTest {
@Mock
private UserAgentProcessorConfig mockConfig;

private final EventKeyFactory eventKeyFactory = TestEventKeyFactory.getTestEventFactory();

@ParameterizedTest
@MethodSource("userAgentStringArguments")
public void testParsingUserAgentStrings(
Expand Down Expand Up @@ -109,6 +113,7 @@ public void testParsingUserAgentStringsExcludeOriginal(
public void testParsingWhenUserAgentStringNotExist() {
when(mockConfig.getSource()).thenReturn("bad_source");
when(mockConfig.getCacheSize()).thenReturn(TEST_CACHE_SIZE);
when(mockConfig.getTarget()).thenReturn("user_agent");

final UserAgentProcessor processor = createObjectUnderTest();
final Record<Event> testRecord = createTestRecord(UUID.randomUUID().toString());
Expand All @@ -122,6 +127,7 @@ public void testParsingWhenUserAgentStringNotExist() {
public void testTagsAddedOnParseFailure() {
when(mockConfig.getSource()).thenReturn("bad_source");
when(mockConfig.getCacheSize()).thenReturn(TEST_CACHE_SIZE);
when(mockConfig.getTarget()).thenReturn("user_agent");

final String tagOnFailure1 = UUID.randomUUID().toString();
final String tagOnFailure2 = UUID.randomUUID().toString();
Expand All @@ -138,7 +144,7 @@ public void testTagsAddedOnParseFailure() {
}

private UserAgentProcessor createObjectUnderTest() {
return new UserAgentProcessor(pluginMetrics, mockConfig);
return new UserAgentProcessor(mockConfig, eventKeyFactory, pluginMetrics);
}

private Record<Event> createTestRecord(String uaString) {
Expand Down

0 comments on commit a903eec

Please sign in to comment.